ohci-platform.c 9.9 KB

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