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