ehci-mxc.c 4.9 KB

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