ehci-mxc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/usb/ulpi.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform_data/usb-ehci-mxc.h>
  26. #include <asm/mach-types.h>
  27. #define ULPI_VIEWPORT_OFFSET 0x170
  28. struct ehci_mxc_priv {
  29. struct clk *usbclk, *ahbclk, *phyclk;
  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. hcd->has_tt = 1;
  36. return ehci_setup(hcd);
  37. }
  38. static const struct hc_driver ehci_mxc_hc_driver = {
  39. .description = hcd_name,
  40. .product_desc = "Freescale On-Chip EHCI Host Controller",
  41. .hcd_priv_size = sizeof(struct ehci_hcd),
  42. /*
  43. * generic hardware linkage
  44. */
  45. .irq = ehci_irq,
  46. .flags = HCD_USB2 | HCD_MEMORY,
  47. /*
  48. * basic lifecycle operations
  49. */
  50. .reset = ehci_mxc_setup,
  51. .start = ehci_run,
  52. .stop = ehci_stop,
  53. .shutdown = ehci_shutdown,
  54. /*
  55. * managing i/o requests and associated device resources
  56. */
  57. .urb_enqueue = ehci_urb_enqueue,
  58. .urb_dequeue = ehci_urb_dequeue,
  59. .endpoint_disable = ehci_endpoint_disable,
  60. .endpoint_reset = ehci_endpoint_reset,
  61. /*
  62. * scheduling support
  63. */
  64. .get_frame_number = ehci_get_frame,
  65. /*
  66. * root hub support
  67. */
  68. .hub_status_data = ehci_hub_status_data,
  69. .hub_control = ehci_hub_control,
  70. .bus_suspend = ehci_bus_suspend,
  71. .bus_resume = ehci_bus_resume,
  72. .relinquish_port = ehci_relinquish_port,
  73. .port_handed_over = ehci_port_handed_over,
  74. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  75. };
  76. static int ehci_mxc_drv_probe(struct platform_device *pdev)
  77. {
  78. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  79. struct usb_hcd *hcd;
  80. struct resource *res;
  81. int irq, ret;
  82. unsigned int flags;
  83. struct ehci_mxc_priv *priv;
  84. struct device *dev = &pdev->dev;
  85. struct ehci_hcd *ehci;
  86. dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
  87. if (!pdata) {
  88. dev_err(dev, "No platform data given, bailing out.\n");
  89. return -EINVAL;
  90. }
  91. irq = platform_get_irq(pdev, 0);
  92. hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
  93. if (!hcd)
  94. return -ENOMEM;
  95. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  96. if (!priv) {
  97. ret = -ENOMEM;
  98. goto err_alloc;
  99. }
  100. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  101. if (!res) {
  102. dev_err(dev, "Found HC with no register addr. Check setup!\n");
  103. ret = -ENODEV;
  104. goto err_alloc;
  105. }
  106. hcd->rsrc_start = res->start;
  107. hcd->rsrc_len = resource_size(res);
  108. hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
  109. if (!hcd->regs) {
  110. dev_err(dev, "error mapping memory\n");
  111. ret = -EFAULT;
  112. goto err_alloc;
  113. }
  114. /* enable clocks */
  115. priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
  116. if (IS_ERR(priv->usbclk)) {
  117. ret = PTR_ERR(priv->usbclk);
  118. goto err_alloc;
  119. }
  120. clk_prepare_enable(priv->usbclk);
  121. priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
  122. if (IS_ERR(priv->ahbclk)) {
  123. ret = PTR_ERR(priv->ahbclk);
  124. goto err_clk_ahb;
  125. }
  126. clk_prepare_enable(priv->ahbclk);
  127. /* "dr" device has its own clock on i.MX51 */
  128. priv->phyclk = devm_clk_get(&pdev->dev, "phy");
  129. if (IS_ERR(priv->phyclk))
  130. priv->phyclk = NULL;
  131. if (priv->phyclk)
  132. clk_prepare_enable(priv->phyclk);
  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. ehci = hcd_to_ehci(hcd);
  144. /* EHCI registers start at offset 0x100 */
  145. ehci->caps = hcd->regs + 0x100;
  146. ehci->regs = hcd->regs + 0x100 +
  147. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  148. /* set up the PORTSCx register */
  149. ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
  150. /* is this really needed? */
  151. msleep(10);
  152. /* Initialize the transceiver */
  153. if (pdata->otg) {
  154. pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
  155. ret = usb_phy_init(pdata->otg);
  156. if (ret) {
  157. dev_err(dev, "unable to init transceiver, probably missing\n");
  158. ret = -ENODEV;
  159. goto err_add;
  160. }
  161. ret = otg_set_vbus(pdata->otg->otg, 1);
  162. if (ret) {
  163. dev_err(dev, "unable to enable vbus on transceiver\n");
  164. goto err_add;
  165. }
  166. }
  167. priv->hcd = hcd;
  168. platform_set_drvdata(pdev, priv);
  169. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  170. if (ret)
  171. goto err_add;
  172. if (pdata->otg) {
  173. /*
  174. * efikamx and efikasb have some hardware bug which is
  175. * preventing usb to work unless CHRGVBUS is set.
  176. * It's in violation of USB specs
  177. */
  178. if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
  179. flags = usb_phy_io_read(pdata->otg,
  180. ULPI_OTG_CTRL);
  181. flags |= ULPI_OTG_CTRL_CHRGVBUS;
  182. ret = usb_phy_io_write(pdata->otg, flags,
  183. ULPI_OTG_CTRL);
  184. if (ret) {
  185. dev_err(dev, "unable to set CHRVBUS\n");
  186. goto err_add;
  187. }
  188. }
  189. }
  190. return 0;
  191. err_add:
  192. if (pdata && pdata->exit)
  193. pdata->exit(pdev);
  194. err_init:
  195. if (priv->phyclk)
  196. clk_disable_unprepare(priv->phyclk);
  197. clk_disable_unprepare(priv->ahbclk);
  198. err_clk_ahb:
  199. clk_disable_unprepare(priv->usbclk);
  200. err_alloc:
  201. usb_put_hcd(hcd);
  202. return ret;
  203. }
  204. static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
  205. {
  206. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  207. struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
  208. struct usb_hcd *hcd = priv->hcd;
  209. if (pdata && pdata->exit)
  210. pdata->exit(pdev);
  211. if (pdata->otg)
  212. usb_phy_shutdown(pdata->otg);
  213. usb_remove_hcd(hcd);
  214. usb_put_hcd(hcd);
  215. platform_set_drvdata(pdev, NULL);
  216. clk_disable_unprepare(priv->usbclk);
  217. clk_disable_unprepare(priv->ahbclk);
  218. if (priv->phyclk)
  219. clk_disable_unprepare(priv->phyclk);
  220. return 0;
  221. }
  222. static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
  223. {
  224. struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
  225. struct usb_hcd *hcd = priv->hcd;
  226. if (hcd->driver->shutdown)
  227. hcd->driver->shutdown(hcd);
  228. }
  229. MODULE_ALIAS("platform:mxc-ehci");
  230. static struct platform_driver ehci_mxc_driver = {
  231. .probe = ehci_mxc_drv_probe,
  232. .remove = __exit_p(ehci_mxc_drv_remove),
  233. .shutdown = ehci_mxc_drv_shutdown,
  234. .driver = {
  235. .name = "mxc-ehci",
  236. },
  237. };