ohci-exynos.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <jg1.han@samsung.com>
  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 phy *phy;
  33. int phy_number, num_phys;
  34. int ret;
  35. /* Get PHYs for the controller */
  36. num_phys = of_count_phandle_with_args(dev->of_node, "phys",
  37. "#phy-cells");
  38. for (phy_number = 0; phy_number < num_phys; phy_number++) {
  39. phy = devm_of_phy_get_by_index(dev, dev->of_node, phy_number);
  40. if (IS_ERR(phy))
  41. return PTR_ERR(phy);
  42. exynos_ohci->phy[phy_number] = phy;
  43. }
  44. if (num_phys > 0)
  45. return 0;
  46. /* Get PHYs using legacy bindings */
  47. for_each_available_child_of_node_scoped(dev->of_node, child) {
  48. ret = of_property_read_u32(child, "reg", &phy_number);
  49. if (ret) {
  50. dev_err(dev, "Failed to parse device tree\n");
  51. return ret;
  52. }
  53. if (phy_number >= PHY_NUMBER) {
  54. dev_err(dev, "Invalid number of PHYs\n");
  55. return -EINVAL;
  56. }
  57. phy = devm_of_phy_optional_get(dev, child, NULL);
  58. exynos_ohci->phy[phy_number] = phy;
  59. if (IS_ERR(phy))
  60. return PTR_ERR(phy);
  61. }
  62. exynos_ohci->legacy_phy = true;
  63. return 0;
  64. }
  65. static int exynos_ohci_phy_enable(struct device *dev)
  66. {
  67. struct usb_hcd *hcd = dev_get_drvdata(dev);
  68. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  69. int i;
  70. int ret = 0;
  71. for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
  72. ret = phy_power_on(exynos_ohci->phy[i]);
  73. if (ret)
  74. for (i--; i >= 0; i--)
  75. phy_power_off(exynos_ohci->phy[i]);
  76. return ret;
  77. }
  78. static void exynos_ohci_phy_disable(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. for (i = 0; i < PHY_NUMBER; i++)
  84. phy_power_off(exynos_ohci->phy[i]);
  85. }
  86. static int exynos_ohci_probe(struct platform_device *pdev)
  87. {
  88. struct exynos_ohci_hcd *exynos_ohci;
  89. struct usb_hcd *hcd;
  90. struct resource *res;
  91. int irq;
  92. int err;
  93. /*
  94. * Right now device-tree probed devices don't get dma_mask set.
  95. * Since shared usb code relies on it, set it here for now.
  96. * Once we move to full device tree support this will vanish off.
  97. */
  98. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  99. if (err)
  100. return err;
  101. hcd = usb_create_hcd(&exynos_ohci_hc_driver,
  102. &pdev->dev, dev_name(&pdev->dev));
  103. if (!hcd) {
  104. dev_err(&pdev->dev, "Unable to create HCD\n");
  105. return -ENOMEM;
  106. }
  107. exynos_ohci = to_exynos_ohci(hcd);
  108. err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
  109. if (err)
  110. goto fail_io;
  111. exynos_ohci->clk = devm_clk_get_enabled(&pdev->dev, "usbhost");
  112. if (IS_ERR(exynos_ohci->clk)) {
  113. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  114. err = PTR_ERR(exynos_ohci->clk);
  115. goto fail_io;
  116. }
  117. hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  118. if (IS_ERR(hcd->regs)) {
  119. err = PTR_ERR(hcd->regs);
  120. goto fail_io;
  121. }
  122. hcd->rsrc_start = res->start;
  123. hcd->rsrc_len = resource_size(res);
  124. irq = platform_get_irq(pdev, 0);
  125. if (irq < 0) {
  126. err = irq;
  127. goto fail_io;
  128. }
  129. platform_set_drvdata(pdev, hcd);
  130. err = exynos_ohci_phy_enable(&pdev->dev);
  131. if (err) {
  132. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  133. goto fail_io;
  134. }
  135. /*
  136. * Workaround: reset of_node pointer to avoid conflict between legacy
  137. * Exynos OHCI port subnodes and generic USB device bindings
  138. */
  139. exynos_ohci->of_node = pdev->dev.of_node;
  140. if (exynos_ohci->legacy_phy)
  141. pdev->dev.of_node = NULL;
  142. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  143. if (err) {
  144. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  145. goto fail_add_hcd;
  146. }
  147. device_wakeup_enable(hcd->self.controller);
  148. return 0;
  149. fail_add_hcd:
  150. exynos_ohci_phy_disable(&pdev->dev);
  151. pdev->dev.of_node = exynos_ohci->of_node;
  152. fail_io:
  153. usb_put_hcd(hcd);
  154. return err;
  155. }
  156. static void exynos_ohci_remove(struct platform_device *pdev)
  157. {
  158. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  159. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  160. pdev->dev.of_node = exynos_ohci->of_node;
  161. usb_remove_hcd(hcd);
  162. exynos_ohci_phy_disable(&pdev->dev);
  163. usb_put_hcd(hcd);
  164. }
  165. static void exynos_ohci_shutdown(struct platform_device *pdev)
  166. {
  167. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  168. if (hcd->driver->shutdown)
  169. hcd->driver->shutdown(hcd);
  170. }
  171. static int exynos_ohci_suspend(struct device *dev)
  172. {
  173. struct usb_hcd *hcd = dev_get_drvdata(dev);
  174. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  175. bool do_wakeup = device_may_wakeup(dev);
  176. int rc = ohci_suspend(hcd, do_wakeup);
  177. if (rc)
  178. return rc;
  179. exynos_ohci_phy_disable(dev);
  180. clk_disable_unprepare(exynos_ohci->clk);
  181. return 0;
  182. }
  183. static int exynos_ohci_resume(struct device *dev)
  184. {
  185. struct usb_hcd *hcd = dev_get_drvdata(dev);
  186. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  187. int ret;
  188. clk_prepare_enable(exynos_ohci->clk);
  189. ret = exynos_ohci_phy_enable(dev);
  190. if (ret) {
  191. dev_err(dev, "Failed to enable USB phy\n");
  192. clk_disable_unprepare(exynos_ohci->clk);
  193. return ret;
  194. }
  195. ohci_resume(hcd, false);
  196. return 0;
  197. }
  198. static const struct ohci_driver_overrides exynos_overrides __initconst = {
  199. .extra_priv_size = sizeof(struct exynos_ohci_hcd),
  200. };
  201. static DEFINE_SIMPLE_DEV_PM_OPS(exynos_ohci_pm_ops,
  202. exynos_ohci_suspend, exynos_ohci_resume);
  203. #ifdef CONFIG_OF
  204. static const struct of_device_id exynos_ohci_match[] = {
  205. { .compatible = "samsung,exynos4210-ohci" },
  206. {},
  207. };
  208. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  209. #endif
  210. static struct platform_driver exynos_ohci_driver = {
  211. .probe = exynos_ohci_probe,
  212. .remove_new = exynos_ohci_remove,
  213. .shutdown = exynos_ohci_shutdown,
  214. .driver = {
  215. .name = "exynos-ohci",
  216. .pm = pm_ptr(&exynos_ohci_pm_ops),
  217. .of_match_table = of_match_ptr(exynos_ohci_match),
  218. }
  219. };
  220. static int __init ohci_exynos_init(void)
  221. {
  222. if (usb_disabled())
  223. return -ENODEV;
  224. ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
  225. return platform_driver_register(&exynos_ohci_driver);
  226. }
  227. module_init(ohci_exynos_init);
  228. static void __exit ohci_exynos_cleanup(void)
  229. {
  230. platform_driver_unregister(&exynos_ohci_driver);
  231. }
  232. module_exit(ohci_exynos_cleanup);
  233. MODULE_ALIAS("platform:exynos-ohci");
  234. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  235. MODULE_DESCRIPTION("OHCI support for Samsung S5P/Exynos SoC Series");
  236. MODULE_LICENSE("GPL v2");