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