ehci-platform.c 9.4 KB

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