ehci-exynos.c 8.1 KB

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