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