ohci-st.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ST OHCI driver
  4. *
  5. * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
  6. *
  7. * Author: Peter Griffin <peter.griffin@linaro.org>
  8. *
  9. * Derived from ohci-platform.c
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/hrtimer.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/err.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/reset.h>
  21. #include <linux/usb/ohci_pdriver.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/hcd.h>
  24. #include "ohci.h"
  25. #define USB_MAX_CLKS 3
  26. struct st_ohci_platform_priv {
  27. struct clk *clks[USB_MAX_CLKS];
  28. struct clk *clk48;
  29. struct reset_control *rst;
  30. struct reset_control *pwr;
  31. struct phy *phy;
  32. };
  33. #define DRIVER_DESC "OHCI STMicroelectronics driver"
  34. #define hcd_to_ohci_priv(h) \
  35. ((struct st_ohci_platform_priv *)hcd_to_ohci(h)->priv)
  36. static const char hcd_name[] = "ohci-st";
  37. static int st_ohci_platform_power_on(struct platform_device *dev)
  38. {
  39. struct usb_hcd *hcd = platform_get_drvdata(dev);
  40. struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  41. int clk, ret;
  42. ret = reset_control_deassert(priv->pwr);
  43. if (ret)
  44. return ret;
  45. ret = reset_control_deassert(priv->rst);
  46. if (ret)
  47. goto err_assert_power;
  48. /* some SoCs don't have a dedicated 48Mhz clock, but those that do
  49. need the rate to be explicitly set */
  50. if (priv->clk48) {
  51. ret = clk_set_rate(priv->clk48, 48000000);
  52. if (ret)
  53. goto err_assert_reset;
  54. }
  55. for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
  56. ret = clk_prepare_enable(priv->clks[clk]);
  57. if (ret)
  58. goto err_disable_clks;
  59. }
  60. ret = phy_init(priv->phy);
  61. if (ret)
  62. goto err_disable_clks;
  63. ret = phy_power_on(priv->phy);
  64. if (ret)
  65. goto err_exit_phy;
  66. return 0;
  67. err_exit_phy:
  68. phy_exit(priv->phy);
  69. err_disable_clks:
  70. while (--clk >= 0)
  71. clk_disable_unprepare(priv->clks[clk]);
  72. err_assert_reset:
  73. reset_control_assert(priv->rst);
  74. err_assert_power:
  75. reset_control_assert(priv->pwr);
  76. return ret;
  77. }
  78. static void st_ohci_platform_power_off(struct platform_device *dev)
  79. {
  80. struct usb_hcd *hcd = platform_get_drvdata(dev);
  81. struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  82. int clk;
  83. reset_control_assert(priv->pwr);
  84. reset_control_assert(priv->rst);
  85. phy_power_off(priv->phy);
  86. phy_exit(priv->phy);
  87. for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
  88. if (priv->clks[clk])
  89. clk_disable_unprepare(priv->clks[clk]);
  90. }
  91. static struct hc_driver __read_mostly ohci_platform_hc_driver;
  92. static const struct ohci_driver_overrides platform_overrides __initconst = {
  93. .product_desc = "ST OHCI controller",
  94. .extra_priv_size = sizeof(struct st_ohci_platform_priv),
  95. };
  96. static struct usb_ohci_pdata ohci_platform_defaults = {
  97. .power_on = st_ohci_platform_power_on,
  98. .power_suspend = st_ohci_platform_power_off,
  99. .power_off = st_ohci_platform_power_off,
  100. };
  101. static int st_ohci_platform_probe(struct platform_device *dev)
  102. {
  103. struct usb_hcd *hcd;
  104. struct resource *res_mem;
  105. struct usb_ohci_pdata *pdata = &ohci_platform_defaults;
  106. struct st_ohci_platform_priv *priv;
  107. struct ohci_hcd *ohci;
  108. int err, irq, clk = 0;
  109. if (usb_disabled())
  110. return -ENODEV;
  111. irq = platform_get_irq(dev, 0);
  112. if (irq < 0) {
  113. dev_err(&dev->dev, "no irq provided");
  114. return irq;
  115. }
  116. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  117. if (!res_mem) {
  118. dev_err(&dev->dev, "no memory resource provided");
  119. return -ENXIO;
  120. }
  121. hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
  122. dev_name(&dev->dev));
  123. if (!hcd)
  124. return -ENOMEM;
  125. platform_set_drvdata(dev, hcd);
  126. dev->dev.platform_data = pdata;
  127. priv = hcd_to_ohci_priv(hcd);
  128. ohci = hcd_to_ohci(hcd);
  129. priv->phy = devm_phy_get(&dev->dev, "usb");
  130. if (IS_ERR(priv->phy)) {
  131. err = PTR_ERR(priv->phy);
  132. goto err_put_hcd;
  133. }
  134. for (clk = 0; clk < USB_MAX_CLKS; clk++) {
  135. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  136. if (IS_ERR(priv->clks[clk])) {
  137. err = PTR_ERR(priv->clks[clk]);
  138. if (err == -EPROBE_DEFER)
  139. goto err_put_clks;
  140. priv->clks[clk] = NULL;
  141. break;
  142. }
  143. }
  144. /* some SoCs don't have a dedicated 48Mhz clock, but those that
  145. do need the rate to be explicitly set */
  146. priv->clk48 = devm_clk_get(&dev->dev, "clk48");
  147. if (IS_ERR(priv->clk48)) {
  148. dev_info(&dev->dev, "48MHz clk not found\n");
  149. priv->clk48 = NULL;
  150. }
  151. priv->pwr =
  152. devm_reset_control_get_optional_shared(&dev->dev, "power");
  153. if (IS_ERR(priv->pwr)) {
  154. err = PTR_ERR(priv->pwr);
  155. goto err_put_clks;
  156. }
  157. priv->rst =
  158. devm_reset_control_get_optional_shared(&dev->dev, "softreset");
  159. if (IS_ERR(priv->rst)) {
  160. err = PTR_ERR(priv->rst);
  161. goto err_put_clks;
  162. }
  163. if (pdata->power_on) {
  164. err = pdata->power_on(dev);
  165. if (err < 0)
  166. goto err_power;
  167. }
  168. hcd->rsrc_start = res_mem->start;
  169. hcd->rsrc_len = resource_size(res_mem);
  170. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  171. if (IS_ERR(hcd->regs)) {
  172. err = PTR_ERR(hcd->regs);
  173. goto err_power;
  174. }
  175. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  176. if (err)
  177. goto err_power;
  178. device_wakeup_enable(hcd->self.controller);
  179. platform_set_drvdata(dev, hcd);
  180. return err;
  181. err_power:
  182. if (pdata->power_off)
  183. pdata->power_off(dev);
  184. err_put_clks:
  185. while (--clk >= 0)
  186. clk_put(priv->clks[clk]);
  187. err_put_hcd:
  188. if (pdata == &ohci_platform_defaults)
  189. dev->dev.platform_data = NULL;
  190. usb_put_hcd(hcd);
  191. return err;
  192. }
  193. static int st_ohci_platform_remove(struct platform_device *dev)
  194. {
  195. struct usb_hcd *hcd = platform_get_drvdata(dev);
  196. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  197. struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  198. int clk;
  199. usb_remove_hcd(hcd);
  200. if (pdata->power_off)
  201. pdata->power_off(dev);
  202. for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
  203. clk_put(priv->clks[clk]);
  204. usb_put_hcd(hcd);
  205. if (pdata == &ohci_platform_defaults)
  206. dev->dev.platform_data = NULL;
  207. return 0;
  208. }
  209. #ifdef CONFIG_PM_SLEEP
  210. static int st_ohci_suspend(struct device *dev)
  211. {
  212. struct usb_hcd *hcd = dev_get_drvdata(dev);
  213. struct usb_ohci_pdata *pdata = dev->platform_data;
  214. struct platform_device *pdev = to_platform_device(dev);
  215. bool do_wakeup = device_may_wakeup(dev);
  216. int ret;
  217. ret = ohci_suspend(hcd, do_wakeup);
  218. if (ret)
  219. return ret;
  220. if (pdata->power_suspend)
  221. pdata->power_suspend(pdev);
  222. return ret;
  223. }
  224. static int st_ohci_resume(struct device *dev)
  225. {
  226. struct usb_hcd *hcd = dev_get_drvdata(dev);
  227. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  228. struct platform_device *pdev = to_platform_device(dev);
  229. int err;
  230. if (pdata->power_on) {
  231. err = pdata->power_on(pdev);
  232. if (err < 0)
  233. return err;
  234. }
  235. ohci_resume(hcd, false);
  236. return 0;
  237. }
  238. static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
  239. #endif /* CONFIG_PM_SLEEP */
  240. static const struct of_device_id st_ohci_platform_ids[] = {
  241. { .compatible = "st,st-ohci-300x", },
  242. { /* sentinel */ }
  243. };
  244. MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
  245. static struct platform_driver ohci_platform_driver = {
  246. .probe = st_ohci_platform_probe,
  247. .remove = st_ohci_platform_remove,
  248. .shutdown = usb_hcd_platform_shutdown,
  249. .driver = {
  250. .name = "st-ohci",
  251. #ifdef CONFIG_PM_SLEEP
  252. .pm = &st_ohci_pm_ops,
  253. #endif
  254. .of_match_table = st_ohci_platform_ids,
  255. }
  256. };
  257. static int __init ohci_platform_init(void)
  258. {
  259. if (usb_disabled())
  260. return -ENODEV;
  261. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  262. ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
  263. return platform_driver_register(&ohci_platform_driver);
  264. }
  265. module_init(ohci_platform_init);
  266. static void __exit ohci_platform_cleanup(void)
  267. {
  268. platform_driver_unregister(&ohci_platform_driver);
  269. }
  270. module_exit(ohci_platform_cleanup);
  271. MODULE_DESCRIPTION(DRIVER_DESC);
  272. MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
  273. MODULE_LICENSE("GPL");