ohci-platform.c 8.7 KB

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