ohci-platform.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. }
  155. }
  156. for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
  157. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  158. if (IS_ERR(priv->clks[clk])) {
  159. err = PTR_ERR(priv->clks[clk]);
  160. if (err == -EPROBE_DEFER)
  161. goto err_put_clks;
  162. priv->clks[clk] = NULL;
  163. break;
  164. }
  165. }
  166. for (rst = 0; rst < OHCI_MAX_RESETS; rst++) {
  167. priv->resets[rst] =
  168. devm_reset_control_get_shared_by_index(
  169. &dev->dev, rst);
  170. if (IS_ERR(priv->resets[rst])) {
  171. err = PTR_ERR(priv->resets[rst]);
  172. if (err == -EPROBE_DEFER)
  173. goto err_reset;
  174. priv->resets[rst] = NULL;
  175. break;
  176. }
  177. err = reset_control_deassert(priv->resets[rst]);
  178. if (err)
  179. goto err_reset;
  180. }
  181. }
  182. if (pdata->big_endian_desc)
  183. ohci->flags |= OHCI_QUIRK_BE_DESC;
  184. if (pdata->big_endian_mmio)
  185. ohci->flags |= OHCI_QUIRK_BE_MMIO;
  186. if (pdata->no_big_frame_no)
  187. ohci->flags |= OHCI_QUIRK_FRAME_NO;
  188. if (pdata->num_ports)
  189. ohci->num_ports = pdata->num_ports;
  190. #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
  191. if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
  192. dev_err(&dev->dev,
  193. "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
  194. err = -EINVAL;
  195. goto err_reset;
  196. }
  197. #endif
  198. #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
  199. if (ohci->flags & OHCI_QUIRK_BE_DESC) {
  200. dev_err(&dev->dev,
  201. "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
  202. err = -EINVAL;
  203. goto err_reset;
  204. }
  205. #endif
  206. if (pdata->power_on) {
  207. err = pdata->power_on(dev);
  208. if (err < 0)
  209. goto err_reset;
  210. }
  211. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  212. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  213. if (IS_ERR(hcd->regs)) {
  214. err = PTR_ERR(hcd->regs);
  215. goto err_power;
  216. }
  217. hcd->rsrc_start = res_mem->start;
  218. hcd->rsrc_len = resource_size(res_mem);
  219. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  220. if (err)
  221. goto err_power;
  222. device_wakeup_enable(hcd->self.controller);
  223. platform_set_drvdata(dev, hcd);
  224. return err;
  225. err_power:
  226. if (pdata->power_off)
  227. pdata->power_off(dev);
  228. err_reset:
  229. while (--rst >= 0)
  230. reset_control_assert(priv->resets[rst]);
  231. err_put_clks:
  232. while (--clk >= 0)
  233. clk_put(priv->clks[clk]);
  234. err_put_hcd:
  235. if (pdata == &ohci_platform_defaults)
  236. dev->dev.platform_data = NULL;
  237. usb_put_hcd(hcd);
  238. return err;
  239. }
  240. static int ohci_platform_remove(struct platform_device *dev)
  241. {
  242. struct usb_hcd *hcd = platform_get_drvdata(dev);
  243. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  244. struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  245. int clk, rst;
  246. usb_remove_hcd(hcd);
  247. if (pdata->power_off)
  248. pdata->power_off(dev);
  249. for (rst = 0; rst < OHCI_MAX_RESETS && priv->resets[rst]; rst++)
  250. reset_control_assert(priv->resets[rst]);
  251. for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
  252. clk_put(priv->clks[clk]);
  253. usb_put_hcd(hcd);
  254. if (pdata == &ohci_platform_defaults)
  255. dev->dev.platform_data = NULL;
  256. return 0;
  257. }
  258. #ifdef CONFIG_PM_SLEEP
  259. static int ohci_platform_suspend(struct device *dev)
  260. {
  261. struct usb_hcd *hcd = dev_get_drvdata(dev);
  262. struct usb_ohci_pdata *pdata = dev->platform_data;
  263. struct platform_device *pdev = to_platform_device(dev);
  264. bool do_wakeup = device_may_wakeup(dev);
  265. int ret;
  266. ret = ohci_suspend(hcd, do_wakeup);
  267. if (ret)
  268. return ret;
  269. if (pdata->power_suspend)
  270. pdata->power_suspend(pdev);
  271. return ret;
  272. }
  273. static int ohci_platform_resume(struct device *dev)
  274. {
  275. struct usb_hcd *hcd = dev_get_drvdata(dev);
  276. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  277. struct platform_device *pdev = to_platform_device(dev);
  278. if (pdata->power_on) {
  279. int err = pdata->power_on(pdev);
  280. if (err < 0)
  281. return err;
  282. }
  283. ohci_resume(hcd, false);
  284. return 0;
  285. }
  286. #endif /* CONFIG_PM_SLEEP */
  287. static const struct of_device_id ohci_platform_ids[] = {
  288. { .compatible = "generic-ohci", },
  289. { .compatible = "cavium,octeon-6335-ohci", },
  290. { }
  291. };
  292. MODULE_DEVICE_TABLE(of, ohci_platform_ids);
  293. static const struct platform_device_id ohci_platform_table[] = {
  294. { "ohci-platform", 0 },
  295. { }
  296. };
  297. MODULE_DEVICE_TABLE(platform, ohci_platform_table);
  298. static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
  299. ohci_platform_resume);
  300. static struct platform_driver ohci_platform_driver = {
  301. .id_table = ohci_platform_table,
  302. .probe = ohci_platform_probe,
  303. .remove = ohci_platform_remove,
  304. .shutdown = usb_hcd_platform_shutdown,
  305. .driver = {
  306. .name = "ohci-platform",
  307. .pm = &ohci_platform_pm_ops,
  308. .of_match_table = ohci_platform_ids,
  309. }
  310. };
  311. static int __init ohci_platform_init(void)
  312. {
  313. if (usb_disabled())
  314. return -ENODEV;
  315. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  316. ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
  317. return platform_driver_register(&ohci_platform_driver);
  318. }
  319. module_init(ohci_platform_init);
  320. static void __exit ohci_platform_cleanup(void)
  321. {
  322. platform_driver_unregister(&ohci_platform_driver);
  323. }
  324. module_exit(ohci_platform_cleanup);
  325. MODULE_DESCRIPTION(DRIVER_DESC);
  326. MODULE_AUTHOR("Hauke Mehrtens");
  327. MODULE_AUTHOR("Alan Stern");
  328. MODULE_LICENSE("GPL");