Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Generic platform ehci driver
0004  *
0005  * Copyright 2007 Steven Brown <sbrown@cortland.com>
0006  * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
0007  * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
0008  *
0009  * Derived from the ohci-ssb driver
0010  * Copyright 2007 Michael Buesch <m@bues.ch>
0011  *
0012  * Derived from the EHCI-PCI driver
0013  * Copyright (c) 2000-2004 by David Brownell
0014  *
0015  * Derived from the ohci-pci driver
0016  * Copyright 1999 Roman Weissgaerber
0017  * Copyright 2000-2002 David Brownell
0018  * Copyright 1999 Linus Torvalds
0019  * Copyright 1999 Gregory P. Smith
0020  */
0021 #include <linux/acpi.h>
0022 #include <linux/clk.h>
0023 #include <linux/dma-mapping.h>
0024 #include <linux/err.h>
0025 #include <linux/kernel.h>
0026 #include <linux/hrtimer.h>
0027 #include <linux/io.h>
0028 #include <linux/module.h>
0029 #include <linux/of.h>
0030 #include <linux/platform_device.h>
0031 #include <linux/reset.h>
0032 #include <linux/sys_soc.h>
0033 #include <linux/timer.h>
0034 #include <linux/usb.h>
0035 #include <linux/usb/hcd.h>
0036 #include <linux/usb/ehci_pdriver.h>
0037 #include <linux/usb/of.h>
0038 
0039 #include "ehci.h"
0040 
0041 #define DRIVER_DESC "EHCI generic platform driver"
0042 #define EHCI_MAX_CLKS 4
0043 #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
0044 
0045 #define BCM_USB_FIFO_THRESHOLD  0x00800040
0046 
0047 struct ehci_platform_priv {
0048     struct clk *clks[EHCI_MAX_CLKS];
0049     struct reset_control *rsts;
0050     bool reset_on_resume;
0051     bool quirk_poll;
0052     struct timer_list poll_timer;
0053     struct delayed_work poll_work;
0054 };
0055 
0056 static const char hcd_name[] = "ehci-platform";
0057 
0058 static int ehci_platform_reset(struct usb_hcd *hcd)
0059 {
0060     struct platform_device *pdev = to_platform_device(hcd->self.controller);
0061     struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
0062     struct ehci_hcd *ehci = hcd_to_ehci(hcd);
0063     int retval;
0064 
0065     ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
0066 
0067     if (pdata->pre_setup) {
0068         retval = pdata->pre_setup(hcd);
0069         if (retval < 0)
0070             return retval;
0071     }
0072 
0073     ehci->caps = hcd->regs + pdata->caps_offset;
0074     retval = ehci_setup(hcd);
0075     if (retval)
0076         return retval;
0077 
0078     if (pdata->no_io_watchdog)
0079         ehci->need_io_watchdog = 0;
0080 
0081     if (of_device_is_compatible(pdev->dev.of_node, "brcm,xgs-iproc-ehci"))
0082         ehci_writel(ehci, BCM_USB_FIFO_THRESHOLD,
0083                 &ehci->regs->brcm_insnreg[1]);
0084 
0085     return 0;
0086 }
0087 
0088 static int ehci_platform_power_on(struct platform_device *dev)
0089 {
0090     struct usb_hcd *hcd = platform_get_drvdata(dev);
0091     struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
0092     int clk, ret;
0093 
0094     for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
0095         ret = clk_prepare_enable(priv->clks[clk]);
0096         if (ret)
0097             goto err_disable_clks;
0098     }
0099 
0100     return 0;
0101 
0102 err_disable_clks:
0103     while (--clk >= 0)
0104         clk_disable_unprepare(priv->clks[clk]);
0105 
0106     return ret;
0107 }
0108 
0109 static void ehci_platform_power_off(struct platform_device *dev)
0110 {
0111     struct usb_hcd *hcd = platform_get_drvdata(dev);
0112     struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
0113     int clk;
0114 
0115     for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
0116         if (priv->clks[clk])
0117             clk_disable_unprepare(priv->clks[clk]);
0118 }
0119 
0120 static struct hc_driver __read_mostly ehci_platform_hc_driver;
0121 
0122 static const struct ehci_driver_overrides platform_overrides __initconst = {
0123     .reset =        ehci_platform_reset,
0124     .extra_priv_size =  sizeof(struct ehci_platform_priv),
0125 };
0126 
0127 static struct usb_ehci_pdata ehci_platform_defaults = {
0128     .power_on =     ehci_platform_power_on,
0129     .power_suspend =    ehci_platform_power_off,
0130     .power_off =        ehci_platform_power_off,
0131 };
0132 
0133 /**
0134  * quirk_poll_check_port_status - Poll port_status if the device sticks
0135  * @ehci: the ehci hcd pointer
0136  *
0137  * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting
0138  * stuck very rarely after a full/low usb device was disconnected. To
0139  * detect such a situation, the controllers require a special way which poll
0140  * the EHCI PORTSC register.
0141  *
0142  * Return: true if the controller's port_status indicated getting stuck
0143  */
0144 static bool quirk_poll_check_port_status(struct ehci_hcd *ehci)
0145 {
0146     u32 port_status = ehci_readl(ehci, &ehci->regs->port_status[0]);
0147 
0148     if (!(port_status & PORT_OWNER) &&
0149          (port_status & PORT_POWER) &&
0150         !(port_status & PORT_CONNECT) &&
0151          (port_status & PORT_LS_MASK))
0152         return true;
0153 
0154     return false;
0155 }
0156 
0157 /**
0158  * quirk_poll_rebind_companion - rebind comanion device to recover
0159  * @ehci: the ehci hcd pointer
0160  *
0161  * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting
0162  * stuck very rarely after a full/low usb device was disconnected. To
0163  * recover from such a situation, the controllers require changing the OHCI
0164  * functional state.
0165  */
0166 static void quirk_poll_rebind_companion(struct ehci_hcd *ehci)
0167 {
0168     struct device *companion_dev;
0169     struct usb_hcd *hcd = ehci_to_hcd(ehci);
0170 
0171     companion_dev = usb_of_get_companion_dev(hcd->self.controller);
0172     if (!companion_dev)
0173         return;
0174 
0175     device_release_driver(companion_dev);
0176     if (device_attach(companion_dev) < 0)
0177         ehci_err(ehci, "%s: failed\n", __func__);
0178 
0179     put_device(companion_dev);
0180 }
0181 
0182 static void quirk_poll_work(struct work_struct *work)
0183 {
0184     struct ehci_platform_priv *priv =
0185         container_of(to_delayed_work(work), struct ehci_platform_priv,
0186                  poll_work);
0187     struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd,
0188                          priv);
0189 
0190     /* check the status twice to reduce misdetection rate */
0191     if (!quirk_poll_check_port_status(ehci))
0192         return;
0193     udelay(10);
0194     if (!quirk_poll_check_port_status(ehci))
0195         return;
0196 
0197     ehci_dbg(ehci, "%s: detected getting stuck. rebind now!\n", __func__);
0198     quirk_poll_rebind_companion(ehci);
0199 }
0200 
0201 static void quirk_poll_timer(struct timer_list *t)
0202 {
0203     struct ehci_platform_priv *priv = from_timer(priv, t, poll_timer);
0204     struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd,
0205                          priv);
0206 
0207     if (quirk_poll_check_port_status(ehci)) {
0208         /*
0209          * Now scheduling the work for testing the port more. Note that
0210          * updating the status is possible to be delayed when
0211          * reconnection. So, this uses delayed work with 5 ms delay
0212          * to avoid misdetection.
0213          */
0214         schedule_delayed_work(&priv->poll_work, msecs_to_jiffies(5));
0215     }
0216 
0217     mod_timer(&priv->poll_timer, jiffies + HZ);
0218 }
0219 
0220 static void quirk_poll_init(struct ehci_platform_priv *priv)
0221 {
0222     INIT_DELAYED_WORK(&priv->poll_work, quirk_poll_work);
0223     timer_setup(&priv->poll_timer, quirk_poll_timer, 0);
0224     mod_timer(&priv->poll_timer, jiffies + HZ);
0225 }
0226 
0227 static void quirk_poll_end(struct ehci_platform_priv *priv)
0228 {
0229     del_timer_sync(&priv->poll_timer);
0230     cancel_delayed_work(&priv->poll_work);
0231 }
0232 
0233 static const struct soc_device_attribute quirk_poll_match[] = {
0234     { .family = "R-Car Gen3" },
0235     { /* sentinel*/ }
0236 };
0237 
0238 static int ehci_platform_probe(struct platform_device *dev)
0239 {
0240     struct usb_hcd *hcd;
0241     struct resource *res_mem;
0242     struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
0243     struct ehci_platform_priv *priv;
0244     struct ehci_hcd *ehci;
0245     int err, irq, clk = 0;
0246 
0247     if (usb_disabled())
0248         return -ENODEV;
0249 
0250     /*
0251      * Use reasonable defaults so platforms don't have to provide these
0252      * with DT probing on ARM.
0253      */
0254     if (!pdata)
0255         pdata = &ehci_platform_defaults;
0256 
0257     err = dma_coerce_mask_and_coherent(&dev->dev,
0258         pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
0259     if (err) {
0260         dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
0261         return err;
0262     }
0263 
0264     irq = platform_get_irq(dev, 0);
0265     if (irq < 0)
0266         return irq;
0267 
0268     hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
0269                  dev_name(&dev->dev));
0270     if (!hcd)
0271         return -ENOMEM;
0272 
0273     platform_set_drvdata(dev, hcd);
0274     dev->dev.platform_data = pdata;
0275     priv = hcd_to_ehci_priv(hcd);
0276     ehci = hcd_to_ehci(hcd);
0277 
0278     if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
0279         if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
0280             ehci->big_endian_mmio = 1;
0281 
0282         if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
0283             ehci->big_endian_desc = 1;
0284 
0285         if (of_property_read_bool(dev->dev.of_node, "big-endian"))
0286             ehci->big_endian_mmio = ehci->big_endian_desc = 1;
0287 
0288         if (of_property_read_bool(dev->dev.of_node, "spurious-oc"))
0289             ehci->spurious_oc = 1;
0290 
0291         if (of_property_read_bool(dev->dev.of_node,
0292                       "needs-reset-on-resume"))
0293             priv->reset_on_resume = true;
0294 
0295         if (of_property_read_bool(dev->dev.of_node,
0296                       "has-transaction-translator"))
0297             hcd->has_tt = 1;
0298 
0299         if (of_device_is_compatible(dev->dev.of_node,
0300                         "aspeed,ast2500-ehci") ||
0301             of_device_is_compatible(dev->dev.of_node,
0302                         "aspeed,ast2600-ehci"))
0303             ehci->is_aspeed = 1;
0304 
0305         if (soc_device_match(quirk_poll_match))
0306             priv->quirk_poll = true;
0307 
0308         for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
0309             priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
0310             if (IS_ERR(priv->clks[clk])) {
0311                 err = PTR_ERR(priv->clks[clk]);
0312                 if (err == -EPROBE_DEFER)
0313                     goto err_put_clks;
0314                 priv->clks[clk] = NULL;
0315                 break;
0316             }
0317         }
0318     }
0319 
0320     priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev);
0321     if (IS_ERR(priv->rsts)) {
0322         err = PTR_ERR(priv->rsts);
0323         goto err_put_clks;
0324     }
0325 
0326     err = reset_control_deassert(priv->rsts);
0327     if (err)
0328         goto err_put_clks;
0329 
0330     if (pdata->big_endian_desc)
0331         ehci->big_endian_desc = 1;
0332     if (pdata->big_endian_mmio)
0333         ehci->big_endian_mmio = 1;
0334     if (pdata->has_tt)
0335         hcd->has_tt = 1;
0336     if (pdata->reset_on_resume)
0337         priv->reset_on_resume = true;
0338     if (pdata->spurious_oc)
0339         ehci->spurious_oc = 1;
0340 
0341 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
0342     if (ehci->big_endian_mmio) {
0343         dev_err(&dev->dev,
0344             "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
0345         err = -EINVAL;
0346         goto err_reset;
0347     }
0348 #endif
0349 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
0350     if (ehci->big_endian_desc) {
0351         dev_err(&dev->dev,
0352             "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
0353         err = -EINVAL;
0354         goto err_reset;
0355     }
0356 #endif
0357 
0358     if (pdata->power_on) {
0359         err = pdata->power_on(dev);
0360         if (err < 0)
0361             goto err_reset;
0362     }
0363 
0364     res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
0365     hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
0366     if (IS_ERR(hcd->regs)) {
0367         err = PTR_ERR(hcd->regs);
0368         goto err_power;
0369     }
0370     hcd->rsrc_start = res_mem->start;
0371     hcd->rsrc_len = resource_size(res_mem);
0372 
0373     hcd->tpl_support = of_usb_host_tpl_support(dev->dev.of_node);
0374 
0375     err = usb_add_hcd(hcd, irq, IRQF_SHARED);
0376     if (err)
0377         goto err_power;
0378 
0379     device_wakeup_enable(hcd->self.controller);
0380     device_enable_async_suspend(hcd->self.controller);
0381     platform_set_drvdata(dev, hcd);
0382 
0383     if (priv->quirk_poll)
0384         quirk_poll_init(priv);
0385 
0386     return err;
0387 
0388 err_power:
0389     if (pdata->power_off)
0390         pdata->power_off(dev);
0391 err_reset:
0392     reset_control_assert(priv->rsts);
0393 err_put_clks:
0394     while (--clk >= 0)
0395         clk_put(priv->clks[clk]);
0396 
0397     if (pdata == &ehci_platform_defaults)
0398         dev->dev.platform_data = NULL;
0399 
0400     usb_put_hcd(hcd);
0401 
0402     return err;
0403 }
0404 
0405 static int ehci_platform_remove(struct platform_device *dev)
0406 {
0407     struct usb_hcd *hcd = platform_get_drvdata(dev);
0408     struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
0409     struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
0410     int clk;
0411 
0412     if (priv->quirk_poll)
0413         quirk_poll_end(priv);
0414 
0415     usb_remove_hcd(hcd);
0416 
0417     if (pdata->power_off)
0418         pdata->power_off(dev);
0419 
0420     reset_control_assert(priv->rsts);
0421 
0422     for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
0423         clk_put(priv->clks[clk]);
0424 
0425     usb_put_hcd(hcd);
0426 
0427     if (pdata == &ehci_platform_defaults)
0428         dev->dev.platform_data = NULL;
0429 
0430     return 0;
0431 }
0432 
0433 static int __maybe_unused ehci_platform_suspend(struct device *dev)
0434 {
0435     struct usb_hcd *hcd = dev_get_drvdata(dev);
0436     struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
0437     struct platform_device *pdev = to_platform_device(dev);
0438     struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
0439     bool do_wakeup = device_may_wakeup(dev);
0440     int ret;
0441 
0442     if (priv->quirk_poll)
0443         quirk_poll_end(priv);
0444 
0445     ret = ehci_suspend(hcd, do_wakeup);
0446     if (ret)
0447         return ret;
0448 
0449     if (pdata->power_suspend)
0450         pdata->power_suspend(pdev);
0451 
0452     return ret;
0453 }
0454 
0455 static int __maybe_unused ehci_platform_resume(struct device *dev)
0456 {
0457     struct usb_hcd *hcd = dev_get_drvdata(dev);
0458     struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
0459     struct platform_device *pdev = to_platform_device(dev);
0460     struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
0461     struct device *companion_dev;
0462 
0463     if (pdata->power_on) {
0464         int err = pdata->power_on(pdev);
0465         if (err < 0)
0466             return err;
0467     }
0468 
0469     companion_dev = usb_of_get_companion_dev(hcd->self.controller);
0470     if (companion_dev) {
0471         device_pm_wait_for_dev(hcd->self.controller, companion_dev);
0472         put_device(companion_dev);
0473     }
0474 
0475     ehci_resume(hcd, priv->reset_on_resume);
0476 
0477     pm_runtime_disable(dev);
0478     pm_runtime_set_active(dev);
0479     pm_runtime_enable(dev);
0480 
0481     if (priv->quirk_poll)
0482         quirk_poll_init(priv);
0483 
0484     return 0;
0485 }
0486 
0487 static const struct of_device_id vt8500_ehci_ids[] = {
0488     { .compatible = "via,vt8500-ehci", },
0489     { .compatible = "wm,prizm-ehci", },
0490     { .compatible = "generic-ehci", },
0491     { .compatible = "cavium,octeon-6335-ehci", },
0492     {}
0493 };
0494 MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
0495 
0496 #ifdef CONFIG_ACPI
0497 static const struct acpi_device_id ehci_acpi_match[] = {
0498     { "PNP0D20", 0 }, /* EHCI controller without debug */
0499     { }
0500 };
0501 MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
0502 #endif
0503 
0504 static const struct platform_device_id ehci_platform_table[] = {
0505     { "ehci-platform", 0 },
0506     { }
0507 };
0508 MODULE_DEVICE_TABLE(platform, ehci_platform_table);
0509 
0510 static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
0511     ehci_platform_resume);
0512 
0513 static struct platform_driver ehci_platform_driver = {
0514     .id_table   = ehci_platform_table,
0515     .probe      = ehci_platform_probe,
0516     .remove     = ehci_platform_remove,
0517     .shutdown   = usb_hcd_platform_shutdown,
0518     .driver     = {
0519         .name   = "ehci-platform",
0520         .pm = pm_ptr(&ehci_platform_pm_ops),
0521         .of_match_table = vt8500_ehci_ids,
0522         .acpi_match_table = ACPI_PTR(ehci_acpi_match),
0523         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
0524     }
0525 };
0526 
0527 static int __init ehci_platform_init(void)
0528 {
0529     if (usb_disabled())
0530         return -ENODEV;
0531 
0532     pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0533 
0534     ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
0535     return platform_driver_register(&ehci_platform_driver);
0536 }
0537 module_init(ehci_platform_init);
0538 
0539 static void __exit ehci_platform_cleanup(void)
0540 {
0541     platform_driver_unregister(&ehci_platform_driver);
0542 }
0543 module_exit(ehci_platform_cleanup);
0544 
0545 MODULE_DESCRIPTION(DRIVER_DESC);
0546 MODULE_AUTHOR("Hauke Mehrtens");
0547 MODULE_AUTHOR("Alan Stern");
0548 MODULE_LICENSE("GPL");