ehci-mxc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/platform_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/usb/otg.h>
  23. #include <linux/usb/ulpi.h>
  24. #include <linux/slab.h>
  25. #include <mach/hardware.h>
  26. #include <linux/platform_data/usb-ehci-mxc.h>
  27. #include <asm/mach-types.h>
  28. #define ULPI_VIEWPORT_OFFSET 0x170
  29. struct ehci_mxc_priv {
  30. struct clk *usbclk, *ahbclk, *phyclk;
  31. struct usb_hcd *hcd;
  32. };
  33. /* called during probe() after chip reset completes */
  34. static int ehci_mxc_setup(struct usb_hcd *hcd)
  35. {
  36. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  37. int retval;
  38. hcd->has_tt = 1;
  39. retval = ehci_setup(hcd);
  40. if (retval)
  41. return retval;
  42. ehci_port_power(ehci, 0);
  43. return 0;
  44. }
  45. static const struct hc_driver ehci_mxc_hc_driver = {
  46. .description = hcd_name,
  47. .product_desc = "Freescale On-Chip EHCI Host Controller",
  48. .hcd_priv_size = sizeof(struct ehci_hcd),
  49. /*
  50. * generic hardware linkage
  51. */
  52. .irq = ehci_irq,
  53. .flags = HCD_USB2 | HCD_MEMORY,
  54. /*
  55. * basic lifecycle operations
  56. */
  57. .reset = ehci_mxc_setup,
  58. .start = ehci_run,
  59. .stop = ehci_stop,
  60. .shutdown = ehci_shutdown,
  61. /*
  62. * managing i/o requests and associated device resources
  63. */
  64. .urb_enqueue = ehci_urb_enqueue,
  65. .urb_dequeue = ehci_urb_dequeue,
  66. .endpoint_disable = ehci_endpoint_disable,
  67. .endpoint_reset = ehci_endpoint_reset,
  68. /*
  69. * scheduling support
  70. */
  71. .get_frame_number = ehci_get_frame,
  72. /*
  73. * root hub support
  74. */
  75. .hub_status_data = ehci_hub_status_data,
  76. .hub_control = ehci_hub_control,
  77. .bus_suspend = ehci_bus_suspend,
  78. .bus_resume = ehci_bus_resume,
  79. .relinquish_port = ehci_relinquish_port,
  80. .port_handed_over = ehci_port_handed_over,
  81. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  82. };
  83. static int ehci_mxc_drv_probe(struct platform_device *pdev)
  84. {
  85. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  86. struct usb_hcd *hcd;
  87. struct resource *res;
  88. int irq, ret;
  89. unsigned int flags;
  90. struct ehci_mxc_priv *priv;
  91. struct device *dev = &pdev->dev;
  92. struct ehci_hcd *ehci;
  93. dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
  94. if (!pdata) {
  95. dev_err(dev, "No platform data given, bailing out.\n");
  96. return -EINVAL;
  97. }
  98. irq = platform_get_irq(pdev, 0);
  99. hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
  100. if (!hcd)
  101. return -ENOMEM;
  102. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  103. if (!priv) {
  104. ret = -ENOMEM;
  105. goto err_alloc;
  106. }
  107. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  108. if (!res) {
  109. dev_err(dev, "Found HC with no register addr. Check setup!\n");
  110. ret = -ENODEV;
  111. goto err_alloc;
  112. }
  113. hcd->rsrc_start = res->start;
  114. hcd->rsrc_len = resource_size(res);
  115. hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
  116. if (!hcd->regs) {
  117. dev_err(dev, "error mapping memory\n");
  118. ret = -EFAULT;
  119. goto err_alloc;
  120. }
  121. /* enable clocks */
  122. priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
  123. if (IS_ERR(priv->usbclk)) {
  124. ret = PTR_ERR(priv->usbclk);
  125. goto err_alloc;
  126. }
  127. clk_prepare_enable(priv->usbclk);
  128. priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
  129. if (IS_ERR(priv->ahbclk)) {
  130. ret = PTR_ERR(priv->ahbclk);
  131. goto err_clk_ahb;
  132. }
  133. clk_prepare_enable(priv->ahbclk);
  134. /* "dr" device has its own clock on i.MX51 */
  135. priv->phyclk = devm_clk_get(&pdev->dev, "phy");
  136. if (IS_ERR(priv->phyclk))
  137. priv->phyclk = NULL;
  138. if (priv->phyclk)
  139. clk_prepare_enable(priv->phyclk);
  140. /* call platform specific init function */
  141. if (pdata->init) {
  142. ret = pdata->init(pdev);
  143. if (ret) {
  144. dev_err(dev, "platform init failed\n");
  145. goto err_init;
  146. }
  147. /* platforms need some time to settle changed IO settings */
  148. mdelay(10);
  149. }
  150. ehci = hcd_to_ehci(hcd);
  151. /* EHCI registers start at offset 0x100 */
  152. ehci->caps = hcd->regs + 0x100;
  153. ehci->regs = hcd->regs + 0x100 +
  154. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  155. /* set up the PORTSCx register */
  156. ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
  157. /* is this really needed? */
  158. msleep(10);
  159. /* Initialize the transceiver */
  160. if (pdata->otg) {
  161. pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
  162. ret = usb_phy_init(pdata->otg);
  163. if (ret) {
  164. dev_err(dev, "unable to init transceiver, probably missing\n");
  165. ret = -ENODEV;
  166. goto err_add;
  167. }
  168. ret = otg_set_vbus(pdata->otg->otg, 1);
  169. if (ret) {
  170. dev_err(dev, "unable to enable vbus on transceiver\n");
  171. goto err_add;
  172. }
  173. }
  174. priv->hcd = hcd;
  175. platform_set_drvdata(pdev, priv);
  176. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  177. if (ret)
  178. goto err_add;
  179. if (pdata->otg) {
  180. /*
  181. * efikamx and efikasb have some hardware bug which is
  182. * preventing usb to work unless CHRGVBUS is set.
  183. * It's in violation of USB specs
  184. */
  185. if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
  186. flags = usb_phy_io_read(pdata->otg,
  187. ULPI_OTG_CTRL);
  188. flags |= ULPI_OTG_CTRL_CHRGVBUS;
  189. ret = usb_phy_io_write(pdata->otg, flags,
  190. ULPI_OTG_CTRL);
  191. if (ret) {
  192. dev_err(dev, "unable to set CHRVBUS\n");
  193. goto err_add;
  194. }
  195. }
  196. }
  197. return 0;
  198. err_add:
  199. if (pdata && pdata->exit)
  200. pdata->exit(pdev);
  201. err_init:
  202. if (priv->phyclk)
  203. clk_disable_unprepare(priv->phyclk);
  204. clk_disable_unprepare(priv->ahbclk);
  205. err_clk_ahb:
  206. clk_disable_unprepare(priv->usbclk);
  207. err_alloc:
  208. usb_put_hcd(hcd);
  209. return ret;
  210. }
  211. static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
  212. {
  213. struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
  214. struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
  215. struct usb_hcd *hcd = priv->hcd;
  216. if (pdata && pdata->exit)
  217. pdata->exit(pdev);
  218. if (pdata->otg)
  219. usb_phy_shutdown(pdata->otg);
  220. usb_remove_hcd(hcd);
  221. usb_put_hcd(hcd);
  222. platform_set_drvdata(pdev, NULL);
  223. clk_disable_unprepare(priv->usbclk);
  224. clk_disable_unprepare(priv->ahbclk);
  225. if (priv->phyclk)
  226. clk_disable_unprepare(priv->phyclk);
  227. return 0;
  228. }
  229. static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
  230. {
  231. struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
  232. struct usb_hcd *hcd = priv->hcd;
  233. if (hcd->driver->shutdown)
  234. hcd->driver->shutdown(hcd);
  235. }
  236. MODULE_ALIAS("platform:mxc-ehci");
  237. static struct platform_driver ehci_mxc_driver = {
  238. .probe = ehci_mxc_drv_probe,
  239. .remove = __exit_p(ehci_mxc_drv_remove),
  240. .shutdown = ehci_mxc_drv_shutdown,
  241. .driver = {
  242. .name = "mxc-ehci",
  243. },
  244. };