ohci-exynos.c 7.5 KB

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