ehci-exynos.c 9.1 KB

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