ehci-exynos.c 8.3 KB

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