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