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