ehci-st.c 8.1 KB

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