ehci-mxc.c 7.8 KB

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