ehci-s5p.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * SAMSUNG S5P USB HOST EHCI Controller
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  5. * Author: Jingoo Han <jg1.han@samsung.com>
  6. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/platform_data/usb-ehci-s5p.h>
  23. #include <linux/usb/phy.h>
  24. #include <linux/usb/samsung_usb_phy.h>
  25. #include <plat/usb-phy.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/hcd.h>
  28. #include <linux/usb/otg.h>
  29. #include "ehci.h"
  30. #define DRIVER_DESC "EHCI s5p driver"
  31. #define EHCI_INSNREG00(base) (base + 0x90)
  32. #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
  33. #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
  34. #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
  35. #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
  36. #define EHCI_INSNREG00_ENABLE_DMA_BURST \
  37. (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
  38. EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
  39. static const char hcd_name[] = "ehci-s5p";
  40. static struct hc_driver __read_mostly s5p_ehci_hc_driver;
  41. struct s5p_ehci_hcd {
  42. struct clk *clk;
  43. struct usb_phy *phy;
  44. struct usb_otg *otg;
  45. struct s5p_ehci_platdata *pdata;
  46. };
  47. #define to_s5p_ehci(hcd) (struct s5p_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
  48. static void s5p_setup_vbus_gpio(struct platform_device *pdev)
  49. {
  50. struct device *dev = &pdev->dev;
  51. int err;
  52. int gpio;
  53. if (!dev->of_node)
  54. return;
  55. gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
  56. if (!gpio_is_valid(gpio))
  57. return;
  58. err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
  59. "ehci_vbus_gpio");
  60. if (err)
  61. dev_err(dev, "can't request ehci vbus gpio %d", gpio);
  62. }
  63. static u64 ehci_s5p_dma_mask = DMA_BIT_MASK(32);
  64. static int s5p_ehci_probe(struct platform_device *pdev)
  65. {
  66. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  67. struct s5p_ehci_hcd *s5p_ehci;
  68. struct usb_hcd *hcd;
  69. struct ehci_hcd *ehci;
  70. struct resource *res;
  71. struct usb_phy *phy;
  72. int irq;
  73. int err;
  74. /*
  75. * Right now device-tree probed devices don't get dma_mask set.
  76. * Since shared usb code relies on it, set it here for now.
  77. * Once we move to full device tree support this will vanish off.
  78. */
  79. if (!pdev->dev.dma_mask)
  80. pdev->dev.dma_mask = &ehci_s5p_dma_mask;
  81. if (!pdev->dev.coherent_dma_mask)
  82. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  83. s5p_setup_vbus_gpio(pdev);
  84. hcd = usb_create_hcd(&s5p_ehci_hc_driver,
  85. &pdev->dev, dev_name(&pdev->dev));
  86. if (!hcd) {
  87. dev_err(&pdev->dev, "Unable to create HCD\n");
  88. return -ENOMEM;
  89. }
  90. s5p_ehci = to_s5p_ehci(hcd);
  91. phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  92. if (IS_ERR(phy)) {
  93. /* Fallback to pdata */
  94. if (!pdata) {
  95. dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
  96. return -EPROBE_DEFER;
  97. } else {
  98. s5p_ehci->pdata = pdata;
  99. }
  100. } else {
  101. s5p_ehci->phy = phy;
  102. s5p_ehci->otg = phy->otg;
  103. }
  104. s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
  105. if (IS_ERR(s5p_ehci->clk)) {
  106. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  107. err = PTR_ERR(s5p_ehci->clk);
  108. goto fail_clk;
  109. }
  110. err = clk_prepare_enable(s5p_ehci->clk);
  111. if (err)
  112. goto fail_clk;
  113. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  114. if (!res) {
  115. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  116. err = -ENXIO;
  117. goto fail_io;
  118. }
  119. hcd->rsrc_start = res->start;
  120. hcd->rsrc_len = resource_size(res);
  121. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  122. if (!hcd->regs) {
  123. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  124. err = -ENOMEM;
  125. goto fail_io;
  126. }
  127. irq = platform_get_irq(pdev, 0);
  128. if (!irq) {
  129. dev_err(&pdev->dev, "Failed to get IRQ\n");
  130. err = -ENODEV;
  131. goto fail_io;
  132. }
  133. if (s5p_ehci->otg)
  134. s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
  135. if (s5p_ehci->phy)
  136. usb_phy_init(s5p_ehci->phy);
  137. else if (s5p_ehci->pdata->phy_init)
  138. s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
  139. ehci = hcd_to_ehci(hcd);
  140. ehci->caps = hcd->regs;
  141. /* DMA burst Enable */
  142. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  143. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  144. if (err) {
  145. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  146. goto fail_add_hcd;
  147. }
  148. platform_set_drvdata(pdev, s5p_ehci);
  149. return 0;
  150. fail_add_hcd:
  151. if (s5p_ehci->phy)
  152. usb_phy_shutdown(s5p_ehci->phy);
  153. else if (s5p_ehci->pdata->phy_exit)
  154. s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
  155. fail_io:
  156. clk_disable_unprepare(s5p_ehci->clk);
  157. fail_clk:
  158. usb_put_hcd(hcd);
  159. return err;
  160. }
  161. static int s5p_ehci_remove(struct platform_device *pdev)
  162. {
  163. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  164. struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
  165. usb_remove_hcd(hcd);
  166. if (s5p_ehci->otg)
  167. s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
  168. if (s5p_ehci->phy)
  169. usb_phy_shutdown(s5p_ehci->phy);
  170. else if (s5p_ehci->pdata->phy_exit)
  171. s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
  172. clk_disable_unprepare(s5p_ehci->clk);
  173. usb_put_hcd(hcd);
  174. return 0;
  175. }
  176. static void s5p_ehci_shutdown(struct platform_device *pdev)
  177. {
  178. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  179. if (hcd->driver->shutdown)
  180. hcd->driver->shutdown(hcd);
  181. }
  182. #ifdef CONFIG_PM
  183. static int s5p_ehci_suspend(struct device *dev)
  184. {
  185. struct usb_hcd *hcd = dev_get_drvdata(dev);
  186. struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
  187. struct platform_device *pdev = to_platform_device(dev);
  188. bool do_wakeup = device_may_wakeup(dev);
  189. int rc;
  190. rc = ehci_suspend(hcd, do_wakeup);
  191. if (s5p_ehci->otg)
  192. s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
  193. if (s5p_ehci->phy)
  194. usb_phy_shutdown(s5p_ehci->phy);
  195. else if (s5p_ehci->pdata->phy_exit)
  196. s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
  197. clk_disable_unprepare(s5p_ehci->clk);
  198. return rc;
  199. }
  200. static int s5p_ehci_resume(struct device *dev)
  201. {
  202. struct usb_hcd *hcd = dev_get_drvdata(dev);
  203. struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
  204. struct platform_device *pdev = to_platform_device(dev);
  205. clk_prepare_enable(s5p_ehci->clk);
  206. if (s5p_ehci->otg)
  207. s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
  208. if (s5p_ehci->phy)
  209. usb_phy_init(s5p_ehci->phy);
  210. else if (s5p_ehci->pdata->phy_init)
  211. s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
  212. /* DMA burst Enable */
  213. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  214. ehci_resume(hcd, false);
  215. return 0;
  216. }
  217. #else
  218. #define s5p_ehci_suspend NULL
  219. #define s5p_ehci_resume NULL
  220. #endif
  221. static const struct dev_pm_ops s5p_ehci_pm_ops = {
  222. .suspend = s5p_ehci_suspend,
  223. .resume = s5p_ehci_resume,
  224. };
  225. #ifdef CONFIG_OF
  226. static const struct of_device_id exynos_ehci_match[] = {
  227. { .compatible = "samsung,exynos4210-ehci" },
  228. {},
  229. };
  230. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  231. #endif
  232. static struct platform_driver s5p_ehci_driver = {
  233. .probe = s5p_ehci_probe,
  234. .remove = s5p_ehci_remove,
  235. .shutdown = s5p_ehci_shutdown,
  236. .driver = {
  237. .name = "s5p-ehci",
  238. .owner = THIS_MODULE,
  239. .pm = &s5p_ehci_pm_ops,
  240. .of_match_table = of_match_ptr(exynos_ehci_match),
  241. }
  242. };
  243. static const struct ehci_driver_overrides s5p_overrides __initdata = {
  244. .extra_priv_size = sizeof(struct s5p_ehci_hcd),
  245. };
  246. static int __init ehci_s5p_init(void)
  247. {
  248. if (usb_disabled())
  249. return -ENODEV;
  250. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  251. ehci_init_driver(&s5p_ehci_hc_driver, &s5p_overrides);
  252. return platform_driver_register(&s5p_ehci_driver);
  253. }
  254. module_init(ehci_s5p_init);
  255. static void __exit ehci_s5p_cleanup(void)
  256. {
  257. platform_driver_unregister(&s5p_ehci_driver);
  258. }
  259. module_exit(ehci_s5p_cleanup);
  260. MODULE_DESCRIPTION(DRIVER_DESC);
  261. MODULE_ALIAS("platform:s5p-ehci");
  262. MODULE_AUTHOR("Jingoo Han");
  263. MODULE_AUTHOR("Joonyoung Shim");
  264. MODULE_LICENSE("GPL v2");