ohci-platform.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic platform ohci driver
  4. *
  5. * Copyright 2007 Michael Buesch <[email protected]>
  6. * Copyright 2011-2012 Hauke Mehrtens <[email protected]>
  7. * Copyright 2014 Hans de Goede <[email protected]>
  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 3
  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. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  170. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  171. if (IS_ERR(hcd->regs)) {
  172. err = PTR_ERR(hcd->regs);
  173. goto err_power;
  174. }
  175. hcd->rsrc_start = res_mem->start;
  176. hcd->rsrc_len = resource_size(res_mem);
  177. hcd->tpl_support = of_usb_host_tpl_support(dev->dev.of_node);
  178. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  179. if (err)
  180. goto err_power;
  181. device_wakeup_enable(hcd->self.controller);
  182. platform_set_drvdata(dev, hcd);
  183. return err;
  184. err_power:
  185. if (pdata->power_off)
  186. pdata->power_off(dev);
  187. err_reset:
  188. pm_runtime_disable(&dev->dev);
  189. reset_control_assert(priv->resets);
  190. err_put_clks:
  191. while (--clk >= 0)
  192. clk_put(priv->clks[clk]);
  193. if (pdata == &ohci_platform_defaults)
  194. dev->dev.platform_data = NULL;
  195. usb_put_hcd(hcd);
  196. return err;
  197. }
  198. static int ohci_platform_remove(struct platform_device *dev)
  199. {
  200. struct usb_hcd *hcd = platform_get_drvdata(dev);
  201. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  202. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  203. int clk;
  204. pm_runtime_get_sync(&dev->dev);
  205. usb_remove_hcd(hcd);
  206. if (pdata->power_off)
  207. pdata->power_off(dev);
  208. reset_control_assert(priv->resets);
  209. for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
  210. clk_put(priv->clks[clk]);
  211. usb_put_hcd(hcd);
  212. pm_runtime_put_sync(&dev->dev);
  213. pm_runtime_disable(&dev->dev);
  214. if (pdata == &ohci_platform_defaults)
  215. dev->dev.platform_data = NULL;
  216. return 0;
  217. }
  218. #ifdef CONFIG_PM_SLEEP
  219. static int ohci_platform_suspend(struct device *dev)
  220. {
  221. struct usb_hcd *hcd = dev_get_drvdata(dev);
  222. struct usb_ohci_pdata *pdata = dev->platform_data;
  223. struct platform_device *pdev = to_platform_device(dev);
  224. bool do_wakeup = device_may_wakeup(dev);
  225. int ret;
  226. ret = ohci_suspend(hcd, do_wakeup);
  227. if (ret)
  228. return ret;
  229. if (pdata->power_suspend)
  230. pdata->power_suspend(pdev);
  231. return ret;
  232. }
  233. static int ohci_platform_resume_common(struct device *dev, bool hibernated)
  234. {
  235. struct usb_hcd *hcd = dev_get_drvdata(dev);
  236. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  237. struct platform_device *pdev = to_platform_device(dev);
  238. if (pdata->power_on) {
  239. int err = pdata->power_on(pdev);
  240. if (err < 0)
  241. return err;
  242. }
  243. ohci_resume(hcd, hibernated);
  244. pm_runtime_disable(dev);
  245. pm_runtime_set_active(dev);
  246. pm_runtime_enable(dev);
  247. return 0;
  248. }
  249. static int ohci_platform_resume(struct device *dev)
  250. {
  251. return ohci_platform_resume_common(dev, false);
  252. }
  253. static int ohci_platform_restore(struct device *dev)
  254. {
  255. return ohci_platform_resume_common(dev, true);
  256. }
  257. #endif /* CONFIG_PM_SLEEP */
  258. static const struct of_device_id ohci_platform_ids[] = {
  259. { .compatible = "generic-ohci", },
  260. { .compatible = "cavium,octeon-6335-ohci", },
  261. { .compatible = "ti,ohci-omap3", },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(of, ohci_platform_ids);
  265. static const struct platform_device_id ohci_platform_table[] = {
  266. { "ohci-platform", 0 },
  267. { }
  268. };
  269. MODULE_DEVICE_TABLE(platform, ohci_platform_table);
  270. #ifdef CONFIG_PM_SLEEP
  271. static const struct dev_pm_ops ohci_platform_pm_ops = {
  272. .suspend = ohci_platform_suspend,
  273. .resume = ohci_platform_resume,
  274. .freeze = ohci_platform_suspend,
  275. .thaw = ohci_platform_resume,
  276. .poweroff = ohci_platform_suspend,
  277. .restore = ohci_platform_restore,
  278. };
  279. #endif
  280. static struct platform_driver ohci_platform_driver = {
  281. .id_table = ohci_platform_table,
  282. .probe = ohci_platform_probe,
  283. .remove = ohci_platform_remove,
  284. .shutdown = usb_hcd_platform_shutdown,
  285. .driver = {
  286. .name = "ohci-platform",
  287. #ifdef CONFIG_PM_SLEEP
  288. .pm = &ohci_platform_pm_ops,
  289. #endif
  290. .of_match_table = ohci_platform_ids,
  291. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  292. }
  293. };
  294. static int __init ohci_platform_init(void)
  295. {
  296. if (usb_disabled())
  297. return -ENODEV;
  298. ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
  299. return platform_driver_register(&ohci_platform_driver);
  300. }
  301. module_init(ohci_platform_init);
  302. static void __exit ohci_platform_cleanup(void)
  303. {
  304. platform_driver_unregister(&ohci_platform_driver);
  305. }
  306. module_exit(ohci_platform_cleanup);
  307. MODULE_DESCRIPTION(DRIVER_DESC);
  308. MODULE_AUTHOR("Hauke Mehrtens");
  309. MODULE_AUTHOR("Alan Stern");
  310. MODULE_LICENSE("GPL");