ehci-s5p.c 8.0 KB

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