ehci-exynos.c 8.1 KB

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