xref: /openbmc/linux/drivers/mtd/hyperbus/hbmc-am654.c (revision b07079f1642c28dac4f6f339d5aca66203519734)
1*b07079f1SVignesh Raghavendra // SPDX-License-Identifier: GPL-2.0
2*b07079f1SVignesh Raghavendra //
3*b07079f1SVignesh Raghavendra // Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
4*b07079f1SVignesh Raghavendra // Author: Vignesh Raghavendra <vigneshr@ti.com>
5*b07079f1SVignesh Raghavendra 
6*b07079f1SVignesh Raghavendra #include <linux/err.h>
7*b07079f1SVignesh Raghavendra #include <linux/kernel.h>
8*b07079f1SVignesh Raghavendra #include <linux/module.h>
9*b07079f1SVignesh Raghavendra #include <linux/mtd/cfi.h>
10*b07079f1SVignesh Raghavendra #include <linux/mtd/hyperbus.h>
11*b07079f1SVignesh Raghavendra #include <linux/mtd/mtd.h>
12*b07079f1SVignesh Raghavendra #include <linux/mux/consumer.h>
13*b07079f1SVignesh Raghavendra #include <linux/of.h>
14*b07079f1SVignesh Raghavendra #include <linux/platform_device.h>
15*b07079f1SVignesh Raghavendra #include <linux/pm_runtime.h>
16*b07079f1SVignesh Raghavendra #include <linux/types.h>
17*b07079f1SVignesh Raghavendra 
18*b07079f1SVignesh Raghavendra #define AM654_HBMC_CALIB_COUNT 25
19*b07079f1SVignesh Raghavendra 
20*b07079f1SVignesh Raghavendra struct am654_hbmc_priv {
21*b07079f1SVignesh Raghavendra 	struct hyperbus_ctlr ctlr;
22*b07079f1SVignesh Raghavendra 	struct hyperbus_device hbdev;
23*b07079f1SVignesh Raghavendra 	struct mux_control *mux_ctrl;
24*b07079f1SVignesh Raghavendra };
25*b07079f1SVignesh Raghavendra 
26*b07079f1SVignesh Raghavendra static int am654_hbmc_calibrate(struct hyperbus_device *hbdev)
27*b07079f1SVignesh Raghavendra {
28*b07079f1SVignesh Raghavendra 	struct map_info *map = &hbdev->map;
29*b07079f1SVignesh Raghavendra 	struct cfi_private cfi;
30*b07079f1SVignesh Raghavendra 	int count = AM654_HBMC_CALIB_COUNT;
31*b07079f1SVignesh Raghavendra 	int pass_count = 0;
32*b07079f1SVignesh Raghavendra 	int ret;
33*b07079f1SVignesh Raghavendra 
34*b07079f1SVignesh Raghavendra 	cfi.interleave = 1;
35*b07079f1SVignesh Raghavendra 	cfi.device_type = CFI_DEVICETYPE_X16;
36*b07079f1SVignesh Raghavendra 	cfi_send_gen_cmd(0xF0, 0, 0, map, &cfi, cfi.device_type, NULL);
37*b07079f1SVignesh Raghavendra 	cfi_send_gen_cmd(0x98, 0x55, 0, map, &cfi, cfi.device_type, NULL);
38*b07079f1SVignesh Raghavendra 
39*b07079f1SVignesh Raghavendra 	while (count--) {
40*b07079f1SVignesh Raghavendra 		ret = cfi_qry_present(map, 0, &cfi);
41*b07079f1SVignesh Raghavendra 		if (ret)
42*b07079f1SVignesh Raghavendra 			pass_count++;
43*b07079f1SVignesh Raghavendra 		else
44*b07079f1SVignesh Raghavendra 			pass_count = 0;
45*b07079f1SVignesh Raghavendra 		if (pass_count == 5)
46*b07079f1SVignesh Raghavendra 			break;
47*b07079f1SVignesh Raghavendra 	}
48*b07079f1SVignesh Raghavendra 
49*b07079f1SVignesh Raghavendra 	cfi_qry_mode_off(0, map, &cfi);
50*b07079f1SVignesh Raghavendra 
51*b07079f1SVignesh Raghavendra 	return ret;
52*b07079f1SVignesh Raghavendra }
53*b07079f1SVignesh Raghavendra 
54*b07079f1SVignesh Raghavendra static const struct hyperbus_ops am654_hbmc_ops = {
55*b07079f1SVignesh Raghavendra 	.calibrate = am654_hbmc_calibrate,
56*b07079f1SVignesh Raghavendra };
57*b07079f1SVignesh Raghavendra 
58*b07079f1SVignesh Raghavendra static int am654_hbmc_probe(struct platform_device *pdev)
59*b07079f1SVignesh Raghavendra {
60*b07079f1SVignesh Raghavendra 	struct device *dev = &pdev->dev;
61*b07079f1SVignesh Raghavendra 	struct am654_hbmc_priv *priv;
62*b07079f1SVignesh Raghavendra 	int ret;
63*b07079f1SVignesh Raghavendra 
64*b07079f1SVignesh Raghavendra 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
65*b07079f1SVignesh Raghavendra 	if (!priv)
66*b07079f1SVignesh Raghavendra 		return -ENOMEM;
67*b07079f1SVignesh Raghavendra 
68*b07079f1SVignesh Raghavendra 	platform_set_drvdata(pdev, priv);
69*b07079f1SVignesh Raghavendra 
70*b07079f1SVignesh Raghavendra 	if (of_property_read_bool(dev->of_node, "mux-controls")) {
71*b07079f1SVignesh Raghavendra 		struct mux_control *control = devm_mux_control_get(dev, NULL);
72*b07079f1SVignesh Raghavendra 
73*b07079f1SVignesh Raghavendra 		if (IS_ERR(control))
74*b07079f1SVignesh Raghavendra 			return PTR_ERR(control);
75*b07079f1SVignesh Raghavendra 
76*b07079f1SVignesh Raghavendra 		ret = mux_control_select(control, 1);
77*b07079f1SVignesh Raghavendra 		if (ret) {
78*b07079f1SVignesh Raghavendra 			dev_err(dev, "Failed to select HBMC mux\n");
79*b07079f1SVignesh Raghavendra 			return ret;
80*b07079f1SVignesh Raghavendra 		}
81*b07079f1SVignesh Raghavendra 		priv->mux_ctrl = control;
82*b07079f1SVignesh Raghavendra 	}
83*b07079f1SVignesh Raghavendra 
84*b07079f1SVignesh Raghavendra 	pm_runtime_enable(dev);
85*b07079f1SVignesh Raghavendra 	ret = pm_runtime_get_sync(dev);
86*b07079f1SVignesh Raghavendra 	if (ret < 0) {
87*b07079f1SVignesh Raghavendra 		pm_runtime_put_noidle(dev);
88*b07079f1SVignesh Raghavendra 		goto disable_pm;
89*b07079f1SVignesh Raghavendra 	}
90*b07079f1SVignesh Raghavendra 
91*b07079f1SVignesh Raghavendra 	priv->ctlr.dev = dev;
92*b07079f1SVignesh Raghavendra 	priv->ctlr.ops = &am654_hbmc_ops;
93*b07079f1SVignesh Raghavendra 	priv->hbdev.ctlr = &priv->ctlr;
94*b07079f1SVignesh Raghavendra 	priv->hbdev.np = of_get_next_child(dev->of_node, NULL);
95*b07079f1SVignesh Raghavendra 	ret = hyperbus_register_device(&priv->hbdev);
96*b07079f1SVignesh Raghavendra 	if (ret) {
97*b07079f1SVignesh Raghavendra 		dev_err(dev, "failed to register controller\n");
98*b07079f1SVignesh Raghavendra 		pm_runtime_put_sync(&pdev->dev);
99*b07079f1SVignesh Raghavendra 		goto disable_pm;
100*b07079f1SVignesh Raghavendra 	}
101*b07079f1SVignesh Raghavendra 
102*b07079f1SVignesh Raghavendra 	return 0;
103*b07079f1SVignesh Raghavendra disable_pm:
104*b07079f1SVignesh Raghavendra 	pm_runtime_disable(dev);
105*b07079f1SVignesh Raghavendra 	if (priv->mux_ctrl)
106*b07079f1SVignesh Raghavendra 		mux_control_deselect(priv->mux_ctrl);
107*b07079f1SVignesh Raghavendra 	return ret;
108*b07079f1SVignesh Raghavendra }
109*b07079f1SVignesh Raghavendra 
110*b07079f1SVignesh Raghavendra static int am654_hbmc_remove(struct platform_device *pdev)
111*b07079f1SVignesh Raghavendra {
112*b07079f1SVignesh Raghavendra 	struct am654_hbmc_priv *priv = platform_get_drvdata(pdev);
113*b07079f1SVignesh Raghavendra 	int ret;
114*b07079f1SVignesh Raghavendra 
115*b07079f1SVignesh Raghavendra 	ret = hyperbus_unregister_device(&priv->hbdev);
116*b07079f1SVignesh Raghavendra 	if (priv->mux_ctrl)
117*b07079f1SVignesh Raghavendra 		mux_control_deselect(priv->mux_ctrl);
118*b07079f1SVignesh Raghavendra 	pm_runtime_put_sync(&pdev->dev);
119*b07079f1SVignesh Raghavendra 	pm_runtime_disable(&pdev->dev);
120*b07079f1SVignesh Raghavendra 
121*b07079f1SVignesh Raghavendra 	return ret;
122*b07079f1SVignesh Raghavendra }
123*b07079f1SVignesh Raghavendra 
124*b07079f1SVignesh Raghavendra static const struct of_device_id am654_hbmc_dt_ids[] = {
125*b07079f1SVignesh Raghavendra 	{
126*b07079f1SVignesh Raghavendra 		.compatible = "ti,am654-hbmc",
127*b07079f1SVignesh Raghavendra 	},
128*b07079f1SVignesh Raghavendra 	{ /* end of table */ }
129*b07079f1SVignesh Raghavendra };
130*b07079f1SVignesh Raghavendra 
131*b07079f1SVignesh Raghavendra MODULE_DEVICE_TABLE(of, am654_hbmc_dt_ids);
132*b07079f1SVignesh Raghavendra 
133*b07079f1SVignesh Raghavendra static struct platform_driver am654_hbmc_platform_driver = {
134*b07079f1SVignesh Raghavendra 	.probe = am654_hbmc_probe,
135*b07079f1SVignesh Raghavendra 	.remove = am654_hbmc_remove,
136*b07079f1SVignesh Raghavendra 	.driver = {
137*b07079f1SVignesh Raghavendra 		.name = "hbmc-am654",
138*b07079f1SVignesh Raghavendra 		.of_match_table = am654_hbmc_dt_ids,
139*b07079f1SVignesh Raghavendra 	},
140*b07079f1SVignesh Raghavendra };
141*b07079f1SVignesh Raghavendra 
142*b07079f1SVignesh Raghavendra module_platform_driver(am654_hbmc_platform_driver);
143*b07079f1SVignesh Raghavendra 
144*b07079f1SVignesh Raghavendra MODULE_DESCRIPTION("HBMC driver for AM654 SoC");
145*b07079f1SVignesh Raghavendra MODULE_LICENSE("GPL v2");
146*b07079f1SVignesh Raghavendra MODULE_ALIAS("platform:hbmc-am654");
147*b07079f1SVignesh Raghavendra MODULE_AUTHOR("Vignesh Raghavendra <vigneshr@ti.com>");
148