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