ehci-mxc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  3. * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/platform_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/usb/otg.h>
  23. #include <mach/mxc_ehci.h>
  24. #define ULPI_VIEWPORT_OFFSET 0x170
  25. #define PORTSC_OFFSET 0x184
  26. #define USBMODE_OFFSET 0x1a8
  27. #define USBMODE_CM_HOST 3
  28. struct ehci_mxc_priv {
  29. struct clk *usbclk, *ahbclk;
  30. struct usb_hcd *hcd;
  31. };
  32. /* called during probe() after chip reset completes */
  33. static int ehci_mxc_setup(struct usb_hcd *hcd)
  34. {
  35. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  36. int retval;
  37. /* EHCI registers start at offset 0x100 */
  38. ehci->caps = hcd->regs + 0x100;
  39. ehci->regs = hcd->regs + 0x100 +
  40. HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
  41. dbg_hcs_params(ehci, "reset");
  42. dbg_hcc_params(ehci, "reset");
  43. /* cache this readonly data; minimize chip reads */
  44. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  45. retval = ehci_halt(ehci);
  46. if (retval)
  47. return retval;
  48. /* data structure init */
  49. retval = ehci_init(hcd);
  50. if (retval)
  51. return retval;
  52. hcd->has_tt = 1;
  53. ehci->sbrn = 0x20;
  54. ehci_reset(ehci);
  55. ehci_port_power(ehci, 0);
  56. return 0;
  57. }
  58. static const struct hc_driver ehci_mxc_hc_driver = {
  59. .description = hcd_name,
  60. .product_desc = "Freescale On-Chip EHCI Host Controller",
  61. .hcd_priv_size = sizeof(struct ehci_hcd),
  62. /*
  63. * generic hardware linkage
  64. */
  65. .irq = ehci_irq,
  66. .flags = HCD_USB2 | HCD_MEMORY,
  67. /*
  68. * basic lifecycle operations
  69. */
  70. .reset = ehci_mxc_setup,
  71. .start = ehci_run,
  72. .stop = ehci_stop,
  73. .shutdown = ehci_shutdown,
  74. /*
  75. * managing i/o requests and associated device resources
  76. */
  77. .urb_enqueue = ehci_urb_enqueue,
  78. .urb_dequeue = ehci_urb_dequeue,
  79. .endpoint_disable = ehci_endpoint_disable,
  80. /*
  81. * scheduling support
  82. */
  83. .get_frame_number = ehci_get_frame,
  84. /*
  85. * root hub support
  86. */
  87. .hub_status_data = ehci_hub_status_data,
  88. .hub_control = ehci_hub_control,
  89. .bus_suspend = ehci_bus_suspend,
  90. .bus_resume = ehci_bus_resume,
  91. .relinquish_port = ehci_relinquish_port,
  92. .port_handed_over = ehci_port_handed_over,
  93. };
  94. static int ehci_mxc_drv_probe(struct platform_device *pdev)
  95. {
  96. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  97. struct usb_hcd *hcd;
  98. struct resource *res;
  99. int irq, ret, temp;
  100. struct ehci_mxc_priv *priv;
  101. struct device *dev = &pdev->dev;
  102. dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
  103. if (!pdata) {
  104. dev_err(dev, "No platform data given, bailing out.\n");
  105. return -EINVAL;
  106. }
  107. irq = platform_get_irq(pdev, 0);
  108. hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
  109. if (!hcd)
  110. return -ENOMEM;
  111. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  112. if (!priv) {
  113. ret = -ENOMEM;
  114. goto err_alloc;
  115. }
  116. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. if (!res) {
  118. dev_err(dev, "Found HC with no register addr. Check setup!\n");
  119. ret = -ENODEV;
  120. goto err_get_resource;
  121. }
  122. hcd->rsrc_start = res->start;
  123. hcd->rsrc_len = resource_size(res);
  124. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  125. dev_dbg(dev, "controller already in use\n");
  126. ret = -EBUSY;
  127. goto err_request_mem;
  128. }
  129. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  130. if (!hcd->regs) {
  131. dev_err(dev, "error mapping memory\n");
  132. ret = -EFAULT;
  133. goto err_ioremap;
  134. }
  135. /* enable clocks */
  136. priv->usbclk = clk_get(dev, "usb");
  137. if (IS_ERR(priv->usbclk)) {
  138. ret = PTR_ERR(priv->usbclk);
  139. goto err_clk;
  140. }
  141. clk_enable(priv->usbclk);
  142. if (!cpu_is_mx35()) {
  143. priv->ahbclk = clk_get(dev, "usb_ahb");
  144. if (IS_ERR(priv->ahbclk)) {
  145. ret = PTR_ERR(priv->ahbclk);
  146. goto err_clk_ahb;
  147. }
  148. clk_enable(priv->ahbclk);
  149. }
  150. /* set USBMODE to host mode */
  151. temp = readl(hcd->regs + USBMODE_OFFSET);
  152. writel(temp | USBMODE_CM_HOST, hcd->regs + USBMODE_OFFSET);
  153. /* set up the PORTSCx register */
  154. writel(pdata->portsc, hcd->regs + PORTSC_OFFSET);
  155. mdelay(10);
  156. /* setup USBCONTROL. */
  157. ret = mxc_set_usbcontrol(pdev->id, pdata->flags);
  158. if (ret < 0)
  159. goto err_init;
  160. /* call platform specific init function */
  161. if (pdata->init) {
  162. ret = pdata->init(pdev);
  163. if (ret) {
  164. dev_err(dev, "platform init failed\n");
  165. goto err_init;
  166. }
  167. }
  168. /* most platforms need some time to settle changed IO settings */
  169. mdelay(10);
  170. /* Initialize the transceiver */
  171. if (pdata->otg) {
  172. pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
  173. if (otg_init(pdata->otg) != 0)
  174. dev_err(dev, "unable to init transceiver\n");
  175. else if (otg_set_vbus(pdata->otg, 1) != 0)
  176. dev_err(dev, "unable to enable vbus on transceiver\n");
  177. }
  178. priv->hcd = hcd;
  179. platform_set_drvdata(pdev, priv);
  180. ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
  181. if (ret)
  182. goto err_add;
  183. return 0;
  184. err_add:
  185. if (pdata && pdata->exit)
  186. pdata->exit(pdev);
  187. err_init:
  188. if (priv->ahbclk) {
  189. clk_disable(priv->ahbclk);
  190. clk_put(priv->ahbclk);
  191. }
  192. err_clk_ahb:
  193. clk_disable(priv->usbclk);
  194. clk_put(priv->usbclk);
  195. err_clk:
  196. iounmap(hcd->regs);
  197. err_ioremap:
  198. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  199. err_request_mem:
  200. err_get_resource:
  201. kfree(priv);
  202. err_alloc:
  203. usb_put_hcd(hcd);
  204. return ret;
  205. }
  206. static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
  207. {
  208. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  209. struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
  210. struct usb_hcd *hcd = priv->hcd;
  211. if (pdata && pdata->exit)
  212. pdata->exit(pdev);
  213. if (pdata->otg)
  214. otg_shutdown(pdata->otg);
  215. usb_remove_hcd(hcd);
  216. iounmap(hcd->regs);
  217. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  218. usb_put_hcd(hcd);
  219. platform_set_drvdata(pdev, NULL);
  220. clk_disable(priv->usbclk);
  221. clk_put(priv->usbclk);
  222. if (priv->ahbclk) {
  223. clk_disable(priv->ahbclk);
  224. clk_put(priv->ahbclk);
  225. }
  226. kfree(priv);
  227. return 0;
  228. }
  229. static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
  230. {
  231. struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
  232. struct usb_hcd *hcd = priv->hcd;
  233. if (hcd->driver->shutdown)
  234. hcd->driver->shutdown(hcd);
  235. }
  236. MODULE_ALIAS("platform:mxc-ehci");
  237. static struct platform_driver ehci_mxc_driver = {
  238. .probe = ehci_mxc_drv_probe,
  239. .remove = __exit_p(ehci_mxc_drv_remove),
  240. .shutdown = ehci_mxc_drv_shutdown,
  241. .driver = {
  242. .name = "mxc-ehci",
  243. },
  244. };