ehci-st.c 8.5 KB

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