ehci-omap.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ehci-omap.c - driver for USBHOST on OMAP3/4 processors
  4. *
  5. * Bus Glue for the EHCI controllers in OMAP3/4
  6. * Tested on several OMAP3 boards, and OMAP4 Pandaboard
  7. *
  8. * Copyright (C) 2007-2013 Texas Instruments, Inc.
  9. * Author: Vikram Pandita <vikram.pandita@ti.com>
  10. * Author: Anand Gadiyar <gadiyar@ti.com>
  11. * Author: Keshava Munegowda <keshava_mgowda@ti.com>
  12. * Author: Roger Quadros <rogerq@ti.com>
  13. *
  14. * Copyright (C) 2009 Nokia Corporation
  15. * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  16. *
  17. * Based on "ehci-fsl.c" and "ehci-au1xxx.c" ehci glue layers
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/io.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/usb/ulpi.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/gpio.h>
  27. #include <linux/clk.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/hcd.h>
  30. #include <linux/of.h>
  31. #include <linux/dma-mapping.h>
  32. #include "ehci.h"
  33. #include <linux/platform_data/usb-omap.h>
  34. /* EHCI Register Set */
  35. #define EHCI_INSNREG04 (0xA0)
  36. #define EHCI_INSNREG04_DISABLE_UNSUSPEND (1 << 5)
  37. #define EHCI_INSNREG05_ULPI (0xA4)
  38. #define EHCI_INSNREG05_ULPI_CONTROL_SHIFT 31
  39. #define EHCI_INSNREG05_ULPI_PORTSEL_SHIFT 24
  40. #define EHCI_INSNREG05_ULPI_OPSEL_SHIFT 22
  41. #define EHCI_INSNREG05_ULPI_REGADD_SHIFT 16
  42. #define EHCI_INSNREG05_ULPI_EXTREGADD_SHIFT 8
  43. #define EHCI_INSNREG05_ULPI_WRDATA_SHIFT 0
  44. #define DRIVER_DESC "OMAP-EHCI Host Controller driver"
  45. static const char hcd_name[] = "ehci-omap";
  46. /*-------------------------------------------------------------------------*/
  47. struct omap_hcd {
  48. struct usb_phy *phy[OMAP3_HS_USB_PORTS]; /* one PHY for each port */
  49. int nports;
  50. };
  51. static inline void ehci_write(void __iomem *base, u32 reg, u32 val)
  52. {
  53. __raw_writel(val, base + reg);
  54. }
  55. static inline u32 ehci_read(void __iomem *base, u32 reg)
  56. {
  57. return __raw_readl(base + reg);
  58. }
  59. /* configure so an HC device and id are always provided */
  60. /* always called with process context; sleeping is OK */
  61. static struct hc_driver __read_mostly ehci_omap_hc_driver;
  62. static const struct ehci_driver_overrides ehci_omap_overrides __initconst = {
  63. .extra_priv_size = sizeof(struct omap_hcd),
  64. };
  65. /**
  66. * ehci_hcd_omap_probe - initialize TI-based HCDs
  67. *
  68. * Allocates basic resources for this USB host controller, and
  69. * then invokes the start() method for the HCD associated with it
  70. * through the hotplug entry's driver_data.
  71. */
  72. static int ehci_hcd_omap_probe(struct platform_device *pdev)
  73. {
  74. struct device *dev = &pdev->dev;
  75. struct usbhs_omap_platform_data *pdata = dev_get_platdata(dev);
  76. struct resource *res;
  77. struct usb_hcd *hcd;
  78. void __iomem *regs;
  79. int ret;
  80. int irq;
  81. int i;
  82. struct omap_hcd *omap;
  83. if (usb_disabled())
  84. return -ENODEV;
  85. if (!dev->parent) {
  86. dev_err(dev, "Missing parent device\n");
  87. return -ENODEV;
  88. }
  89. /* For DT boot, get platform data from parent. i.e. usbhshost */
  90. if (dev->of_node) {
  91. pdata = dev_get_platdata(dev->parent);
  92. dev->platform_data = pdata;
  93. }
  94. if (!pdata) {
  95. dev_err(dev, "Missing platform data\n");
  96. return -ENODEV;
  97. }
  98. irq = platform_get_irq(pdev, 0);
  99. if (irq < 0) {
  100. dev_err(dev, "EHCI irq failed: %d\n", irq);
  101. return irq;
  102. }
  103. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  104. regs = devm_ioremap_resource(dev, res);
  105. if (IS_ERR(regs))
  106. return PTR_ERR(regs);
  107. /*
  108. * Right now device-tree probed devices don't get dma_mask set.
  109. * Since shared usb code relies on it, set it here for now.
  110. * Once we have dma capability bindings this can go away.
  111. */
  112. ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
  113. if (ret)
  114. return ret;
  115. ret = -ENODEV;
  116. hcd = usb_create_hcd(&ehci_omap_hc_driver, dev,
  117. dev_name(dev));
  118. if (!hcd) {
  119. dev_err(dev, "Failed to create HCD\n");
  120. return -ENOMEM;
  121. }
  122. hcd->rsrc_start = res->start;
  123. hcd->rsrc_len = resource_size(res);
  124. hcd->regs = regs;
  125. hcd_to_ehci(hcd)->caps = regs;
  126. omap = (struct omap_hcd *)hcd_to_ehci(hcd)->priv;
  127. omap->nports = pdata->nports;
  128. platform_set_drvdata(pdev, hcd);
  129. /* get the PHY devices if needed */
  130. for (i = 0 ; i < omap->nports ; i++) {
  131. struct usb_phy *phy;
  132. /* get the PHY device */
  133. phy = devm_usb_get_phy_by_phandle(dev, "phys", i);
  134. if (IS_ERR(phy)) {
  135. ret = PTR_ERR(phy);
  136. if (ret == -ENODEV) { /* no PHY */
  137. phy = NULL;
  138. continue;
  139. }
  140. if (ret != -EPROBE_DEFER)
  141. dev_err(dev, "Can't get PHY for port %d: %d\n",
  142. i, ret);
  143. goto err_phy;
  144. }
  145. omap->phy[i] = phy;
  146. if (pdata->port_mode[i] == OMAP_EHCI_PORT_MODE_PHY) {
  147. usb_phy_init(omap->phy[i]);
  148. /* bring PHY out of suspend */
  149. usb_phy_set_suspend(omap->phy[i], 0);
  150. }
  151. }
  152. pm_runtime_enable(dev);
  153. pm_runtime_get_sync(dev);
  154. /*
  155. * An undocumented "feature" in the OMAP3 EHCI controller,
  156. * causes suspended ports to be taken out of suspend when
  157. * the USBCMD.Run/Stop bit is cleared (for example when
  158. * we do ehci_bus_suspend).
  159. * This breaks suspend-resume if the root-hub is allowed
  160. * to suspend. Writing 1 to this undocumented register bit
  161. * disables this feature and restores normal behavior.
  162. */
  163. ehci_write(regs, EHCI_INSNREG04,
  164. EHCI_INSNREG04_DISABLE_UNSUSPEND);
  165. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  166. if (ret) {
  167. dev_err(dev, "failed to add hcd with err %d\n", ret);
  168. goto err_pm_runtime;
  169. }
  170. device_wakeup_enable(hcd->self.controller);
  171. /*
  172. * Bring PHYs out of reset for non PHY modes.
  173. * Even though HSIC mode is a PHY-less mode, the reset
  174. * line exists between the chips and can be modelled
  175. * as a PHY device for reset control.
  176. */
  177. for (i = 0; i < omap->nports; i++) {
  178. if (!omap->phy[i] ||
  179. pdata->port_mode[i] == OMAP_EHCI_PORT_MODE_PHY)
  180. continue;
  181. usb_phy_init(omap->phy[i]);
  182. /* bring PHY out of suspend */
  183. usb_phy_set_suspend(omap->phy[i], 0);
  184. }
  185. return 0;
  186. err_pm_runtime:
  187. pm_runtime_put_sync(dev);
  188. err_phy:
  189. for (i = 0; i < omap->nports; i++) {
  190. if (omap->phy[i])
  191. usb_phy_shutdown(omap->phy[i]);
  192. }
  193. usb_put_hcd(hcd);
  194. return ret;
  195. }
  196. /**
  197. * ehci_hcd_omap_remove - shutdown processing for EHCI HCDs
  198. * @pdev: USB Host Controller being removed
  199. *
  200. * Reverses the effect of usb_ehci_hcd_omap_probe(), first invoking
  201. * the HCD's stop() method. It is always called from a thread
  202. * context, normally "rmmod", "apmd", or something similar.
  203. */
  204. static int ehci_hcd_omap_remove(struct platform_device *pdev)
  205. {
  206. struct device *dev = &pdev->dev;
  207. struct usb_hcd *hcd = dev_get_drvdata(dev);
  208. struct omap_hcd *omap = (struct omap_hcd *)hcd_to_ehci(hcd)->priv;
  209. int i;
  210. usb_remove_hcd(hcd);
  211. for (i = 0; i < omap->nports; i++) {
  212. if (omap->phy[i])
  213. usb_phy_shutdown(omap->phy[i]);
  214. }
  215. usb_put_hcd(hcd);
  216. pm_runtime_put_sync(dev);
  217. pm_runtime_disable(dev);
  218. return 0;
  219. }
  220. static const struct of_device_id omap_ehci_dt_ids[] = {
  221. { .compatible = "ti,ehci-omap" },
  222. { }
  223. };
  224. MODULE_DEVICE_TABLE(of, omap_ehci_dt_ids);
  225. static struct platform_driver ehci_hcd_omap_driver = {
  226. .probe = ehci_hcd_omap_probe,
  227. .remove = ehci_hcd_omap_remove,
  228. .shutdown = usb_hcd_platform_shutdown,
  229. /*.suspend = ehci_hcd_omap_suspend, */
  230. /*.resume = ehci_hcd_omap_resume, */
  231. .driver = {
  232. .name = hcd_name,
  233. .of_match_table = omap_ehci_dt_ids,
  234. }
  235. };
  236. /*-------------------------------------------------------------------------*/
  237. static int __init ehci_omap_init(void)
  238. {
  239. if (usb_disabled())
  240. return -ENODEV;
  241. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  242. ehci_init_driver(&ehci_omap_hc_driver, &ehci_omap_overrides);
  243. return platform_driver_register(&ehci_hcd_omap_driver);
  244. }
  245. module_init(ehci_omap_init);
  246. static void __exit ehci_omap_cleanup(void)
  247. {
  248. platform_driver_unregister(&ehci_hcd_omap_driver);
  249. }
  250. module_exit(ehci_omap_cleanup);
  251. MODULE_ALIAS("platform:ehci-omap");
  252. MODULE_AUTHOR("Texas Instruments, Inc.");
  253. MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
  254. MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
  255. MODULE_DESCRIPTION(DRIVER_DESC);
  256. MODULE_LICENSE("GPL");