ohci-platform.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/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 *rst;
  37. struct phy *phy;
  38. };
  39. static const char hcd_name[] = "ohci-platform";
  40. static int ohci_platform_power_on(struct platform_device *dev)
  41. {
  42. struct usb_hcd *hcd = platform_get_drvdata(dev);
  43. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  44. int clk, ret;
  45. for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
  46. ret = clk_prepare_enable(priv->clks[clk]);
  47. if (ret)
  48. goto err_disable_clks;
  49. }
  50. if (priv->phy) {
  51. ret = phy_init(priv->phy);
  52. if (ret)
  53. goto err_disable_clks;
  54. ret = phy_power_on(priv->phy);
  55. if (ret)
  56. goto err_exit_phy;
  57. }
  58. return 0;
  59. err_exit_phy:
  60. phy_exit(priv->phy);
  61. err_disable_clks:
  62. while (--clk >= 0)
  63. clk_disable_unprepare(priv->clks[clk]);
  64. return ret;
  65. }
  66. static void ohci_platform_power_off(struct platform_device *dev)
  67. {
  68. struct usb_hcd *hcd = platform_get_drvdata(dev);
  69. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  70. int clk;
  71. if (priv->phy) {
  72. phy_power_off(priv->phy);
  73. phy_exit(priv->phy);
  74. }
  75. for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
  76. if (priv->clks[clk])
  77. clk_disable_unprepare(priv->clks[clk]);
  78. }
  79. static struct hc_driver __read_mostly ohci_platform_hc_driver;
  80. static const struct ohci_driver_overrides platform_overrides __initconst = {
  81. .product_desc = "Generic Platform OHCI controller",
  82. .extra_priv_size = sizeof(struct ohci_platform_priv),
  83. };
  84. static struct usb_ohci_pdata ohci_platform_defaults = {
  85. .power_on = ohci_platform_power_on,
  86. .power_suspend = ohci_platform_power_off,
  87. .power_off = ohci_platform_power_off,
  88. };
  89. static int ohci_platform_probe(struct platform_device *dev)
  90. {
  91. struct usb_hcd *hcd;
  92. struct resource *res_mem;
  93. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  94. struct ohci_platform_priv *priv;
  95. struct ohci_hcd *ohci;
  96. int err, irq, clk = 0;
  97. if (usb_disabled())
  98. return -ENODEV;
  99. /*
  100. * Use reasonable defaults so platforms don't have to provide these
  101. * with DT probing on ARM.
  102. */
  103. if (!pdata)
  104. pdata = &ohci_platform_defaults;
  105. err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
  106. if (err)
  107. return err;
  108. irq = platform_get_irq(dev, 0);
  109. if (irq < 0) {
  110. dev_err(&dev->dev, "no irq provided");
  111. return irq;
  112. }
  113. hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
  114. dev_name(&dev->dev));
  115. if (!hcd)
  116. return -ENOMEM;
  117. platform_set_drvdata(dev, hcd);
  118. dev->dev.platform_data = pdata;
  119. priv = hcd_to_ohci_priv(hcd);
  120. ohci = hcd_to_ohci(hcd);
  121. if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
  122. if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  123. ohci->flags |= OHCI_QUIRK_BE_MMIO;
  124. if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  125. ohci->flags |= OHCI_QUIRK_BE_DESC;
  126. if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  127. ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
  128. if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
  129. ohci->flags |= OHCI_QUIRK_FRAME_NO;
  130. of_property_read_u32(dev->dev.of_node, "num-ports",
  131. &ohci->num_ports);
  132. priv->phy = devm_phy_get(&dev->dev, "usb");
  133. if (IS_ERR(priv->phy)) {
  134. err = PTR_ERR(priv->phy);
  135. if (err == -EPROBE_DEFER)
  136. goto err_put_hcd;
  137. priv->phy = NULL;
  138. }
  139. for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
  140. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  141. if (IS_ERR(priv->clks[clk])) {
  142. err = PTR_ERR(priv->clks[clk]);
  143. if (err == -EPROBE_DEFER)
  144. goto err_put_clks;
  145. priv->clks[clk] = NULL;
  146. break;
  147. }
  148. }
  149. }
  150. priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
  151. if (IS_ERR(priv->rst)) {
  152. err = PTR_ERR(priv->rst);
  153. if (err == -EPROBE_DEFER)
  154. goto err_put_clks;
  155. priv->rst = NULL;
  156. } else {
  157. err = reset_control_deassert(priv->rst);
  158. if (err)
  159. goto err_put_clks;
  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. if (pdata->no_big_frame_no)
  166. ohci->flags |= OHCI_QUIRK_FRAME_NO;
  167. if (pdata->num_ports)
  168. ohci->num_ports = pdata->num_ports;
  169. #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
  170. if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
  171. dev_err(&dev->dev,
  172. "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
  173. err = -EINVAL;
  174. goto err_reset;
  175. }
  176. #endif
  177. #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
  178. if (ohci->flags & OHCI_QUIRK_BE_DESC) {
  179. dev_err(&dev->dev,
  180. "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
  181. err = -EINVAL;
  182. goto err_reset;
  183. }
  184. #endif
  185. if (pdata->power_on) {
  186. err = pdata->power_on(dev);
  187. if (err < 0)
  188. goto err_reset;
  189. }
  190. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  191. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  192. if (IS_ERR(hcd->regs)) {
  193. err = PTR_ERR(hcd->regs);
  194. goto err_power;
  195. }
  196. hcd->rsrc_start = res_mem->start;
  197. hcd->rsrc_len = resource_size(res_mem);
  198. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  199. if (err)
  200. goto err_power;
  201. device_wakeup_enable(hcd->self.controller);
  202. platform_set_drvdata(dev, hcd);
  203. return err;
  204. err_power:
  205. if (pdata->power_off)
  206. pdata->power_off(dev);
  207. err_reset:
  208. if (priv->rst)
  209. reset_control_assert(priv->rst);
  210. err_put_clks:
  211. while (--clk >= 0)
  212. clk_put(priv->clks[clk]);
  213. err_put_hcd:
  214. if (pdata == &ohci_platform_defaults)
  215. dev->dev.platform_data = NULL;
  216. usb_put_hcd(hcd);
  217. return err;
  218. }
  219. static int ohci_platform_remove(struct platform_device *dev)
  220. {
  221. struct usb_hcd *hcd = platform_get_drvdata(dev);
  222. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  223. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  224. int clk;
  225. usb_remove_hcd(hcd);
  226. if (pdata->power_off)
  227. pdata->power_off(dev);
  228. if (priv->rst)
  229. reset_control_assert(priv->rst);
  230. for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
  231. clk_put(priv->clks[clk]);
  232. usb_put_hcd(hcd);
  233. if (pdata == &ohci_platform_defaults)
  234. dev->dev.platform_data = NULL;
  235. return 0;
  236. }
  237. #ifdef CONFIG_PM_SLEEP
  238. static int ohci_platform_suspend(struct device *dev)
  239. {
  240. struct usb_hcd *hcd = dev_get_drvdata(dev);
  241. struct usb_ohci_pdata *pdata = dev->platform_data;
  242. struct platform_device *pdev =
  243. container_of(dev, struct platform_device, dev);
  244. bool do_wakeup = device_may_wakeup(dev);
  245. int ret;
  246. ret = ohci_suspend(hcd, do_wakeup);
  247. if (ret)
  248. return ret;
  249. if (pdata->power_suspend)
  250. pdata->power_suspend(pdev);
  251. return ret;
  252. }
  253. static int ohci_platform_resume(struct device *dev)
  254. {
  255. struct usb_hcd *hcd = dev_get_drvdata(dev);
  256. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  257. struct platform_device *pdev =
  258. container_of(dev, struct platform_device, dev);
  259. if (pdata->power_on) {
  260. int err = pdata->power_on(pdev);
  261. if (err < 0)
  262. return err;
  263. }
  264. ohci_resume(hcd, false);
  265. return 0;
  266. }
  267. #endif /* CONFIG_PM_SLEEP */
  268. static const struct of_device_id ohci_platform_ids[] = {
  269. { .compatible = "generic-ohci", },
  270. { }
  271. };
  272. MODULE_DEVICE_TABLE(of, ohci_platform_ids);
  273. static const struct platform_device_id ohci_platform_table[] = {
  274. { "ohci-platform", 0 },
  275. { }
  276. };
  277. MODULE_DEVICE_TABLE(platform, ohci_platform_table);
  278. static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
  279. ohci_platform_resume);
  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. .pm = &ohci_platform_pm_ops,
  288. .of_match_table = ohci_platform_ids,
  289. }
  290. };
  291. static int __init ohci_platform_init(void)
  292. {
  293. if (usb_disabled())
  294. return -ENODEV;
  295. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  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");