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