GNU Linux-libre 4.19.211-gnu1
[releases.git] / drivers / usb / host / ehci-platform.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic platform ehci driver
4  *
5  * Copyright 2007 Steven Brown <sbrown@cortland.com>
6  * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
7  * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
8  *
9  * Derived from the ohci-ssb driver
10  * Copyright 2007 Michael Buesch <m@bues.ch>
11  *
12  * Derived from the EHCI-PCI driver
13  * Copyright (c) 2000-2004 by David Brownell
14  *
15  * Derived from the ohci-pci driver
16  * Copyright 1999 Roman Weissgaerber
17  * Copyright 2000-2002 David Brownell
18  * Copyright 1999 Linus Torvalds
19  * Copyright 1999 Gregory P. Smith
20  */
21 #include <linux/acpi.h>
22 #include <linux/clk.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/err.h>
25 #include <linux/kernel.h>
26 #include <linux/hrtimer.h>
27 #include <linux/io.h>
28 #include <linux/module.h>
29 #include <linux/of.h>
30 #include <linux/platform_device.h>
31 #include <linux/reset.h>
32 #include <linux/sys_soc.h>
33 #include <linux/timer.h>
34 #include <linux/usb.h>
35 #include <linux/usb/hcd.h>
36 #include <linux/usb/ehci_pdriver.h>
37 #include <linux/usb/of.h>
38
39 #include "ehci.h"
40
41 #define DRIVER_DESC "EHCI generic platform driver"
42 #define EHCI_MAX_CLKS 4
43 #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
44
45 struct ehci_platform_priv {
46         struct clk *clks[EHCI_MAX_CLKS];
47         struct reset_control *rsts;
48         bool reset_on_resume;
49         bool quirk_poll;
50         struct timer_list poll_timer;
51         struct delayed_work poll_work;
52 };
53
54 static const char hcd_name[] = "ehci-platform";
55
56 static int ehci_platform_reset(struct usb_hcd *hcd)
57 {
58         struct platform_device *pdev = to_platform_device(hcd->self.controller);
59         struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
60         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
61         int retval;
62
63         ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
64
65         if (pdata->pre_setup) {
66                 retval = pdata->pre_setup(hcd);
67                 if (retval < 0)
68                         return retval;
69         }
70
71         ehci->caps = hcd->regs + pdata->caps_offset;
72         retval = ehci_setup(hcd);
73         if (retval)
74                 return retval;
75
76         if (pdata->no_io_watchdog)
77                 ehci->need_io_watchdog = 0;
78         return 0;
79 }
80
81 static int ehci_platform_power_on(struct platform_device *dev)
82 {
83         struct usb_hcd *hcd = platform_get_drvdata(dev);
84         struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
85         int clk, ret;
86
87         for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
88                 ret = clk_prepare_enable(priv->clks[clk]);
89                 if (ret)
90                         goto err_disable_clks;
91         }
92
93         return 0;
94
95 err_disable_clks:
96         while (--clk >= 0)
97                 clk_disable_unprepare(priv->clks[clk]);
98
99         return ret;
100 }
101
102 static void ehci_platform_power_off(struct platform_device *dev)
103 {
104         struct usb_hcd *hcd = platform_get_drvdata(dev);
105         struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
106         int clk;
107
108         for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
109                 if (priv->clks[clk])
110                         clk_disable_unprepare(priv->clks[clk]);
111 }
112
113 static struct hc_driver __read_mostly ehci_platform_hc_driver;
114
115 static const struct ehci_driver_overrides platform_overrides __initconst = {
116         .reset =                ehci_platform_reset,
117         .extra_priv_size =      sizeof(struct ehci_platform_priv),
118 };
119
120 static struct usb_ehci_pdata ehci_platform_defaults = {
121         .power_on =             ehci_platform_power_on,
122         .power_suspend =        ehci_platform_power_off,
123         .power_off =            ehci_platform_power_off,
124 };
125
126 /**
127  * quirk_poll_check_port_status - Poll port_status if the device sticks
128  * @ehci: the ehci hcd pointer
129  *
130  * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting
131  * stuck very rarely after a full/low usb device was disconnected. To
132  * detect such a situation, the controllers require a special way which poll
133  * the EHCI PORTSC register.
134  *
135  * Return: true if the controller's port_status indicated getting stuck
136  */
137 static bool quirk_poll_check_port_status(struct ehci_hcd *ehci)
138 {
139         u32 port_status = ehci_readl(ehci, &ehci->regs->port_status[0]);
140
141         if (!(port_status & PORT_OWNER) &&
142              (port_status & PORT_POWER) &&
143             !(port_status & PORT_CONNECT) &&
144              (port_status & PORT_LS_MASK))
145                 return true;
146
147         return false;
148 }
149
150 /**
151  * quirk_poll_rebind_companion - rebind comanion device to recover
152  * @ehci: the ehci hcd pointer
153  *
154  * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting
155  * stuck very rarely after a full/low usb device was disconnected. To
156  * recover from such a situation, the controllers require changing the OHCI
157  * functional state.
158  */
159 static void quirk_poll_rebind_companion(struct ehci_hcd *ehci)
160 {
161         struct device *companion_dev;
162         struct usb_hcd *hcd = ehci_to_hcd(ehci);
163
164         companion_dev = usb_of_get_companion_dev(hcd->self.controller);
165         if (!companion_dev)
166                 return;
167
168         device_release_driver(companion_dev);
169         if (device_attach(companion_dev) < 0)
170                 ehci_err(ehci, "%s: failed\n", __func__);
171
172         put_device(companion_dev);
173 }
174
175 static void quirk_poll_work(struct work_struct *work)
176 {
177         struct ehci_platform_priv *priv =
178                 container_of(to_delayed_work(work), struct ehci_platform_priv,
179                              poll_work);
180         struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd,
181                                              priv);
182
183         /* check the status twice to reduce misdetection rate */
184         if (!quirk_poll_check_port_status(ehci))
185                 return;
186         udelay(10);
187         if (!quirk_poll_check_port_status(ehci))
188                 return;
189
190         ehci_dbg(ehci, "%s: detected getting stuck. rebind now!\n", __func__);
191         quirk_poll_rebind_companion(ehci);
192 }
193
194 static void quirk_poll_timer(struct timer_list *t)
195 {
196         struct ehci_platform_priv *priv = from_timer(priv, t, poll_timer);
197         struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd,
198                                              priv);
199
200         if (quirk_poll_check_port_status(ehci)) {
201                 /*
202                  * Now scheduling the work for testing the port more. Note that
203                  * updating the status is possible to be delayed when
204                  * reconnection. So, this uses delayed work with 5 ms delay
205                  * to avoid misdetection.
206                  */
207                 schedule_delayed_work(&priv->poll_work, msecs_to_jiffies(5));
208         }
209
210         mod_timer(&priv->poll_timer, jiffies + HZ);
211 }
212
213 static void quirk_poll_init(struct ehci_platform_priv *priv)
214 {
215         INIT_DELAYED_WORK(&priv->poll_work, quirk_poll_work);
216         timer_setup(&priv->poll_timer, quirk_poll_timer, 0);
217         mod_timer(&priv->poll_timer, jiffies + HZ);
218 }
219
220 static void quirk_poll_end(struct ehci_platform_priv *priv)
221 {
222         del_timer_sync(&priv->poll_timer);
223         cancel_delayed_work(&priv->poll_work);
224 }
225
226 static const struct soc_device_attribute quirk_poll_match[] = {
227         { .family = "R-Car Gen3" },
228         { /* sentinel*/ }
229 };
230
231 static int ehci_platform_probe(struct platform_device *dev)
232 {
233         struct usb_hcd *hcd;
234         struct resource *res_mem;
235         struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
236         struct ehci_platform_priv *priv;
237         struct ehci_hcd *ehci;
238         int err, irq, clk = 0;
239
240         if (usb_disabled())
241                 return -ENODEV;
242
243         /*
244          * Use reasonable defaults so platforms don't have to provide these
245          * with DT probing on ARM.
246          */
247         if (!pdata)
248                 pdata = &ehci_platform_defaults;
249
250         err = dma_coerce_mask_and_coherent(&dev->dev,
251                 pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
252         if (err) {
253                 dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
254                 return err;
255         }
256
257         irq = platform_get_irq(dev, 0);
258         if (irq < 0) {
259                 dev_err(&dev->dev, "no irq provided");
260                 return irq;
261         }
262
263         hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
264                              dev_name(&dev->dev));
265         if (!hcd)
266                 return -ENOMEM;
267
268         platform_set_drvdata(dev, hcd);
269         dev->dev.platform_data = pdata;
270         priv = hcd_to_ehci_priv(hcd);
271         ehci = hcd_to_ehci(hcd);
272
273         if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
274                 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
275                         ehci->big_endian_mmio = 1;
276
277                 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
278                         ehci->big_endian_desc = 1;
279
280                 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
281                         ehci->big_endian_mmio = ehci->big_endian_desc = 1;
282
283                 if (of_property_read_bool(dev->dev.of_node,
284                                           "needs-reset-on-resume"))
285                         priv->reset_on_resume = true;
286
287                 if (of_property_read_bool(dev->dev.of_node,
288                                           "has-transaction-translator"))
289                         hcd->has_tt = 1;
290
291                 if (soc_device_match(quirk_poll_match))
292                         priv->quirk_poll = true;
293
294                 for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
295                         priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
296                         if (IS_ERR(priv->clks[clk])) {
297                                 err = PTR_ERR(priv->clks[clk]);
298                                 if (err == -EPROBE_DEFER)
299                                         goto err_put_clks;
300                                 priv->clks[clk] = NULL;
301                                 break;
302                         }
303                 }
304         }
305
306         priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev);
307         if (IS_ERR(priv->rsts)) {
308                 err = PTR_ERR(priv->rsts);
309                 goto err_put_clks;
310         }
311
312         err = reset_control_deassert(priv->rsts);
313         if (err)
314                 goto err_put_clks;
315
316         if (pdata->big_endian_desc)
317                 ehci->big_endian_desc = 1;
318         if (pdata->big_endian_mmio)
319                 ehci->big_endian_mmio = 1;
320         if (pdata->has_tt)
321                 hcd->has_tt = 1;
322         if (pdata->reset_on_resume)
323                 priv->reset_on_resume = true;
324
325 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
326         if (ehci->big_endian_mmio) {
327                 dev_err(&dev->dev,
328                         "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
329                 err = -EINVAL;
330                 goto err_reset;
331         }
332 #endif
333 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
334         if (ehci->big_endian_desc) {
335                 dev_err(&dev->dev,
336                         "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
337                 err = -EINVAL;
338                 goto err_reset;
339         }
340 #endif
341
342         if (pdata->power_on) {
343                 err = pdata->power_on(dev);
344                 if (err < 0)
345                         goto err_reset;
346         }
347
348         res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
349         hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
350         if (IS_ERR(hcd->regs)) {
351                 err = PTR_ERR(hcd->regs);
352                 goto err_power;
353         }
354         hcd->rsrc_start = res_mem->start;
355         hcd->rsrc_len = resource_size(res_mem);
356
357         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
358         if (err)
359                 goto err_power;
360
361         device_wakeup_enable(hcd->self.controller);
362         device_enable_async_suspend(hcd->self.controller);
363         platform_set_drvdata(dev, hcd);
364
365         if (priv->quirk_poll)
366                 quirk_poll_init(priv);
367
368         return err;
369
370 err_power:
371         if (pdata->power_off)
372                 pdata->power_off(dev);
373 err_reset:
374         reset_control_assert(priv->rsts);
375 err_put_clks:
376         while (--clk >= 0)
377                 clk_put(priv->clks[clk]);
378
379         if (pdata == &ehci_platform_defaults)
380                 dev->dev.platform_data = NULL;
381
382         usb_put_hcd(hcd);
383
384         return err;
385 }
386
387 static int ehci_platform_remove(struct platform_device *dev)
388 {
389         struct usb_hcd *hcd = platform_get_drvdata(dev);
390         struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
391         struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
392         int clk;
393
394         if (priv->quirk_poll)
395                 quirk_poll_end(priv);
396
397         usb_remove_hcd(hcd);
398
399         if (pdata->power_off)
400                 pdata->power_off(dev);
401
402         reset_control_assert(priv->rsts);
403
404         for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
405                 clk_put(priv->clks[clk]);
406
407         usb_put_hcd(hcd);
408
409         if (pdata == &ehci_platform_defaults)
410                 dev->dev.platform_data = NULL;
411
412         return 0;
413 }
414
415 #ifdef CONFIG_PM_SLEEP
416 static int ehci_platform_suspend(struct device *dev)
417 {
418         struct usb_hcd *hcd = dev_get_drvdata(dev);
419         struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
420         struct platform_device *pdev = to_platform_device(dev);
421         struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
422         bool do_wakeup = device_may_wakeup(dev);
423         int ret;
424
425         if (priv->quirk_poll)
426                 quirk_poll_end(priv);
427
428         ret = ehci_suspend(hcd, do_wakeup);
429         if (ret)
430                 return ret;
431
432         if (pdata->power_suspend)
433                 pdata->power_suspend(pdev);
434
435         return ret;
436 }
437
438 static int ehci_platform_resume(struct device *dev)
439 {
440         struct usb_hcd *hcd = dev_get_drvdata(dev);
441         struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
442         struct platform_device *pdev = to_platform_device(dev);
443         struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
444         struct device *companion_dev;
445
446         if (pdata->power_on) {
447                 int err = pdata->power_on(pdev);
448                 if (err < 0)
449                         return err;
450         }
451
452         companion_dev = usb_of_get_companion_dev(hcd->self.controller);
453         if (companion_dev) {
454                 device_pm_wait_for_dev(hcd->self.controller, companion_dev);
455                 put_device(companion_dev);
456         }
457
458         ehci_resume(hcd, priv->reset_on_resume);
459
460         if (priv->quirk_poll)
461                 quirk_poll_init(priv);
462
463         return 0;
464 }
465 #endif /* CONFIG_PM_SLEEP */
466
467 static const struct of_device_id vt8500_ehci_ids[] = {
468         { .compatible = "via,vt8500-ehci", },
469         { .compatible = "wm,prizm-ehci", },
470         { .compatible = "generic-ehci", },
471         { .compatible = "cavium,octeon-6335-ehci", },
472         {}
473 };
474 MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
475
476 static const struct acpi_device_id ehci_acpi_match[] = {
477         { "PNP0D20", 0 }, /* EHCI controller without debug */
478         { }
479 };
480 MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
481
482 static const struct platform_device_id ehci_platform_table[] = {
483         { "ehci-platform", 0 },
484         { }
485 };
486 MODULE_DEVICE_TABLE(platform, ehci_platform_table);
487
488 static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
489         ehci_platform_resume);
490
491 static struct platform_driver ehci_platform_driver = {
492         .id_table       = ehci_platform_table,
493         .probe          = ehci_platform_probe,
494         .remove         = ehci_platform_remove,
495         .shutdown       = usb_hcd_platform_shutdown,
496         .driver         = {
497                 .name   = "ehci-platform",
498                 .pm     = &ehci_platform_pm_ops,
499                 .of_match_table = vt8500_ehci_ids,
500                 .acpi_match_table = ACPI_PTR(ehci_acpi_match),
501         }
502 };
503
504 static int __init ehci_platform_init(void)
505 {
506         if (usb_disabled())
507                 return -ENODEV;
508
509         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
510
511         ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
512         return platform_driver_register(&ehci_platform_driver);
513 }
514 module_init(ehci_platform_init);
515
516 static void __exit ehci_platform_cleanup(void)
517 {
518         platform_driver_unregister(&ehci_platform_driver);
519 }
520 module_exit(ehci_platform_cleanup);
521
522 MODULE_DESCRIPTION(DRIVER_DESC);
523 MODULE_AUTHOR("Hauke Mehrtens");
524 MODULE_AUTHOR("Alan Stern");
525 MODULE_LICENSE("GPL");