ehci-mxc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. unsigned int flags;
  50. struct ehci_mxc_priv *priv;
  51. struct device *dev = &pdev->dev;
  52. struct ehci_hcd *ehci;
  53. dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
  54. if (!pdata) {
  55. dev_err(dev, "No platform data given, bailing out.\n");
  56. return -EINVAL;
  57. }
  58. irq = platform_get_irq(pdev, 0);
  59. hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
  60. if (!hcd)
  61. return -ENOMEM;
  62. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  63. if (!res) {
  64. dev_err(dev, "Found HC with no register addr. Check setup!\n");
  65. ret = -ENODEV;
  66. goto err_alloc;
  67. }
  68. hcd->rsrc_start = res->start;
  69. hcd->rsrc_len = resource_size(res);
  70. hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
  71. if (!hcd->regs) {
  72. dev_err(dev, "error mapping memory\n");
  73. ret = -EFAULT;
  74. goto err_alloc;
  75. }
  76. hcd->has_tt = 1;
  77. ehci = hcd_to_ehci(hcd);
  78. priv = (struct ehci_mxc_priv *) ehci->priv;
  79. /* enable clocks */
  80. priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
  81. if (IS_ERR(priv->usbclk)) {
  82. ret = PTR_ERR(priv->usbclk);
  83. goto err_alloc;
  84. }
  85. clk_prepare_enable(priv->usbclk);
  86. priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
  87. if (IS_ERR(priv->ahbclk)) {
  88. ret = PTR_ERR(priv->ahbclk);
  89. goto err_clk_ahb;
  90. }
  91. clk_prepare_enable(priv->ahbclk);
  92. /* "dr" device has its own clock on i.MX51 */
  93. priv->phyclk = devm_clk_get(&pdev->dev, "phy");
  94. if (IS_ERR(priv->phyclk))
  95. priv->phyclk = NULL;
  96. if (priv->phyclk)
  97. clk_prepare_enable(priv->phyclk);
  98. /* call platform specific init function */
  99. if (pdata->init) {
  100. ret = pdata->init(pdev);
  101. if (ret) {
  102. dev_err(dev, "platform init failed\n");
  103. goto err_init;
  104. }
  105. /* platforms need some time to settle changed IO settings */
  106. mdelay(10);
  107. }
  108. /* EHCI registers start at offset 0x100 */
  109. ehci->caps = hcd->regs + 0x100;
  110. ehci->regs = hcd->regs + 0x100 +
  111. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  112. /* set up the PORTSCx register */
  113. ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
  114. /* is this really needed? */
  115. msleep(10);
  116. /* Initialize the transceiver */
  117. if (pdata->otg) {
  118. pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
  119. ret = usb_phy_init(pdata->otg);
  120. if (ret) {
  121. dev_err(dev, "unable to init transceiver, probably missing\n");
  122. ret = -ENODEV;
  123. goto err_add;
  124. }
  125. ret = otg_set_vbus(pdata->otg->otg, 1);
  126. if (ret) {
  127. dev_err(dev, "unable to enable vbus on transceiver\n");
  128. goto err_add;
  129. }
  130. }
  131. platform_set_drvdata(pdev, hcd);
  132. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  133. if (ret)
  134. goto err_add;
  135. if (pdata->otg) {
  136. /*
  137. * efikamx and efikasb have some hardware bug which is
  138. * preventing usb to work unless CHRGVBUS is set.
  139. * It's in violation of USB specs
  140. */
  141. if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
  142. flags = usb_phy_io_read(pdata->otg,
  143. ULPI_OTG_CTRL);
  144. flags |= ULPI_OTG_CTRL_CHRGVBUS;
  145. ret = usb_phy_io_write(pdata->otg, flags,
  146. ULPI_OTG_CTRL);
  147. if (ret) {
  148. dev_err(dev, "unable to set CHRVBUS\n");
  149. goto err_add;
  150. }
  151. }
  152. }
  153. return 0;
  154. err_add:
  155. if (pdata && pdata->exit)
  156. pdata->exit(pdev);
  157. err_init:
  158. if (priv->phyclk)
  159. clk_disable_unprepare(priv->phyclk);
  160. clk_disable_unprepare(priv->ahbclk);
  161. err_clk_ahb:
  162. clk_disable_unprepare(priv->usbclk);
  163. err_alloc:
  164. usb_put_hcd(hcd);
  165. return ret;
  166. }
  167. static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
  168. {
  169. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  170. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  171. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  172. struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
  173. usb_remove_hcd(hcd);
  174. if (pdata && pdata->exit)
  175. pdata->exit(pdev);
  176. if (pdata->otg)
  177. usb_phy_shutdown(pdata->otg);
  178. clk_disable_unprepare(priv->usbclk);
  179. clk_disable_unprepare(priv->ahbclk);
  180. if (priv->phyclk)
  181. clk_disable_unprepare(priv->phyclk);
  182. usb_put_hcd(hcd);
  183. platform_set_drvdata(pdev, NULL);
  184. return 0;
  185. }
  186. static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
  187. {
  188. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  189. if (hcd->driver->shutdown)
  190. hcd->driver->shutdown(hcd);
  191. }
  192. MODULE_ALIAS("platform:mxc-ehci");
  193. static struct platform_driver ehci_mxc_driver = {
  194. .probe = ehci_mxc_drv_probe,
  195. .remove = ehci_mxc_drv_remove,
  196. .shutdown = ehci_mxc_drv_shutdown,
  197. .driver = {
  198. .name = "mxc-ehci",
  199. },
  200. };
  201. static int __init ehci_mxc_init(void)
  202. {
  203. if (usb_disabled())
  204. return -ENODEV;
  205. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  206. ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
  207. return platform_driver_register(&ehci_mxc_driver);
  208. }
  209. module_init(ehci_mxc_init);
  210. static void __exit ehci_mxc_cleanup(void)
  211. {
  212. platform_driver_unregister(&ehci_mxc_driver);
  213. }
  214. module_exit(ehci_mxc_cleanup);
  215. MODULE_DESCRIPTION(DRIVER_DESC);
  216. MODULE_AUTHOR("Sascha Hauer");
  217. MODULE_LICENSE("GPL");