ehci-exynos.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SAMSUNG EXYNOS USB HOST EHCI Controller
  4. *
  5. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  6. * Author: Jingoo Han <jg1.han@samsung.com>
  7. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/hcd.h>
  20. #include "ehci.h"
  21. #define DRIVER_DESC "EHCI EXYNOS driver"
  22. #define EHCI_INSNREG00(base) (base + 0x90)
  23. #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
  24. #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
  25. #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
  26. #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
  27. #define EHCI_INSNREG00_ENABLE_DMA_BURST \
  28. (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
  29. EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
  30. static const char hcd_name[] = "ehci-exynos";
  31. static struct hc_driver __read_mostly exynos_ehci_hc_driver;
  32. #define PHY_NUMBER 3
  33. struct exynos_ehci_hcd {
  34. struct clk *clk;
  35. struct phy *phy[PHY_NUMBER];
  36. };
  37. #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
  38. static int exynos_ehci_get_phy(struct device *dev,
  39. struct exynos_ehci_hcd *exynos_ehci)
  40. {
  41. struct device_node *child;
  42. struct phy *phy;
  43. int phy_number;
  44. int ret;
  45. /* Get PHYs for the controller */
  46. for_each_available_child_of_node(dev->of_node, child) {
  47. ret = of_property_read_u32(child, "reg", &phy_number);
  48. if (ret) {
  49. dev_err(dev, "Failed to parse device tree\n");
  50. of_node_put(child);
  51. return ret;
  52. }
  53. if (phy_number >= PHY_NUMBER) {
  54. dev_err(dev, "Invalid number of PHYs\n");
  55. of_node_put(child);
  56. return -EINVAL;
  57. }
  58. phy = devm_of_phy_get(dev, child, NULL);
  59. exynos_ehci->phy[phy_number] = phy;
  60. if (IS_ERR(phy)) {
  61. ret = PTR_ERR(phy);
  62. if (ret == -EPROBE_DEFER) {
  63. of_node_put(child);
  64. return ret;
  65. } else if (ret != -ENOSYS && ret != -ENODEV) {
  66. dev_err(dev,
  67. "Error retrieving usb2 phy: %d\n", ret);
  68. of_node_put(child);
  69. return ret;
  70. }
  71. }
  72. }
  73. return 0;
  74. }
  75. static int exynos_ehci_phy_enable(struct device *dev)
  76. {
  77. struct usb_hcd *hcd = dev_get_drvdata(dev);
  78. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  79. int i;
  80. int ret = 0;
  81. for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
  82. if (!IS_ERR(exynos_ehci->phy[i]))
  83. ret = phy_power_on(exynos_ehci->phy[i]);
  84. if (ret)
  85. for (i--; i >= 0; i--)
  86. if (!IS_ERR(exynos_ehci->phy[i]))
  87. phy_power_off(exynos_ehci->phy[i]);
  88. return ret;
  89. }
  90. static void exynos_ehci_phy_disable(struct device *dev)
  91. {
  92. struct usb_hcd *hcd = dev_get_drvdata(dev);
  93. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  94. int i;
  95. for (i = 0; i < PHY_NUMBER; i++)
  96. if (!IS_ERR(exynos_ehci->phy[i]))
  97. phy_power_off(exynos_ehci->phy[i]);
  98. }
  99. static void exynos_setup_vbus_gpio(struct device *dev)
  100. {
  101. int err;
  102. int gpio;
  103. if (!dev->of_node)
  104. return;
  105. gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
  106. if (!gpio_is_valid(gpio))
  107. return;
  108. err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
  109. "ehci_vbus_gpio");
  110. if (err)
  111. dev_err(dev, "can't request ehci vbus gpio %d", gpio);
  112. }
  113. static int exynos_ehci_probe(struct platform_device *pdev)
  114. {
  115. struct exynos_ehci_hcd *exynos_ehci;
  116. struct usb_hcd *hcd;
  117. struct ehci_hcd *ehci;
  118. struct resource *res;
  119. int irq;
  120. int err;
  121. /*
  122. * Right now device-tree probed devices don't get dma_mask set.
  123. * Since shared usb code relies on it, set it here for now.
  124. * Once we move to full device tree support this will vanish off.
  125. */
  126. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  127. if (err)
  128. return err;
  129. exynos_setup_vbus_gpio(&pdev->dev);
  130. hcd = usb_create_hcd(&exynos_ehci_hc_driver,
  131. &pdev->dev, dev_name(&pdev->dev));
  132. if (!hcd) {
  133. dev_err(&pdev->dev, "Unable to create HCD\n");
  134. return -ENOMEM;
  135. }
  136. exynos_ehci = to_exynos_ehci(hcd);
  137. err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
  138. if (err)
  139. goto fail_clk;
  140. exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
  141. if (IS_ERR(exynos_ehci->clk)) {
  142. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  143. err = PTR_ERR(exynos_ehci->clk);
  144. goto fail_clk;
  145. }
  146. err = clk_prepare_enable(exynos_ehci->clk);
  147. if (err)
  148. goto fail_clk;
  149. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  150. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  151. if (IS_ERR(hcd->regs)) {
  152. err = PTR_ERR(hcd->regs);
  153. goto fail_io;
  154. }
  155. hcd->rsrc_start = res->start;
  156. hcd->rsrc_len = resource_size(res);
  157. irq = platform_get_irq(pdev, 0);
  158. if (!irq) {
  159. dev_err(&pdev->dev, "Failed to get IRQ\n");
  160. err = -ENODEV;
  161. goto fail_io;
  162. }
  163. err = exynos_ehci_phy_enable(&pdev->dev);
  164. if (err) {
  165. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  166. goto fail_io;
  167. }
  168. ehci = hcd_to_ehci(hcd);
  169. ehci->caps = hcd->regs;
  170. /* DMA burst Enable */
  171. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  172. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  173. if (err) {
  174. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  175. goto fail_add_hcd;
  176. }
  177. device_wakeup_enable(hcd->self.controller);
  178. platform_set_drvdata(pdev, hcd);
  179. return 0;
  180. fail_add_hcd:
  181. exynos_ehci_phy_disable(&pdev->dev);
  182. fail_io:
  183. clk_disable_unprepare(exynos_ehci->clk);
  184. fail_clk:
  185. usb_put_hcd(hcd);
  186. return err;
  187. }
  188. static int exynos_ehci_remove(struct platform_device *pdev)
  189. {
  190. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  191. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  192. usb_remove_hcd(hcd);
  193. exynos_ehci_phy_disable(&pdev->dev);
  194. clk_disable_unprepare(exynos_ehci->clk);
  195. usb_put_hcd(hcd);
  196. return 0;
  197. }
  198. #ifdef CONFIG_PM
  199. static int exynos_ehci_suspend(struct device *dev)
  200. {
  201. struct usb_hcd *hcd = dev_get_drvdata(dev);
  202. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  203. bool do_wakeup = device_may_wakeup(dev);
  204. int rc;
  205. rc = ehci_suspend(hcd, do_wakeup);
  206. if (rc)
  207. return rc;
  208. exynos_ehci_phy_disable(dev);
  209. clk_disable_unprepare(exynos_ehci->clk);
  210. return rc;
  211. }
  212. static int exynos_ehci_resume(struct device *dev)
  213. {
  214. struct usb_hcd *hcd = dev_get_drvdata(dev);
  215. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  216. int ret;
  217. ret = clk_prepare_enable(exynos_ehci->clk);
  218. if (ret)
  219. return ret;
  220. ret = exynos_ehci_phy_enable(dev);
  221. if (ret) {
  222. dev_err(dev, "Failed to enable USB phy\n");
  223. clk_disable_unprepare(exynos_ehci->clk);
  224. return ret;
  225. }
  226. /* DMA burst Enable */
  227. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  228. ehci_resume(hcd, false);
  229. return 0;
  230. }
  231. #else
  232. #define exynos_ehci_suspend NULL
  233. #define exynos_ehci_resume NULL
  234. #endif
  235. static const struct dev_pm_ops exynos_ehci_pm_ops = {
  236. .suspend = exynos_ehci_suspend,
  237. .resume = exynos_ehci_resume,
  238. };
  239. #ifdef CONFIG_OF
  240. static const struct of_device_id exynos_ehci_match[] = {
  241. { .compatible = "samsung,exynos4210-ehci" },
  242. {},
  243. };
  244. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  245. #endif
  246. static struct platform_driver exynos_ehci_driver = {
  247. .probe = exynos_ehci_probe,
  248. .remove = exynos_ehci_remove,
  249. .shutdown = usb_hcd_platform_shutdown,
  250. .driver = {
  251. .name = "exynos-ehci",
  252. .pm = &exynos_ehci_pm_ops,
  253. .of_match_table = of_match_ptr(exynos_ehci_match),
  254. }
  255. };
  256. static const struct ehci_driver_overrides exynos_overrides __initconst = {
  257. .extra_priv_size = sizeof(struct exynos_ehci_hcd),
  258. };
  259. static int __init ehci_exynos_init(void)
  260. {
  261. if (usb_disabled())
  262. return -ENODEV;
  263. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  264. ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
  265. return platform_driver_register(&exynos_ehci_driver);
  266. }
  267. module_init(ehci_exynos_init);
  268. static void __exit ehci_exynos_cleanup(void)
  269. {
  270. platform_driver_unregister(&exynos_ehci_driver);
  271. }
  272. module_exit(ehci_exynos_cleanup);
  273. MODULE_DESCRIPTION(DRIVER_DESC);
  274. MODULE_ALIAS("platform:exynos-ehci");
  275. MODULE_AUTHOR("Jingoo Han");
  276. MODULE_AUTHOR("Joonyoung Shim");
  277. MODULE_LICENSE("GPL v2");