ehci-exynos.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/gpio/consumer.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 struct hc_driver __read_mostly exynos_ehci_hc_driver;
  31. #define PHY_NUMBER 3
  32. struct exynos_ehci_hcd {
  33. struct clk *clk;
  34. struct device_node *of_node;
  35. struct phy *phy[PHY_NUMBER];
  36. bool legacy_phy;
  37. };
  38. #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
  39. static int exynos_ehci_get_phy(struct device *dev,
  40. struct exynos_ehci_hcd *exynos_ehci)
  41. {
  42. struct phy *phy;
  43. int phy_number, num_phys;
  44. int ret;
  45. /* Get PHYs for the controller */
  46. num_phys = of_count_phandle_with_args(dev->of_node, "phys",
  47. "#phy-cells");
  48. for (phy_number = 0; phy_number < num_phys; phy_number++) {
  49. phy = devm_of_phy_get_by_index(dev, dev->of_node, phy_number);
  50. if (IS_ERR(phy))
  51. return PTR_ERR(phy);
  52. exynos_ehci->phy[phy_number] = phy;
  53. }
  54. if (num_phys > 0)
  55. return 0;
  56. /* Get PHYs using legacy bindings */
  57. for_each_available_child_of_node_scoped(dev->of_node, child) {
  58. ret = of_property_read_u32(child, "reg", &phy_number);
  59. if (ret) {
  60. dev_err(dev, "Failed to parse device tree\n");
  61. return ret;
  62. }
  63. if (phy_number >= PHY_NUMBER) {
  64. dev_err(dev, "Invalid number of PHYs\n");
  65. return -EINVAL;
  66. }
  67. phy = devm_of_phy_optional_get(dev, child, NULL);
  68. exynos_ehci->phy[phy_number] = phy;
  69. if (IS_ERR(phy))
  70. return PTR_ERR(phy);
  71. }
  72. exynos_ehci->legacy_phy = true;
  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. ret = phy_power_on(exynos_ehci->phy[i]);
  83. if (ret)
  84. for (i--; i >= 0; i--)
  85. phy_power_off(exynos_ehci->phy[i]);
  86. return ret;
  87. }
  88. static void exynos_ehci_phy_disable(struct device *dev)
  89. {
  90. struct usb_hcd *hcd = dev_get_drvdata(dev);
  91. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  92. int i;
  93. for (i = 0; i < PHY_NUMBER; i++)
  94. phy_power_off(exynos_ehci->phy[i]);
  95. }
  96. static void exynos_setup_vbus_gpio(struct device *dev)
  97. {
  98. struct gpio_desc *gpio;
  99. int err;
  100. gpio = devm_gpiod_get_optional(dev, "samsung,vbus", GPIOD_OUT_HIGH);
  101. err = PTR_ERR_OR_ZERO(gpio);
  102. if (err)
  103. dev_err(dev, "can't request ehci vbus gpio: %d\n", err);
  104. }
  105. static int exynos_ehci_probe(struct platform_device *pdev)
  106. {
  107. struct exynos_ehci_hcd *exynos_ehci;
  108. struct usb_hcd *hcd;
  109. struct ehci_hcd *ehci;
  110. struct resource *res;
  111. int irq;
  112. int err;
  113. /*
  114. * Right now device-tree probed devices don't get dma_mask set.
  115. * Since shared usb code relies on it, set it here for now.
  116. * Once we move to full device tree support this will vanish off.
  117. */
  118. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  119. if (err)
  120. return err;
  121. exynos_setup_vbus_gpio(&pdev->dev);
  122. hcd = usb_create_hcd(&exynos_ehci_hc_driver,
  123. &pdev->dev, dev_name(&pdev->dev));
  124. if (!hcd) {
  125. dev_err(&pdev->dev, "Unable to create HCD\n");
  126. return -ENOMEM;
  127. }
  128. exynos_ehci = to_exynos_ehci(hcd);
  129. err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
  130. if (err)
  131. goto fail_io;
  132. exynos_ehci->clk = devm_clk_get_enabled(&pdev->dev, "usbhost");
  133. if (IS_ERR(exynos_ehci->clk)) {
  134. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  135. err = PTR_ERR(exynos_ehci->clk);
  136. goto fail_io;
  137. }
  138. hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  139. if (IS_ERR(hcd->regs)) {
  140. err = PTR_ERR(hcd->regs);
  141. goto fail_io;
  142. }
  143. hcd->rsrc_start = res->start;
  144. hcd->rsrc_len = resource_size(res);
  145. irq = platform_get_irq(pdev, 0);
  146. if (irq < 0) {
  147. err = irq;
  148. goto fail_io;
  149. }
  150. err = exynos_ehci_phy_enable(&pdev->dev);
  151. if (err) {
  152. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  153. goto fail_io;
  154. }
  155. ehci = hcd_to_ehci(hcd);
  156. ehci->caps = hcd->regs;
  157. /*
  158. * Workaround: reset of_node pointer to avoid conflict between legacy
  159. * Exynos EHCI port subnodes and generic USB device bindings
  160. */
  161. exynos_ehci->of_node = pdev->dev.of_node;
  162. if (exynos_ehci->legacy_phy)
  163. pdev->dev.of_node = NULL;
  164. /* DMA burst Enable */
  165. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  166. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  167. if (err) {
  168. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  169. goto fail_add_hcd;
  170. }
  171. device_wakeup_enable(hcd->self.controller);
  172. platform_set_drvdata(pdev, hcd);
  173. return 0;
  174. fail_add_hcd:
  175. exynos_ehci_phy_disable(&pdev->dev);
  176. pdev->dev.of_node = exynos_ehci->of_node;
  177. fail_io:
  178. usb_put_hcd(hcd);
  179. return err;
  180. }
  181. static void exynos_ehci_remove(struct platform_device *pdev)
  182. {
  183. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  184. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  185. pdev->dev.of_node = exynos_ehci->of_node;
  186. usb_remove_hcd(hcd);
  187. exynos_ehci_phy_disable(&pdev->dev);
  188. usb_put_hcd(hcd);
  189. }
  190. static int exynos_ehci_suspend(struct device *dev)
  191. {
  192. struct usb_hcd *hcd = dev_get_drvdata(dev);
  193. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  194. bool do_wakeup = device_may_wakeup(dev);
  195. int rc;
  196. rc = ehci_suspend(hcd, do_wakeup);
  197. if (rc)
  198. return rc;
  199. exynos_ehci_phy_disable(dev);
  200. clk_disable_unprepare(exynos_ehci->clk);
  201. return rc;
  202. }
  203. static int exynos_ehci_resume(struct device *dev)
  204. {
  205. struct usb_hcd *hcd = dev_get_drvdata(dev);
  206. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  207. int ret;
  208. ret = clk_prepare_enable(exynos_ehci->clk);
  209. if (ret)
  210. return ret;
  211. ret = exynos_ehci_phy_enable(dev);
  212. if (ret) {
  213. dev_err(dev, "Failed to enable USB phy\n");
  214. clk_disable_unprepare(exynos_ehci->clk);
  215. return ret;
  216. }
  217. /* DMA burst Enable */
  218. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  219. ehci_resume(hcd, false);
  220. return 0;
  221. }
  222. static DEFINE_SIMPLE_DEV_PM_OPS(exynos_ehci_pm_ops,
  223. exynos_ehci_suspend, exynos_ehci_resume);
  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 exynos_ehci_driver = {
  232. .probe = exynos_ehci_probe,
  233. .remove_new = exynos_ehci_remove,
  234. .shutdown = usb_hcd_platform_shutdown,
  235. .driver = {
  236. .name = "exynos-ehci",
  237. .pm = pm_ptr(&exynos_ehci_pm_ops),
  238. .of_match_table = of_match_ptr(exynos_ehci_match),
  239. }
  240. };
  241. static const struct ehci_driver_overrides exynos_overrides __initconst = {
  242. .extra_priv_size = sizeof(struct exynos_ehci_hcd),
  243. };
  244. static int __init ehci_exynos_init(void)
  245. {
  246. if (usb_disabled())
  247. return -ENODEV;
  248. ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
  249. return platform_driver_register(&exynos_ehci_driver);
  250. }
  251. module_init(ehci_exynos_init);
  252. static void __exit ehci_exynos_cleanup(void)
  253. {
  254. platform_driver_unregister(&exynos_ehci_driver);
  255. }
  256. module_exit(ehci_exynos_cleanup);
  257. MODULE_DESCRIPTION(DRIVER_DESC);
  258. MODULE_ALIAS("platform:exynos-ehci");
  259. MODULE_AUTHOR("Jingoo Han");
  260. MODULE_AUTHOR("Joonyoung Shim");
  261. MODULE_LICENSE("GPL v2");