ohci-platform.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "ohci.h"
  31. #define DRIVER_DESC "OHCI generic platform driver"
  32. #define OHCI_MAX_CLKS 3
  33. #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
  34. struct ohci_platform_priv {
  35. struct clk *clks[OHCI_MAX_CLKS];
  36. struct reset_control *resets;
  37. };
  38. static const char hcd_name[] = "ohci-platform";
  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. dev_err(&dev->dev, "no irq provided");
  96. return irq;
  97. }
  98. hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
  99. dev_name(&dev->dev));
  100. if (!hcd)
  101. return -ENOMEM;
  102. platform_set_drvdata(dev, hcd);
  103. dev->dev.platform_data = pdata;
  104. priv = hcd_to_ohci_priv(hcd);
  105. ohci = hcd_to_ohci(hcd);
  106. if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
  107. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  108. ohci->flags |= OHCI_QUIRK_BE_MMIO;
  109. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  110. ohci->flags |= OHCI_QUIRK_BE_DESC;
  111. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  112. ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
  113. if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
  114. ohci->flags |= OHCI_QUIRK_FRAME_NO;
  115. if (of_property_read_bool(dev->dev.of_node,
  116. "remote-wakeup-connected"))
  117. ohci->hc_control = OHCI_CTRL_RWC;
  118. of_property_read_u32(dev->dev.of_node, "num-ports",
  119. &ohci->num_ports);
  120. for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
  121. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  122. if (IS_ERR(priv->clks[clk])) {
  123. err = PTR_ERR(priv->clks[clk]);
  124. if (err == -EPROBE_DEFER)
  125. goto err_put_clks;
  126. priv->clks[clk] = NULL;
  127. break;
  128. }
  129. }
  130. priv->resets = devm_reset_control_array_get_optional_shared(
  131. &dev->dev);
  132. if (IS_ERR(priv->resets)) {
  133. err = PTR_ERR(priv->resets);
  134. goto err_put_clks;
  135. }
  136. err = reset_control_deassert(priv->resets);
  137. if (err)
  138. goto err_put_clks;
  139. }
  140. if (pdata->big_endian_desc)
  141. ohci->flags |= OHCI_QUIRK_BE_DESC;
  142. if (pdata->big_endian_mmio)
  143. ohci->flags |= OHCI_QUIRK_BE_MMIO;
  144. if (pdata->no_big_frame_no)
  145. ohci->flags |= OHCI_QUIRK_FRAME_NO;
  146. if (pdata->num_ports)
  147. ohci->num_ports = pdata->num_ports;
  148. #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
  149. if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
  150. dev_err(&dev->dev,
  151. "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
  152. err = -EINVAL;
  153. goto err_reset;
  154. }
  155. #endif
  156. #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
  157. if (ohci->flags & OHCI_QUIRK_BE_DESC) {
  158. dev_err(&dev->dev,
  159. "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
  160. err = -EINVAL;
  161. goto err_reset;
  162. }
  163. #endif
  164. pm_runtime_set_active(&dev->dev);
  165. pm_runtime_enable(&dev->dev);
  166. if (pdata->power_on) {
  167. err = pdata->power_on(dev);
  168. if (err < 0)
  169. goto err_reset;
  170. }
  171. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  172. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  173. if (IS_ERR(hcd->regs)) {
  174. err = PTR_ERR(hcd->regs);
  175. goto err_power;
  176. }
  177. hcd->rsrc_start = res_mem->start;
  178. hcd->rsrc_len = resource_size(res_mem);
  179. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  180. if (err)
  181. goto err_power;
  182. device_wakeup_enable(hcd->self.controller);
  183. platform_set_drvdata(dev, hcd);
  184. return err;
  185. err_power:
  186. if (pdata->power_off)
  187. pdata->power_off(dev);
  188. err_reset:
  189. pm_runtime_disable(&dev->dev);
  190. reset_control_assert(priv->resets);
  191. err_put_clks:
  192. while (--clk >= 0)
  193. clk_put(priv->clks[clk]);
  194. if (pdata == &ohci_platform_defaults)
  195. dev->dev.platform_data = NULL;
  196. usb_put_hcd(hcd);
  197. return err;
  198. }
  199. static int ohci_platform_remove(struct platform_device *dev)
  200. {
  201. struct usb_hcd *hcd = platform_get_drvdata(dev);
  202. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  203. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  204. int clk;
  205. pm_runtime_get_sync(&dev->dev);
  206. usb_remove_hcd(hcd);
  207. if (pdata->power_off)
  208. pdata->power_off(dev);
  209. reset_control_assert(priv->resets);
  210. for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
  211. clk_put(priv->clks[clk]);
  212. usb_put_hcd(hcd);
  213. pm_runtime_put_sync(&dev->dev);
  214. pm_runtime_disable(&dev->dev);
  215. if (pdata == &ohci_platform_defaults)
  216. dev->dev.platform_data = NULL;
  217. return 0;
  218. }
  219. #ifdef CONFIG_PM_SLEEP
  220. static int ohci_platform_suspend(struct device *dev)
  221. {
  222. struct usb_hcd *hcd = dev_get_drvdata(dev);
  223. struct usb_ohci_pdata *pdata = dev->platform_data;
  224. struct platform_device *pdev = to_platform_device(dev);
  225. bool do_wakeup = device_may_wakeup(dev);
  226. int ret;
  227. ret = ohci_suspend(hcd, do_wakeup);
  228. if (ret)
  229. return ret;
  230. if (pdata->power_suspend)
  231. pdata->power_suspend(pdev);
  232. return ret;
  233. }
  234. static int ohci_platform_resume(struct device *dev)
  235. {
  236. struct usb_hcd *hcd = dev_get_drvdata(dev);
  237. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  238. struct platform_device *pdev = to_platform_device(dev);
  239. if (pdata->power_on) {
  240. int err = pdata->power_on(pdev);
  241. if (err < 0)
  242. return err;
  243. }
  244. ohci_resume(hcd, false);
  245. return 0;
  246. }
  247. #endif /* CONFIG_PM_SLEEP */
  248. static const struct of_device_id ohci_platform_ids[] = {
  249. { .compatible = "generic-ohci", },
  250. { .compatible = "cavium,octeon-6335-ohci", },
  251. { .compatible = "ti,ohci-omap3", },
  252. { }
  253. };
  254. MODULE_DEVICE_TABLE(of, ohci_platform_ids);
  255. static const struct platform_device_id ohci_platform_table[] = {
  256. { "ohci-platform", 0 },
  257. { }
  258. };
  259. MODULE_DEVICE_TABLE(platform, ohci_platform_table);
  260. static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
  261. ohci_platform_resume);
  262. static struct platform_driver ohci_platform_driver = {
  263. .id_table = ohci_platform_table,
  264. .probe = ohci_platform_probe,
  265. .remove = ohci_platform_remove,
  266. .shutdown = usb_hcd_platform_shutdown,
  267. .driver = {
  268. .name = "ohci-platform",
  269. .pm = &ohci_platform_pm_ops,
  270. .of_match_table = ohci_platform_ids,
  271. }
  272. };
  273. static int __init ohci_platform_init(void)
  274. {
  275. if (usb_disabled())
  276. return -ENODEV;
  277. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  278. ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
  279. return platform_driver_register(&ohci_platform_driver);
  280. }
  281. module_init(ohci_platform_init);
  282. static void __exit ohci_platform_cleanup(void)
  283. {
  284. platform_driver_unregister(&ohci_platform_driver);
  285. }
  286. module_exit(ohci_platform_cleanup);
  287. MODULE_DESCRIPTION(DRIVER_DESC);
  288. MODULE_AUTHOR("Hauke Mehrtens");
  289. MODULE_AUTHOR("Alan Stern");
  290. MODULE_LICENSE("GPL");