ehci-s5p.c 7.8 KB

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