ehci-mxc.c 7.7 KB

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