ohci-st.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. int err, irq, clk = 0;
  108. if (usb_disabled())
  109. return -ENODEV;
  110. irq = platform_get_irq(dev, 0);
  111. if (irq < 0)
  112. return irq;
  113. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  114. if (!res_mem) {
  115. dev_err(&dev->dev, "no memory resource provided");
  116. return -ENXIO;
  117. }
  118. hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
  119. dev_name(&dev->dev));
  120. if (!hcd)
  121. return -ENOMEM;
  122. platform_set_drvdata(dev, hcd);
  123. dev->dev.platform_data = pdata;
  124. priv = hcd_to_ohci_priv(hcd);
  125. priv->phy = devm_phy_get(&dev->dev, "usb");
  126. if (IS_ERR(priv->phy)) {
  127. err = PTR_ERR(priv->phy);
  128. goto err_put_hcd;
  129. }
  130. for (clk = 0; clk < USB_MAX_CLKS; clk++) {
  131. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  132. if (IS_ERR(priv->clks[clk])) {
  133. err = PTR_ERR(priv->clks[clk]);
  134. if (err == -EPROBE_DEFER)
  135. goto err_put_clks;
  136. priv->clks[clk] = NULL;
  137. break;
  138. }
  139. }
  140. /* some SoCs don't have a dedicated 48Mhz clock, but those that
  141. do need the rate to be explicitly set */
  142. priv->clk48 = devm_clk_get(&dev->dev, "clk48");
  143. if (IS_ERR(priv->clk48)) {
  144. dev_info(&dev->dev, "48MHz clk not found\n");
  145. priv->clk48 = NULL;
  146. }
  147. priv->pwr =
  148. devm_reset_control_get_optional_shared(&dev->dev, "power");
  149. if (IS_ERR(priv->pwr)) {
  150. err = PTR_ERR(priv->pwr);
  151. goto err_put_clks;
  152. }
  153. priv->rst =
  154. devm_reset_control_get_optional_shared(&dev->dev, "softreset");
  155. if (IS_ERR(priv->rst)) {
  156. err = PTR_ERR(priv->rst);
  157. goto err_put_clks;
  158. }
  159. if (pdata->power_on) {
  160. err = pdata->power_on(dev);
  161. if (err < 0)
  162. goto err_power;
  163. }
  164. hcd->rsrc_start = res_mem->start;
  165. hcd->rsrc_len = resource_size(res_mem);
  166. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  167. if (IS_ERR(hcd->regs)) {
  168. err = PTR_ERR(hcd->regs);
  169. goto err_power;
  170. }
  171. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  172. if (err)
  173. goto err_power;
  174. device_wakeup_enable(hcd->self.controller);
  175. platform_set_drvdata(dev, hcd);
  176. return err;
  177. err_power:
  178. if (pdata->power_off)
  179. pdata->power_off(dev);
  180. err_put_clks:
  181. while (--clk >= 0)
  182. clk_put(priv->clks[clk]);
  183. err_put_hcd:
  184. if (pdata == &ohci_platform_defaults)
  185. dev->dev.platform_data = NULL;
  186. usb_put_hcd(hcd);
  187. return err;
  188. }
  189. static int st_ohci_platform_remove(struct platform_device *dev)
  190. {
  191. struct usb_hcd *hcd = platform_get_drvdata(dev);
  192. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  193. struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  194. int clk;
  195. usb_remove_hcd(hcd);
  196. if (pdata->power_off)
  197. pdata->power_off(dev);
  198. for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
  199. clk_put(priv->clks[clk]);
  200. usb_put_hcd(hcd);
  201. if (pdata == &ohci_platform_defaults)
  202. dev->dev.platform_data = NULL;
  203. return 0;
  204. }
  205. #ifdef CONFIG_PM_SLEEP
  206. static int st_ohci_suspend(struct device *dev)
  207. {
  208. struct usb_hcd *hcd = dev_get_drvdata(dev);
  209. struct usb_ohci_pdata *pdata = dev->platform_data;
  210. struct platform_device *pdev = to_platform_device(dev);
  211. bool do_wakeup = device_may_wakeup(dev);
  212. int ret;
  213. ret = ohci_suspend(hcd, do_wakeup);
  214. if (ret)
  215. return ret;
  216. if (pdata->power_suspend)
  217. pdata->power_suspend(pdev);
  218. return ret;
  219. }
  220. static int st_ohci_resume(struct device *dev)
  221. {
  222. struct usb_hcd *hcd = dev_get_drvdata(dev);
  223. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  224. struct platform_device *pdev = to_platform_device(dev);
  225. int err;
  226. if (pdata->power_on) {
  227. err = pdata->power_on(pdev);
  228. if (err < 0)
  229. return err;
  230. }
  231. ohci_resume(hcd, false);
  232. return 0;
  233. }
  234. static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
  235. #endif /* CONFIG_PM_SLEEP */
  236. static const struct of_device_id st_ohci_platform_ids[] = {
  237. { .compatible = "st,st-ohci-300x", },
  238. { /* sentinel */ }
  239. };
  240. MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
  241. static struct platform_driver ohci_platform_driver = {
  242. .probe = st_ohci_platform_probe,
  243. .remove = st_ohci_platform_remove,
  244. .shutdown = usb_hcd_platform_shutdown,
  245. .driver = {
  246. .name = "st-ohci",
  247. #ifdef CONFIG_PM_SLEEP
  248. .pm = &st_ohci_pm_ops,
  249. #endif
  250. .of_match_table = st_ohci_platform_ids,
  251. }
  252. };
  253. static int __init ohci_platform_init(void)
  254. {
  255. if (usb_disabled())
  256. return -ENODEV;
  257. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  258. ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
  259. return platform_driver_register(&ohci_platform_driver);
  260. }
  261. module_init(ohci_platform_init);
  262. static void __exit ohci_platform_cleanup(void)
  263. {
  264. platform_driver_unregister(&ohci_platform_driver);
  265. }
  266. module_exit(ohci_platform_cleanup);
  267. MODULE_DESCRIPTION(DRIVER_DESC);
  268. MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
  269. MODULE_LICENSE("GPL");