ehci-mxc.c 4.9 KB

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