ehci-s5p.c 7.9 KB

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