ehci-st.c 8.4 KB

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