ehci-mxc.c 5.9 KB

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