ehci-platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Generic platform ehci driver
  3. *
  4. * Copyright 2007 Steven Brown <sbrown@cortland.com>
  5. * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
  6. * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  7. *
  8. * Derived from the ohci-ssb driver
  9. * Copyright 2007 Michael Buesch <m@bues.ch>
  10. *
  11. * Derived from the EHCI-PCI driver
  12. * Copyright (c) 2000-2004 by David Brownell
  13. *
  14. * Derived from the ohci-pci driver
  15. * Copyright 1999 Roman Weissgaerber
  16. * Copyright 2000-2002 David Brownell
  17. * Copyright 1999 Linus Torvalds
  18. * Copyright 1999 Gregory P. Smith
  19. *
  20. * Licensed under the GNU/GPL. See COPYING for details.
  21. */
  22. #include <linux/acpi.h>
  23. #include <linux/clk.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/err.h>
  26. #include <linux/kernel.h>
  27. #include <linux/hrtimer.h>
  28. #include <linux/io.h>
  29. #include <linux/module.h>
  30. #include <linux/of.h>
  31. #include <linux/phy/phy.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/reset.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 EHCI_MAX_RSTS 4
  42. #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
  43. struct ehci_platform_priv {
  44. struct clk *clks[EHCI_MAX_CLKS];
  45. struct reset_control *rsts[EHCI_MAX_RSTS];
  46. struct phy **phys;
  47. int num_phys;
  48. bool reset_on_resume;
  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, phy_num;
  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. for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  82. ret = phy_init(priv->phys[phy_num]);
  83. if (ret)
  84. goto err_exit_phy;
  85. ret = phy_power_on(priv->phys[phy_num]);
  86. if (ret) {
  87. phy_exit(priv->phys[phy_num]);
  88. goto err_exit_phy;
  89. }
  90. }
  91. return 0;
  92. err_exit_phy:
  93. while (--phy_num >= 0) {
  94. phy_power_off(priv->phys[phy_num]);
  95. phy_exit(priv->phys[phy_num]);
  96. }
  97. err_disable_clks:
  98. while (--clk >= 0)
  99. clk_disable_unprepare(priv->clks[clk]);
  100. return ret;
  101. }
  102. static void ehci_platform_power_off(struct platform_device *dev)
  103. {
  104. struct usb_hcd *hcd = platform_get_drvdata(dev);
  105. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  106. int clk, phy_num;
  107. for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  108. phy_power_off(priv->phys[phy_num]);
  109. phy_exit(priv->phys[phy_num]);
  110. }
  111. for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
  112. if (priv->clks[clk])
  113. clk_disable_unprepare(priv->clks[clk]);
  114. }
  115. static struct hc_driver __read_mostly ehci_platform_hc_driver;
  116. static const struct ehci_driver_overrides platform_overrides __initconst = {
  117. .reset = ehci_platform_reset,
  118. .extra_priv_size = sizeof(struct ehci_platform_priv),
  119. };
  120. static struct usb_ehci_pdata ehci_platform_defaults = {
  121. .power_on = ehci_platform_power_on,
  122. .power_suspend = ehci_platform_power_off,
  123. .power_off = ehci_platform_power_off,
  124. };
  125. static int ehci_platform_probe(struct platform_device *dev)
  126. {
  127. struct usb_hcd *hcd;
  128. struct resource *res_mem;
  129. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  130. struct ehci_platform_priv *priv;
  131. struct ehci_hcd *ehci;
  132. int err, irq, phy_num, clk = 0, rst;
  133. if (usb_disabled())
  134. return -ENODEV;
  135. /*
  136. * Use reasonable defaults so platforms don't have to provide these
  137. * with DT probing on ARM.
  138. */
  139. if (!pdata)
  140. pdata = &ehci_platform_defaults;
  141. err = dma_coerce_mask_and_coherent(&dev->dev,
  142. pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
  143. if (err) {
  144. dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
  145. return err;
  146. }
  147. irq = platform_get_irq(dev, 0);
  148. if (irq < 0) {
  149. dev_err(&dev->dev, "no irq provided");
  150. return irq;
  151. }
  152. hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
  153. dev_name(&dev->dev));
  154. if (!hcd)
  155. return -ENOMEM;
  156. platform_set_drvdata(dev, hcd);
  157. dev->dev.platform_data = pdata;
  158. priv = hcd_to_ehci_priv(hcd);
  159. ehci = hcd_to_ehci(hcd);
  160. if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
  161. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  162. ehci->big_endian_mmio = 1;
  163. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  164. ehci->big_endian_desc = 1;
  165. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  166. ehci->big_endian_mmio = ehci->big_endian_desc = 1;
  167. if (of_property_read_bool(dev->dev.of_node,
  168. "needs-reset-on-resume"))
  169. priv->reset_on_resume = true;
  170. if (of_property_read_bool(dev->dev.of_node,
  171. "has-transaction-translator"))
  172. hcd->has_tt = 1;
  173. priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
  174. "phys", "#phy-cells");
  175. if (priv->num_phys > 0) {
  176. priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
  177. sizeof(struct phy *), GFP_KERNEL);
  178. if (!priv->phys)
  179. return -ENOMEM;
  180. } else
  181. priv->num_phys = 0;
  182. for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  183. priv->phys[phy_num] = devm_of_phy_get_by_index(
  184. &dev->dev, dev->dev.of_node, phy_num);
  185. if (IS_ERR(priv->phys[phy_num])) {
  186. err = PTR_ERR(priv->phys[phy_num]);
  187. goto err_put_hcd;
  188. } else if (!hcd->phy) {
  189. /* Avoiding phy_get() in usb_add_hcd() */
  190. hcd->phy = priv->phys[phy_num];
  191. }
  192. }
  193. for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
  194. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  195. if (IS_ERR(priv->clks[clk])) {
  196. err = PTR_ERR(priv->clks[clk]);
  197. if (err == -EPROBE_DEFER)
  198. goto err_put_clks;
  199. priv->clks[clk] = NULL;
  200. break;
  201. }
  202. }
  203. }
  204. for (rst = 0; rst < EHCI_MAX_RSTS; rst++) {
  205. priv->rsts[rst] = devm_reset_control_get_shared_by_index(
  206. &dev->dev, rst);
  207. if (IS_ERR(priv->rsts[rst])) {
  208. err = PTR_ERR(priv->rsts[rst]);
  209. if (err == -EPROBE_DEFER)
  210. goto err_reset;
  211. priv->rsts[rst] = NULL;
  212. break;
  213. }
  214. err = reset_control_deassert(priv->rsts[rst]);
  215. if (err)
  216. goto err_reset;
  217. }
  218. if (pdata->big_endian_desc)
  219. ehci->big_endian_desc = 1;
  220. if (pdata->big_endian_mmio)
  221. ehci->big_endian_mmio = 1;
  222. if (pdata->has_tt)
  223. hcd->has_tt = 1;
  224. if (pdata->reset_on_resume)
  225. priv->reset_on_resume = true;
  226. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
  227. if (ehci->big_endian_mmio) {
  228. dev_err(&dev->dev,
  229. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
  230. err = -EINVAL;
  231. goto err_reset;
  232. }
  233. #endif
  234. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
  235. if (ehci->big_endian_desc) {
  236. dev_err(&dev->dev,
  237. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
  238. err = -EINVAL;
  239. goto err_reset;
  240. }
  241. #endif
  242. if (pdata->power_on) {
  243. err = pdata->power_on(dev);
  244. if (err < 0)
  245. goto err_reset;
  246. }
  247. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  248. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  249. if (IS_ERR(hcd->regs)) {
  250. err = PTR_ERR(hcd->regs);
  251. goto err_power;
  252. }
  253. hcd->rsrc_start = res_mem->start;
  254. hcd->rsrc_len = resource_size(res_mem);
  255. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  256. if (err)
  257. goto err_power;
  258. device_wakeup_enable(hcd->self.controller);
  259. device_enable_async_suspend(hcd->self.controller);
  260. platform_set_drvdata(dev, hcd);
  261. return err;
  262. err_power:
  263. if (pdata->power_off)
  264. pdata->power_off(dev);
  265. err_reset:
  266. while (--rst >= 0)
  267. reset_control_assert(priv->rsts[rst]);
  268. err_put_clks:
  269. while (--clk >= 0)
  270. clk_put(priv->clks[clk]);
  271. err_put_hcd:
  272. if (pdata == &ehci_platform_defaults)
  273. dev->dev.platform_data = NULL;
  274. usb_put_hcd(hcd);
  275. return err;
  276. }
  277. static int ehci_platform_remove(struct platform_device *dev)
  278. {
  279. struct usb_hcd *hcd = platform_get_drvdata(dev);
  280. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  281. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  282. int clk, rst;
  283. usb_remove_hcd(hcd);
  284. if (pdata->power_off)
  285. pdata->power_off(dev);
  286. for (rst = 0; rst < EHCI_MAX_RSTS && priv->rsts[rst]; rst++)
  287. reset_control_assert(priv->rsts[rst]);
  288. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
  289. clk_put(priv->clks[clk]);
  290. usb_put_hcd(hcd);
  291. if (pdata == &ehci_platform_defaults)
  292. dev->dev.platform_data = NULL;
  293. return 0;
  294. }
  295. #ifdef CONFIG_PM_SLEEP
  296. static int ehci_platform_suspend(struct device *dev)
  297. {
  298. struct usb_hcd *hcd = dev_get_drvdata(dev);
  299. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  300. struct platform_device *pdev = to_platform_device(dev);
  301. bool do_wakeup = device_may_wakeup(dev);
  302. int ret;
  303. ret = ehci_suspend(hcd, do_wakeup);
  304. if (ret)
  305. return ret;
  306. if (pdata->power_suspend)
  307. pdata->power_suspend(pdev);
  308. return ret;
  309. }
  310. static int ehci_platform_resume(struct device *dev)
  311. {
  312. struct usb_hcd *hcd = dev_get_drvdata(dev);
  313. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  314. struct platform_device *pdev = to_platform_device(dev);
  315. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  316. struct device *companion_dev;
  317. if (pdata->power_on) {
  318. int err = pdata->power_on(pdev);
  319. if (err < 0)
  320. return err;
  321. }
  322. companion_dev = usb_of_get_companion_dev(hcd->self.controller);
  323. if (companion_dev) {
  324. device_pm_wait_for_dev(hcd->self.controller, companion_dev);
  325. put_device(companion_dev);
  326. }
  327. ehci_resume(hcd, priv->reset_on_resume);
  328. return 0;
  329. }
  330. #endif /* CONFIG_PM_SLEEP */
  331. static const struct of_device_id vt8500_ehci_ids[] = {
  332. { .compatible = "via,vt8500-ehci", },
  333. { .compatible = "wm,prizm-ehci", },
  334. { .compatible = "generic-ehci", },
  335. { .compatible = "cavium,octeon-6335-ehci", },
  336. {}
  337. };
  338. MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
  339. static const struct acpi_device_id ehci_acpi_match[] = {
  340. { "PNP0D20", 0 }, /* EHCI controller without debug */
  341. { }
  342. };
  343. MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
  344. static const struct platform_device_id ehci_platform_table[] = {
  345. { "ehci-platform", 0 },
  346. { }
  347. };
  348. MODULE_DEVICE_TABLE(platform, ehci_platform_table);
  349. static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
  350. ehci_platform_resume);
  351. static struct platform_driver ehci_platform_driver = {
  352. .id_table = ehci_platform_table,
  353. .probe = ehci_platform_probe,
  354. .remove = ehci_platform_remove,
  355. .shutdown = usb_hcd_platform_shutdown,
  356. .driver = {
  357. .name = "ehci-platform",
  358. .pm = &ehci_platform_pm_ops,
  359. .of_match_table = vt8500_ehci_ids,
  360. .acpi_match_table = ACPI_PTR(ehci_acpi_match),
  361. }
  362. };
  363. static int __init ehci_platform_init(void)
  364. {
  365. if (usb_disabled())
  366. return -ENODEV;
  367. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  368. ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
  369. return platform_driver_register(&ehci_platform_driver);
  370. }
  371. module_init(ehci_platform_init);
  372. static void __exit ehci_platform_cleanup(void)
  373. {
  374. platform_driver_unregister(&ehci_platform_driver);
  375. }
  376. module_exit(ehci_platform_cleanup);
  377. MODULE_DESCRIPTION(DRIVER_DESC);
  378. MODULE_AUTHOR("Hauke Mehrtens");
  379. MODULE_AUTHOR("Alan Stern");
  380. MODULE_LICENSE("GPL");