ohci-exynos.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SAMSUNG EXYNOS USB HOST OHCI Controller
  4. *
  5. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  6. * Author: Jingoo Han <[email protected]>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/phy/phy.h>
  16. #include <linux/usb.h>
  17. #include <linux/usb/hcd.h>
  18. #include "ohci.h"
  19. #define DRIVER_DESC "OHCI Exynos driver"
  20. static struct hc_driver __read_mostly exynos_ohci_hc_driver;
  21. #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
  22. #define PHY_NUMBER 3
  23. struct exynos_ohci_hcd {
  24. struct clk *clk;
  25. struct device_node *of_node;
  26. struct phy *phy[PHY_NUMBER];
  27. bool legacy_phy;
  28. };
  29. static int exynos_ohci_get_phy(struct device *dev,
  30. struct exynos_ohci_hcd *exynos_ohci)
  31. {
  32. struct device_node *child;
  33. struct phy *phy;
  34. int phy_number, num_phys;
  35. int ret;
  36. /* Get PHYs for the controller */
  37. num_phys = of_count_phandle_with_args(dev->of_node, "phys",
  38. "#phy-cells");
  39. for (phy_number = 0; phy_number < num_phys; phy_number++) {
  40. phy = devm_of_phy_get_by_index(dev, dev->of_node, phy_number);
  41. if (IS_ERR(phy))
  42. return PTR_ERR(phy);
  43. exynos_ohci->phy[phy_number] = phy;
  44. }
  45. if (num_phys > 0)
  46. return 0;
  47. /* Get PHYs using legacy bindings */
  48. for_each_available_child_of_node(dev->of_node, child) {
  49. ret = of_property_read_u32(child, "reg", &phy_number);
  50. if (ret) {
  51. dev_err(dev, "Failed to parse device tree\n");
  52. of_node_put(child);
  53. return ret;
  54. }
  55. if (phy_number >= PHY_NUMBER) {
  56. dev_err(dev, "Invalid number of PHYs\n");
  57. of_node_put(child);
  58. return -EINVAL;
  59. }
  60. phy = devm_of_phy_get(dev, child, NULL);
  61. exynos_ohci->phy[phy_number] = phy;
  62. if (IS_ERR(phy)) {
  63. ret = PTR_ERR(phy);
  64. if (ret == -EPROBE_DEFER) {
  65. of_node_put(child);
  66. return ret;
  67. } else if (ret != -ENOSYS && ret != -ENODEV) {
  68. dev_err(dev,
  69. "Error retrieving usb2 phy: %d\n", ret);
  70. of_node_put(child);
  71. return ret;
  72. }
  73. }
  74. }
  75. exynos_ohci->legacy_phy = true;
  76. return 0;
  77. }
  78. static int exynos_ohci_phy_enable(struct device *dev)
  79. {
  80. struct usb_hcd *hcd = dev_get_drvdata(dev);
  81. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  82. int i;
  83. int ret = 0;
  84. for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
  85. if (!IS_ERR(exynos_ohci->phy[i]))
  86. ret = phy_power_on(exynos_ohci->phy[i]);
  87. if (ret)
  88. for (i--; i >= 0; i--)
  89. if (!IS_ERR(exynos_ohci->phy[i]))
  90. phy_power_off(exynos_ohci->phy[i]);
  91. return ret;
  92. }
  93. static void exynos_ohci_phy_disable(struct device *dev)
  94. {
  95. struct usb_hcd *hcd = dev_get_drvdata(dev);
  96. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  97. int i;
  98. for (i = 0; i < PHY_NUMBER; i++)
  99. if (!IS_ERR(exynos_ohci->phy[i]))
  100. phy_power_off(exynos_ohci->phy[i]);
  101. }
  102. static int exynos_ohci_probe(struct platform_device *pdev)
  103. {
  104. struct exynos_ohci_hcd *exynos_ohci;
  105. struct usb_hcd *hcd;
  106. struct resource *res;
  107. int irq;
  108. int err;
  109. /*
  110. * Right now device-tree probed devices don't get dma_mask set.
  111. * Since shared usb code relies on it, set it here for now.
  112. * Once we move to full device tree support this will vanish off.
  113. */
  114. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  115. if (err)
  116. return err;
  117. hcd = usb_create_hcd(&exynos_ohci_hc_driver,
  118. &pdev->dev, dev_name(&pdev->dev));
  119. if (!hcd) {
  120. dev_err(&pdev->dev, "Unable to create HCD\n");
  121. return -ENOMEM;
  122. }
  123. exynos_ohci = to_exynos_ohci(hcd);
  124. err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
  125. if (err)
  126. goto fail_clk;
  127. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  128. if (IS_ERR(exynos_ohci->clk)) {
  129. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  130. err = PTR_ERR(exynos_ohci->clk);
  131. goto fail_clk;
  132. }
  133. err = clk_prepare_enable(exynos_ohci->clk);
  134. if (err)
  135. goto fail_clk;
  136. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  137. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  138. if (IS_ERR(hcd->regs)) {
  139. err = PTR_ERR(hcd->regs);
  140. goto fail_io;
  141. }
  142. hcd->rsrc_start = res->start;
  143. hcd->rsrc_len = resource_size(res);
  144. irq = platform_get_irq(pdev, 0);
  145. if (irq < 0) {
  146. err = irq;
  147. goto fail_io;
  148. }
  149. platform_set_drvdata(pdev, hcd);
  150. err = exynos_ohci_phy_enable(&pdev->dev);
  151. if (err) {
  152. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  153. goto fail_io;
  154. }
  155. /*
  156. * Workaround: reset of_node pointer to avoid conflict between legacy
  157. * Exynos OHCI port subnodes and generic USB device bindings
  158. */
  159. exynos_ohci->of_node = pdev->dev.of_node;
  160. if (exynos_ohci->legacy_phy)
  161. pdev->dev.of_node = NULL;
  162. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  163. if (err) {
  164. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  165. goto fail_add_hcd;
  166. }
  167. device_wakeup_enable(hcd->self.controller);
  168. return 0;
  169. fail_add_hcd:
  170. exynos_ohci_phy_disable(&pdev->dev);
  171. pdev->dev.of_node = exynos_ohci->of_node;
  172. fail_io:
  173. clk_disable_unprepare(exynos_ohci->clk);
  174. fail_clk:
  175. usb_put_hcd(hcd);
  176. return err;
  177. }
  178. static int exynos_ohci_remove(struct platform_device *pdev)
  179. {
  180. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  181. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  182. pdev->dev.of_node = exynos_ohci->of_node;
  183. usb_remove_hcd(hcd);
  184. exynos_ohci_phy_disable(&pdev->dev);
  185. clk_disable_unprepare(exynos_ohci->clk);
  186. usb_put_hcd(hcd);
  187. return 0;
  188. }
  189. static void exynos_ohci_shutdown(struct platform_device *pdev)
  190. {
  191. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  192. if (hcd->driver->shutdown)
  193. hcd->driver->shutdown(hcd);
  194. }
  195. #ifdef CONFIG_PM
  196. static int exynos_ohci_suspend(struct device *dev)
  197. {
  198. struct usb_hcd *hcd = dev_get_drvdata(dev);
  199. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  200. bool do_wakeup = device_may_wakeup(dev);
  201. int rc = ohci_suspend(hcd, do_wakeup);
  202. if (rc)
  203. return rc;
  204. exynos_ohci_phy_disable(dev);
  205. clk_disable_unprepare(exynos_ohci->clk);
  206. return 0;
  207. }
  208. static int exynos_ohci_resume(struct device *dev)
  209. {
  210. struct usb_hcd *hcd = dev_get_drvdata(dev);
  211. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  212. int ret;
  213. clk_prepare_enable(exynos_ohci->clk);
  214. ret = exynos_ohci_phy_enable(dev);
  215. if (ret) {
  216. dev_err(dev, "Failed to enable USB phy\n");
  217. clk_disable_unprepare(exynos_ohci->clk);
  218. return ret;
  219. }
  220. ohci_resume(hcd, false);
  221. return 0;
  222. }
  223. #else
  224. #define exynos_ohci_suspend NULL
  225. #define exynos_ohci_resume NULL
  226. #endif
  227. static const struct ohci_driver_overrides exynos_overrides __initconst = {
  228. .extra_priv_size = sizeof(struct exynos_ohci_hcd),
  229. };
  230. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  231. .suspend = exynos_ohci_suspend,
  232. .resume = exynos_ohci_resume,
  233. };
  234. #ifdef CONFIG_OF
  235. static const struct of_device_id exynos_ohci_match[] = {
  236. { .compatible = "samsung,exynos4210-ohci" },
  237. {},
  238. };
  239. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  240. #endif
  241. static struct platform_driver exynos_ohci_driver = {
  242. .probe = exynos_ohci_probe,
  243. .remove = exynos_ohci_remove,
  244. .shutdown = exynos_ohci_shutdown,
  245. .driver = {
  246. .name = "exynos-ohci",
  247. .pm = &exynos_ohci_pm_ops,
  248. .of_match_table = of_match_ptr(exynos_ohci_match),
  249. }
  250. };
  251. static int __init ohci_exynos_init(void)
  252. {
  253. if (usb_disabled())
  254. return -ENODEV;
  255. ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
  256. return platform_driver_register(&exynos_ohci_driver);
  257. }
  258. module_init(ohci_exynos_init);
  259. static void __exit ohci_exynos_cleanup(void)
  260. {
  261. platform_driver_unregister(&exynos_ohci_driver);
  262. }
  263. module_exit(ohci_exynos_cleanup);
  264. MODULE_ALIAS("platform:exynos-ohci");
  265. MODULE_AUTHOR("Jingoo Han <[email protected]>");
  266. MODULE_LICENSE("GPL v2");