Merge tag 'nfsd-4.16-1' of git://linux-nfs.org/~bfields/linux
[sfrench/cifs-2.6.git] / drivers / usb / host / ohci-platform.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic platform ohci driver
4  *
5  * Copyright 2007 Michael Buesch <m@bues.ch>
6  * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
7  * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
8  *
9  * Derived from the OCHI-SSB driver
10  * Derived from the OHCI-PCI driver
11  * Copyright 1999 Roman Weissgaerber
12  * Copyright 2000-2002 David Brownell
13  * Copyright 1999 Linus Torvalds
14  * Copyright 1999 Gregory P. Smith
15  */
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/pm_runtime.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 *resets;
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                 if (of_property_read_bool(dev->dev.of_node,
166                                           "remote-wakeup-connected"))
167                         ohci->hc_control = OHCI_CTRL_RWC;
168
169                 of_property_read_u32(dev->dev.of_node, "num-ports",
170                                      &ohci->num_ports);
171
172                 priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
173                                 "phys", "#phy-cells");
174
175                 if (priv->num_phys > 0) {
176                         priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
177                                             sizeof(struct phy *), GFP_KERNEL);
178                         if (!priv->phys)
179                                 return -ENOMEM;
180                 } else
181                         priv->num_phys = 0;
182
183                 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
184                         priv->phys[phy_num] = devm_of_phy_get_by_index(
185                                         &dev->dev, dev->dev.of_node, phy_num);
186                         if (IS_ERR(priv->phys[phy_num])) {
187                                 err = PTR_ERR(priv->phys[phy_num]);
188                                 goto err_put_hcd;
189                         } else if (!hcd->phy) {
190                                 /* Avoiding phy_get() in usb_add_hcd() */
191                                 hcd->phy = priv->phys[phy_num];
192                         }
193                 }
194
195                 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
196                         priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
197                         if (IS_ERR(priv->clks[clk])) {
198                                 err = PTR_ERR(priv->clks[clk]);
199                                 if (err == -EPROBE_DEFER)
200                                         goto err_put_clks;
201                                 priv->clks[clk] = NULL;
202                                 break;
203                         }
204                 }
205
206                 priv->resets = devm_reset_control_array_get_optional_shared(
207                                                                 &dev->dev);
208                 if (IS_ERR(priv->resets)) {
209                         err = PTR_ERR(priv->resets);
210                         goto err_put_clks;
211                 }
212
213                 err = reset_control_deassert(priv->resets);
214                 if (err)
215                         goto err_put_clks;
216         }
217
218         if (pdata->big_endian_desc)
219                 ohci->flags |= OHCI_QUIRK_BE_DESC;
220         if (pdata->big_endian_mmio)
221                 ohci->flags |= OHCI_QUIRK_BE_MMIO;
222         if (pdata->no_big_frame_no)
223                 ohci->flags |= OHCI_QUIRK_FRAME_NO;
224         if (pdata->num_ports)
225                 ohci->num_ports = pdata->num_ports;
226
227 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
228         if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
229                 dev_err(&dev->dev,
230                         "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
231                 err = -EINVAL;
232                 goto err_reset;
233         }
234 #endif
235 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
236         if (ohci->flags & OHCI_QUIRK_BE_DESC) {
237                 dev_err(&dev->dev,
238                         "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
239                 err = -EINVAL;
240                 goto err_reset;
241         }
242 #endif
243
244         pm_runtime_set_active(&dev->dev);
245         pm_runtime_enable(&dev->dev);
246         if (pdata->power_on) {
247                 err = pdata->power_on(dev);
248                 if (err < 0)
249                         goto err_reset;
250         }
251
252         res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
253         hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
254         if (IS_ERR(hcd->regs)) {
255                 err = PTR_ERR(hcd->regs);
256                 goto err_power;
257         }
258         hcd->rsrc_start = res_mem->start;
259         hcd->rsrc_len = resource_size(res_mem);
260
261         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
262         if (err)
263                 goto err_power;
264
265         device_wakeup_enable(hcd->self.controller);
266
267         platform_set_drvdata(dev, hcd);
268
269         return err;
270
271 err_power:
272         if (pdata->power_off)
273                 pdata->power_off(dev);
274 err_reset:
275         pm_runtime_disable(&dev->dev);
276         reset_control_assert(priv->resets);
277 err_put_clks:
278         while (--clk >= 0)
279                 clk_put(priv->clks[clk]);
280 err_put_hcd:
281         if (pdata == &ohci_platform_defaults)
282                 dev->dev.platform_data = NULL;
283
284         usb_put_hcd(hcd);
285
286         return err;
287 }
288
289 static int ohci_platform_remove(struct platform_device *dev)
290 {
291         struct usb_hcd *hcd = platform_get_drvdata(dev);
292         struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
293         struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
294         int clk;
295
296         pm_runtime_get_sync(&dev->dev);
297         usb_remove_hcd(hcd);
298
299         if (pdata->power_off)
300                 pdata->power_off(dev);
301
302         reset_control_assert(priv->resets);
303
304         for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
305                 clk_put(priv->clks[clk]);
306
307         usb_put_hcd(hcd);
308
309         pm_runtime_put_sync(&dev->dev);
310         pm_runtime_disable(&dev->dev);
311
312         if (pdata == &ohci_platform_defaults)
313                 dev->dev.platform_data = NULL;
314
315         return 0;
316 }
317
318 #ifdef CONFIG_PM_SLEEP
319 static int ohci_platform_suspend(struct device *dev)
320 {
321         struct usb_hcd *hcd = dev_get_drvdata(dev);
322         struct usb_ohci_pdata *pdata = dev->platform_data;
323         struct platform_device *pdev = to_platform_device(dev);
324         bool do_wakeup = device_may_wakeup(dev);
325         int ret;
326
327         ret = ohci_suspend(hcd, do_wakeup);
328         if (ret)
329                 return ret;
330
331         if (pdata->power_suspend)
332                 pdata->power_suspend(pdev);
333
334         return ret;
335 }
336
337 static int ohci_platform_resume(struct device *dev)
338 {
339         struct usb_hcd *hcd = dev_get_drvdata(dev);
340         struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
341         struct platform_device *pdev = to_platform_device(dev);
342
343         if (pdata->power_on) {
344                 int err = pdata->power_on(pdev);
345                 if (err < 0)
346                         return err;
347         }
348
349         ohci_resume(hcd, false);
350         return 0;
351 }
352 #endif /* CONFIG_PM_SLEEP */
353
354 static const struct of_device_id ohci_platform_ids[] = {
355         { .compatible = "generic-ohci", },
356         { .compatible = "cavium,octeon-6335-ohci", },
357         { .compatible = "ti,ohci-omap3", },
358         { }
359 };
360 MODULE_DEVICE_TABLE(of, ohci_platform_ids);
361
362 static const struct platform_device_id ohci_platform_table[] = {
363         { "ohci-platform", 0 },
364         { }
365 };
366 MODULE_DEVICE_TABLE(platform, ohci_platform_table);
367
368 static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
369         ohci_platform_resume);
370
371 static struct platform_driver ohci_platform_driver = {
372         .id_table       = ohci_platform_table,
373         .probe          = ohci_platform_probe,
374         .remove         = ohci_platform_remove,
375         .shutdown       = usb_hcd_platform_shutdown,
376         .driver         = {
377                 .name   = "ohci-platform",
378                 .pm     = &ohci_platform_pm_ops,
379                 .of_match_table = ohci_platform_ids,
380         }
381 };
382
383 static int __init ohci_platform_init(void)
384 {
385         if (usb_disabled())
386                 return -ENODEV;
387
388         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
389
390         ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
391         return platform_driver_register(&ohci_platform_driver);
392 }
393 module_init(ohci_platform_init);
394
395 static void __exit ohci_platform_cleanup(void)
396 {
397         platform_driver_unregister(&ohci_platform_driver);
398 }
399 module_exit(ohci_platform_cleanup);
400
401 MODULE_DESCRIPTION(DRIVER_DESC);
402 MODULE_AUTHOR("Hauke Mehrtens");
403 MODULE_AUTHOR("Alan Stern");
404 MODULE_LICENSE("GPL");