195-1-ohci-plat-changes.patch 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. From 49ff47db168655cac5213cf5dd1844b08fb9823c Mon Sep 17 00:00:00 2001
  2. From: Hans de Goede <hdegoede@redhat.com>
  3. Date: Sun, 5 Jan 2014 14:42:39 +0100
  4. Subject: [PATCH] ohci-platform: Add support for devicetree instantiation
  5. Add support for ohci-platform instantiation from devicetree, including
  6. optionally getting clks and a phy from devicetree, and enabling / disabling
  7. those on power_on / off.
  8. This should allow using ohci-platform from devicetree in various cases.
  9. Specifically after this commit it can be used for the ohci controller found
  10. on Allwinner sunxi SoCs.
  11. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
  12. Acked-by: Alan Stern <stern@rowland.harvard.edu>
  13. ---
  14. Documentation/devicetree/bindings/usb/usb-ohci.txt | 22 +++
  15. drivers/usb/host/ohci-platform.c | 162 ++++++++++++++++++---
  16. 2 files changed, 162 insertions(+), 22 deletions(-)
  17. create mode 100644 Documentation/devicetree/bindings/usb/usb-ohci.txt
  18. --- /dev/null
  19. +++ b/Documentation/devicetree/bindings/usb/usb-ohci.txt
  20. @@ -0,0 +1,25 @@
  21. +USB OHCI controllers
  22. +
  23. +Required properties:
  24. +- compatible : "generic-ohci"
  25. +- reg : ohci controller register range (address and length)
  26. +- interrupts : ohci controller interrupt
  27. +
  28. +Optional properties:
  29. +- big-endian-regs : boolean, set this for hcds with big-endian registers
  30. +- big-endian-desc : boolean, set this for hcds with big-endian descriptors
  31. +- big-endian : boolean, for hcds with big-endian-regs + big-endian-desc
  32. +- clocks : a list of phandle + clock specifier pairs
  33. +- phys : phandle + phy specifier pair
  34. +- phy-names : "usb"
  35. +
  36. +Example:
  37. +
  38. + ohci0: usb@01c14400 {
  39. + compatible = "allwinner,sun4i-a10-ohci", "generic-ohci";
  40. + reg = <0x01c14400 0x100>;
  41. + interrupts = <64>;
  42. + clocks = <&usb_clk 6>, <&ahb_gates 2>;
  43. + phys = <&usbphy 1>;
  44. + phy-names = "usb";
  45. + };
  46. --- a/drivers/usb/host/ohci-platform.c
  47. +++ b/drivers/usb/host/ohci-platform.c
  48. @@ -3,6 +3,7 @@
  49. *
  50. * Copyright 2007 Michael Buesch <m@bues.ch>
  51. * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
  52. + * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  53. *
  54. * Derived from the OCHI-SSB driver
  55. * Derived from the OHCI-PCI driver
  56. @@ -14,11 +15,14 @@
  57. * Licensed under the GNU/GPL. See COPYING for details.
  58. */
  59. +#include <linux/clk.h>
  60. +#include <linux/dma-mapping.h>
  61. #include <linux/hrtimer.h>
  62. #include <linux/io.h>
  63. #include <linux/kernel.h>
  64. #include <linux/module.h>
  65. #include <linux/err.h>
  66. +#include <linux/phy/phy.h>
  67. #include <linux/platform_device.h>
  68. #include <linux/usb/ohci_pdriver.h>
  69. #include <linux/usb.h>
  70. @@ -27,6 +31,13 @@
  71. #include "ohci.h"
  72. #define DRIVER_DESC "OHCI generic platform driver"
  73. +#define OHCI_MAX_CLKS 3
  74. +#define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
  75. +
  76. +struct ohci_platform_priv {
  77. + struct clk *clks[OHCI_MAX_CLKS];
  78. + struct phy *phy;
  79. +};
  80. static const char hcd_name[] = "ohci-platform";
  81. @@ -48,11 +59,67 @@ static int ohci_platform_reset(struct us
  82. return ohci_setup(hcd);
  83. }
  84. +static int ohci_platform_power_on(struct platform_device *dev)
  85. +{
  86. + struct usb_hcd *hcd = platform_get_drvdata(dev);
  87. + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  88. + int clk, ret;
  89. +
  90. + for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
  91. + ret = clk_prepare_enable(priv->clks[clk]);
  92. + if (ret)
  93. + goto err_disable_clks;
  94. + }
  95. +
  96. + if (priv->phy) {
  97. + ret = phy_init(priv->phy);
  98. + if (ret)
  99. + goto err_disable_clks;
  100. +
  101. + ret = phy_power_on(priv->phy);
  102. + if (ret)
  103. + goto err_exit_phy;
  104. + }
  105. +
  106. + return 0;
  107. +
  108. +err_exit_phy:
  109. + phy_exit(priv->phy);
  110. +err_disable_clks:
  111. + while (--clk >= 0)
  112. + clk_disable_unprepare(priv->clks[clk]);
  113. +
  114. + return ret;
  115. +}
  116. +
  117. +static void ohci_platform_power_off(struct platform_device *dev)
  118. +{
  119. + struct usb_hcd *hcd = platform_get_drvdata(dev);
  120. + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  121. + int clk;
  122. +
  123. + if (priv->phy) {
  124. + phy_power_off(priv->phy);
  125. + phy_exit(priv->phy);
  126. + }
  127. +
  128. + for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
  129. + if (priv->clks[clk])
  130. + clk_disable_unprepare(priv->clks[clk]);
  131. +}
  132. +
  133. static struct hc_driver __read_mostly ohci_platform_hc_driver;
  134. static const struct ohci_driver_overrides platform_overrides __initconst = {
  135. - .product_desc = "Generic Platform OHCI controller",
  136. - .reset = ohci_platform_reset,
  137. + .product_desc = "Generic Platform OHCI controller",
  138. + .reset = ohci_platform_reset,
  139. + .extra_priv_size = sizeof(struct ohci_platform_priv),
  140. +};
  141. +
  142. +static struct usb_ohci_pdata ohci_platform_defaults = {
  143. + .power_on = ohci_platform_power_on,
  144. + .power_suspend = ohci_platform_power_off,
  145. + .power_off = ohci_platform_power_off,
  146. };
  147. static int ohci_platform_probe(struct platform_device *dev)
  148. @@ -60,17 +127,24 @@ static int ohci_platform_probe(struct pl
  149. struct usb_hcd *hcd;
  150. struct resource *res_mem;
  151. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  152. - int irq;
  153. - int err = -ENOMEM;
  154. -
  155. - if (!pdata) {
  156. - WARN_ON(1);
  157. - return -ENODEV;
  158. - }
  159. + struct ohci_platform_priv *priv;
  160. + struct ohci_hcd *ohci;
  161. + int err, irq, clk = 0;
  162. if (usb_disabled())
  163. return -ENODEV;
  164. + /*
  165. + * Use reasonable defaults so platforms don't have to provide these
  166. + * with DT probing on ARM.
  167. + */
  168. + if (!pdata)
  169. + pdata = &ohci_platform_defaults;
  170. +
  171. + err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
  172. + if (err)
  173. + return err;
  174. +
  175. irq = platform_get_irq(dev, 0);
  176. if (irq < 0) {
  177. dev_err(&dev->dev, "no irq provided");
  178. @@ -83,17 +157,66 @@ static int ohci_platform_probe(struct pl
  179. return -ENXIO;
  180. }
  181. + hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
  182. + dev_name(&dev->dev));
  183. + if (!hcd)
  184. + return -ENOMEM;
  185. +
  186. + platform_set_drvdata(dev, hcd);
  187. + dev->dev.platform_data = pdata;
  188. + priv = hcd_to_ohci_priv(hcd);
  189. + ohci = hcd_to_ohci(hcd);
  190. +
  191. + if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
  192. + if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
  193. + ohci->flags |= OHCI_QUIRK_BE_MMIO;
  194. +
  195. + if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
  196. + ohci->flags |= OHCI_QUIRK_BE_DESC;
  197. +
  198. + if (of_property_read_bool(dev->dev.of_node, "big-endian"))
  199. + ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
  200. +
  201. +#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
  202. + if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
  203. + dev_err(&dev->dev,
  204. + "Error big-endian-regs not compiled in\n");
  205. + err = -EINVAL;
  206. + goto err_put_hcd;
  207. + }
  208. +#endif
  209. +#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
  210. + if (ohci->flags & OHCI_QUIRK_BE_DESC) {
  211. + dev_err(&dev->dev,
  212. + "Error big-endian-desc not compiled in\n");
  213. + err = -EINVAL;
  214. + goto err_put_hcd;
  215. + }
  216. +#endif
  217. + priv->phy = devm_phy_get(&dev->dev, "usb");
  218. + if (IS_ERR(priv->phy)) {
  219. + err = PTR_ERR(priv->phy);
  220. + if (err == -EPROBE_DEFER)
  221. + goto err_put_hcd;
  222. + priv->phy = NULL;
  223. + }
  224. +
  225. + for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
  226. + priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  227. + if (IS_ERR(priv->clks[clk])) {
  228. + err = PTR_ERR(priv->clks[clk]);
  229. + if (err == -EPROBE_DEFER)
  230. + goto err_put_clks;
  231. + priv->clks[clk] = NULL;
  232. + break;
  233. + }
  234. + }
  235. + }
  236. +
  237. if (pdata->power_on) {
  238. err = pdata->power_on(dev);
  239. if (err < 0)
  240. - return err;
  241. - }
  242. -
  243. - hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
  244. - dev_name(&dev->dev));
  245. - if (!hcd) {
  246. - err = -ENOMEM;
  247. - goto err_power;
  248. + goto err_put_clks;
  249. }
  250. hcd->rsrc_start = res_mem->start;
  251. @@ -102,11 +225,11 @@ static int ohci_platform_probe(struct pl
  252. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  253. if (IS_ERR(hcd->regs)) {
  254. err = PTR_ERR(hcd->regs);
  255. - goto err_put_hcd;
  256. + goto err_power;
  257. }
  258. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  259. if (err)
  260. - goto err_put_hcd;
  261. + goto err_power;
  262. device_wakeup_enable(hcd->self.controller);
  263. @@ -114,11 +237,17 @@ static int ohci_platform_probe(struct pl
  264. return err;
  265. -err_put_hcd:
  266. - usb_put_hcd(hcd);
  267. err_power:
  268. if (pdata->power_off)
  269. pdata->power_off(dev);
  270. +err_put_clks:
  271. + while (--clk >= 0)
  272. + clk_put(priv->clks[clk]);
  273. +err_put_hcd:
  274. + if (pdata == &ohci_platform_defaults)
  275. + dev->dev.platform_data = NULL;
  276. +
  277. + usb_put_hcd(hcd);
  278. return err;
  279. }
  280. @@ -127,13 +256,22 @@ static int ohci_platform_remove(struct p
  281. {
  282. struct usb_hcd *hcd = platform_get_drvdata(dev);
  283. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  284. + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  285. + int clk;
  286. usb_remove_hcd(hcd);
  287. - usb_put_hcd(hcd);
  288. if (pdata->power_off)
  289. pdata->power_off(dev);
  290. + for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
  291. + clk_put(priv->clks[clk]);
  292. +
  293. + usb_put_hcd(hcd);
  294. +
  295. + if (pdata == &ohci_platform_defaults)
  296. + dev->dev.platform_data = NULL;
  297. +
  298. return 0;
  299. }
  300. @@ -180,6 +318,12 @@ static int ohci_platform_resume(struct d
  301. #define ohci_platform_resume NULL
  302. #endif /* CONFIG_PM */
  303. +static const struct of_device_id ohci_platform_ids[] = {
  304. + { .compatible = "generic-ohci", },
  305. + { }
  306. +};
  307. +MODULE_DEVICE_TABLE(of, ohci_platform_ids);
  308. +
  309. static const struct platform_device_id ohci_platform_table[] = {
  310. { "ohci-platform", 0 },
  311. { }
  312. @@ -200,6 +344,7 @@ static struct platform_driver ohci_platf
  313. .owner = THIS_MODULE,
  314. .name = "ohci-platform",
  315. .pm = &ohci_platform_pm_ops,
  316. + .of_match_table = ohci_platform_ids,
  317. }
  318. };