ohci-platform.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. 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. 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 int 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. return 0;
  216. }
  217. #ifdef CONFIG_PM_SLEEP
  218. static int ohci_platform_suspend(struct device *dev)
  219. {
  220. struct usb_hcd *hcd = dev_get_drvdata(dev);
  221. struct usb_ohci_pdata *pdata = dev->platform_data;
  222. struct platform_device *pdev = to_platform_device(dev);
  223. bool do_wakeup = device_may_wakeup(dev);
  224. int ret;
  225. ret = ohci_suspend(hcd, do_wakeup);
  226. if (ret)
  227. return ret;
  228. if (pdata->power_suspend)
  229. pdata->power_suspend(pdev);
  230. return ret;
  231. }
  232. static int ohci_platform_resume(struct device *dev)
  233. {
  234. struct usb_hcd *hcd = dev_get_drvdata(dev);
  235. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  236. struct platform_device *pdev = to_platform_device(dev);
  237. if (pdata->power_on) {
  238. int err = pdata->power_on(pdev);
  239. if (err < 0)
  240. return err;
  241. }
  242. ohci_resume(hcd, false);
  243. pm_runtime_disable(dev);
  244. pm_runtime_set_active(dev);
  245. pm_runtime_enable(dev);
  246. return 0;
  247. }
  248. #endif /* CONFIG_PM_SLEEP */
  249. static const struct of_device_id ohci_platform_ids[] = {
  250. { .compatible = "generic-ohci", },
  251. { .compatible = "cavium,octeon-6335-ohci", },
  252. { .compatible = "ti,ohci-omap3", },
  253. { }
  254. };
  255. MODULE_DEVICE_TABLE(of, ohci_platform_ids);
  256. static const struct platform_device_id ohci_platform_table[] = {
  257. { "ohci-platform", 0 },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(platform, ohci_platform_table);
  261. static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
  262. ohci_platform_resume);
  263. static struct platform_driver ohci_platform_driver = {
  264. .id_table = ohci_platform_table,
  265. .probe = ohci_platform_probe,
  266. .remove = ohci_platform_remove,
  267. .shutdown = usb_hcd_platform_shutdown,
  268. .driver = {
  269. .name = "ohci-platform",
  270. .pm = &ohci_platform_pm_ops,
  271. .of_match_table = ohci_platform_ids,
  272. }
  273. };
  274. static int __init ohci_platform_init(void)
  275. {
  276. if (usb_disabled())
  277. return -ENODEV;
  278. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  279. ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
  280. return platform_driver_register(&ohci_platform_driver);
  281. }
  282. module_init(ohci_platform_init);
  283. static void __exit ohci_platform_cleanup(void)
  284. {
  285. platform_driver_unregister(&ohci_platform_driver);
  286. }
  287. module_exit(ohci_platform_cleanup);
  288. MODULE_DESCRIPTION(DRIVER_DESC);
  289. MODULE_AUTHOR("Hauke Mehrtens");
  290. MODULE_AUTHOR("Alan Stern");
  291. MODULE_LICENSE("GPL");