ohci-st.c 7.4 KB

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