ehci-platform.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. ret = phy_init(priv->phys[phy_num]);
  80. if (ret)
  81. goto err_exit_phy;
  82. ret = phy_power_on(priv->phys[phy_num]);
  83. if (ret) {
  84. phy_exit(priv->phys[phy_num]);
  85. goto err_exit_phy;
  86. }
  87. }
  88. return 0;
  89. err_exit_phy:
  90. while (--phy_num >= 0) {
  91. phy_power_off(priv->phys[phy_num]);
  92. phy_exit(priv->phys[phy_num]);
  93. }
  94. err_disable_clks:
  95. while (--clk >= 0)
  96. clk_disable_unprepare(priv->clks[clk]);
  97. return ret;
  98. }
  99. static void ehci_platform_power_off(struct platform_device *dev)
  100. {
  101. struct usb_hcd *hcd = platform_get_drvdata(dev);
  102. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  103. int clk, phy_num;
  104. for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  105. phy_power_off(priv->phys[phy_num]);
  106. phy_exit(priv->phys[phy_num]);
  107. }
  108. for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
  109. if (priv->clks[clk])
  110. clk_disable_unprepare(priv->clks[clk]);
  111. }
  112. static struct hc_driver __read_mostly ehci_platform_hc_driver;
  113. static const struct ehci_driver_overrides platform_overrides __initconst = {
  114. .reset = ehci_platform_reset,
  115. .extra_priv_size = sizeof(struct ehci_platform_priv),
  116. };
  117. static struct usb_ehci_pdata ehci_platform_defaults = {
  118. .power_on = ehci_platform_power_on,
  119. .power_suspend = ehci_platform_power_off,
  120. .power_off = ehci_platform_power_off,
  121. };
  122. static int ehci_platform_probe(struct platform_device *dev)
  123. {
  124. struct usb_hcd *hcd;
  125. struct resource *res_mem;
  126. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  127. struct ehci_platform_priv *priv;
  128. struct ehci_hcd *ehci;
  129. int err, irq, phy_num, clk = 0;
  130. if (usb_disabled())
  131. return -ENODEV;
  132. /*
  133. * Use reasonable defaults so platforms don't have to provide these
  134. * with DT probing on ARM.
  135. */
  136. if (!pdata)
  137. pdata = &ehci_platform_defaults;
  138. err = dma_coerce_mask_and_coherent(&dev->dev,
  139. pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
  140. if (err)
  141. return err;
  142. irq = platform_get_irq(dev, 0);
  143. if (irq < 0) {
  144. dev_err(&dev->dev, "no irq provided");
  145. return irq;
  146. }
  147. hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
  148. dev_name(&dev->dev));
  149. if (!hcd)
  150. return -ENOMEM;
  151. platform_set_drvdata(dev, hcd);
  152. dev->dev.platform_data = pdata;
  153. priv = hcd_to_ehci_priv(hcd);
  154. ehci = hcd_to_ehci(hcd);
  155. if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
  156. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  157. ehci->big_endian_mmio = 1;
  158. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  159. ehci->big_endian_desc = 1;
  160. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  161. ehci->big_endian_mmio = ehci->big_endian_desc = 1;
  162. if (of_property_read_bool(dev->dev.of_node,
  163. "needs-reset-on-resume"))
  164. pdata->reset_on_resume = 1;
  165. if (of_property_read_bool(dev->dev.of_node,
  166. "has-transaction-translator"))
  167. pdata->has_tt = 1;
  168. priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
  169. "phys", "#phy-cells");
  170. if (priv->num_phys > 0) {
  171. priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
  172. sizeof(struct phy *), GFP_KERNEL);
  173. if (!priv->phys)
  174. return -ENOMEM;
  175. } else
  176. priv->num_phys = 0;
  177. for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  178. priv->phys[phy_num] = devm_of_phy_get_by_index(
  179. &dev->dev, dev->dev.of_node, phy_num);
  180. if (IS_ERR(priv->phys[phy_num])) {
  181. err = PTR_ERR(priv->phys[phy_num]);
  182. goto err_put_hcd;
  183. }
  184. }
  185. for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
  186. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  187. if (IS_ERR(priv->clks[clk])) {
  188. err = PTR_ERR(priv->clks[clk]);
  189. if (err == -EPROBE_DEFER)
  190. goto err_put_clks;
  191. priv->clks[clk] = NULL;
  192. break;
  193. }
  194. }
  195. }
  196. priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
  197. if (IS_ERR(priv->rst)) {
  198. err = PTR_ERR(priv->rst);
  199. if (err == -EPROBE_DEFER)
  200. goto err_put_clks;
  201. priv->rst = NULL;
  202. } else {
  203. err = reset_control_deassert(priv->rst);
  204. if (err)
  205. goto err_put_clks;
  206. }
  207. if (pdata->big_endian_desc)
  208. ehci->big_endian_desc = 1;
  209. if (pdata->big_endian_mmio)
  210. ehci->big_endian_mmio = 1;
  211. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
  212. if (ehci->big_endian_mmio) {
  213. dev_err(&dev->dev,
  214. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
  215. err = -EINVAL;
  216. goto err_reset;
  217. }
  218. #endif
  219. #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
  220. if (ehci->big_endian_desc) {
  221. dev_err(&dev->dev,
  222. "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
  223. err = -EINVAL;
  224. goto err_reset;
  225. }
  226. #endif
  227. if (pdata->power_on) {
  228. err = pdata->power_on(dev);
  229. if (err < 0)
  230. goto err_reset;
  231. }
  232. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  233. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  234. if (IS_ERR(hcd->regs)) {
  235. err = PTR_ERR(hcd->regs);
  236. goto err_power;
  237. }
  238. hcd->rsrc_start = res_mem->start;
  239. hcd->rsrc_len = resource_size(res_mem);
  240. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  241. if (err)
  242. goto err_power;
  243. device_wakeup_enable(hcd->self.controller);
  244. platform_set_drvdata(dev, hcd);
  245. return err;
  246. err_power:
  247. if (pdata->power_off)
  248. pdata->power_off(dev);
  249. err_reset:
  250. if (priv->rst)
  251. reset_control_assert(priv->rst);
  252. err_put_clks:
  253. while (--clk >= 0)
  254. clk_put(priv->clks[clk]);
  255. err_put_hcd:
  256. if (pdata == &ehci_platform_defaults)
  257. dev->dev.platform_data = NULL;
  258. usb_put_hcd(hcd);
  259. return err;
  260. }
  261. static int ehci_platform_remove(struct platform_device *dev)
  262. {
  263. struct usb_hcd *hcd = platform_get_drvdata(dev);
  264. struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
  265. struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
  266. int clk;
  267. usb_remove_hcd(hcd);
  268. if (pdata->power_off)
  269. pdata->power_off(dev);
  270. if (priv->rst)
  271. reset_control_assert(priv->rst);
  272. for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
  273. clk_put(priv->clks[clk]);
  274. usb_put_hcd(hcd);
  275. if (pdata == &ehci_platform_defaults)
  276. dev->dev.platform_data = NULL;
  277. return 0;
  278. }
  279. #ifdef CONFIG_PM_SLEEP
  280. static int ehci_platform_suspend(struct device *dev)
  281. {
  282. struct usb_hcd *hcd = dev_get_drvdata(dev);
  283. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  284. struct platform_device *pdev =
  285. container_of(dev, struct platform_device, dev);
  286. bool do_wakeup = device_may_wakeup(dev);
  287. int ret;
  288. ret = ehci_suspend(hcd, do_wakeup);
  289. if (ret)
  290. return ret;
  291. if (pdata->power_suspend)
  292. pdata->power_suspend(pdev);
  293. return ret;
  294. }
  295. static int ehci_platform_resume(struct device *dev)
  296. {
  297. struct usb_hcd *hcd = dev_get_drvdata(dev);
  298. struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
  299. struct platform_device *pdev =
  300. container_of(dev, struct platform_device, dev);
  301. if (pdata->power_on) {
  302. int err = pdata->power_on(pdev);
  303. if (err < 0)
  304. return err;
  305. }
  306. ehci_resume(hcd, pdata->reset_on_resume);
  307. return 0;
  308. }
  309. #endif /* CONFIG_PM_SLEEP */
  310. static const struct of_device_id vt8500_ehci_ids[] = {
  311. { .compatible = "via,vt8500-ehci", },
  312. { .compatible = "wm,prizm-ehci", },
  313. { .compatible = "generic-ehci", },
  314. { .compatible = "cavium,octeon-6335-ehci", },
  315. {}
  316. };
  317. MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
  318. static const struct platform_device_id ehci_platform_table[] = {
  319. { "ehci-platform", 0 },
  320. { }
  321. };
  322. MODULE_DEVICE_TABLE(platform, ehci_platform_table);
  323. static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
  324. ehci_platform_resume);
  325. static struct platform_driver ehci_platform_driver = {
  326. .id_table = ehci_platform_table,
  327. .probe = ehci_platform_probe,
  328. .remove = ehci_platform_remove,
  329. .shutdown = usb_hcd_platform_shutdown,
  330. .driver = {
  331. .name = "ehci-platform",
  332. .pm = &ehci_platform_pm_ops,
  333. .of_match_table = vt8500_ehci_ids,
  334. }
  335. };
  336. static int __init ehci_platform_init(void)
  337. {
  338. if (usb_disabled())
  339. return -ENODEV;
  340. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  341. ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
  342. return platform_driver_register(&ehci_platform_driver);
  343. }
  344. module_init(ehci_platform_init);
  345. static void __exit ehci_platform_cleanup(void)
  346. {
  347. platform_driver_unregister(&ehci_platform_driver);
  348. }
  349. module_exit(ehci_platform_cleanup);
  350. MODULE_DESCRIPTION(DRIVER_DESC);
  351. MODULE_AUTHOR("Hauke Mehrtens");
  352. MODULE_AUTHOR("Alan Stern");
  353. MODULE_LICENSE("GPL");