ehci-platform.c 11 KB

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