ohci-st.c 7.3 KB

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