ohci-platform.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic platform ohci driver
  4. *
  5. * Copyright 2007 Michael Buesch <m@bues.ch>
  6. * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
  7. * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  8. *
  9. * Derived from the OCHI-SSB driver
  10. * Derived from the OHCI-PCI driver
  11. * Copyright 1999 Roman Weissgaerber
  12. * Copyright 2000-2002 David Brownell
  13. * Copyright 1999 Linus Torvalds
  14. * Copyright 1999 Gregory P. Smith
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/hrtimer.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/err.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/reset.h>
  27. #include <linux/usb/ohci_pdriver.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/hcd.h>
  30. #include <linux/usb/of.h>
  31. #include "ohci.h"
  32. #define DRIVER_DESC "OHCI generic platform driver"
  33. #define OHCI_MAX_CLKS 4
  34. #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
  35. struct ohci_platform_priv {
  36. struct clk *clks[OHCI_MAX_CLKS];
  37. struct reset_control *resets;
  38. };
  39. static int ohci_platform_power_on(struct platform_device *dev)
  40. {
  41. struct usb_hcd *hcd = platform_get_drvdata(dev);
  42. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  43. int clk, ret;
  44. for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
  45. ret = clk_prepare_enable(priv->clks[clk]);
  46. if (ret)
  47. goto err_disable_clks;
  48. }
  49. return 0;
  50. err_disable_clks:
  51. while (--clk >= 0)
  52. clk_disable_unprepare(priv->clks[clk]);
  53. return ret;
  54. }
  55. static void ohci_platform_power_off(struct platform_device *dev)
  56. {
  57. struct usb_hcd *hcd = platform_get_drvdata(dev);
  58. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  59. int clk;
  60. for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
  61. if (priv->clks[clk])
  62. clk_disable_unprepare(priv->clks[clk]);
  63. }
  64. static struct hc_driver __read_mostly ohci_platform_hc_driver;
  65. static const struct ohci_driver_overrides platform_overrides __initconst = {
  66. .product_desc = "Generic Platform OHCI controller",
  67. .extra_priv_size = sizeof(struct ohci_platform_priv),
  68. };
  69. static struct usb_ohci_pdata ohci_platform_defaults = {
  70. .power_on = ohci_platform_power_on,
  71. .power_suspend = ohci_platform_power_off,
  72. .power_off = ohci_platform_power_off,
  73. };
  74. static int ohci_platform_probe(struct platform_device *dev)
  75. {
  76. struct usb_hcd *hcd;
  77. struct resource *res_mem;
  78. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  79. struct ohci_platform_priv *priv;
  80. struct ohci_hcd *ohci;
  81. int err, irq, clk = 0;
  82. if (usb_disabled())
  83. return -ENODEV;
  84. /*
  85. * Use reasonable defaults so platforms don't have to provide these
  86. * with DT probing on ARM.
  87. */
  88. if (!pdata)
  89. pdata = &ohci_platform_defaults;
  90. err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
  91. if (err)
  92. return err;
  93. irq = platform_get_irq(dev, 0);
  94. if (irq < 0)
  95. return irq;
  96. hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
  97. dev_name(&dev->dev));
  98. if (!hcd)
  99. return -ENOMEM;
  100. platform_set_drvdata(dev, hcd);
  101. dev->dev.platform_data = pdata;
  102. priv = hcd_to_ohci_priv(hcd);
  103. ohci = hcd_to_ohci(hcd);
  104. if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
  105. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  106. ohci->flags |= OHCI_QUIRK_BE_MMIO;
  107. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  108. ohci->flags |= OHCI_QUIRK_BE_DESC;
  109. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  110. ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
  111. if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
  112. ohci->flags |= OHCI_QUIRK_FRAME_NO;
  113. if (of_property_read_bool(dev->dev.of_node,
  114. "remote-wakeup-connected"))
  115. ohci->hc_control = OHCI_CTRL_RWC;
  116. of_property_read_u32(dev->dev.of_node, "num-ports",
  117. &ohci->num_ports);
  118. for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
  119. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  120. if (IS_ERR(priv->clks[clk])) {
  121. err = PTR_ERR(priv->clks[clk]);
  122. if (err == -EPROBE_DEFER)
  123. goto err_put_clks;
  124. priv->clks[clk] = NULL;
  125. break;
  126. }
  127. }
  128. priv->resets = devm_reset_control_array_get_optional_shared(
  129. &dev->dev);
  130. if (IS_ERR(priv->resets)) {
  131. err = PTR_ERR(priv->resets);
  132. goto err_put_clks;
  133. }
  134. err = reset_control_deassert(priv->resets);
  135. if (err)
  136. goto err_put_clks;
  137. }
  138. if (pdata->big_endian_desc)
  139. ohci->flags |= OHCI_QUIRK_BE_DESC;
  140. if (pdata->big_endian_mmio)
  141. ohci->flags |= OHCI_QUIRK_BE_MMIO;
  142. if (pdata->no_big_frame_no)
  143. ohci->flags |= OHCI_QUIRK_FRAME_NO;
  144. if (pdata->num_ports)
  145. ohci->num_ports = pdata->num_ports;
  146. #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
  147. if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
  148. dev_err(&dev->dev,
  149. "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
  150. err = -EINVAL;
  151. goto err_reset;
  152. }
  153. #endif
  154. #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
  155. if (ohci->flags & OHCI_QUIRK_BE_DESC) {
  156. dev_err(&dev->dev,
  157. "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
  158. err = -EINVAL;
  159. goto err_reset;
  160. }
  161. #endif
  162. pm_runtime_set_active(&dev->dev);
  163. pm_runtime_enable(&dev->dev);
  164. if (pdata->power_on) {
  165. err = pdata->power_on(dev);
  166. if (err < 0)
  167. goto err_reset;
  168. }
  169. hcd->regs = devm_platform_get_and_ioremap_resource(dev, 0, &res_mem);
  170. if (IS_ERR(hcd->regs)) {
  171. err = PTR_ERR(hcd->regs);
  172. goto err_power;
  173. }
  174. hcd->rsrc_start = res_mem->start;
  175. hcd->rsrc_len = resource_size(res_mem);
  176. hcd->tpl_support = of_usb_host_tpl_support(dev->dev.of_node);
  177. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  178. if (err)
  179. goto err_power;
  180. device_wakeup_enable(hcd->self.controller);
  181. platform_set_drvdata(dev, hcd);
  182. return err;
  183. err_power:
  184. if (pdata->power_off)
  185. pdata->power_off(dev);
  186. err_reset:
  187. pm_runtime_disable(&dev->dev);
  188. reset_control_assert(priv->resets);
  189. err_put_clks:
  190. while (--clk >= 0)
  191. clk_put(priv->clks[clk]);
  192. if (pdata == &ohci_platform_defaults)
  193. dev->dev.platform_data = NULL;
  194. usb_put_hcd(hcd);
  195. return err;
  196. }
  197. static void ohci_platform_remove(struct platform_device *dev)
  198. {
  199. struct usb_hcd *hcd = platform_get_drvdata(dev);
  200. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  201. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  202. int clk;
  203. pm_runtime_get_sync(&dev->dev);
  204. usb_remove_hcd(hcd);
  205. if (pdata->power_off)
  206. pdata->power_off(dev);
  207. reset_control_assert(priv->resets);
  208. for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
  209. clk_put(priv->clks[clk]);
  210. usb_put_hcd(hcd);
  211. pm_runtime_put_sync(&dev->dev);
  212. pm_runtime_disable(&dev->dev);
  213. if (pdata == &ohci_platform_defaults)
  214. dev->dev.platform_data = NULL;
  215. }
  216. #ifdef CONFIG_PM_SLEEP
  217. static int ohci_platform_suspend(struct device *dev)
  218. {
  219. struct usb_hcd *hcd = dev_get_drvdata(dev);
  220. struct usb_ohci_pdata *pdata = dev->platform_data;
  221. struct platform_device *pdev = to_platform_device(dev);
  222. bool do_wakeup = device_may_wakeup(dev);
  223. int ret;
  224. ret = ohci_suspend(hcd, do_wakeup);
  225. if (ret)
  226. return ret;
  227. if (pdata->power_suspend)
  228. pdata->power_suspend(pdev);
  229. return ret;
  230. }
  231. static int ohci_platform_resume_common(struct device *dev, bool hibernated)
  232. {
  233. struct usb_hcd *hcd = dev_get_drvdata(dev);
  234. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  235. struct platform_device *pdev = to_platform_device(dev);
  236. if (pdata->power_on) {
  237. int err = pdata->power_on(pdev);
  238. if (err < 0)
  239. return err;
  240. }
  241. ohci_resume(hcd, hibernated);
  242. pm_runtime_disable(dev);
  243. pm_runtime_set_active(dev);
  244. pm_runtime_enable(dev);
  245. return 0;
  246. }
  247. static int ohci_platform_resume(struct device *dev)
  248. {
  249. return ohci_platform_resume_common(dev, false);
  250. }
  251. static int ohci_platform_restore(struct device *dev)
  252. {
  253. return ohci_platform_resume_common(dev, true);
  254. }
  255. #endif /* CONFIG_PM_SLEEP */
  256. static const struct of_device_id ohci_platform_ids[] = {
  257. { .compatible = "generic-ohci", },
  258. { .compatible = "cavium,octeon-6335-ohci", },
  259. { .compatible = "ti,ohci-omap3", },
  260. { }
  261. };
  262. MODULE_DEVICE_TABLE(of, ohci_platform_ids);
  263. static const struct platform_device_id ohci_platform_table[] = {
  264. { "ohci-platform", 0 },
  265. { }
  266. };
  267. MODULE_DEVICE_TABLE(platform, ohci_platform_table);
  268. #ifdef CONFIG_PM_SLEEP
  269. static const struct dev_pm_ops ohci_platform_pm_ops = {
  270. .suspend = ohci_platform_suspend,
  271. .resume = ohci_platform_resume,
  272. .freeze = ohci_platform_suspend,
  273. .thaw = ohci_platform_resume,
  274. .poweroff = ohci_platform_suspend,
  275. .restore = ohci_platform_restore,
  276. };
  277. #endif
  278. static struct platform_driver ohci_platform_driver = {
  279. .id_table = ohci_platform_table,
  280. .probe = ohci_platform_probe,
  281. .remove_new = ohci_platform_remove,
  282. .shutdown = usb_hcd_platform_shutdown,
  283. .driver = {
  284. .name = "ohci-platform",
  285. #ifdef CONFIG_PM_SLEEP
  286. .pm = &ohci_platform_pm_ops,
  287. #endif
  288. .of_match_table = ohci_platform_ids,
  289. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  290. }
  291. };
  292. static int __init ohci_platform_init(void)
  293. {
  294. if (usb_disabled())
  295. return -ENODEV;
  296. ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
  297. return platform_driver_register(&ohci_platform_driver);
  298. }
  299. module_init(ohci_platform_init);
  300. static void __exit ohci_platform_cleanup(void)
  301. {
  302. platform_driver_unregister(&ohci_platform_driver);
  303. }
  304. module_exit(ohci_platform_cleanup);
  305. MODULE_DESCRIPTION(DRIVER_DESC);
  306. MODULE_AUTHOR("Hauke Mehrtens");
  307. MODULE_AUTHOR("Alan Stern");
  308. MODULE_LICENSE("GPL");