ehci-omap.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * ehci-omap.c - driver for USBHOST on OMAP3/4 processors
  3. *
  4. * Bus Glue for the EHCI controllers in OMAP3/4
  5. * Tested on several OMAP3 boards, and OMAP4 Pandaboard
  6. *
  7. * Copyright (C) 2007-2011 Texas Instruments, Inc.
  8. * Author: Vikram Pandita <vikram.pandita@ti.com>
  9. * Author: Anand Gadiyar <gadiyar@ti.com>
  10. * Author: Keshava Munegowda <keshava_mgowda@ti.com>
  11. *
  12. * Copyright (C) 2009 Nokia Corporation
  13. * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  14. *
  15. * Based on "ehci-fsl.c" and "ehci-au1xxx.c" ehci glue layers
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. * TODO (last updated Feb 27, 2010):
  32. * - add kernel-doc
  33. * - enable AUTOIDLE
  34. * - add suspend/resume
  35. * - add HSIC and TLL support
  36. * - convert to use hwmod and runtime PM
  37. */
  38. #include <linux/platform_device.h>
  39. #include <linux/slab.h>
  40. #include <linux/usb/ulpi.h>
  41. #include <linux/regulator/consumer.h>
  42. #include <linux/pm_runtime.h>
  43. #include <linux/gpio.h>
  44. #include <linux/clk.h>
  45. #include <linux/platform_data/usb-omap.h>
  46. /* EHCI Register Set */
  47. #define EHCI_INSNREG04 (0xA0)
  48. #define EHCI_INSNREG04_DISABLE_UNSUSPEND (1 << 5)
  49. #define EHCI_INSNREG05_ULPI (0xA4)
  50. #define EHCI_INSNREG05_ULPI_CONTROL_SHIFT 31
  51. #define EHCI_INSNREG05_ULPI_PORTSEL_SHIFT 24
  52. #define EHCI_INSNREG05_ULPI_OPSEL_SHIFT 22
  53. #define EHCI_INSNREG05_ULPI_REGADD_SHIFT 16
  54. #define EHCI_INSNREG05_ULPI_EXTREGADD_SHIFT 8
  55. #define EHCI_INSNREG05_ULPI_WRDATA_SHIFT 0
  56. /*-------------------------------------------------------------------------*/
  57. static const struct hc_driver ehci_omap_hc_driver;
  58. static inline void ehci_write(void __iomem *base, u32 reg, u32 val)
  59. {
  60. __raw_writel(val, base + reg);
  61. }
  62. static inline u32 ehci_read(void __iomem *base, u32 reg)
  63. {
  64. return __raw_readl(base + reg);
  65. }
  66. static void omap_ehci_soft_phy_reset(struct usb_hcd *hcd, u8 port)
  67. {
  68. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  69. unsigned reg = 0;
  70. reg = ULPI_FUNC_CTRL_RESET
  71. /* FUNCTION_CTRL_SET register */
  72. | (ULPI_SET(ULPI_FUNC_CTRL) << EHCI_INSNREG05_ULPI_REGADD_SHIFT)
  73. /* Write */
  74. | (2 << EHCI_INSNREG05_ULPI_OPSEL_SHIFT)
  75. /* PORTn */
  76. | ((port + 1) << EHCI_INSNREG05_ULPI_PORTSEL_SHIFT)
  77. /* start ULPI access*/
  78. | (1 << EHCI_INSNREG05_ULPI_CONTROL_SHIFT);
  79. ehci_write(hcd->regs, EHCI_INSNREG05_ULPI, reg);
  80. /* Wait for ULPI access completion */
  81. while ((ehci_read(hcd->regs, EHCI_INSNREG05_ULPI)
  82. & (1 << EHCI_INSNREG05_ULPI_CONTROL_SHIFT))) {
  83. cpu_relax();
  84. if (time_after(jiffies, timeout)) {
  85. dev_dbg(hcd->self.controller,
  86. "phy reset operation timed out\n");
  87. break;
  88. }
  89. }
  90. }
  91. static int omap_ehci_init(struct usb_hcd *hcd)
  92. {
  93. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  94. int rc;
  95. struct usbhs_omap_platform_data *pdata;
  96. pdata = hcd->self.controller->platform_data;
  97. /* Hold PHYs in reset while initializing EHCI controller */
  98. if (pdata->phy_reset) {
  99. if (gpio_is_valid(pdata->reset_gpio_port[0]))
  100. gpio_set_value_cansleep(pdata->reset_gpio_port[0], 0);
  101. if (gpio_is_valid(pdata->reset_gpio_port[1]))
  102. gpio_set_value_cansleep(pdata->reset_gpio_port[1], 0);
  103. /* Hold the PHY in RESET for enough time till DIR is high */
  104. udelay(10);
  105. }
  106. /* Soft reset the PHY using PHY reset command over ULPI */
  107. if (pdata->port_mode[0] == OMAP_EHCI_PORT_MODE_PHY)
  108. omap_ehci_soft_phy_reset(hcd, 0);
  109. if (pdata->port_mode[1] == OMAP_EHCI_PORT_MODE_PHY)
  110. omap_ehci_soft_phy_reset(hcd, 1);
  111. /* we know this is the memory we want, no need to ioremap again */
  112. ehci->caps = hcd->regs;
  113. rc = ehci_setup(hcd);
  114. if (pdata->phy_reset) {
  115. /* Hold the PHY in RESET for enough time till
  116. * PHY is settled and ready
  117. */
  118. udelay(10);
  119. if (gpio_is_valid(pdata->reset_gpio_port[0]))
  120. gpio_set_value_cansleep(pdata->reset_gpio_port[0], 1);
  121. if (gpio_is_valid(pdata->reset_gpio_port[1]))
  122. gpio_set_value_cansleep(pdata->reset_gpio_port[1], 1);
  123. }
  124. return rc;
  125. }
  126. static void disable_put_regulator(
  127. struct usbhs_omap_platform_data *pdata)
  128. {
  129. int i;
  130. for (i = 0 ; i < OMAP3_HS_USB_PORTS ; i++) {
  131. if (pdata->regulator[i]) {
  132. regulator_disable(pdata->regulator[i]);
  133. regulator_put(pdata->regulator[i]);
  134. }
  135. }
  136. }
  137. /* configure so an HC device and id are always provided */
  138. /* always called with process context; sleeping is OK */
  139. /**
  140. * ehci_hcd_omap_probe - initialize TI-based HCDs
  141. *
  142. * Allocates basic resources for this USB host controller, and
  143. * then invokes the start() method for the HCD associated with it
  144. * through the hotplug entry's driver_data.
  145. */
  146. static int ehci_hcd_omap_probe(struct platform_device *pdev)
  147. {
  148. struct device *dev = &pdev->dev;
  149. struct usbhs_omap_platform_data *pdata = dev->platform_data;
  150. struct resource *res;
  151. struct usb_hcd *hcd;
  152. void __iomem *regs;
  153. int ret = -ENODEV;
  154. int irq;
  155. int i;
  156. char supply[7];
  157. if (usb_disabled())
  158. return -ENODEV;
  159. if (!dev->parent) {
  160. dev_err(dev, "Missing parent device\n");
  161. return -ENODEV;
  162. }
  163. irq = platform_get_irq_byname(pdev, "ehci-irq");
  164. if (irq < 0) {
  165. dev_err(dev, "EHCI irq failed\n");
  166. return -ENODEV;
  167. }
  168. res = platform_get_resource_byname(pdev,
  169. IORESOURCE_MEM, "ehci");
  170. if (!res) {
  171. dev_err(dev, "UHH EHCI get resource failed\n");
  172. return -ENODEV;
  173. }
  174. regs = ioremap(res->start, resource_size(res));
  175. if (!regs) {
  176. dev_err(dev, "UHH EHCI ioremap failed\n");
  177. return -ENOMEM;
  178. }
  179. hcd = usb_create_hcd(&ehci_omap_hc_driver, dev,
  180. dev_name(dev));
  181. if (!hcd) {
  182. dev_err(dev, "failed to create hcd with err %d\n", ret);
  183. ret = -ENOMEM;
  184. goto err_io;
  185. }
  186. hcd->rsrc_start = res->start;
  187. hcd->rsrc_len = resource_size(res);
  188. hcd->regs = regs;
  189. /* get ehci regulator and enable */
  190. for (i = 0 ; i < OMAP3_HS_USB_PORTS ; i++) {
  191. if (pdata->port_mode[i] != OMAP_EHCI_PORT_MODE_PHY) {
  192. pdata->regulator[i] = NULL;
  193. continue;
  194. }
  195. snprintf(supply, sizeof(supply), "hsusb%d", i);
  196. pdata->regulator[i] = regulator_get(dev, supply);
  197. if (IS_ERR(pdata->regulator[i])) {
  198. pdata->regulator[i] = NULL;
  199. dev_dbg(dev,
  200. "failed to get ehci port%d regulator\n", i);
  201. } else {
  202. regulator_enable(pdata->regulator[i]);
  203. }
  204. }
  205. pm_runtime_enable(dev);
  206. pm_runtime_get_sync(dev);
  207. /*
  208. * An undocumented "feature" in the OMAP3 EHCI controller,
  209. * causes suspended ports to be taken out of suspend when
  210. * the USBCMD.Run/Stop bit is cleared (for example when
  211. * we do ehci_bus_suspend).
  212. * This breaks suspend-resume if the root-hub is allowed
  213. * to suspend. Writing 1 to this undocumented register bit
  214. * disables this feature and restores normal behavior.
  215. */
  216. ehci_write(regs, EHCI_INSNREG04,
  217. EHCI_INSNREG04_DISABLE_UNSUSPEND);
  218. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  219. if (ret) {
  220. dev_err(dev, "failed to add hcd with err %d\n", ret);
  221. goto err_pm_runtime;
  222. }
  223. return 0;
  224. err_pm_runtime:
  225. disable_put_regulator(pdata);
  226. pm_runtime_put_sync(dev);
  227. usb_put_hcd(hcd);
  228. err_io:
  229. iounmap(regs);
  230. return ret;
  231. }
  232. /**
  233. * ehci_hcd_omap_remove - shutdown processing for EHCI HCDs
  234. * @pdev: USB Host Controller being removed
  235. *
  236. * Reverses the effect of usb_ehci_hcd_omap_probe(), first invoking
  237. * the HCD's stop() method. It is always called from a thread
  238. * context, normally "rmmod", "apmd", or something similar.
  239. */
  240. static int ehci_hcd_omap_remove(struct platform_device *pdev)
  241. {
  242. struct device *dev = &pdev->dev;
  243. struct usb_hcd *hcd = dev_get_drvdata(dev);
  244. usb_remove_hcd(hcd);
  245. disable_put_regulator(dev->platform_data);
  246. iounmap(hcd->regs);
  247. usb_put_hcd(hcd);
  248. pm_runtime_put_sync(dev);
  249. pm_runtime_disable(dev);
  250. return 0;
  251. }
  252. static void ehci_hcd_omap_shutdown(struct platform_device *pdev)
  253. {
  254. struct usb_hcd *hcd = dev_get_drvdata(&pdev->dev);
  255. if (hcd->driver->shutdown)
  256. hcd->driver->shutdown(hcd);
  257. }
  258. static struct platform_driver ehci_hcd_omap_driver = {
  259. .probe = ehci_hcd_omap_probe,
  260. .remove = ehci_hcd_omap_remove,
  261. .shutdown = ehci_hcd_omap_shutdown,
  262. /*.suspend = ehci_hcd_omap_suspend, */
  263. /*.resume = ehci_hcd_omap_resume, */
  264. .driver = {
  265. .name = "ehci-omap",
  266. }
  267. };
  268. /*-------------------------------------------------------------------------*/
  269. static const struct hc_driver ehci_omap_hc_driver = {
  270. .description = hcd_name,
  271. .product_desc = "OMAP-EHCI Host Controller",
  272. .hcd_priv_size = sizeof(struct ehci_hcd),
  273. /*
  274. * generic hardware linkage
  275. */
  276. .irq = ehci_irq,
  277. .flags = HCD_MEMORY | HCD_USB2,
  278. /*
  279. * basic lifecycle operations
  280. */
  281. .reset = omap_ehci_init,
  282. .start = ehci_run,
  283. .stop = ehci_stop,
  284. .shutdown = ehci_shutdown,
  285. /*
  286. * managing i/o requests and associated device resources
  287. */
  288. .urb_enqueue = ehci_urb_enqueue,
  289. .urb_dequeue = ehci_urb_dequeue,
  290. .endpoint_disable = ehci_endpoint_disable,
  291. .endpoint_reset = ehci_endpoint_reset,
  292. /*
  293. * scheduling support
  294. */
  295. .get_frame_number = ehci_get_frame,
  296. /*
  297. * root hub support
  298. */
  299. .hub_status_data = ehci_hub_status_data,
  300. .hub_control = ehci_hub_control,
  301. .bus_suspend = ehci_bus_suspend,
  302. .bus_resume = ehci_bus_resume,
  303. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  304. };
  305. MODULE_ALIAS("platform:ehci-omap");
  306. MODULE_AUTHOR("Texas Instruments, Inc.");
  307. MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");