ehci-platform.c 11 KB

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