ehci-exynos.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 < 0) {
  159. err = irq;
  160. goto fail_io;
  161. }
  162. err = exynos_ehci_phy_enable(&pdev->dev);
  163. if (err) {
  164. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  165. goto fail_io;
  166. }
  167. ehci = hcd_to_ehci(hcd);
  168. ehci->caps = hcd->regs;
  169. /* DMA burst Enable */
  170. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  171. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  172. if (err) {
  173. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  174. goto fail_add_hcd;
  175. }
  176. device_wakeup_enable(hcd->self.controller);
  177. platform_set_drvdata(pdev, hcd);
  178. return 0;
  179. fail_add_hcd:
  180. exynos_ehci_phy_disable(&pdev->dev);
  181. fail_io:
  182. clk_disable_unprepare(exynos_ehci->clk);
  183. fail_clk:
  184. usb_put_hcd(hcd);
  185. return err;
  186. }
  187. static int exynos_ehci_remove(struct platform_device *pdev)
  188. {
  189. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  190. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  191. usb_remove_hcd(hcd);
  192. exynos_ehci_phy_disable(&pdev->dev);
  193. clk_disable_unprepare(exynos_ehci->clk);
  194. usb_put_hcd(hcd);
  195. return 0;
  196. }
  197. #ifdef CONFIG_PM
  198. static int exynos_ehci_suspend(struct device *dev)
  199. {
  200. struct usb_hcd *hcd = dev_get_drvdata(dev);
  201. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  202. bool do_wakeup = device_may_wakeup(dev);
  203. int rc;
  204. rc = ehci_suspend(hcd, do_wakeup);
  205. if (rc)
  206. return rc;
  207. exynos_ehci_phy_disable(dev);
  208. clk_disable_unprepare(exynos_ehci->clk);
  209. return rc;
  210. }
  211. static int exynos_ehci_resume(struct device *dev)
  212. {
  213. struct usb_hcd *hcd = dev_get_drvdata(dev);
  214. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  215. int ret;
  216. ret = clk_prepare_enable(exynos_ehci->clk);
  217. if (ret)
  218. return ret;
  219. ret = exynos_ehci_phy_enable(dev);
  220. if (ret) {
  221. dev_err(dev, "Failed to enable USB phy\n");
  222. clk_disable_unprepare(exynos_ehci->clk);
  223. return ret;
  224. }
  225. /* DMA burst Enable */
  226. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  227. ehci_resume(hcd, false);
  228. return 0;
  229. }
  230. #else
  231. #define exynos_ehci_suspend NULL
  232. #define exynos_ehci_resume NULL
  233. #endif
  234. static const struct dev_pm_ops exynos_ehci_pm_ops = {
  235. .suspend = exynos_ehci_suspend,
  236. .resume = exynos_ehci_resume,
  237. };
  238. #ifdef CONFIG_OF
  239. static const struct of_device_id exynos_ehci_match[] = {
  240. { .compatible = "samsung,exynos4210-ehci" },
  241. {},
  242. };
  243. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  244. #endif
  245. static struct platform_driver exynos_ehci_driver = {
  246. .probe = exynos_ehci_probe,
  247. .remove = exynos_ehci_remove,
  248. .shutdown = usb_hcd_platform_shutdown,
  249. .driver = {
  250. .name = "exynos-ehci",
  251. .pm = &exynos_ehci_pm_ops,
  252. .of_match_table = of_match_ptr(exynos_ehci_match),
  253. }
  254. };
  255. static const struct ehci_driver_overrides exynos_overrides __initconst = {
  256. .extra_priv_size = sizeof(struct exynos_ehci_hcd),
  257. };
  258. static int __init ehci_exynos_init(void)
  259. {
  260. if (usb_disabled())
  261. return -ENODEV;
  262. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  263. ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
  264. return platform_driver_register(&exynos_ehci_driver);
  265. }
  266. module_init(ehci_exynos_init);
  267. static void __exit ehci_exynos_cleanup(void)
  268. {
  269. platform_driver_unregister(&exynos_ehci_driver);
  270. }
  271. module_exit(ehci_exynos_cleanup);
  272. MODULE_DESCRIPTION(DRIVER_DESC);
  273. MODULE_ALIAS("platform:exynos-ehci");
  274. MODULE_AUTHOR("Jingoo Han");
  275. MODULE_AUTHOR("Joonyoung Shim");
  276. MODULE_LICENSE("GPL v2");