ohci-platform.c 9.1 KB

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