ehci-exynos.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. of_node_put(child);
  66. if (IS_ERR(phy)) {
  67. ret = PTR_ERR(phy);
  68. if (ret == -EPROBE_DEFER) {
  69. return ret;
  70. } else if (ret != -ENOSYS && ret != -ENODEV) {
  71. dev_err(dev,
  72. "Error retrieving usb2 phy: %d\n", ret);
  73. return ret;
  74. }
  75. }
  76. }
  77. return 0;
  78. }
  79. static int exynos_ehci_phy_enable(struct device *dev)
  80. {
  81. struct usb_hcd *hcd = dev_get_drvdata(dev);
  82. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  83. int i;
  84. int ret = 0;
  85. for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
  86. if (!IS_ERR(exynos_ehci->phy[i]))
  87. ret = phy_power_on(exynos_ehci->phy[i]);
  88. if (ret)
  89. for (i--; i >= 0; i--)
  90. if (!IS_ERR(exynos_ehci->phy[i]))
  91. phy_power_off(exynos_ehci->phy[i]);
  92. return ret;
  93. }
  94. static void exynos_ehci_phy_disable(struct device *dev)
  95. {
  96. struct usb_hcd *hcd = dev_get_drvdata(dev);
  97. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  98. int i;
  99. for (i = 0; i < PHY_NUMBER; i++)
  100. if (!IS_ERR(exynos_ehci->phy[i]))
  101. phy_power_off(exynos_ehci->phy[i]);
  102. }
  103. static void exynos_setup_vbus_gpio(struct device *dev)
  104. {
  105. int err;
  106. int gpio;
  107. if (!dev->of_node)
  108. return;
  109. gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
  110. if (!gpio_is_valid(gpio))
  111. return;
  112. err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
  113. "ehci_vbus_gpio");
  114. if (err)
  115. dev_err(dev, "can't request ehci vbus gpio %d", gpio);
  116. }
  117. static int exynos_ehci_probe(struct platform_device *pdev)
  118. {
  119. struct exynos_ehci_hcd *exynos_ehci;
  120. struct usb_hcd *hcd;
  121. struct ehci_hcd *ehci;
  122. struct resource *res;
  123. int irq;
  124. int err;
  125. /*
  126. * Right now device-tree probed devices don't get dma_mask set.
  127. * Since shared usb code relies on it, set it here for now.
  128. * Once we move to full device tree support this will vanish off.
  129. */
  130. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  131. if (err)
  132. return err;
  133. exynos_setup_vbus_gpio(&pdev->dev);
  134. hcd = usb_create_hcd(&exynos_ehci_hc_driver,
  135. &pdev->dev, dev_name(&pdev->dev));
  136. if (!hcd) {
  137. dev_err(&pdev->dev, "Unable to create HCD\n");
  138. return -ENOMEM;
  139. }
  140. exynos_ehci = to_exynos_ehci(hcd);
  141. if (of_device_is_compatible(pdev->dev.of_node,
  142. "samsung,exynos5440-ehci"))
  143. goto skip_phy;
  144. err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
  145. if (err)
  146. goto fail_clk;
  147. skip_phy:
  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. if (!res) {
  159. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  160. err = -ENXIO;
  161. goto fail_io;
  162. }
  163. hcd->rsrc_start = res->start;
  164. hcd->rsrc_len = resource_size(res);
  165. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  166. if (IS_ERR(hcd->regs)) {
  167. err = PTR_ERR(hcd->regs);
  168. goto fail_io;
  169. }
  170. irq = platform_get_irq(pdev, 0);
  171. if (!irq) {
  172. dev_err(&pdev->dev, "Failed to get IRQ\n");
  173. err = -ENODEV;
  174. goto fail_io;
  175. }
  176. err = exynos_ehci_phy_enable(&pdev->dev);
  177. if (err) {
  178. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  179. goto fail_io;
  180. }
  181. ehci = hcd_to_ehci(hcd);
  182. ehci->caps = hcd->regs;
  183. /* DMA burst Enable */
  184. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  185. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  186. if (err) {
  187. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  188. goto fail_add_hcd;
  189. }
  190. device_wakeup_enable(hcd->self.controller);
  191. platform_set_drvdata(pdev, hcd);
  192. return 0;
  193. fail_add_hcd:
  194. exynos_ehci_phy_disable(&pdev->dev);
  195. fail_io:
  196. clk_disable_unprepare(exynos_ehci->clk);
  197. fail_clk:
  198. usb_put_hcd(hcd);
  199. return err;
  200. }
  201. static int exynos_ehci_remove(struct platform_device *pdev)
  202. {
  203. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  204. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  205. usb_remove_hcd(hcd);
  206. exynos_ehci_phy_disable(&pdev->dev);
  207. clk_disable_unprepare(exynos_ehci->clk);
  208. usb_put_hcd(hcd);
  209. return 0;
  210. }
  211. #ifdef CONFIG_PM
  212. static int exynos_ehci_suspend(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. bool do_wakeup = device_may_wakeup(dev);
  217. int rc;
  218. rc = ehci_suspend(hcd, do_wakeup);
  219. if (rc)
  220. return rc;
  221. exynos_ehci_phy_disable(dev);
  222. clk_disable_unprepare(exynos_ehci->clk);
  223. return rc;
  224. }
  225. static int exynos_ehci_resume(struct device *dev)
  226. {
  227. struct usb_hcd *hcd = dev_get_drvdata(dev);
  228. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  229. int ret;
  230. clk_prepare_enable(exynos_ehci->clk);
  231. ret = exynos_ehci_phy_enable(dev);
  232. if (ret) {
  233. dev_err(dev, "Failed to enable USB phy\n");
  234. clk_disable_unprepare(exynos_ehci->clk);
  235. return ret;
  236. }
  237. /* DMA burst Enable */
  238. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  239. ehci_resume(hcd, false);
  240. return 0;
  241. }
  242. #else
  243. #define exynos_ehci_suspend NULL
  244. #define exynos_ehci_resume NULL
  245. #endif
  246. static const struct dev_pm_ops exynos_ehci_pm_ops = {
  247. .suspend = exynos_ehci_suspend,
  248. .resume = exynos_ehci_resume,
  249. };
  250. #ifdef CONFIG_OF
  251. static const struct of_device_id exynos_ehci_match[] = {
  252. { .compatible = "samsung,exynos4210-ehci" },
  253. { .compatible = "samsung,exynos5440-ehci" },
  254. {},
  255. };
  256. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  257. #endif
  258. static struct platform_driver exynos_ehci_driver = {
  259. .probe = exynos_ehci_probe,
  260. .remove = exynos_ehci_remove,
  261. .shutdown = usb_hcd_platform_shutdown,
  262. .driver = {
  263. .name = "exynos-ehci",
  264. .owner = THIS_MODULE,
  265. .pm = &exynos_ehci_pm_ops,
  266. .of_match_table = of_match_ptr(exynos_ehci_match),
  267. }
  268. };
  269. static const struct ehci_driver_overrides exynos_overrides __initdata = {
  270. .extra_priv_size = sizeof(struct exynos_ehci_hcd),
  271. };
  272. static int __init ehci_exynos_init(void)
  273. {
  274. if (usb_disabled())
  275. return -ENODEV;
  276. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  277. ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
  278. return platform_driver_register(&exynos_ehci_driver);
  279. }
  280. module_init(ehci_exynos_init);
  281. static void __exit ehci_exynos_cleanup(void)
  282. {
  283. platform_driver_unregister(&exynos_ehci_driver);
  284. }
  285. module_exit(ehci_exynos_cleanup);
  286. MODULE_DESCRIPTION(DRIVER_DESC);
  287. MODULE_ALIAS("platform:exynos-ehci");
  288. MODULE_AUTHOR("Jingoo Han");
  289. MODULE_AUTHOR("Joonyoung Shim");
  290. MODULE_LICENSE("GPL v2");