]>
Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0 |
fa3364b5 HM |
2 | /* |
3 | * Generic platform ohci driver | |
4 | * | |
5 | * Copyright 2007 Michael Buesch <m@bues.ch> | |
6 | * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de> | |
ca52a17b | 7 | * Copyright 2014 Hans de Goede <hdegoede@redhat.com> |
fa3364b5 HM |
8 | * |
9 | * Derived from the OCHI-SSB driver | |
10 | * Derived from the OHCI-PCI driver | |
11 | * Copyright 1999 Roman Weissgaerber | |
12 | * Copyright 2000-2002 David Brownell | |
13 | * Copyright 1999 Linus Torvalds | |
14 | * Copyright 1999 Gregory P. Smith | |
fa3364b5 | 15 | */ |
928fb68e | 16 | |
ca52a17b HG |
17 | #include <linux/clk.h> |
18 | #include <linux/dma-mapping.h> | |
928fb68e MG |
19 | #include <linux/hrtimer.h> |
20 | #include <linux/io.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/module.h> | |
148e1134 | 23 | #include <linux/err.h> |
1255dfd1 | 24 | #include <linux/of.h> |
fa3364b5 | 25 | #include <linux/platform_device.h> |
0aa0b93e | 26 | #include <linux/pm_runtime.h> |
4615f3bd | 27 | #include <linux/reset.h> |
fa3364b5 | 28 | #include <linux/usb/ohci_pdriver.h> |
928fb68e MG |
29 | #include <linux/usb.h> |
30 | #include <linux/usb/hcd.h> | |
31 | ||
32 | #include "ohci.h" | |
33 | ||
34 | #define DRIVER_DESC "OHCI generic platform driver" | |
ca52a17b HG |
35 | #define OHCI_MAX_CLKS 3 |
36 | #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv) | |
37 | ||
38 | struct ohci_platform_priv { | |
39 | struct clk *clks[OHCI_MAX_CLKS]; | |
20a7f3ad | 40 | struct reset_control *resets; |
ca52a17b | 41 | }; |
928fb68e MG |
42 | |
43 | static const char hcd_name[] = "ohci-platform"; | |
fa3364b5 | 44 | |
ca52a17b HG |
45 | static int ohci_platform_power_on(struct platform_device *dev) |
46 | { | |
47 | struct usb_hcd *hcd = platform_get_drvdata(dev); | |
48 | struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd); | |
1255dfd1 | 49 | int clk, ret; |
ca52a17b HG |
50 | |
51 | for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) { | |
52 | ret = clk_prepare_enable(priv->clks[clk]); | |
53 | if (ret) | |
54 | goto err_disable_clks; | |
55 | } | |
56 | ||
ca52a17b HG |
57 | return 0; |
58 | ||
ca52a17b HG |
59 | err_disable_clks: |
60 | while (--clk >= 0) | |
61 | clk_disable_unprepare(priv->clks[clk]); | |
62 | ||
63 | return ret; | |
64 | } | |
65 | ||
66 | static void ohci_platform_power_off(struct platform_device *dev) | |
67 | { | |
68 | struct usb_hcd *hcd = platform_get_drvdata(dev); | |
69 | struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd); | |
1255dfd1 | 70 | int clk; |
ca52a17b HG |
71 | |
72 | for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--) | |
73 | if (priv->clks[clk]) | |
74 | clk_disable_unprepare(priv->clks[clk]); | |
75 | } | |
76 | ||
928fb68e | 77 | static struct hc_driver __read_mostly ohci_platform_hc_driver; |
fa3364b5 | 78 | |
928fb68e | 79 | static const struct ohci_driver_overrides platform_overrides __initconst = { |
ca52a17b | 80 | .product_desc = "Generic Platform OHCI controller", |
ca52a17b HG |
81 | .extra_priv_size = sizeof(struct ohci_platform_priv), |
82 | }; | |
83 | ||
84 | static struct usb_ohci_pdata ohci_platform_defaults = { | |
85 | .power_on = ohci_platform_power_on, | |
86 | .power_suspend = ohci_platform_power_off, | |
87 | .power_off = ohci_platform_power_off, | |
fa3364b5 HM |
88 | }; |
89 | ||
41ac7b3a | 90 | static int ohci_platform_probe(struct platform_device *dev) |
fa3364b5 HM |
91 | { |
92 | struct usb_hcd *hcd; | |
93 | struct resource *res_mem; | |
d4f09e28 | 94 | struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev); |
ca52a17b | 95 | struct ohci_platform_priv *priv; |
b1034412 | 96 | struct ohci_hcd *ohci; |
1255dfd1 | 97 | int err, irq, clk = 0; |
fa3364b5 HM |
98 | |
99 | if (usb_disabled()) | |
100 | return -ENODEV; | |
101 | ||
ca52a17b HG |
102 | /* |
103 | * Use reasonable defaults so platforms don't have to provide these | |
104 | * with DT probing on ARM. | |
105 | */ | |
106 | if (!pdata) | |
107 | pdata = &ohci_platform_defaults; | |
108 | ||
109 | err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)); | |
110 | if (err) | |
111 | return err; | |
112 | ||
fa3364b5 | 113 | irq = platform_get_irq(dev, 0); |
b33f3706 | 114 | if (irq < 0) |
fa3364b5 | 115 | return irq; |
fa3364b5 | 116 | |
ca52a17b HG |
117 | hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev, |
118 | dev_name(&dev->dev)); | |
119 | if (!hcd) | |
120 | return -ENOMEM; | |
121 | ||
122 | platform_set_drvdata(dev, hcd); | |
123 | dev->dev.platform_data = pdata; | |
124 | priv = hcd_to_ohci_priv(hcd); | |
b1034412 | 125 | ohci = hcd_to_ohci(hcd); |
ca52a17b HG |
126 | |
127 | if (pdata == &ohci_platform_defaults && dev->dev.of_node) { | |
b1034412 HG |
128 | if (of_property_read_bool(dev->dev.of_node, "big-endian-regs")) |
129 | ohci->flags |= OHCI_QUIRK_BE_MMIO; | |
130 | ||
131 | if (of_property_read_bool(dev->dev.of_node, "big-endian-desc")) | |
132 | ohci->flags |= OHCI_QUIRK_BE_DESC; | |
133 | ||
134 | if (of_property_read_bool(dev->dev.of_node, "big-endian")) | |
135 | ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC; | |
136 | ||
c4b8692a KC |
137 | if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no")) |
138 | ohci->flags |= OHCI_QUIRK_FRAME_NO; | |
139 | ||
2545d853 TL |
140 | if (of_property_read_bool(dev->dev.of_node, |
141 | "remote-wakeup-connected")) | |
142 | ohci->hc_control = OHCI_CTRL_RWC; | |
143 | ||
c4b8692a KC |
144 | of_property_read_u32(dev->dev.of_node, "num-ports", |
145 | &ohci->num_ports); | |
146 | ||
ca52a17b HG |
147 | for (clk = 0; clk < OHCI_MAX_CLKS; clk++) { |
148 | priv->clks[clk] = of_clk_get(dev->dev.of_node, clk); | |
149 | if (IS_ERR(priv->clks[clk])) { | |
150 | err = PTR_ERR(priv->clks[clk]); | |
151 | if (err == -EPROBE_DEFER) | |
152 | goto err_put_clks; | |
153 | priv->clks[clk] = NULL; | |
154 | break; | |
155 | } | |
156 | } | |
20a7f3ad MY |
157 | |
158 | priv->resets = devm_reset_control_array_get_optional_shared( | |
159 | &dev->dev); | |
160 | if (IS_ERR(priv->resets)) { | |
161 | err = PTR_ERR(priv->resets); | |
162 | goto err_put_clks; | |
62d9694a | 163 | } |
20a7f3ad MY |
164 | |
165 | err = reset_control_deassert(priv->resets); | |
166 | if (err) | |
167 | goto err_put_clks; | |
ca52a17b HG |
168 | } |
169 | ||
adff5295 AS |
170 | if (pdata->big_endian_desc) |
171 | ohci->flags |= OHCI_QUIRK_BE_DESC; | |
172 | if (pdata->big_endian_mmio) | |
173 | ohci->flags |= OHCI_QUIRK_BE_MMIO; | |
46f21949 KC |
174 | if (pdata->no_big_frame_no) |
175 | ohci->flags |= OHCI_QUIRK_FRAME_NO; | |
176 | if (pdata->num_ports) | |
177 | ohci->num_ports = pdata->num_ports; | |
adff5295 AS |
178 | |
179 | #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO | |
180 | if (ohci->flags & OHCI_QUIRK_BE_MMIO) { | |
181 | dev_err(&dev->dev, | |
182 | "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n"); | |
183 | err = -EINVAL; | |
4615f3bd | 184 | goto err_reset; |
adff5295 AS |
185 | } |
186 | #endif | |
187 | #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC | |
188 | if (ohci->flags & OHCI_QUIRK_BE_DESC) { | |
189 | dev_err(&dev->dev, | |
190 | "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n"); | |
191 | err = -EINVAL; | |
4615f3bd | 192 | goto err_reset; |
adff5295 AS |
193 | } |
194 | #endif | |
195 | ||
0aa0b93e TL |
196 | pm_runtime_set_active(&dev->dev); |
197 | pm_runtime_enable(&dev->dev); | |
e4d37aeb KM |
198 | if (pdata->power_on) { |
199 | err = pdata->power_on(dev); | |
200 | if (err < 0) | |
4615f3bd | 201 | goto err_reset; |
e4d37aeb | 202 | } |
fa3364b5 | 203 | |
7b519291 | 204 | res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0); |
148e1134 TR |
205 | hcd->regs = devm_ioremap_resource(&dev->dev, res_mem); |
206 | if (IS_ERR(hcd->regs)) { | |
207 | err = PTR_ERR(hcd->regs); | |
ca52a17b | 208 | goto err_power; |
aece389e | 209 | } |
7b519291 VB |
210 | hcd->rsrc_start = res_mem->start; |
211 | hcd->rsrc_len = resource_size(res_mem); | |
212 | ||
fa3364b5 HM |
213 | err = usb_add_hcd(hcd, irq, IRQF_SHARED); |
214 | if (err) | |
ca52a17b | 215 | goto err_power; |
fa3364b5 | 216 | |
3c9740a1 PC |
217 | device_wakeup_enable(hcd->self.controller); |
218 | ||
fa3364b5 HM |
219 | platform_set_drvdata(dev, hcd); |
220 | ||
221 | return err; | |
222 | ||
e4d37aeb KM |
223 | err_power: |
224 | if (pdata->power_off) | |
225 | pdata->power_off(dev); | |
4615f3bd | 226 | err_reset: |
0aa0b93e | 227 | pm_runtime_disable(&dev->dev); |
20a7f3ad | 228 | reset_control_assert(priv->resets); |
ca52a17b HG |
229 | err_put_clks: |
230 | while (--clk >= 0) | |
231 | clk_put(priv->clks[clk]); | |
1255dfd1 | 232 | |
ca52a17b HG |
233 | if (pdata == &ohci_platform_defaults) |
234 | dev->dev.platform_data = NULL; | |
235 | ||
236 | usb_put_hcd(hcd); | |
e4d37aeb | 237 | |
fa3364b5 HM |
238 | return err; |
239 | } | |
240 | ||
fb4e98ab | 241 | static int ohci_platform_remove(struct platform_device *dev) |
fa3364b5 HM |
242 | { |
243 | struct usb_hcd *hcd = platform_get_drvdata(dev); | |
d4f09e28 | 244 | struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev); |
ca52a17b | 245 | struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd); |
20a7f3ad | 246 | int clk; |
fa3364b5 | 247 | |
0aa0b93e | 248 | pm_runtime_get_sync(&dev->dev); |
fa3364b5 | 249 | usb_remove_hcd(hcd); |
fa3364b5 | 250 | |
e4d37aeb KM |
251 | if (pdata->power_off) |
252 | pdata->power_off(dev); | |
253 | ||
20a7f3ad | 254 | reset_control_assert(priv->resets); |
4615f3bd | 255 | |
ca52a17b HG |
256 | for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) |
257 | clk_put(priv->clks[clk]); | |
258 | ||
259 | usb_put_hcd(hcd); | |
260 | ||
0aa0b93e TL |
261 | pm_runtime_put_sync(&dev->dev); |
262 | pm_runtime_disable(&dev->dev); | |
263 | ||
ca52a17b HG |
264 | if (pdata == &ohci_platform_defaults) |
265 | dev->dev.platform_data = NULL; | |
266 | ||
fa3364b5 HM |
267 | return 0; |
268 | } | |
269 | ||
5e4ccd9e | 270 | #ifdef CONFIG_PM_SLEEP |
fa3364b5 HM |
271 | static int ohci_platform_suspend(struct device *dev) |
272 | { | |
39dbd7df MG |
273 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
274 | struct usb_ohci_pdata *pdata = dev->platform_data; | |
20db5513 | 275 | struct platform_device *pdev = to_platform_device(dev); |
39dbd7df MG |
276 | bool do_wakeup = device_may_wakeup(dev); |
277 | int ret; | |
278 | ||
279 | ret = ohci_suspend(hcd, do_wakeup); | |
280 | if (ret) | |
281 | return ret; | |
e4d37aeb KM |
282 | |
283 | if (pdata->power_suspend) | |
284 | pdata->power_suspend(pdev); | |
285 | ||
39dbd7df | 286 | return ret; |
fa3364b5 HM |
287 | } |
288 | ||
289 | static int ohci_platform_resume(struct device *dev) | |
290 | { | |
291 | struct usb_hcd *hcd = dev_get_drvdata(dev); | |
d4f09e28 | 292 | struct usb_ohci_pdata *pdata = dev_get_platdata(dev); |
20db5513 | 293 | struct platform_device *pdev = to_platform_device(dev); |
e4d37aeb KM |
294 | |
295 | if (pdata->power_on) { | |
296 | int err = pdata->power_on(pdev); | |
297 | if (err < 0) | |
298 | return err; | |
299 | } | |
fa3364b5 | 300 | |
cfa49b4b | 301 | ohci_resume(hcd, false); |
1cb3b009 QY |
302 | |
303 | pm_runtime_disable(dev); | |
304 | pm_runtime_set_active(dev); | |
305 | pm_runtime_enable(dev); | |
306 | ||
fa3364b5 HM |
307 | return 0; |
308 | } | |
5e4ccd9e | 309 | #endif /* CONFIG_PM_SLEEP */ |
fa3364b5 | 310 | |
ca52a17b | 311 | static const struct of_device_id ohci_platform_ids[] = { |
ce149c30 | 312 | { .compatible = "generic-ohci", }, |
a95cfa6b | 313 | { .compatible = "cavium,octeon-6335-ohci", }, |
2545d853 | 314 | { .compatible = "ti,ohci-omap3", }, |
ca52a17b HG |
315 | { } |
316 | }; | |
317 | MODULE_DEVICE_TABLE(of, ohci_platform_ids); | |
318 | ||
fa3364b5 HM |
319 | static const struct platform_device_id ohci_platform_table[] = { |
320 | { "ohci-platform", 0 }, | |
321 | { } | |
322 | }; | |
323 | MODULE_DEVICE_TABLE(platform, ohci_platform_table); | |
324 | ||
5e4ccd9e WK |
325 | static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend, |
326 | ohci_platform_resume); | |
fa3364b5 HM |
327 | |
328 | static struct platform_driver ohci_platform_driver = { | |
329 | .id_table = ohci_platform_table, | |
330 | .probe = ohci_platform_probe, | |
7690417d | 331 | .remove = ohci_platform_remove, |
fa3364b5 HM |
332 | .shutdown = usb_hcd_platform_shutdown, |
333 | .driver = { | |
fa3364b5 HM |
334 | .name = "ohci-platform", |
335 | .pm = &ohci_platform_pm_ops, | |
ca52a17b | 336 | .of_match_table = ohci_platform_ids, |
fa3364b5 HM |
337 | } |
338 | }; | |
928fb68e MG |
339 | |
340 | static int __init ohci_platform_init(void) | |
341 | { | |
342 | if (usb_disabled()) | |
343 | return -ENODEV; | |
344 | ||
345 | pr_info("%s: " DRIVER_DESC "\n", hcd_name); | |
346 | ||
347 | ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides); | |
348 | return platform_driver_register(&ohci_platform_driver); | |
349 | } | |
350 | module_init(ohci_platform_init); | |
351 | ||
352 | static void __exit ohci_platform_cleanup(void) | |
353 | { | |
354 | platform_driver_unregister(&ohci_platform_driver); | |
355 | } | |
356 | module_exit(ohci_platform_cleanup); | |
357 | ||
358 | MODULE_DESCRIPTION(DRIVER_DESC); | |
359 | MODULE_AUTHOR("Hauke Mehrtens"); | |
360 | MODULE_AUTHOR("Alan Stern"); | |
361 | MODULE_LICENSE("GPL"); |