ehci-mxc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/io.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/clk.h>
  24. #include <linux/delay.h>
  25. #include <linux/usb/otg.h>
  26. #include <linux/usb/ulpi.h>
  27. #include <linux/slab.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/hcd.h>
  30. #include <linux/platform_data/usb-ehci-mxc.h>
  31. #include "ehci.h"
  32. #define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
  33. static const char hcd_name[] = "ehci-mxc";
  34. #define ULPI_VIEWPORT_OFFSET 0x170
  35. struct ehci_mxc_priv {
  36. struct clk *usbclk, *ahbclk, *phyclk;
  37. };
  38. static struct hc_driver __read_mostly ehci_mxc_hc_driver;
  39. static const struct ehci_driver_overrides ehci_mxc_overrides __initdata = {
  40. .extra_priv_size = sizeof(struct ehci_mxc_priv),
  41. };
  42. static int ehci_mxc_drv_probe(struct platform_device *pdev)
  43. {
  44. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  45. struct usb_hcd *hcd;
  46. struct resource *res;
  47. int irq, ret;
  48. struct ehci_mxc_priv *priv;
  49. struct device *dev = &pdev->dev;
  50. struct ehci_hcd *ehci;
  51. dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
  52. if (!pdata) {
  53. dev_err(dev, "No platform data given, bailing out.\n");
  54. return -EINVAL;
  55. }
  56. irq = platform_get_irq(pdev, 0);
  57. hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
  58. if (!hcd)
  59. return -ENOMEM;
  60. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  61. if (!res) {
  62. dev_err(dev, "Found HC with no register addr. Check setup!\n");
  63. ret = -ENODEV;
  64. goto err_alloc;
  65. }
  66. hcd->rsrc_start = res->start;
  67. hcd->rsrc_len = resource_size(res);
  68. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  69. if (IS_ERR(hcd->regs)) {
  70. ret = PTR_ERR(hcd->regs);
  71. goto err_alloc;
  72. }
  73. hcd->has_tt = 1;
  74. ehci = hcd_to_ehci(hcd);
  75. priv = (struct ehci_mxc_priv *) ehci->priv;
  76. /* enable clocks */
  77. priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
  78. if (IS_ERR(priv->usbclk)) {
  79. ret = PTR_ERR(priv->usbclk);
  80. goto err_alloc;
  81. }
  82. clk_prepare_enable(priv->usbclk);
  83. priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
  84. if (IS_ERR(priv->ahbclk)) {
  85. ret = PTR_ERR(priv->ahbclk);
  86. goto err_clk_ahb;
  87. }
  88. clk_prepare_enable(priv->ahbclk);
  89. /* "dr" device has its own clock on i.MX51 */
  90. priv->phyclk = devm_clk_get(&pdev->dev, "phy");
  91. if (IS_ERR(priv->phyclk))
  92. priv->phyclk = NULL;
  93. if (priv->phyclk)
  94. clk_prepare_enable(priv->phyclk);
  95. /* call platform specific init function */
  96. if (pdata->init) {
  97. ret = pdata->init(pdev);
  98. if (ret) {
  99. dev_err(dev, "platform init failed\n");
  100. goto err_init;
  101. }
  102. /* platforms need some time to settle changed IO settings */
  103. mdelay(10);
  104. }
  105. /* EHCI registers start at offset 0x100 */
  106. ehci->caps = hcd->regs + 0x100;
  107. ehci->regs = hcd->regs + 0x100 +
  108. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  109. /* set up the PORTSCx register */
  110. ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
  111. /* is this really needed? */
  112. msleep(10);
  113. /* Initialize the transceiver */
  114. if (pdata->otg) {
  115. pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
  116. ret = usb_phy_init(pdata->otg);
  117. if (ret) {
  118. dev_err(dev, "unable to init transceiver, probably missing\n");
  119. ret = -ENODEV;
  120. goto err_add;
  121. }
  122. ret = otg_set_vbus(pdata->otg->otg, 1);
  123. if (ret) {
  124. dev_err(dev, "unable to enable vbus on transceiver\n");
  125. goto err_add;
  126. }
  127. }
  128. platform_set_drvdata(pdev, hcd);
  129. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  130. if (ret)
  131. goto err_add;
  132. return 0;
  133. err_add:
  134. if (pdata && pdata->exit)
  135. pdata->exit(pdev);
  136. err_init:
  137. if (priv->phyclk)
  138. clk_disable_unprepare(priv->phyclk);
  139. clk_disable_unprepare(priv->ahbclk);
  140. err_clk_ahb:
  141. clk_disable_unprepare(priv->usbclk);
  142. err_alloc:
  143. usb_put_hcd(hcd);
  144. return ret;
  145. }
  146. static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
  147. {
  148. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  149. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  150. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  151. struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
  152. usb_remove_hcd(hcd);
  153. if (pdata && pdata->exit)
  154. pdata->exit(pdev);
  155. if (pdata->otg)
  156. usb_phy_shutdown(pdata->otg);
  157. clk_disable_unprepare(priv->usbclk);
  158. clk_disable_unprepare(priv->ahbclk);
  159. if (priv->phyclk)
  160. clk_disable_unprepare(priv->phyclk);
  161. usb_put_hcd(hcd);
  162. platform_set_drvdata(pdev, NULL);
  163. return 0;
  164. }
  165. static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
  166. {
  167. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  168. if (hcd->driver->shutdown)
  169. hcd->driver->shutdown(hcd);
  170. }
  171. MODULE_ALIAS("platform:mxc-ehci");
  172. static struct platform_driver ehci_mxc_driver = {
  173. .probe = ehci_mxc_drv_probe,
  174. .remove = ehci_mxc_drv_remove,
  175. .shutdown = ehci_mxc_drv_shutdown,
  176. .driver = {
  177. .name = "mxc-ehci",
  178. },
  179. };
  180. static int __init ehci_mxc_init(void)
  181. {
  182. if (usb_disabled())
  183. return -ENODEV;
  184. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  185. ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
  186. return platform_driver_register(&ehci_mxc_driver);
  187. }
  188. module_init(ehci_mxc_init);
  189. static void __exit ehci_mxc_cleanup(void)
  190. {
  191. platform_driver_unregister(&ehci_mxc_driver);
  192. }
  193. module_exit(ehci_mxc_cleanup);
  194. MODULE_DESCRIPTION(DRIVER_DESC);
  195. MODULE_AUTHOR("Sascha Hauer");
  196. MODULE_LICENSE("GPL");