ehci-s5p.c 7.8 KB

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