ohci-platform.c 10 KB

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