ohci-exynos.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * SAMSUNG EXYNOS USB HOST OHCI Controller
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  5. * Author: Jingoo Han <jg1.han@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/usb/phy.h>
  22. #include <linux/usb/samsung_usb_phy.h>
  23. #include <linux/usb.h>
  24. #include <linux/usb/hcd.h>
  25. #include <linux/usb/otg.h>
  26. #include "ohci.h"
  27. #define DRIVER_DESC "OHCI EXYNOS driver"
  28. static const char hcd_name[] = "ohci-exynos";
  29. static struct hc_driver __read_mostly exynos_ohci_hc_driver;
  30. #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
  31. #define PHY_NUMBER 3
  32. struct exynos_ohci_hcd {
  33. struct clk *clk;
  34. struct usb_phy *phy;
  35. struct usb_otg *otg;
  36. struct phy *phy_g[PHY_NUMBER];
  37. };
  38. static int exynos_ohci_get_phy(struct device *dev,
  39. struct exynos_ohci_hcd *exynos_ohci)
  40. {
  41. struct device_node *child;
  42. struct phy *phy;
  43. int phy_number;
  44. int ret = 0;
  45. exynos_ohci->phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  46. if (IS_ERR(exynos_ohci->phy)) {
  47. ret = PTR_ERR(exynos_ohci->phy);
  48. if (ret != -ENXIO && ret != -ENODEV) {
  49. dev_err(dev, "no usb2 phy configured\n");
  50. return ret;
  51. }
  52. dev_dbg(dev, "Failed to get usb2 phy\n");
  53. } else {
  54. exynos_ohci->otg = exynos_ohci->phy->otg;
  55. }
  56. /*
  57. * Getting generic phy:
  58. * We are keeping both types of phys as a part of transiting OHCI
  59. * to generic phy framework, so as to maintain backward compatibilty
  60. * with old DTB.
  61. * If there are existing devices using DTB files built from them,
  62. * to remove the support for old bindings in this driver,
  63. * we need to make sure that such devices have their DTBs
  64. * updated to ones built from new DTS.
  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_ohci->phy_g[phy_number] = phy;
  89. }
  90. return ret;
  91. }
  92. static int exynos_ohci_phy_enable(struct device *dev)
  93. {
  94. struct usb_hcd *hcd = dev_get_drvdata(dev);
  95. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  96. int i;
  97. int ret = 0;
  98. if (!IS_ERR(exynos_ohci->phy))
  99. return usb_phy_init(exynos_ohci->phy);
  100. for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
  101. if (!IS_ERR(exynos_ohci->phy_g[i]))
  102. ret = phy_power_on(exynos_ohci->phy_g[i]);
  103. if (ret)
  104. for (i--; i >= 0; i--)
  105. if (!IS_ERR(exynos_ohci->phy_g[i]))
  106. phy_power_off(exynos_ohci->phy_g[i]);
  107. return ret;
  108. }
  109. static void exynos_ohci_phy_disable(struct device *dev)
  110. {
  111. struct usb_hcd *hcd = dev_get_drvdata(dev);
  112. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  113. int i;
  114. if (!IS_ERR(exynos_ohci->phy)) {
  115. usb_phy_shutdown(exynos_ohci->phy);
  116. return;
  117. }
  118. for (i = 0; i < PHY_NUMBER; i++)
  119. if (!IS_ERR(exynos_ohci->phy_g[i]))
  120. phy_power_off(exynos_ohci->phy_g[i]);
  121. }
  122. static int exynos_ohci_probe(struct platform_device *pdev)
  123. {
  124. struct exynos_ohci_hcd *exynos_ohci;
  125. struct usb_hcd *hcd;
  126. struct resource *res;
  127. int irq;
  128. int err;
  129. /*
  130. * Right now device-tree probed devices don't get dma_mask set.
  131. * Since shared usb code relies on it, set it here for now.
  132. * Once we move to full device tree support this will vanish off.
  133. */
  134. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  135. if (err)
  136. return err;
  137. hcd = usb_create_hcd(&exynos_ohci_hc_driver,
  138. &pdev->dev, dev_name(&pdev->dev));
  139. if (!hcd) {
  140. dev_err(&pdev->dev, "Unable to create HCD\n");
  141. return -ENOMEM;
  142. }
  143. exynos_ohci = to_exynos_ohci(hcd);
  144. if (of_device_is_compatible(pdev->dev.of_node,
  145. "samsung,exynos5440-ohci"))
  146. goto skip_phy;
  147. err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
  148. if (err)
  149. goto fail_clk;
  150. skip_phy:
  151. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  152. if (IS_ERR(exynos_ohci->clk)) {
  153. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  154. err = PTR_ERR(exynos_ohci->clk);
  155. goto fail_clk;
  156. }
  157. err = clk_prepare_enable(exynos_ohci->clk);
  158. if (err)
  159. goto fail_clk;
  160. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. if (!res) {
  162. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  163. err = -ENXIO;
  164. goto fail_io;
  165. }
  166. hcd->rsrc_start = res->start;
  167. hcd->rsrc_len = resource_size(res);
  168. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  169. if (IS_ERR(hcd->regs)) {
  170. err = PTR_ERR(hcd->regs);
  171. goto fail_io;
  172. }
  173. irq = platform_get_irq(pdev, 0);
  174. if (!irq) {
  175. dev_err(&pdev->dev, "Failed to get IRQ\n");
  176. err = -ENODEV;
  177. goto fail_io;
  178. }
  179. if (exynos_ohci->otg)
  180. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  181. platform_set_drvdata(pdev, hcd);
  182. err = exynos_ohci_phy_enable(&pdev->dev);
  183. if (err) {
  184. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  185. goto fail_io;
  186. }
  187. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  188. if (err) {
  189. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  190. goto fail_add_hcd;
  191. }
  192. device_wakeup_enable(hcd->self.controller);
  193. return 0;
  194. fail_add_hcd:
  195. exynos_ohci_phy_disable(&pdev->dev);
  196. fail_io:
  197. clk_disable_unprepare(exynos_ohci->clk);
  198. fail_clk:
  199. usb_put_hcd(hcd);
  200. return err;
  201. }
  202. static int exynos_ohci_remove(struct platform_device *pdev)
  203. {
  204. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  205. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  206. usb_remove_hcd(hcd);
  207. if (exynos_ohci->otg)
  208. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  209. exynos_ohci_phy_disable(&pdev->dev);
  210. clk_disable_unprepare(exynos_ohci->clk);
  211. usb_put_hcd(hcd);
  212. return 0;
  213. }
  214. static void exynos_ohci_shutdown(struct platform_device *pdev)
  215. {
  216. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  217. if (hcd->driver->shutdown)
  218. hcd->driver->shutdown(hcd);
  219. }
  220. #ifdef CONFIG_PM
  221. static int exynos_ohci_suspend(struct device *dev)
  222. {
  223. struct usb_hcd *hcd = dev_get_drvdata(dev);
  224. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  225. bool do_wakeup = device_may_wakeup(dev);
  226. int rc = ohci_suspend(hcd, do_wakeup);
  227. if (rc)
  228. return rc;
  229. if (exynos_ohci->otg)
  230. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  231. exynos_ohci_phy_disable(dev);
  232. clk_disable_unprepare(exynos_ohci->clk);
  233. return 0;
  234. }
  235. static int exynos_ohci_resume(struct device *dev)
  236. {
  237. struct usb_hcd *hcd = dev_get_drvdata(dev);
  238. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  239. int ret;
  240. clk_prepare_enable(exynos_ohci->clk);
  241. if (exynos_ohci->otg)
  242. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  243. ret = exynos_ohci_phy_enable(dev);
  244. if (ret) {
  245. dev_err(dev, "Failed to enable USB phy\n");
  246. clk_disable_unprepare(exynos_ohci->clk);
  247. return ret;
  248. }
  249. ohci_resume(hcd, false);
  250. return 0;
  251. }
  252. #else
  253. #define exynos_ohci_suspend NULL
  254. #define exynos_ohci_resume NULL
  255. #endif
  256. static const struct ohci_driver_overrides exynos_overrides __initconst = {
  257. .extra_priv_size = sizeof(struct exynos_ohci_hcd),
  258. };
  259. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  260. .suspend = exynos_ohci_suspend,
  261. .resume = exynos_ohci_resume,
  262. };
  263. #ifdef CONFIG_OF
  264. static const struct of_device_id exynos_ohci_match[] = {
  265. { .compatible = "samsung,exynos4210-ohci" },
  266. { .compatible = "samsung,exynos5440-ohci" },
  267. {},
  268. };
  269. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  270. #endif
  271. static struct platform_driver exynos_ohci_driver = {
  272. .probe = exynos_ohci_probe,
  273. .remove = exynos_ohci_remove,
  274. .shutdown = exynos_ohci_shutdown,
  275. .driver = {
  276. .name = "exynos-ohci",
  277. .owner = THIS_MODULE,
  278. .pm = &exynos_ohci_pm_ops,
  279. .of_match_table = of_match_ptr(exynos_ohci_match),
  280. }
  281. };
  282. static int __init ohci_exynos_init(void)
  283. {
  284. if (usb_disabled())
  285. return -ENODEV;
  286. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  287. ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
  288. return platform_driver_register(&exynos_ohci_driver);
  289. }
  290. module_init(ohci_exynos_init);
  291. static void __exit ohci_exynos_cleanup(void)
  292. {
  293. platform_driver_unregister(&exynos_ohci_driver);
  294. }
  295. module_exit(ohci_exynos_cleanup);
  296. MODULE_ALIAS("platform:exynos-ohci");
  297. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  298. MODULE_LICENSE("GPL v2");