ehci-mxc.c 7.3 KB

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