ohci-platform.c 9.8 KB

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