1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Generic platform ehci driver
4 *
5 * Copyright 2007 Steven Brown <sbrown@cortland.com>
6 * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
7 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
8 *
9 * Derived from the ohci-ssb driver
10 * Copyright 2007 Michael Buesch <m@bues.ch>
11 *
12 * Derived from the EHCI-PCI driver
13 * Copyright (c) 2000-2004 by David Brownell
14 *
15 * Derived from the ohci-pci driver
16 * Copyright 1999 Roman Weissgaerber
17 * Copyright 2000-2002 David Brownell
18 * Copyright 1999 Linus Torvalds
19 * Copyright 1999 Gregory P. Smith
20 */
21 #include <linux/acpi.h>
22 #include <linux/clk.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/err.h>
25 #include <linux/kernel.h>
26 #include <linux/hrtimer.h>
27 #include <linux/io.h>
28 #include <linux/module.h>
29 #include <linux/of.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/reset.h>
33 #include <linux/sys_soc.h>
34 #include <linux/timer.h>
35 #include <linux/usb.h>
36 #include <linux/usb/hcd.h>
37 #include <linux/usb/ehci_pdriver.h>
38 #include <linux/usb/of.h>
39
40 #include "ehci.h"
41
42 #define DRIVER_DESC "EHCI generic platform driver"
43 #define EHCI_MAX_CLKS 4
44 #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
45
46 #define BCM_USB_FIFO_THRESHOLD 0x00800040
47 #define bcm_iproc_insnreg01 hostpc[0]
48
49 struct ehci_platform_priv {
50 struct clk *clks[EHCI_MAX_CLKS];
51 struct reset_control *rsts;
52 bool reset_on_resume;
53 bool quirk_poll;
54 struct timer_list poll_timer;
55 struct delayed_work poll_work;
56 };
57
58 static const char hcd_name[] = "ehci-platform";
59
ehci_rockchip_relinquish_port(struct usb_hcd * hcd,int portnum)60 static void ehci_rockchip_relinquish_port(struct usb_hcd *hcd, int portnum)
61 {
62 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
63 u32 __iomem *status_reg = &ehci->regs->port_status[--portnum];
64 u32 portsc;
65
66 portsc = ehci_readl(ehci, status_reg);
67 portsc &= ~(PORT_OWNER | PORT_RWC_BITS);
68
69 ehci_writel(ehci, portsc, status_reg);
70 }
71
72 #define USIC_MICROFRAME_OFFSET 0x90
73 #define USIC_SCALE_DOWN_OFFSET 0xa0
74 #define USIC_ENABLE_OFFSET 0xb0
75 #define USIC_ENABLE BIT(0)
76 #define USIC_SCALE_DOWN BIT(2)
77 #define USIC_MICROFRAME_COUNT 0x1d4d
78
ehci_usic_init(struct usb_hcd * hcd)79 static void ehci_usic_init(struct usb_hcd *hcd)
80 {
81 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
82
83 ehci_writel(ehci, USIC_ENABLE,
84 hcd->regs + USIC_ENABLE_OFFSET);
85 ehci_writel(ehci, USIC_MICROFRAME_COUNT,
86 hcd->regs + USIC_MICROFRAME_OFFSET);
87 ehci_writel(ehci, USIC_SCALE_DOWN,
88 hcd->regs + USIC_SCALE_DOWN_OFFSET);
89 }
90
ehci_platform_reset(struct usb_hcd * hcd)91 static int ehci_platform_reset(struct usb_hcd *hcd)
92 {
93 struct platform_device *pdev = to_platform_device(hcd->self.controller);
94 struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
95 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
96 int retval;
97
98 ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
99
100 if (pdata->pre_setup) {
101 retval = pdata->pre_setup(hcd);
102 if (retval < 0)
103 return retval;
104 }
105
106 ehci->caps = hcd->regs + pdata->caps_offset;
107 retval = ehci_setup(hcd);
108 if (retval)
109 return retval;
110
111 if (pdata->no_io_watchdog)
112 ehci->need_io_watchdog = 0;
113
114 if (of_device_is_compatible(pdev->dev.of_node, "brcm,xgs-iproc-ehci"))
115 ehci_writel(ehci, BCM_USB_FIFO_THRESHOLD,
116 &ehci->regs->bcm_iproc_insnreg01);
117
118 return 0;
119 }
120
ehci_platform_power_on(struct platform_device * dev)121 static int ehci_platform_power_on(struct platform_device *dev)
122 {
123 struct usb_hcd *hcd = platform_get_drvdata(dev);
124 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
125 int clk, ret;
126
127 for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
128 ret = clk_prepare_enable(priv->clks[clk]);
129 if (ret)
130 goto err_disable_clks;
131 }
132
133 return 0;
134
135 err_disable_clks:
136 while (--clk >= 0)
137 clk_disable_unprepare(priv->clks[clk]);
138
139 return ret;
140 }
141
ehci_platform_power_off(struct platform_device * dev)142 static void ehci_platform_power_off(struct platform_device *dev)
143 {
144 struct usb_hcd *hcd = platform_get_drvdata(dev);
145 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
146 int clk;
147
148 for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
149 if (priv->clks[clk])
150 clk_disable_unprepare(priv->clks[clk]);
151 }
152
153 static struct hc_driver __read_mostly ehci_platform_hc_driver;
154
155 static const struct ehci_driver_overrides platform_overrides __initconst = {
156 .reset = ehci_platform_reset,
157 .extra_priv_size = sizeof(struct ehci_platform_priv),
158 };
159
160 static struct usb_ehci_pdata ehci_platform_defaults = {
161 .power_on = ehci_platform_power_on,
162 .power_suspend = ehci_platform_power_off,
163 .power_off = ehci_platform_power_off,
164 };
165
166 /**
167 * quirk_poll_check_port_status - Poll port_status if the device sticks
168 * @ehci: the ehci hcd pointer
169 *
170 * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting
171 * stuck very rarely after a full/low usb device was disconnected. To
172 * detect such a situation, the controllers require a special way which poll
173 * the EHCI PORTSC register.
174 *
175 * Return: true if the controller's port_status indicated getting stuck
176 */
quirk_poll_check_port_status(struct ehci_hcd * ehci)177 static bool quirk_poll_check_port_status(struct ehci_hcd *ehci)
178 {
179 u32 port_status = ehci_readl(ehci, &ehci->regs->port_status[0]);
180
181 if (!(port_status & PORT_OWNER) &&
182 (port_status & PORT_POWER) &&
183 !(port_status & PORT_CONNECT) &&
184 (port_status & PORT_LS_MASK))
185 return true;
186
187 return false;
188 }
189
190 /**
191 * quirk_poll_rebind_companion - rebind comanion device to recover
192 * @ehci: the ehci hcd pointer
193 *
194 * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting
195 * stuck very rarely after a full/low usb device was disconnected. To
196 * recover from such a situation, the controllers require changing the OHCI
197 * functional state.
198 */
quirk_poll_rebind_companion(struct ehci_hcd * ehci)199 static void quirk_poll_rebind_companion(struct ehci_hcd *ehci)
200 {
201 struct device *companion_dev;
202 struct usb_hcd *hcd = ehci_to_hcd(ehci);
203
204 companion_dev = usb_of_get_companion_dev(hcd->self.controller);
205 if (!companion_dev)
206 return;
207
208 device_release_driver(companion_dev);
209 if (device_attach(companion_dev) < 0)
210 ehci_err(ehci, "%s: failed\n", __func__);
211
212 put_device(companion_dev);
213 }
214
quirk_poll_work(struct work_struct * work)215 static void quirk_poll_work(struct work_struct *work)
216 {
217 struct ehci_platform_priv *priv =
218 container_of(to_delayed_work(work), struct ehci_platform_priv,
219 poll_work);
220 struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd,
221 priv);
222
223 /* check the status twice to reduce misdetection rate */
224 if (!quirk_poll_check_port_status(ehci))
225 return;
226 udelay(10);
227 if (!quirk_poll_check_port_status(ehci))
228 return;
229
230 ehci_dbg(ehci, "%s: detected getting stuck. rebind now!\n", __func__);
231 quirk_poll_rebind_companion(ehci);
232 }
233
quirk_poll_timer(struct timer_list * t)234 static void quirk_poll_timer(struct timer_list *t)
235 {
236 struct ehci_platform_priv *priv = from_timer(priv, t, poll_timer);
237 struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd,
238 priv);
239
240 if (quirk_poll_check_port_status(ehci)) {
241 /*
242 * Now scheduling the work for testing the port more. Note that
243 * updating the status is possible to be delayed when
244 * reconnection. So, this uses delayed work with 5 ms delay
245 * to avoid misdetection.
246 */
247 schedule_delayed_work(&priv->poll_work, msecs_to_jiffies(5));
248 }
249
250 mod_timer(&priv->poll_timer, jiffies + HZ);
251 }
252
quirk_poll_init(struct ehci_platform_priv * priv)253 static void quirk_poll_init(struct ehci_platform_priv *priv)
254 {
255 INIT_DELAYED_WORK(&priv->poll_work, quirk_poll_work);
256 timer_setup(&priv->poll_timer, quirk_poll_timer, 0);
257 mod_timer(&priv->poll_timer, jiffies + HZ);
258 }
259
quirk_poll_end(struct ehci_platform_priv * priv)260 static void quirk_poll_end(struct ehci_platform_priv *priv)
261 {
262 del_timer_sync(&priv->poll_timer);
263 cancel_delayed_work(&priv->poll_work);
264 }
265
266 static const struct soc_device_attribute quirk_poll_match[] = {
267 { .family = "R-Car Gen3" },
268 { /* sentinel*/ }
269 };
270
ehci_platform_probe(struct platform_device * dev)271 static int ehci_platform_probe(struct platform_device *dev)
272 {
273 struct usb_hcd *hcd;
274 struct resource *res_mem;
275 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
276 struct ehci_platform_priv *priv;
277 struct ehci_hcd *ehci;
278 int err, irq, clk = 0;
279 struct device *companion_dev;
280 struct device_link *link;
281
282 if (usb_disabled())
283 return -ENODEV;
284
285 /*
286 * Use reasonable defaults so platforms don't have to provide these
287 * with DT probing on ARM.
288 */
289 if (!pdata)
290 pdata = &ehci_platform_defaults;
291
292 err = dma_coerce_mask_and_coherent(&dev->dev,
293 pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
294 if (err) {
295 dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
296 return err;
297 }
298
299 irq = platform_get_irq(dev, 0);
300 if (irq < 0)
301 return irq;
302
303 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
304 dev_name(&dev->dev));
305 if (!hcd)
306 return -ENOMEM;
307
308 platform_set_drvdata(dev, hcd);
309 dev->dev.platform_data = pdata;
310 priv = hcd_to_ehci_priv(hcd);
311 ehci = hcd_to_ehci(hcd);
312
313 if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
314 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
315 ehci->big_endian_mmio = 1;
316
317 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
318 ehci->big_endian_desc = 1;
319
320 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
321 ehci->big_endian_mmio = ehci->big_endian_desc = 1;
322
323 if (of_property_read_bool(dev->dev.of_node,
324 "needs-reset-on-resume"))
325 priv->reset_on_resume = true;
326
327 if (of_property_read_bool(dev->dev.of_node,
328 "has-transaction-translator"))
329 hcd->has_tt = 1;
330
331 if (of_device_is_compatible(dev->dev.of_node,
332 "aspeed,ast2500-ehci") ||
333 of_device_is_compatible(dev->dev.of_node,
334 "aspeed,ast2600-ehci"))
335 ehci->is_aspeed = 1;
336
337 if (soc_device_match(quirk_poll_match))
338 priv->quirk_poll = true;
339
340 if (of_machine_is_compatible("rockchip,rk3288") &&
341 of_property_read_bool(dev->dev.of_node,
342 "rockchip-relinquish-port"))
343 ehci_platform_hc_driver.relinquish_port =
344 ehci_rockchip_relinquish_port;
345
346 for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
347 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
348 if (IS_ERR(priv->clks[clk])) {
349 err = PTR_ERR(priv->clks[clk]);
350 if (err == -EPROBE_DEFER)
351 goto err_put_clks;
352 priv->clks[clk] = NULL;
353 break;
354 }
355 }
356 }
357
358 priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev);
359 if (IS_ERR(priv->rsts)) {
360 err = PTR_ERR(priv->rsts);
361 goto err_put_clks;
362 }
363
364 err = reset_control_deassert(priv->rsts);
365 if (err)
366 goto err_put_clks;
367
368 if (pdata->big_endian_desc)
369 ehci->big_endian_desc = 1;
370 if (pdata->big_endian_mmio)
371 ehci->big_endian_mmio = 1;
372 if (pdata->has_tt)
373 hcd->has_tt = 1;
374 if (pdata->reset_on_resume)
375 priv->reset_on_resume = true;
376
377 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
378 if (ehci->big_endian_mmio) {
379 dev_err(&dev->dev,
380 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
381 err = -EINVAL;
382 goto err_reset;
383 }
384 #endif
385 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
386 if (ehci->big_endian_desc) {
387 dev_err(&dev->dev,
388 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
389 err = -EINVAL;
390 goto err_reset;
391 }
392 #endif
393
394 pm_runtime_set_active(&dev->dev);
395 pm_runtime_enable(&dev->dev);
396 pm_runtime_get_sync(&dev->dev);
397 if (pdata->power_on) {
398 err = pdata->power_on(dev);
399 if (err < 0)
400 goto err_reset;
401 }
402
403 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
404 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
405 if (IS_ERR(hcd->regs)) {
406 err = PTR_ERR(hcd->regs);
407 goto err_power;
408 }
409 hcd->rsrc_start = res_mem->start;
410 hcd->rsrc_len = resource_size(res_mem);
411
412 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
413 if (err)
414 goto err_power;
415
416 if (of_usb_get_phy_mode(dev->dev.of_node) == USBPHY_INTERFACE_MODE_HSIC)
417 ehci_usic_init(hcd);
418
419 if (of_device_is_compatible(dev->dev.of_node,
420 "rockchip,rk3588-ehci")) {
421 companion_dev = usb_of_get_companion_dev(hcd->self.controller);
422 if (companion_dev) {
423 link = device_link_add(companion_dev, hcd->self.controller,
424 DL_FLAG_STATELESS);
425 put_device(companion_dev);
426 if (!link) {
427 dev_err(&dev->dev, "Unable to link %s\n",
428 dev_name(companion_dev));
429 err = -EINVAL;
430 goto err_power;
431 }
432 }
433 }
434
435 device_wakeup_enable(hcd->self.controller);
436 device_enable_async_suspend(hcd->self.controller);
437 platform_set_drvdata(dev, hcd);
438
439 if (priv->quirk_poll)
440 quirk_poll_init(priv);
441
442 return err;
443
444 err_power:
445 if (pdata->power_off)
446 pdata->power_off(dev);
447 err_reset:
448 pm_runtime_put_sync(&dev->dev);
449 pm_runtime_disable(&dev->dev);
450 reset_control_assert(priv->rsts);
451 err_put_clks:
452 while (--clk >= 0)
453 clk_put(priv->clks[clk]);
454
455 if (pdata == &ehci_platform_defaults)
456 dev->dev.platform_data = NULL;
457
458 usb_put_hcd(hcd);
459
460 return err;
461 }
462
ehci_platform_remove(struct platform_device * dev)463 static int ehci_platform_remove(struct platform_device *dev)
464 {
465 struct usb_hcd *hcd = platform_get_drvdata(dev);
466 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
467 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
468 struct device *companion_dev;
469 int clk;
470
471 if (priv->quirk_poll)
472 quirk_poll_end(priv);
473
474 if (of_device_is_compatible(dev->dev.of_node,
475 "rockchip,rk3588-ehci")) {
476 companion_dev = usb_of_get_companion_dev(hcd->self.controller);
477 if (companion_dev) {
478 device_link_remove(companion_dev, hcd->self.controller);
479 put_device(companion_dev);
480 }
481 }
482
483 usb_remove_hcd(hcd);
484
485 if (pdata->power_off)
486 pdata->power_off(dev);
487
488 reset_control_assert(priv->rsts);
489
490 for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
491 clk_put(priv->clks[clk]);
492
493 usb_put_hcd(hcd);
494
495 pm_runtime_put_sync(&dev->dev);
496 pm_runtime_disable(&dev->dev);
497
498 if (pdata == &ehci_platform_defaults)
499 dev->dev.platform_data = NULL;
500
501 return 0;
502 }
503
ehci_platform_suspend(struct device * dev)504 static int __maybe_unused ehci_platform_suspend(struct device *dev)
505 {
506 struct usb_hcd *hcd = dev_get_drvdata(dev);
507 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
508 struct platform_device *pdev = to_platform_device(dev);
509 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
510 bool do_wakeup = device_may_wakeup(dev);
511 int ret;
512
513 if (priv->quirk_poll)
514 quirk_poll_end(priv);
515
516 ret = ehci_suspend(hcd, do_wakeup);
517 if (ret)
518 return ret;
519
520 if (pdata->power_suspend)
521 pdata->power_suspend(pdev);
522
523 return ret;
524 }
525
ehci_platform_resume(struct device * dev)526 static int __maybe_unused ehci_platform_resume(struct device *dev)
527 {
528 struct usb_hcd *hcd = dev_get_drvdata(dev);
529 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
530 struct platform_device *pdev = to_platform_device(dev);
531 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
532 struct device *companion_dev;
533
534 if (pdata->power_on) {
535 int err = pdata->power_on(pdev);
536 if (err < 0)
537 return err;
538 }
539
540 companion_dev = usb_of_get_companion_dev(hcd->self.controller);
541 if (companion_dev) {
542 if (!device_is_dependent(hcd->self.controller, companion_dev))
543 device_pm_wait_for_dev(hcd->self.controller, companion_dev);
544 put_device(companion_dev);
545 }
546
547 ehci_resume(hcd, priv->reset_on_resume);
548
549 pm_runtime_disable(dev);
550 pm_runtime_set_active(dev);
551 pm_runtime_enable(dev);
552
553 if (priv->quirk_poll)
554 quirk_poll_init(priv);
555
556 return 0;
557 }
558
559 static const struct of_device_id vt8500_ehci_ids[] = {
560 { .compatible = "via,vt8500-ehci", },
561 { .compatible = "wm,prizm-ehci", },
562 { .compatible = "generic-ehci", },
563 { .compatible = "cavium,octeon-6335-ehci", },
564 {}
565 };
566 MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
567
568 #ifdef CONFIG_ACPI
569 static const struct acpi_device_id ehci_acpi_match[] = {
570 { "PNP0D20", 0 }, /* EHCI controller without debug */
571 { }
572 };
573 MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
574 #endif
575
576 static const struct platform_device_id ehci_platform_table[] = {
577 { "ehci-platform", 0 },
578 { }
579 };
580 MODULE_DEVICE_TABLE(platform, ehci_platform_table);
581
582 static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
583 ehci_platform_resume);
584
585 static struct platform_driver ehci_platform_driver = {
586 .id_table = ehci_platform_table,
587 .probe = ehci_platform_probe,
588 .remove = ehci_platform_remove,
589 .shutdown = usb_hcd_platform_shutdown,
590 .driver = {
591 .name = "ehci-platform",
592 .pm = pm_ptr(&ehci_platform_pm_ops),
593 .of_match_table = vt8500_ehci_ids,
594 .acpi_match_table = ACPI_PTR(ehci_acpi_match),
595 }
596 };
597
ehci_platform_init(void)598 static int __init ehci_platform_init(void)
599 {
600 if (usb_disabled())
601 return -ENODEV;
602
603 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
604
605 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
606 return platform_driver_register(&ehci_platform_driver);
607 }
608 module_init(ehci_platform_init);
609
ehci_platform_cleanup(void)610 static void __exit ehci_platform_cleanup(void)
611 {
612 platform_driver_unregister(&ehci_platform_driver);
613 }
614 module_exit(ehci_platform_cleanup);
615
616 MODULE_DESCRIPTION(DRIVER_DESC);
617 MODULE_AUTHOR("Hauke Mehrtens");
618 MODULE_AUTHOR("Alan Stern");
619 MODULE_LICENSE("GPL");
620