ehci-mxc.c 6.8 KB

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