ehci-platform.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
  141. dev_name(&dev->dev));
  142. if (!hcd)
  143. return -ENOMEM;
  144. platform_set_drvdata(dev, hcd);
  145. dev->dev.platform_data = pdata;
  146. priv = hcd_to_ehci_priv(hcd);
  147. ehci = hcd_to_ehci(hcd);
  148. if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
  149. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  150. ehci->big_endian_mmio = 1;
  151. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  152. ehci->big_endian_desc = 1;
  153. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  154. ehci->big_endian_mmio = ehci->big_endian_desc = 1;
  155. priv->phy = devm_phy_get(&dev->dev, "usb");
  156. if (IS_ERR(priv->phy)) {
  157. err = PTR_ERR(priv->phy);
  158. if (err == -EPROBE_DEFER)
  159. goto err_put_hcd;
  160. priv->phy = NULL;
  161. }
  162. for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
  163. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  164. if (IS_ERR(priv->clks[clk])) {
  165. err = PTR_ERR(priv->clks[clk]);
  166. if (err == -EPROBE_DEFER)
  167. goto err_put_clks;
  168. priv->clks[clk] = NULL;
  169. break;
  170. }
  171. }
  172. }
  173. priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
  174. if (IS_ERR(priv->rst)) {
  175. err = PTR_ERR(priv->rst);
  176. if (err == -EPROBE_DEFER)
  177. goto err_put_clks;
  178. priv->rst = NULL;
  179. } else {
  180. err = reset_control_deassert(priv->rst);
  181. if (err)
  182. goto err_put_clks;
  183. }
  184. if (pdata->big_endian_desc)
  185. ehci->big_endian_desc = 1;
  186. if (pdata->big_endian_mmio)
  187. ehci->big_endian_mmio = 1;
  188. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
  189. if (ehci->big_endian_mmio) {
  190. dev_err(&dev->dev,
  191. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
  192. err = -EINVAL;
  193. goto err_reset;
  194. }
  195. #endif
  196. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
  197. if (ehci->big_endian_desc) {
  198. dev_err(&dev->dev,
  199. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
  200. err = -EINVAL;
  201. goto err_reset;
  202. }
  203. #endif
  204. if (pdata->power_on) {
  205. err = pdata->power_on(dev);
  206. if (err < 0)
  207. goto err_reset;
  208. }
  209. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  210. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  211. if (IS_ERR(hcd->regs)) {
  212. err = PTR_ERR(hcd->regs);
  213. goto err_power;
  214. }
  215. hcd->rsrc_start = res_mem->start;
  216. hcd->rsrc_len = resource_size(res_mem);
  217. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  218. if (err)
  219. goto err_power;
  220. device_wakeup_enable(hcd->self.controller);
  221. platform_set_drvdata(dev, hcd);
  222. return err;
  223. err_power:
  224. if (pdata->power_off)
  225. pdata->power_off(dev);
  226. err_reset:
  227. if (priv->rst)
  228. reset_control_assert(priv->rst);
  229. err_put_clks:
  230. while (--clk >= 0)
  231. clk_put(priv->clks[clk]);
  232. err_put_hcd:
  233. if (pdata == &ehci_platform_defaults)
  234. dev->dev.platform_data = NULL;
  235. usb_put_hcd(hcd);
  236. return err;
  237. }
  238. static int ehci_platform_remove(struct platform_device *dev)
  239. {
  240. struct usb_hcd *hcd = platform_get_drvdata(dev);
  241. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  242. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  243. int clk;
  244. usb_remove_hcd(hcd);
  245. if (pdata->power_off)
  246. pdata->power_off(dev);
  247. if (priv->rst)
  248. reset_control_assert(priv->rst);
  249. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
  250. clk_put(priv->clks[clk]);
  251. usb_put_hcd(hcd);
  252. if (pdata == &ehci_platform_defaults)
  253. dev->dev.platform_data = NULL;
  254. return 0;
  255. }
  256. #ifdef CONFIG_PM_SLEEP
  257. static int ehci_platform_suspend(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 =
  262. container_of(dev, struct platform_device, dev);
  263. bool do_wakeup = device_may_wakeup(dev);
  264. int ret;
  265. ret = ehci_suspend(hcd, do_wakeup);
  266. if (ret)
  267. return ret;
  268. if (pdata->power_suspend)
  269. pdata->power_suspend(pdev);
  270. return ret;
  271. }
  272. static int ehci_platform_resume(struct device *dev)
  273. {
  274. struct usb_hcd *hcd = dev_get_drvdata(dev);
  275. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  276. struct platform_device *pdev =
  277. container_of(dev, struct platform_device, dev);
  278. if (pdata->power_on) {
  279. int err = pdata->power_on(pdev);
  280. if (err < 0)
  281. return err;
  282. }
  283. ehci_resume(hcd, false);
  284. return 0;
  285. }
  286. #endif /* CONFIG_PM_SLEEP */
  287. static const struct of_device_id vt8500_ehci_ids[] = {
  288. { .compatible = "via,vt8500-ehci", },
  289. { .compatible = "wm,prizm-ehci", },
  290. { .compatible = "generic-ehci", },
  291. {}
  292. };
  293. MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
  294. static const struct platform_device_id ehci_platform_table[] = {
  295. { "ehci-platform", 0 },
  296. { }
  297. };
  298. MODULE_DEVICE_TABLE(platform, ehci_platform_table);
  299. static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
  300. ehci_platform_resume);
  301. static struct platform_driver ehci_platform_driver = {
  302. .id_table = ehci_platform_table,
  303. .probe = ehci_platform_probe,
  304. .remove = ehci_platform_remove,
  305. .shutdown = usb_hcd_platform_shutdown,
  306. .driver = {
  307. .name = "ehci-platform",
  308. .pm = &ehci_platform_pm_ops,
  309. .of_match_table = vt8500_ehci_ids,
  310. }
  311. };
  312. static int __init ehci_platform_init(void)
  313. {
  314. if (usb_disabled())
  315. return -ENODEV;
  316. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  317. ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
  318. return platform_driver_register(&ehci_platform_driver);
  319. }
  320. module_init(ehci_platform_init);
  321. static void __exit ehci_platform_cleanup(void)
  322. {
  323. platform_driver_unregister(&ehci_platform_driver);
  324. }
  325. module_exit(ehci_platform_cleanup);
  326. MODULE_DESCRIPTION(DRIVER_DESC);
  327. MODULE_AUTHOR("Hauke Mehrtens");
  328. MODULE_AUTHOR("Alan Stern");
  329. MODULE_LICENSE("GPL");