xref: /linux/drivers/usb/host/ohci-st.c (revision b33f37064b743d4c5771d3bfc65976790b7396cc)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d1158372SPeter Griffin /*
3d1158372SPeter Griffin  * ST OHCI driver
4d1158372SPeter Griffin  *
5d1158372SPeter Griffin  * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
6d1158372SPeter Griffin  *
7d1158372SPeter Griffin  * Author: Peter Griffin <peter.griffin@linaro.org>
8d1158372SPeter Griffin  *
9d1158372SPeter Griffin  * Derived from ohci-platform.c
10d1158372SPeter Griffin  */
11d1158372SPeter Griffin 
12d1158372SPeter Griffin #include <linux/clk.h>
13d1158372SPeter Griffin #include <linux/dma-mapping.h>
14d1158372SPeter Griffin #include <linux/hrtimer.h>
15d1158372SPeter Griffin #include <linux/io.h>
16d1158372SPeter Griffin #include <linux/kernel.h>
17d1158372SPeter Griffin #include <linux/module.h>
18d1158372SPeter Griffin #include <linux/err.h>
19d1158372SPeter Griffin #include <linux/phy/phy.h>
20d1158372SPeter Griffin #include <linux/platform_device.h>
21d1158372SPeter Griffin #include <linux/reset.h>
22d1158372SPeter Griffin #include <linux/usb/ohci_pdriver.h>
23d1158372SPeter Griffin #include <linux/usb.h>
24d1158372SPeter Griffin #include <linux/usb/hcd.h>
25d1158372SPeter Griffin 
26d1158372SPeter Griffin #include "ohci.h"
27d1158372SPeter Griffin 
28d1158372SPeter Griffin #define USB_MAX_CLKS 3
29d1158372SPeter Griffin 
30d1158372SPeter Griffin struct st_ohci_platform_priv {
31d1158372SPeter Griffin 	struct clk *clks[USB_MAX_CLKS];
32d1158372SPeter Griffin 	struct clk *clk48;
33d1158372SPeter Griffin 	struct reset_control *rst;
34d1158372SPeter Griffin 	struct reset_control *pwr;
35d1158372SPeter Griffin 	struct phy *phy;
36d1158372SPeter Griffin };
37d1158372SPeter Griffin 
38d1158372SPeter Griffin #define DRIVER_DESC "OHCI STMicroelectronics driver"
39d1158372SPeter Griffin 
40d1158372SPeter Griffin #define hcd_to_ohci_priv(h) \
41d1158372SPeter Griffin 	((struct st_ohci_platform_priv *)hcd_to_ohci(h)->priv)
42d1158372SPeter Griffin 
43d1158372SPeter Griffin static const char hcd_name[] = "ohci-st";
44d1158372SPeter Griffin 
45d1158372SPeter Griffin static int st_ohci_platform_power_on(struct platform_device *dev)
46d1158372SPeter Griffin {
47d1158372SPeter Griffin 	struct usb_hcd *hcd = platform_get_drvdata(dev);
48d1158372SPeter Griffin 	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
49d1158372SPeter Griffin 	int clk, ret;
50d1158372SPeter Griffin 
51d1158372SPeter Griffin 	ret = reset_control_deassert(priv->pwr);
52d1158372SPeter Griffin 	if (ret)
53d1158372SPeter Griffin 		return ret;
54d1158372SPeter Griffin 
55d1158372SPeter Griffin 	ret = reset_control_deassert(priv->rst);
56d1158372SPeter Griffin 	if (ret)
57d1158372SPeter Griffin 		goto err_assert_power;
58d1158372SPeter Griffin 
59d1158372SPeter Griffin 	/* some SoCs don't have a dedicated 48Mhz clock, but those that do
60d1158372SPeter Griffin 	   need the rate to be explicitly set */
61d1158372SPeter Griffin 	if (priv->clk48) {
62d1158372SPeter Griffin 		ret = clk_set_rate(priv->clk48, 48000000);
63d1158372SPeter Griffin 		if (ret)
64d1158372SPeter Griffin 			goto err_assert_reset;
65d1158372SPeter Griffin 	}
66d1158372SPeter Griffin 
67d1158372SPeter Griffin 	for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
68d1158372SPeter Griffin 		ret = clk_prepare_enable(priv->clks[clk]);
69d1158372SPeter Griffin 		if (ret)
70d1158372SPeter Griffin 			goto err_disable_clks;
71d1158372SPeter Griffin 	}
72d1158372SPeter Griffin 
73d1158372SPeter Griffin 	ret = phy_init(priv->phy);
74d1158372SPeter Griffin 	if (ret)
75d1158372SPeter Griffin 		goto err_disable_clks;
76d1158372SPeter Griffin 
77d1158372SPeter Griffin 	ret = phy_power_on(priv->phy);
78d1158372SPeter Griffin 	if (ret)
79d1158372SPeter Griffin 		goto err_exit_phy;
80d1158372SPeter Griffin 
81d1158372SPeter Griffin 	return 0;
82d1158372SPeter Griffin 
83d1158372SPeter Griffin err_exit_phy:
84d1158372SPeter Griffin 	phy_exit(priv->phy);
85d1158372SPeter Griffin err_disable_clks:
86d1158372SPeter Griffin 	while (--clk >= 0)
87d1158372SPeter Griffin 		clk_disable_unprepare(priv->clks[clk]);
88d1158372SPeter Griffin err_assert_reset:
89d1158372SPeter Griffin 	reset_control_assert(priv->rst);
90d1158372SPeter Griffin err_assert_power:
91d1158372SPeter Griffin 	reset_control_assert(priv->pwr);
92d1158372SPeter Griffin 
93d1158372SPeter Griffin 	return ret;
94d1158372SPeter Griffin }
95d1158372SPeter Griffin 
96d1158372SPeter Griffin static void st_ohci_platform_power_off(struct platform_device *dev)
97d1158372SPeter Griffin {
98d1158372SPeter Griffin 	struct usb_hcd *hcd = platform_get_drvdata(dev);
99d1158372SPeter Griffin 	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
100d1158372SPeter Griffin 
101d1158372SPeter Griffin 	int clk;
102d1158372SPeter Griffin 
103d1158372SPeter Griffin 	reset_control_assert(priv->pwr);
104d1158372SPeter Griffin 
105d1158372SPeter Griffin 	reset_control_assert(priv->rst);
106d1158372SPeter Griffin 
107d1158372SPeter Griffin 	phy_power_off(priv->phy);
108d1158372SPeter Griffin 
109d1158372SPeter Griffin 	phy_exit(priv->phy);
110d1158372SPeter Griffin 
111d1158372SPeter Griffin 	for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
112d1158372SPeter Griffin 		if (priv->clks[clk])
113d1158372SPeter Griffin 			clk_disable_unprepare(priv->clks[clk]);
114d1158372SPeter Griffin }
115d1158372SPeter Griffin 
116d1158372SPeter Griffin static struct hc_driver __read_mostly ohci_platform_hc_driver;
117d1158372SPeter Griffin 
118d1158372SPeter Griffin static const struct ohci_driver_overrides platform_overrides __initconst = {
119d1158372SPeter Griffin 	.product_desc =		"ST OHCI controller",
120d1158372SPeter Griffin 	.extra_priv_size =	sizeof(struct st_ohci_platform_priv),
121d1158372SPeter Griffin };
122d1158372SPeter Griffin 
123d1158372SPeter Griffin static struct usb_ohci_pdata ohci_platform_defaults = {
124d1158372SPeter Griffin 	.power_on =		st_ohci_platform_power_on,
125d1158372SPeter Griffin 	.power_suspend =	st_ohci_platform_power_off,
126d1158372SPeter Griffin 	.power_off =		st_ohci_platform_power_off,
127d1158372SPeter Griffin };
128d1158372SPeter Griffin 
129d1158372SPeter Griffin static int st_ohci_platform_probe(struct platform_device *dev)
130d1158372SPeter Griffin {
131d1158372SPeter Griffin 	struct usb_hcd *hcd;
132d1158372SPeter Griffin 	struct resource *res_mem;
133d1158372SPeter Griffin 	struct usb_ohci_pdata *pdata = &ohci_platform_defaults;
134d1158372SPeter Griffin 	struct st_ohci_platform_priv *priv;
135d1158372SPeter Griffin 	int err, irq, clk = 0;
136d1158372SPeter Griffin 
137d1158372SPeter Griffin 	if (usb_disabled())
138d1158372SPeter Griffin 		return -ENODEV;
139d1158372SPeter Griffin 
140d1158372SPeter Griffin 	irq = platform_get_irq(dev, 0);
141*b33f3706SStephen Boyd 	if (irq < 0)
142d1158372SPeter Griffin 		return irq;
143d1158372SPeter Griffin 
144d1158372SPeter Griffin 	res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
145d1158372SPeter Griffin 	if (!res_mem) {
146d1158372SPeter Griffin 		dev_err(&dev->dev, "no memory resource provided");
147d1158372SPeter Griffin 		return -ENXIO;
148d1158372SPeter Griffin 	}
149d1158372SPeter Griffin 
150d1158372SPeter Griffin 	hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
151d1158372SPeter Griffin 			dev_name(&dev->dev));
152d1158372SPeter Griffin 	if (!hcd)
153d1158372SPeter Griffin 		return -ENOMEM;
154d1158372SPeter Griffin 
155d1158372SPeter Griffin 	platform_set_drvdata(dev, hcd);
156d1158372SPeter Griffin 	dev->dev.platform_data = pdata;
157d1158372SPeter Griffin 	priv = hcd_to_ohci_priv(hcd);
158d1158372SPeter Griffin 
159d1158372SPeter Griffin 	priv->phy = devm_phy_get(&dev->dev, "usb");
160d1158372SPeter Griffin 	if (IS_ERR(priv->phy)) {
161d1158372SPeter Griffin 		err = PTR_ERR(priv->phy);
162d1158372SPeter Griffin 		goto err_put_hcd;
163d1158372SPeter Griffin 	}
164d1158372SPeter Griffin 
165d1158372SPeter Griffin 	for (clk = 0; clk < USB_MAX_CLKS; clk++) {
166d1158372SPeter Griffin 		priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
167d1158372SPeter Griffin 		if (IS_ERR(priv->clks[clk])) {
168d1158372SPeter Griffin 			err = PTR_ERR(priv->clks[clk]);
169d1158372SPeter Griffin 			if (err == -EPROBE_DEFER)
170d1158372SPeter Griffin 				goto err_put_clks;
171d1158372SPeter Griffin 			priv->clks[clk] = NULL;
172d1158372SPeter Griffin 			break;
173d1158372SPeter Griffin 		}
174d1158372SPeter Griffin 	}
175d1158372SPeter Griffin 
176d1158372SPeter Griffin 	/* some SoCs don't have a dedicated 48Mhz clock, but those that
177d1158372SPeter Griffin 	   do need the rate to be explicitly set */
178d1158372SPeter Griffin 	priv->clk48 = devm_clk_get(&dev->dev, "clk48");
179d1158372SPeter Griffin 	if (IS_ERR(priv->clk48)) {
180d1158372SPeter Griffin 		dev_info(&dev->dev, "48MHz clk not found\n");
181d1158372SPeter Griffin 		priv->clk48 = NULL;
182d1158372SPeter Griffin 	}
183d1158372SPeter Griffin 
184f5ee3f2bSLee Jones 	priv->pwr =
185f5ee3f2bSLee Jones 		devm_reset_control_get_optional_shared(&dev->dev, "power");
186d1158372SPeter Griffin 	if (IS_ERR(priv->pwr)) {
187d1158372SPeter Griffin 		err = PTR_ERR(priv->pwr);
188d1158372SPeter Griffin 		goto err_put_clks;
189d1158372SPeter Griffin 	}
190d1158372SPeter Griffin 
191f5ee3f2bSLee Jones 	priv->rst =
192f5ee3f2bSLee Jones 		devm_reset_control_get_optional_shared(&dev->dev, "softreset");
193d1158372SPeter Griffin 	if (IS_ERR(priv->rst)) {
194d1158372SPeter Griffin 		err = PTR_ERR(priv->rst);
195d1158372SPeter Griffin 		goto err_put_clks;
196d1158372SPeter Griffin 	}
197d1158372SPeter Griffin 
198d1158372SPeter Griffin 	if (pdata->power_on) {
199d1158372SPeter Griffin 		err = pdata->power_on(dev);
200d1158372SPeter Griffin 		if (err < 0)
201d1158372SPeter Griffin 			goto err_power;
202d1158372SPeter Griffin 	}
203d1158372SPeter Griffin 
204d1158372SPeter Griffin 	hcd->rsrc_start = res_mem->start;
205d1158372SPeter Griffin 	hcd->rsrc_len = resource_size(res_mem);
206d1158372SPeter Griffin 
207d1158372SPeter Griffin 	hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
208d1158372SPeter Griffin 	if (IS_ERR(hcd->regs)) {
209d1158372SPeter Griffin 		err = PTR_ERR(hcd->regs);
210d1158372SPeter Griffin 		goto err_power;
211d1158372SPeter Griffin 	}
212d1158372SPeter Griffin 	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
213d1158372SPeter Griffin 	if (err)
214d1158372SPeter Griffin 		goto err_power;
215d1158372SPeter Griffin 
216d1158372SPeter Griffin 	device_wakeup_enable(hcd->self.controller);
217d1158372SPeter Griffin 
218d1158372SPeter Griffin 	platform_set_drvdata(dev, hcd);
219d1158372SPeter Griffin 
220d1158372SPeter Griffin 	return err;
221d1158372SPeter Griffin 
222d1158372SPeter Griffin err_power:
223d1158372SPeter Griffin 	if (pdata->power_off)
224d1158372SPeter Griffin 		pdata->power_off(dev);
225d1158372SPeter Griffin 
226d1158372SPeter Griffin err_put_clks:
227d1158372SPeter Griffin 	while (--clk >= 0)
228d1158372SPeter Griffin 		clk_put(priv->clks[clk]);
229d1158372SPeter Griffin err_put_hcd:
230d1158372SPeter Griffin 	if (pdata == &ohci_platform_defaults)
231d1158372SPeter Griffin 		dev->dev.platform_data = NULL;
232d1158372SPeter Griffin 
233d1158372SPeter Griffin 	usb_put_hcd(hcd);
234d1158372SPeter Griffin 
235d1158372SPeter Griffin 	return err;
236d1158372SPeter Griffin }
237d1158372SPeter Griffin 
238d1158372SPeter Griffin static int st_ohci_platform_remove(struct platform_device *dev)
239d1158372SPeter Griffin {
240d1158372SPeter Griffin 	struct usb_hcd *hcd = platform_get_drvdata(dev);
241d1158372SPeter Griffin 	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
242d1158372SPeter Griffin 	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
243d1158372SPeter Griffin 	int clk;
244d1158372SPeter Griffin 
245d1158372SPeter Griffin 	usb_remove_hcd(hcd);
246d1158372SPeter Griffin 
247d1158372SPeter Griffin 	if (pdata->power_off)
248d1158372SPeter Griffin 		pdata->power_off(dev);
249d1158372SPeter Griffin 
250d1158372SPeter Griffin 
251d1158372SPeter Griffin 	for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
252d1158372SPeter Griffin 		clk_put(priv->clks[clk]);
253d1158372SPeter Griffin 
254d1158372SPeter Griffin 	usb_put_hcd(hcd);
255d1158372SPeter Griffin 
256d1158372SPeter Griffin 	if (pdata == &ohci_platform_defaults)
257d1158372SPeter Griffin 		dev->dev.platform_data = NULL;
258d1158372SPeter Griffin 
259d1158372SPeter Griffin 	return 0;
260d1158372SPeter Griffin }
261d1158372SPeter Griffin 
262d1158372SPeter Griffin #ifdef CONFIG_PM_SLEEP
263d1158372SPeter Griffin 
264d1158372SPeter Griffin static int st_ohci_suspend(struct device *dev)
265d1158372SPeter Griffin {
266d1158372SPeter Griffin 	struct usb_hcd *hcd = dev_get_drvdata(dev);
267d1158372SPeter Griffin 	struct usb_ohci_pdata *pdata = dev->platform_data;
26820db5513SGeliang Tang 	struct platform_device *pdev = to_platform_device(dev);
269d1158372SPeter Griffin 	bool do_wakeup = device_may_wakeup(dev);
270d1158372SPeter Griffin 	int ret;
271d1158372SPeter Griffin 
272d1158372SPeter Griffin 	ret = ohci_suspend(hcd, do_wakeup);
273d1158372SPeter Griffin 	if (ret)
274d1158372SPeter Griffin 		return ret;
275d1158372SPeter Griffin 
276d1158372SPeter Griffin 	if (pdata->power_suspend)
277d1158372SPeter Griffin 		pdata->power_suspend(pdev);
278d1158372SPeter Griffin 
279d1158372SPeter Griffin 	return ret;
280d1158372SPeter Griffin }
281d1158372SPeter Griffin 
282d1158372SPeter Griffin static int st_ohci_resume(struct device *dev)
283d1158372SPeter Griffin {
284d1158372SPeter Griffin 	struct usb_hcd *hcd = dev_get_drvdata(dev);
285d1158372SPeter Griffin 	struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
28620db5513SGeliang Tang 	struct platform_device *pdev = to_platform_device(dev);
287d1158372SPeter Griffin 	int err;
288d1158372SPeter Griffin 
289d1158372SPeter Griffin 	if (pdata->power_on) {
290d1158372SPeter Griffin 		err = pdata->power_on(pdev);
291d1158372SPeter Griffin 		if (err < 0)
292d1158372SPeter Griffin 			return err;
293d1158372SPeter Griffin 	}
294d1158372SPeter Griffin 
295d1158372SPeter Griffin 	ohci_resume(hcd, false);
296d1158372SPeter Griffin 	return 0;
297d1158372SPeter Griffin }
298d1158372SPeter Griffin 
299d1158372SPeter Griffin static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
300d1158372SPeter Griffin 
301d1158372SPeter Griffin #endif /* CONFIG_PM_SLEEP */
302d1158372SPeter Griffin 
303d1158372SPeter Griffin static const struct of_device_id st_ohci_platform_ids[] = {
304d1158372SPeter Griffin 	{ .compatible = "st,st-ohci-300x", },
305d1158372SPeter Griffin 	{ /* sentinel */ }
306d1158372SPeter Griffin };
307d1158372SPeter Griffin MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
308d1158372SPeter Griffin 
309d1158372SPeter Griffin static struct platform_driver ohci_platform_driver = {
310d1158372SPeter Griffin 	.probe		= st_ohci_platform_probe,
311d1158372SPeter Griffin 	.remove		= st_ohci_platform_remove,
312d1158372SPeter Griffin 	.shutdown	= usb_hcd_platform_shutdown,
313d1158372SPeter Griffin 	.driver		= {
314d1158372SPeter Griffin 		.name	= "st-ohci",
315d1158372SPeter Griffin #ifdef CONFIG_PM_SLEEP
316d1158372SPeter Griffin 		.pm	= &st_ohci_pm_ops,
317d1158372SPeter Griffin #endif
318d1158372SPeter Griffin 		.of_match_table = st_ohci_platform_ids,
319d1158372SPeter Griffin 	}
320d1158372SPeter Griffin };
321d1158372SPeter Griffin 
322d1158372SPeter Griffin static int __init ohci_platform_init(void)
323d1158372SPeter Griffin {
324d1158372SPeter Griffin 	if (usb_disabled())
325d1158372SPeter Griffin 		return -ENODEV;
326d1158372SPeter Griffin 
327d1158372SPeter Griffin 	pr_info("%s: " DRIVER_DESC "\n", hcd_name);
328d1158372SPeter Griffin 
329d1158372SPeter Griffin 	ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
330d1158372SPeter Griffin 	return platform_driver_register(&ohci_platform_driver);
331d1158372SPeter Griffin }
332d1158372SPeter Griffin module_init(ohci_platform_init);
333d1158372SPeter Griffin 
334d1158372SPeter Griffin static void __exit ohci_platform_cleanup(void)
335d1158372SPeter Griffin {
336d1158372SPeter Griffin 	platform_driver_unregister(&ohci_platform_driver);
337d1158372SPeter Griffin }
338d1158372SPeter Griffin module_exit(ohci_platform_cleanup);
339d1158372SPeter Griffin 
340d1158372SPeter Griffin MODULE_DESCRIPTION(DRIVER_DESC);
341d1158372SPeter Griffin MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
342d1158372SPeter Griffin MODULE_LICENSE("GPL");
343