ehci-mxc.c 6.8 KB

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