ehci-platform.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic platform ehci driver
  4. *
  5. * Copyright 2007 Steven Brown <[email protected]>
  6. * Copyright 2010-2012 Hauke Mehrtens <[email protected]>
  7. * Copyright 2014 Hans de Goede <[email protected]>
  8. *
  9. * Derived from the ohci-ssb driver
  10. * Copyright 2007 Michael Buesch <[email protected]>
  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/reset.h>
  32. #include <linux/sys_soc.h>
  33. #include <linux/timer.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/hcd.h>
  36. #include <linux/usb/ehci_pdriver.h>
  37. #include <linux/usb/of.h>
  38. #include "ehci.h"
  39. #define DRIVER_DESC "EHCI generic platform driver"
  40. #define EHCI_MAX_CLKS 4
  41. #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
  42. #define BCM_USB_FIFO_THRESHOLD 0x00800040
  43. struct ehci_platform_priv {
  44. struct clk *clks[EHCI_MAX_CLKS];
  45. struct reset_control *rsts;
  46. bool reset_on_resume;
  47. bool quirk_poll;
  48. struct timer_list poll_timer;
  49. struct delayed_work poll_work;
  50. };
  51. static int ehci_platform_reset(struct usb_hcd *hcd)
  52. {
  53. struct platform_device *pdev = to_platform_device(hcd->self.controller);
  54. struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
  55. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  56. int retval;
  57. ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
  58. if (pdata->pre_setup) {
  59. retval = pdata->pre_setup(hcd);
  60. if (retval < 0)
  61. return retval;
  62. }
  63. ehci->caps = hcd->regs + pdata->caps_offset;
  64. retval = ehci_setup(hcd);
  65. if (retval)
  66. return retval;
  67. if (pdata->no_io_watchdog)
  68. ehci->need_io_watchdog = 0;
  69. if (of_device_is_compatible(pdev->dev.of_node, "brcm,xgs-iproc-ehci"))
  70. ehci_writel(ehci, BCM_USB_FIFO_THRESHOLD,
  71. &ehci->regs->brcm_insnreg[1]);
  72. return 0;
  73. }
  74. static int ehci_platform_power_on(struct platform_device *dev)
  75. {
  76. struct usb_hcd *hcd = platform_get_drvdata(dev);
  77. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  78. int clk, ret;
  79. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
  80. ret = clk_prepare_enable(priv->clks[clk]);
  81. if (ret)
  82. goto err_disable_clks;
  83. }
  84. return 0;
  85. err_disable_clks:
  86. while (--clk >= 0)
  87. clk_disable_unprepare(priv->clks[clk]);
  88. return ret;
  89. }
  90. static void ehci_platform_power_off(struct platform_device *dev)
  91. {
  92. struct usb_hcd *hcd = platform_get_drvdata(dev);
  93. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  94. int clk;
  95. for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
  96. if (priv->clks[clk])
  97. clk_disable_unprepare(priv->clks[clk]);
  98. }
  99. static struct hc_driver __read_mostly ehci_platform_hc_driver;
  100. static const struct ehci_driver_overrides platform_overrides __initconst = {
  101. .reset = ehci_platform_reset,
  102. .extra_priv_size = sizeof(struct ehci_platform_priv),
  103. };
  104. static struct usb_ehci_pdata ehci_platform_defaults = {
  105. .power_on = ehci_platform_power_on,
  106. .power_suspend = ehci_platform_power_off,
  107. .power_off = ehci_platform_power_off,
  108. };
  109. /**
  110. * quirk_poll_check_port_status - Poll port_status if the device sticks
  111. * @ehci: the ehci hcd pointer
  112. *
  113. * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting
  114. * stuck very rarely after a full/low usb device was disconnected. To
  115. * detect such a situation, the controllers require a special way which poll
  116. * the EHCI PORTSC register.
  117. *
  118. * Return: true if the controller's port_status indicated getting stuck
  119. */
  120. static bool quirk_poll_check_port_status(struct ehci_hcd *ehci)
  121. {
  122. u32 port_status = ehci_readl(ehci, &ehci->regs->port_status[0]);
  123. if (!(port_status & PORT_OWNER) &&
  124. (port_status & PORT_POWER) &&
  125. !(port_status & PORT_CONNECT) &&
  126. (port_status & PORT_LS_MASK))
  127. return true;
  128. return false;
  129. }
  130. /**
  131. * quirk_poll_rebind_companion - rebind comanion device to recover
  132. * @ehci: the ehci hcd pointer
  133. *
  134. * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting
  135. * stuck very rarely after a full/low usb device was disconnected. To
  136. * recover from such a situation, the controllers require changing the OHCI
  137. * functional state.
  138. */
  139. static void quirk_poll_rebind_companion(struct ehci_hcd *ehci)
  140. {
  141. struct device *companion_dev;
  142. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  143. companion_dev = usb_of_get_companion_dev(hcd->self.controller);
  144. if (!companion_dev)
  145. return;
  146. device_release_driver(companion_dev);
  147. if (device_attach(companion_dev) < 0)
  148. ehci_err(ehci, "%s: failed\n", __func__);
  149. put_device(companion_dev);
  150. }
  151. static void quirk_poll_work(struct work_struct *work)
  152. {
  153. struct ehci_platform_priv *priv =
  154. container_of(to_delayed_work(work), struct ehci_platform_priv,
  155. poll_work);
  156. struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd,
  157. priv);
  158. /* check the status twice to reduce misdetection rate */
  159. if (!quirk_poll_check_port_status(ehci))
  160. return;
  161. udelay(10);
  162. if (!quirk_poll_check_port_status(ehci))
  163. return;
  164. ehci_dbg(ehci, "%s: detected getting stuck. rebind now!\n", __func__);
  165. quirk_poll_rebind_companion(ehci);
  166. }
  167. static void quirk_poll_timer(struct timer_list *t)
  168. {
  169. struct ehci_platform_priv *priv = from_timer(priv, t, poll_timer);
  170. struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd,
  171. priv);
  172. if (quirk_poll_check_port_status(ehci)) {
  173. /*
  174. * Now scheduling the work for testing the port more. Note that
  175. * updating the status is possible to be delayed when
  176. * reconnection. So, this uses delayed work with 5 ms delay
  177. * to avoid misdetection.
  178. */
  179. schedule_delayed_work(&priv->poll_work, msecs_to_jiffies(5));
  180. }
  181. mod_timer(&priv->poll_timer, jiffies + HZ);
  182. }
  183. static void quirk_poll_init(struct ehci_platform_priv *priv)
  184. {
  185. INIT_DELAYED_WORK(&priv->poll_work, quirk_poll_work);
  186. timer_setup(&priv->poll_timer, quirk_poll_timer, 0);
  187. mod_timer(&priv->poll_timer, jiffies + HZ);
  188. }
  189. static void quirk_poll_end(struct ehci_platform_priv *priv)
  190. {
  191. del_timer_sync(&priv->poll_timer);
  192. cancel_delayed_work(&priv->poll_work);
  193. }
  194. static const struct soc_device_attribute quirk_poll_match[] = {
  195. { .family = "R-Car Gen3" },
  196. { /* sentinel*/ }
  197. };
  198. static int ehci_platform_probe(struct platform_device *dev)
  199. {
  200. struct usb_hcd *hcd;
  201. struct resource *res_mem;
  202. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  203. struct ehci_platform_priv *priv;
  204. struct ehci_hcd *ehci;
  205. int err, irq, clk = 0;
  206. if (usb_disabled())
  207. return -ENODEV;
  208. /*
  209. * Use reasonable defaults so platforms don't have to provide these
  210. * with DT probing on ARM.
  211. */
  212. if (!pdata)
  213. pdata = &ehci_platform_defaults;
  214. err = dma_coerce_mask_and_coherent(&dev->dev,
  215. pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
  216. if (err) {
  217. dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
  218. return err;
  219. }
  220. irq = platform_get_irq(dev, 0);
  221. if (irq < 0)
  222. return irq;
  223. hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
  224. dev_name(&dev->dev));
  225. if (!hcd)
  226. return -ENOMEM;
  227. platform_set_drvdata(dev, hcd);
  228. dev->dev.platform_data = pdata;
  229. priv = hcd_to_ehci_priv(hcd);
  230. ehci = hcd_to_ehci(hcd);
  231. if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
  232. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  233. ehci->big_endian_mmio = 1;
  234. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  235. ehci->big_endian_desc = 1;
  236. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  237. ehci->big_endian_mmio = ehci->big_endian_desc = 1;
  238. if (of_property_read_bool(dev->dev.of_node, "spurious-oc"))
  239. ehci->spurious_oc = 1;
  240. if (of_property_read_bool(dev->dev.of_node,
  241. "needs-reset-on-resume"))
  242. priv->reset_on_resume = true;
  243. if (of_property_read_bool(dev->dev.of_node,
  244. "has-transaction-translator"))
  245. hcd->has_tt = 1;
  246. if (of_device_is_compatible(dev->dev.of_node,
  247. "aspeed,ast2500-ehci") ||
  248. of_device_is_compatible(dev->dev.of_node,
  249. "aspeed,ast2600-ehci"))
  250. ehci->is_aspeed = 1;
  251. if (soc_device_match(quirk_poll_match))
  252. priv->quirk_poll = true;
  253. for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
  254. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  255. if (IS_ERR(priv->clks[clk])) {
  256. err = PTR_ERR(priv->clks[clk]);
  257. if (err == -EPROBE_DEFER)
  258. goto err_put_clks;
  259. priv->clks[clk] = NULL;
  260. break;
  261. }
  262. }
  263. }
  264. priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev);
  265. if (IS_ERR(priv->rsts)) {
  266. err = PTR_ERR(priv->rsts);
  267. goto err_put_clks;
  268. }
  269. err = reset_control_deassert(priv->rsts);
  270. if (err)
  271. goto err_put_clks;
  272. if (pdata->big_endian_desc)
  273. ehci->big_endian_desc = 1;
  274. if (pdata->big_endian_mmio)
  275. ehci->big_endian_mmio = 1;
  276. if (pdata->has_tt)
  277. hcd->has_tt = 1;
  278. if (pdata->reset_on_resume)
  279. priv->reset_on_resume = true;
  280. if (pdata->spurious_oc)
  281. ehci->spurious_oc = 1;
  282. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
  283. if (ehci->big_endian_mmio) {
  284. dev_err(&dev->dev,
  285. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
  286. err = -EINVAL;
  287. goto err_reset;
  288. }
  289. #endif
  290. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
  291. if (ehci->big_endian_desc) {
  292. dev_err(&dev->dev,
  293. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
  294. err = -EINVAL;
  295. goto err_reset;
  296. }
  297. #endif
  298. if (pdata->power_on) {
  299. err = pdata->power_on(dev);
  300. if (err < 0)
  301. goto err_reset;
  302. }
  303. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  304. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  305. if (IS_ERR(hcd->regs)) {
  306. err = PTR_ERR(hcd->regs);
  307. goto err_power;
  308. }
  309. hcd->rsrc_start = res_mem->start;
  310. hcd->rsrc_len = resource_size(res_mem);
  311. hcd->tpl_support = of_usb_host_tpl_support(dev->dev.of_node);
  312. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  313. if (err)
  314. goto err_power;
  315. device_wakeup_enable(hcd->self.controller);
  316. device_enable_async_suspend(hcd->self.controller);
  317. platform_set_drvdata(dev, hcd);
  318. if (priv->quirk_poll)
  319. quirk_poll_init(priv);
  320. return err;
  321. err_power:
  322. if (pdata->power_off)
  323. pdata->power_off(dev);
  324. err_reset:
  325. reset_control_assert(priv->rsts);
  326. err_put_clks:
  327. while (--clk >= 0)
  328. clk_put(priv->clks[clk]);
  329. if (pdata == &ehci_platform_defaults)
  330. dev->dev.platform_data = NULL;
  331. usb_put_hcd(hcd);
  332. return err;
  333. }
  334. static int ehci_platform_remove(struct platform_device *dev)
  335. {
  336. struct usb_hcd *hcd = platform_get_drvdata(dev);
  337. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  338. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  339. int clk;
  340. if (priv->quirk_poll)
  341. quirk_poll_end(priv);
  342. usb_remove_hcd(hcd);
  343. if (pdata->power_off)
  344. pdata->power_off(dev);
  345. reset_control_assert(priv->rsts);
  346. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
  347. clk_put(priv->clks[clk]);
  348. usb_put_hcd(hcd);
  349. if (pdata == &ehci_platform_defaults)
  350. dev->dev.platform_data = NULL;
  351. return 0;
  352. }
  353. static int __maybe_unused ehci_platform_suspend(struct device *dev)
  354. {
  355. struct usb_hcd *hcd = dev_get_drvdata(dev);
  356. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  357. struct platform_device *pdev = to_platform_device(dev);
  358. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  359. bool do_wakeup = device_may_wakeup(dev);
  360. int ret;
  361. if (priv->quirk_poll)
  362. quirk_poll_end(priv);
  363. ret = ehci_suspend(hcd, do_wakeup);
  364. if (ret)
  365. return ret;
  366. if (pdata->power_suspend)
  367. pdata->power_suspend(pdev);
  368. return ret;
  369. }
  370. static int __maybe_unused ehci_platform_resume(struct device *dev)
  371. {
  372. struct usb_hcd *hcd = dev_get_drvdata(dev);
  373. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  374. struct platform_device *pdev = to_platform_device(dev);
  375. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  376. struct device *companion_dev;
  377. if (pdata->power_on) {
  378. int err = pdata->power_on(pdev);
  379. if (err < 0)
  380. return err;
  381. }
  382. companion_dev = usb_of_get_companion_dev(hcd->self.controller);
  383. if (companion_dev) {
  384. device_pm_wait_for_dev(hcd->self.controller, companion_dev);
  385. put_device(companion_dev);
  386. }
  387. ehci_resume(hcd, priv->reset_on_resume);
  388. pm_runtime_disable(dev);
  389. pm_runtime_set_active(dev);
  390. pm_runtime_enable(dev);
  391. if (priv->quirk_poll)
  392. quirk_poll_init(priv);
  393. return 0;
  394. }
  395. static const struct of_device_id vt8500_ehci_ids[] = {
  396. { .compatible = "via,vt8500-ehci", },
  397. { .compatible = "wm,prizm-ehci", },
  398. { .compatible = "generic-ehci", },
  399. { .compatible = "cavium,octeon-6335-ehci", },
  400. {}
  401. };
  402. MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
  403. #ifdef CONFIG_ACPI
  404. static const struct acpi_device_id ehci_acpi_match[] = {
  405. { "PNP0D20", 0 }, /* EHCI controller without debug */
  406. { }
  407. };
  408. MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
  409. #endif
  410. static const struct platform_device_id ehci_platform_table[] = {
  411. { "ehci-platform", 0 },
  412. { }
  413. };
  414. MODULE_DEVICE_TABLE(platform, ehci_platform_table);
  415. static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
  416. ehci_platform_resume);
  417. static struct platform_driver ehci_platform_driver = {
  418. .id_table = ehci_platform_table,
  419. .probe = ehci_platform_probe,
  420. .remove = ehci_platform_remove,
  421. .shutdown = usb_hcd_platform_shutdown,
  422. .driver = {
  423. .name = "ehci-platform",
  424. .pm = pm_ptr(&ehci_platform_pm_ops),
  425. .of_match_table = vt8500_ehci_ids,
  426. .acpi_match_table = ACPI_PTR(ehci_acpi_match),
  427. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  428. }
  429. };
  430. static int __init ehci_platform_init(void)
  431. {
  432. if (usb_disabled())
  433. return -ENODEV;
  434. ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
  435. return platform_driver_register(&ehci_platform_driver);
  436. }
  437. module_init(ehci_platform_init);
  438. static void __exit ehci_platform_cleanup(void)
  439. {
  440. platform_driver_unregister(&ehci_platform_driver);
  441. }
  442. module_exit(ehci_platform_cleanup);
  443. MODULE_DESCRIPTION(DRIVER_DESC);
  444. MODULE_AUTHOR("Hauke Mehrtens");
  445. MODULE_AUTHOR("Alan Stern");
  446. MODULE_LICENSE("GPL");