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