ehci-platform.c 13 KB

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