ehci-st.c 8.3 KB

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