ehci-platform.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/usb.h>
  33. #include <linux/usb/hcd.h>
  34. #include <linux/usb/ehci_pdriver.h>
  35. #include <linux/usb/of.h>
  36. #include "ehci.h"
  37. #define DRIVER_DESC "EHCI generic platform driver"
  38. #define EHCI_MAX_CLKS 4
  39. #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
  40. struct ehci_platform_priv {
  41. struct clk *clks[EHCI_MAX_CLKS];
  42. struct reset_control *rsts;
  43. bool reset_on_resume;
  44. };
  45. static const char hcd_name[] = "ehci-platform";
  46. static int ehci_platform_reset(struct usb_hcd *hcd)
  47. {
  48. struct platform_device *pdev = to_platform_device(hcd->self.controller);
  49. struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
  50. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  51. int retval;
  52. ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
  53. if (pdata->pre_setup) {
  54. retval = pdata->pre_setup(hcd);
  55. if (retval < 0)
  56. return retval;
  57. }
  58. ehci->caps = hcd->regs + pdata->caps_offset;
  59. retval = ehci_setup(hcd);
  60. if (retval)
  61. return retval;
  62. if (pdata->no_io_watchdog)
  63. ehci->need_io_watchdog = 0;
  64. return 0;
  65. }
  66. static int ehci_platform_power_on(struct platform_device *dev)
  67. {
  68. struct usb_hcd *hcd = platform_get_drvdata(dev);
  69. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  70. int clk, ret;
  71. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
  72. ret = clk_prepare_enable(priv->clks[clk]);
  73. if (ret)
  74. goto err_disable_clks;
  75. }
  76. return 0;
  77. err_disable_clks:
  78. while (--clk >= 0)
  79. clk_disable_unprepare(priv->clks[clk]);
  80. return ret;
  81. }
  82. static void ehci_platform_power_off(struct platform_device *dev)
  83. {
  84. struct usb_hcd *hcd = platform_get_drvdata(dev);
  85. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  86. int clk;
  87. for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
  88. if (priv->clks[clk])
  89. clk_disable_unprepare(priv->clks[clk]);
  90. }
  91. static struct hc_driver __read_mostly ehci_platform_hc_driver;
  92. static const struct ehci_driver_overrides platform_overrides __initconst = {
  93. .reset = ehci_platform_reset,
  94. .extra_priv_size = sizeof(struct ehci_platform_priv),
  95. };
  96. static struct usb_ehci_pdata ehci_platform_defaults = {
  97. .power_on = ehci_platform_power_on,
  98. .power_suspend = ehci_platform_power_off,
  99. .power_off = ehci_platform_power_off,
  100. };
  101. static int ehci_platform_probe(struct platform_device *dev)
  102. {
  103. struct usb_hcd *hcd;
  104. struct resource *res_mem;
  105. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  106. struct ehci_platform_priv *priv;
  107. struct ehci_hcd *ehci;
  108. int err, irq, clk = 0;
  109. if (usb_disabled())
  110. return -ENODEV;
  111. /*
  112. * Use reasonable defaults so platforms don't have to provide these
  113. * with DT probing on ARM.
  114. */
  115. if (!pdata)
  116. pdata = &ehci_platform_defaults;
  117. err = dma_coerce_mask_and_coherent(&dev->dev,
  118. pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
  119. if (err) {
  120. dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
  121. return err;
  122. }
  123. irq = platform_get_irq(dev, 0);
  124. if (irq < 0) {
  125. dev_err(&dev->dev, "no irq provided");
  126. return irq;
  127. }
  128. hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
  129. dev_name(&dev->dev));
  130. if (!hcd)
  131. return -ENOMEM;
  132. platform_set_drvdata(dev, hcd);
  133. dev->dev.platform_data = pdata;
  134. priv = hcd_to_ehci_priv(hcd);
  135. ehci = hcd_to_ehci(hcd);
  136. if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
  137. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  138. ehci->big_endian_mmio = 1;
  139. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  140. ehci->big_endian_desc = 1;
  141. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  142. ehci->big_endian_mmio = ehci->big_endian_desc = 1;
  143. if (of_property_read_bool(dev->dev.of_node,
  144. "needs-reset-on-resume"))
  145. priv->reset_on_resume = true;
  146. if (of_property_read_bool(dev->dev.of_node,
  147. "has-transaction-translator"))
  148. hcd->has_tt = 1;
  149. for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
  150. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  151. if (IS_ERR(priv->clks[clk])) {
  152. err = PTR_ERR(priv->clks[clk]);
  153. if (err == -EPROBE_DEFER)
  154. goto err_put_clks;
  155. priv->clks[clk] = NULL;
  156. break;
  157. }
  158. }
  159. }
  160. priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev);
  161. if (IS_ERR(priv->rsts)) {
  162. err = PTR_ERR(priv->rsts);
  163. goto err_put_clks;
  164. }
  165. err = reset_control_deassert(priv->rsts);
  166. if (err)
  167. goto err_put_clks;
  168. if (pdata->big_endian_desc)
  169. ehci->big_endian_desc = 1;
  170. if (pdata->big_endian_mmio)
  171. ehci->big_endian_mmio = 1;
  172. if (pdata->has_tt)
  173. hcd->has_tt = 1;
  174. if (pdata->reset_on_resume)
  175. priv->reset_on_resume = true;
  176. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
  177. if (ehci->big_endian_mmio) {
  178. dev_err(&dev->dev,
  179. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
  180. err = -EINVAL;
  181. goto err_reset;
  182. }
  183. #endif
  184. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
  185. if (ehci->big_endian_desc) {
  186. dev_err(&dev->dev,
  187. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
  188. err = -EINVAL;
  189. goto err_reset;
  190. }
  191. #endif
  192. if (pdata->power_on) {
  193. err = pdata->power_on(dev);
  194. if (err < 0)
  195. goto err_reset;
  196. }
  197. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  198. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  199. if (IS_ERR(hcd->regs)) {
  200. err = PTR_ERR(hcd->regs);
  201. goto err_power;
  202. }
  203. hcd->rsrc_start = res_mem->start;
  204. hcd->rsrc_len = resource_size(res_mem);
  205. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  206. if (err)
  207. goto err_power;
  208. device_wakeup_enable(hcd->self.controller);
  209. device_enable_async_suspend(hcd->self.controller);
  210. platform_set_drvdata(dev, hcd);
  211. return err;
  212. err_power:
  213. if (pdata->power_off)
  214. pdata->power_off(dev);
  215. err_reset:
  216. reset_control_assert(priv->rsts);
  217. err_put_clks:
  218. while (--clk >= 0)
  219. clk_put(priv->clks[clk]);
  220. if (pdata == &ehci_platform_defaults)
  221. dev->dev.platform_data = NULL;
  222. usb_put_hcd(hcd);
  223. return err;
  224. }
  225. static int ehci_platform_remove(struct platform_device *dev)
  226. {
  227. struct usb_hcd *hcd = platform_get_drvdata(dev);
  228. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  229. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  230. int clk;
  231. usb_remove_hcd(hcd);
  232. if (pdata->power_off)
  233. pdata->power_off(dev);
  234. reset_control_assert(priv->rsts);
  235. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
  236. clk_put(priv->clks[clk]);
  237. usb_put_hcd(hcd);
  238. if (pdata == &ehci_platform_defaults)
  239. dev->dev.platform_data = NULL;
  240. return 0;
  241. }
  242. #ifdef CONFIG_PM_SLEEP
  243. static int ehci_platform_suspend(struct device *dev)
  244. {
  245. struct usb_hcd *hcd = dev_get_drvdata(dev);
  246. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  247. struct platform_device *pdev = to_platform_device(dev);
  248. bool do_wakeup = device_may_wakeup(dev);
  249. int ret;
  250. ret = ehci_suspend(hcd, do_wakeup);
  251. if (ret)
  252. return ret;
  253. if (pdata->power_suspend)
  254. pdata->power_suspend(pdev);
  255. return ret;
  256. }
  257. static int ehci_platform_resume(struct device *dev)
  258. {
  259. struct usb_hcd *hcd = dev_get_drvdata(dev);
  260. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  261. struct platform_device *pdev = to_platform_device(dev);
  262. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  263. struct device *companion_dev;
  264. if (pdata->power_on) {
  265. int err = pdata->power_on(pdev);
  266. if (err < 0)
  267. return err;
  268. }
  269. companion_dev = usb_of_get_companion_dev(hcd->self.controller);
  270. if (companion_dev) {
  271. device_pm_wait_for_dev(hcd->self.controller, companion_dev);
  272. put_device(companion_dev);
  273. }
  274. ehci_resume(hcd, priv->reset_on_resume);
  275. return 0;
  276. }
  277. #endif /* CONFIG_PM_SLEEP */
  278. static const struct of_device_id vt8500_ehci_ids[] = {
  279. { .compatible = "via,vt8500-ehci", },
  280. { .compatible = "wm,prizm-ehci", },
  281. { .compatible = "generic-ehci", },
  282. { .compatible = "cavium,octeon-6335-ehci", },
  283. {}
  284. };
  285. MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
  286. static const struct acpi_device_id ehci_acpi_match[] = {
  287. { "PNP0D20", 0 }, /* EHCI controller without debug */
  288. { }
  289. };
  290. MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
  291. static const struct platform_device_id ehci_platform_table[] = {
  292. { "ehci-platform", 0 },
  293. { }
  294. };
  295. MODULE_DEVICE_TABLE(platform, ehci_platform_table);
  296. static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
  297. ehci_platform_resume);
  298. static struct platform_driver ehci_platform_driver = {
  299. .id_table = ehci_platform_table,
  300. .probe = ehci_platform_probe,
  301. .remove = ehci_platform_remove,
  302. .shutdown = usb_hcd_platform_shutdown,
  303. .driver = {
  304. .name = "ehci-platform",
  305. .pm = &ehci_platform_pm_ops,
  306. .of_match_table = vt8500_ehci_ids,
  307. .acpi_match_table = ACPI_PTR(ehci_acpi_match),
  308. }
  309. };
  310. static int __init ehci_platform_init(void)
  311. {
  312. if (usb_disabled())
  313. return -ENODEV;
  314. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  315. ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
  316. return platform_driver_register(&ehci_platform_driver);
  317. }
  318. module_init(ehci_platform_init);
  319. static void __exit ehci_platform_cleanup(void)
  320. {
  321. platform_driver_unregister(&ehci_platform_driver);
  322. }
  323. module_exit(ehci_platform_cleanup);
  324. MODULE_DESCRIPTION(DRIVER_DESC);
  325. MODULE_AUTHOR("Hauke Mehrtens");
  326. MODULE_AUTHOR("Alan Stern");
  327. MODULE_LICENSE("GPL");