ehci-platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Generic platform ehci driver
  3. *
  4. * Copyright 2007 Steven Brown <sbrown@cortland.com>
  5. * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
  6. * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  7. *
  8. * Derived from the ohci-ssb driver
  9. * Copyright 2007 Michael Buesch <m@bues.ch>
  10. *
  11. * Derived from the EHCI-PCI driver
  12. * Copyright (c) 2000-2004 by David Brownell
  13. *
  14. * Derived from the ohci-pci driver
  15. * Copyright 1999 Roman Weissgaerber
  16. * Copyright 2000-2002 David Brownell
  17. * Copyright 1999 Linus Torvalds
  18. * Copyright 1999 Gregory P. Smith
  19. *
  20. * Licensed under the GNU/GPL. See COPYING for details.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/err.h>
  25. #include <linux/kernel.h>
  26. #include <linux/hrtimer.h>
  27. #include <linux/io.h>
  28. #include <linux/module.h>
  29. #include <linux/of.h>
  30. #include <linux/phy/phy.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/reset.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/hcd.h>
  35. #include <linux/usb/ehci_pdriver.h>
  36. #include "ehci.h"
  37. #define DRIVER_DESC "EHCI generic platform driver"
  38. #define EHCI_MAX_CLKS 3
  39. #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
  40. struct ehci_platform_priv {
  41. struct clk *clks[EHCI_MAX_CLKS];
  42. struct reset_control *rst;
  43. struct phy **phys;
  44. int num_phys;
  45. };
  46. static const char hcd_name[] = "ehci-platform";
  47. static int ehci_platform_reset(struct usb_hcd *hcd)
  48. {
  49. struct platform_device *pdev = to_platform_device(hcd->self.controller);
  50. struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
  51. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  52. int retval;
  53. hcd->has_tt = pdata->has_tt;
  54. ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
  55. if (pdata->pre_setup) {
  56. retval = pdata->pre_setup(hcd);
  57. if (retval < 0)
  58. return retval;
  59. }
  60. ehci->caps = hcd->regs + pdata->caps_offset;
  61. retval = ehci_setup(hcd);
  62. if (retval)
  63. return retval;
  64. if (pdata->no_io_watchdog)
  65. ehci->need_io_watchdog = 0;
  66. return 0;
  67. }
  68. static int ehci_platform_power_on(struct platform_device *dev)
  69. {
  70. struct usb_hcd *hcd = platform_get_drvdata(dev);
  71. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  72. int clk, ret, phy_num;
  73. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
  74. ret = clk_prepare_enable(priv->clks[clk]);
  75. if (ret)
  76. goto err_disable_clks;
  77. }
  78. for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  79. if (priv->phys[phy_num]) {
  80. ret = phy_init(priv->phys[phy_num]);
  81. if (ret)
  82. goto err_exit_phy;
  83. ret = phy_power_on(priv->phys[phy_num]);
  84. if (ret) {
  85. phy_exit(priv->phys[phy_num]);
  86. goto err_exit_phy;
  87. }
  88. }
  89. }
  90. return 0;
  91. err_exit_phy:
  92. while (--phy_num >= 0) {
  93. if (priv->phys[phy_num]) {
  94. phy_power_off(priv->phys[phy_num]);
  95. phy_exit(priv->phys[phy_num]);
  96. }
  97. }
  98. err_disable_clks:
  99. while (--clk >= 0)
  100. clk_disable_unprepare(priv->clks[clk]);
  101. return ret;
  102. }
  103. static void ehci_platform_power_off(struct platform_device *dev)
  104. {
  105. struct usb_hcd *hcd = platform_get_drvdata(dev);
  106. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  107. int clk, phy_num;
  108. for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  109. if (priv->phys[phy_num]) {
  110. phy_power_off(priv->phys[phy_num]);
  111. phy_exit(priv->phys[phy_num]);
  112. }
  113. }
  114. for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
  115. if (priv->clks[clk])
  116. clk_disable_unprepare(priv->clks[clk]);
  117. }
  118. static struct hc_driver __read_mostly ehci_platform_hc_driver;
  119. static const struct ehci_driver_overrides platform_overrides __initconst = {
  120. .reset = ehci_platform_reset,
  121. .extra_priv_size = sizeof(struct ehci_platform_priv),
  122. };
  123. static struct usb_ehci_pdata ehci_platform_defaults = {
  124. .power_on = ehci_platform_power_on,
  125. .power_suspend = ehci_platform_power_off,
  126. .power_off = ehci_platform_power_off,
  127. };
  128. static int ehci_platform_probe(struct platform_device *dev)
  129. {
  130. struct usb_hcd *hcd;
  131. struct resource *res_mem;
  132. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  133. struct ehci_platform_priv *priv;
  134. struct ehci_hcd *ehci;
  135. const char *phy_name;
  136. int err, irq, phy_num, clk = 0;
  137. if (usb_disabled())
  138. return -ENODEV;
  139. /*
  140. * Use reasonable defaults so platforms don't have to provide these
  141. * with DT probing on ARM.
  142. */
  143. if (!pdata)
  144. pdata = &ehci_platform_defaults;
  145. err = dma_coerce_mask_and_coherent(&dev->dev,
  146. pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
  147. if (err)
  148. return err;
  149. irq = platform_get_irq(dev, 0);
  150. if (irq < 0) {
  151. dev_err(&dev->dev, "no irq provided");
  152. return irq;
  153. }
  154. hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
  155. dev_name(&dev->dev));
  156. if (!hcd)
  157. return -ENOMEM;
  158. platform_set_drvdata(dev, hcd);
  159. dev->dev.platform_data = pdata;
  160. priv = hcd_to_ehci_priv(hcd);
  161. ehci = hcd_to_ehci(hcd);
  162. if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
  163. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  164. ehci->big_endian_mmio = 1;
  165. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  166. ehci->big_endian_desc = 1;
  167. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  168. ehci->big_endian_mmio = ehci->big_endian_desc = 1;
  169. if (of_property_read_bool(dev->dev.of_node,
  170. "needs-reset-on-resume"))
  171. pdata->reset_on_resume = 1;
  172. priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
  173. "phys", "#phy-cells");
  174. priv->num_phys = priv->num_phys > 0 ? priv->num_phys : 1;
  175. priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
  176. sizeof(struct phy *), GFP_KERNEL);
  177. if (!priv->phys)
  178. return -ENOMEM;
  179. for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  180. err = of_property_read_string_index(
  181. dev->dev.of_node,
  182. "phy-names", phy_num,
  183. &phy_name);
  184. if (err < 0) {
  185. if (priv->num_phys > 1) {
  186. dev_err(&dev->dev, "phy-names not provided");
  187. goto err_put_hcd;
  188. } else
  189. phy_name = "usb";
  190. }
  191. priv->phys[phy_num] = devm_phy_get(&dev->dev,
  192. phy_name);
  193. if (IS_ERR(priv->phys[phy_num])) {
  194. err = PTR_ERR(priv->phys[phy_num]);
  195. if ((priv->num_phys > 1) ||
  196. (err == -EPROBE_DEFER))
  197. goto err_put_hcd;
  198. priv->phys[phy_num] = NULL;
  199. }
  200. }
  201. for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
  202. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  203. if (IS_ERR(priv->clks[clk])) {
  204. err = PTR_ERR(priv->clks[clk]);
  205. if (err == -EPROBE_DEFER)
  206. goto err_put_clks;
  207. priv->clks[clk] = NULL;
  208. break;
  209. }
  210. }
  211. }
  212. priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
  213. if (IS_ERR(priv->rst)) {
  214. err = PTR_ERR(priv->rst);
  215. if (err == -EPROBE_DEFER)
  216. goto err_put_clks;
  217. priv->rst = NULL;
  218. } else {
  219. err = reset_control_deassert(priv->rst);
  220. if (err)
  221. goto err_put_clks;
  222. }
  223. if (pdata->big_endian_desc)
  224. ehci->big_endian_desc = 1;
  225. if (pdata->big_endian_mmio)
  226. ehci->big_endian_mmio = 1;
  227. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
  228. if (ehci->big_endian_mmio) {
  229. dev_err(&dev->dev,
  230. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
  231. err = -EINVAL;
  232. goto err_reset;
  233. }
  234. #endif
  235. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
  236. if (ehci->big_endian_desc) {
  237. dev_err(&dev->dev,
  238. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
  239. err = -EINVAL;
  240. goto err_reset;
  241. }
  242. #endif
  243. if (pdata->power_on) {
  244. err = pdata->power_on(dev);
  245. if (err < 0)
  246. goto err_reset;
  247. }
  248. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  249. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  250. if (IS_ERR(hcd->regs)) {
  251. err = PTR_ERR(hcd->regs);
  252. goto err_power;
  253. }
  254. hcd->rsrc_start = res_mem->start;
  255. hcd->rsrc_len = resource_size(res_mem);
  256. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  257. if (err)
  258. goto err_power;
  259. device_wakeup_enable(hcd->self.controller);
  260. platform_set_drvdata(dev, hcd);
  261. return err;
  262. err_power:
  263. if (pdata->power_off)
  264. pdata->power_off(dev);
  265. err_reset:
  266. if (priv->rst)
  267. reset_control_assert(priv->rst);
  268. err_put_clks:
  269. while (--clk >= 0)
  270. clk_put(priv->clks[clk]);
  271. err_put_hcd:
  272. if (pdata == &ehci_platform_defaults)
  273. dev->dev.platform_data = NULL;
  274. usb_put_hcd(hcd);
  275. return err;
  276. }
  277. static int ehci_platform_remove(struct platform_device *dev)
  278. {
  279. struct usb_hcd *hcd = platform_get_drvdata(dev);
  280. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  281. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  282. int clk;
  283. usb_remove_hcd(hcd);
  284. if (pdata->power_off)
  285. pdata->power_off(dev);
  286. if (priv->rst)
  287. reset_control_assert(priv->rst);
  288. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
  289. clk_put(priv->clks[clk]);
  290. usb_put_hcd(hcd);
  291. if (pdata == &ehci_platform_defaults)
  292. dev->dev.platform_data = NULL;
  293. return 0;
  294. }
  295. #ifdef CONFIG_PM_SLEEP
  296. static int ehci_platform_suspend(struct device *dev)
  297. {
  298. struct usb_hcd *hcd = dev_get_drvdata(dev);
  299. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  300. struct platform_device *pdev =
  301. container_of(dev, struct platform_device, dev);
  302. bool do_wakeup = device_may_wakeup(dev);
  303. int ret;
  304. ret = ehci_suspend(hcd, do_wakeup);
  305. if (ret)
  306. return ret;
  307. if (pdata->power_suspend)
  308. pdata->power_suspend(pdev);
  309. return ret;
  310. }
  311. static int ehci_platform_resume(struct device *dev)
  312. {
  313. struct usb_hcd *hcd = dev_get_drvdata(dev);
  314. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  315. struct platform_device *pdev =
  316. container_of(dev, struct platform_device, dev);
  317. if (pdata->power_on) {
  318. int err = pdata->power_on(pdev);
  319. if (err < 0)
  320. return err;
  321. }
  322. ehci_resume(hcd, pdata->reset_on_resume);
  323. return 0;
  324. }
  325. #endif /* CONFIG_PM_SLEEP */
  326. static const struct of_device_id vt8500_ehci_ids[] = {
  327. { .compatible = "via,vt8500-ehci", },
  328. { .compatible = "wm,prizm-ehci", },
  329. { .compatible = "generic-ehci", },
  330. { .compatible = "cavium,octeon-6335-ehci", },
  331. {}
  332. };
  333. MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
  334. static const struct platform_device_id ehci_platform_table[] = {
  335. { "ehci-platform", 0 },
  336. { }
  337. };
  338. MODULE_DEVICE_TABLE(platform, ehci_platform_table);
  339. static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
  340. ehci_platform_resume);
  341. static struct platform_driver ehci_platform_driver = {
  342. .id_table = ehci_platform_table,
  343. .probe = ehci_platform_probe,
  344. .remove = ehci_platform_remove,
  345. .shutdown = usb_hcd_platform_shutdown,
  346. .driver = {
  347. .name = "ehci-platform",
  348. .pm = &ehci_platform_pm_ops,
  349. .of_match_table = vt8500_ehci_ids,
  350. }
  351. };
  352. static int __init ehci_platform_init(void)
  353. {
  354. if (usb_disabled())
  355. return -ENODEV;
  356. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  357. ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
  358. return platform_driver_register(&ehci_platform_driver);
  359. }
  360. module_init(ehci_platform_init);
  361. static void __exit ehci_platform_cleanup(void)
  362. {
  363. platform_driver_unregister(&ehci_platform_driver);
  364. }
  365. module_exit(ehci_platform_cleanup);
  366. MODULE_DESCRIPTION(DRIVER_DESC);
  367. MODULE_AUTHOR("Hauke Mehrtens");
  368. MODULE_AUTHOR("Alan Stern");
  369. MODULE_LICENSE("GPL");