ehci-st.c 8.0 KB

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