ehci-mxc.c 5.6 KB

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