linux/drivers/usb/host/xhci-plat.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * xhci-plat.c - xHCI host controller driver platform Bus Glue.
   4 *
   5 * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com
   6 * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
   7 *
   8 * A lot of code borrowed from the Linux xHCI driver.
   9 */
  10
  11#include <linux/clk.h>
  12#include <linux/dma-mapping.h>
  13#include <linux/module.h>
  14#include <linux/pci.h>
  15#include <linux/of.h>
  16#include <linux/of_device.h>
  17#include <linux/platform_device.h>
  18#include <linux/usb/phy.h>
  19#include <linux/slab.h>
  20#include <linux/acpi.h>
  21#include <linux/usb/of.h>
  22#include <linux/usb/otg.h>
  23
  24#include "xhci.h"
  25#include "xhci-plat.h"
  26#include "xhci-mvebu.h"
  27#include "xhci-rcar.h"
  28
  29static struct hc_driver __read_mostly xhci_plat_hc_driver;
  30
  31static int xhci_plat_setup(struct usb_hcd *hcd);
  32static int xhci_plat_start(struct usb_hcd *hcd);
  33
  34static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
  35        .extra_priv_size = sizeof(struct xhci_plat_priv),
  36        .reset = xhci_plat_setup,
  37        .start = xhci_plat_start,
  38};
  39
  40static void xhci_priv_plat_start(struct usb_hcd *hcd)
  41{
  42        struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  43
  44        if (priv->plat_start)
  45                priv->plat_start(hcd);
  46}
  47
  48static int xhci_priv_init_quirk(struct usb_hcd *hcd)
  49{
  50        struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  51
  52        if (!priv->init_quirk)
  53                return 0;
  54
  55        return priv->init_quirk(hcd);
  56}
  57
  58static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
  59{
  60        struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  61
  62        if (!priv->suspend_quirk)
  63                return 0;
  64
  65        return priv->suspend_quirk(hcd);
  66}
  67
  68static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
  69{
  70        struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  71
  72        if (!priv->resume_quirk)
  73                return 0;
  74
  75        return priv->resume_quirk(hcd);
  76}
  77
  78static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  79{
  80        struct xhci_plat_priv *priv = xhci_to_priv(xhci);
  81
  82        /*
  83         * As of now platform drivers don't provide MSI support so we ensure
  84         * here that the generic code does not try to make a pci_dev from our
  85         * dev struct in order to setup MSI
  86         */
  87        xhci->quirks |= XHCI_PLAT | priv->quirks;
  88}
  89
  90/* called during probe() after chip reset completes */
  91static int xhci_plat_setup(struct usb_hcd *hcd)
  92{
  93        int ret;
  94
  95
  96        ret = xhci_priv_init_quirk(hcd);
  97        if (ret)
  98                return ret;
  99
 100        return xhci_gen_setup(hcd, xhci_plat_quirks);
 101}
 102
 103static int xhci_plat_start(struct usb_hcd *hcd)
 104{
 105        xhci_priv_plat_start(hcd);
 106        return xhci_run(hcd);
 107}
 108
 109#ifdef CONFIG_OF
 110static const struct xhci_plat_priv xhci_plat_marvell_armada = {
 111        .init_quirk = xhci_mvebu_mbus_init_quirk,
 112};
 113
 114static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
 115        .init_quirk = xhci_mvebu_a3700_init_quirk,
 116};
 117
 118static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
 119        SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
 120};
 121
 122static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
 123        SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
 124};
 125
 126static const struct xhci_plat_priv xhci_plat_brcm = {
 127        .quirks = XHCI_RESET_ON_RESUME,
 128};
 129
 130static const struct of_device_id usb_xhci_of_match[] = {
 131        {
 132                .compatible = "generic-xhci",
 133        }, {
 134                .compatible = "xhci-platform",
 135        }, {
 136                .compatible = "marvell,armada-375-xhci",
 137                .data = &xhci_plat_marvell_armada,
 138        }, {
 139                .compatible = "marvell,armada-380-xhci",
 140                .data = &xhci_plat_marvell_armada,
 141        }, {
 142                .compatible = "marvell,armada3700-xhci",
 143                .data = &xhci_plat_marvell_armada3700,
 144        }, {
 145                .compatible = "renesas,xhci-r8a7790",
 146                .data = &xhci_plat_renesas_rcar_gen2,
 147        }, {
 148                .compatible = "renesas,xhci-r8a7791",
 149                .data = &xhci_plat_renesas_rcar_gen2,
 150        }, {
 151                .compatible = "renesas,xhci-r8a7793",
 152                .data = &xhci_plat_renesas_rcar_gen2,
 153        }, {
 154                .compatible = "renesas,xhci-r8a7795",
 155                .data = &xhci_plat_renesas_rcar_gen3,
 156        }, {
 157                .compatible = "renesas,xhci-r8a7796",
 158                .data = &xhci_plat_renesas_rcar_gen3,
 159        }, {
 160                .compatible = "renesas,rcar-gen2-xhci",
 161                .data = &xhci_plat_renesas_rcar_gen2,
 162        }, {
 163                .compatible = "renesas,rcar-gen3-xhci",
 164                .data = &xhci_plat_renesas_rcar_gen3,
 165        }, {
 166                .compatible = "brcm,xhci-brcm-v2",
 167                .data = &xhci_plat_brcm,
 168        }, {
 169                .compatible = "brcm,bcm7445-xhci",
 170                .data = &xhci_plat_brcm,
 171        },
 172        {},
 173};
 174MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
 175#endif
 176
 177static int usb_otg_set_host(struct device *dev, struct usb_hcd *hcd, bool yes)
 178{
 179        int ret = 0;
 180
 181        hcd->usb_phy = usb_get_phy(USB_PHY_TYPE_USB3);
 182        if (!IS_ERR_OR_NULL(hcd->usb_phy) && hcd->usb_phy->otg) {
 183                if (yes) {
 184                        if (otg_set_host(hcd->usb_phy->otg, &hcd->self)) {
 185                                usb_put_phy(hcd->usb_phy);
 186                                goto disable_phy;
 187                        }
 188                } else {
 189                        ret = otg_set_host(hcd->usb_phy->otg, NULL);
 190                        usb_put_phy(hcd->usb_phy);
 191                        goto disable_phy;
 192                }
 193
 194        } else {
 195                goto disable_phy;
 196        }
 197
 198        return 0;
 199
 200disable_phy:
 201        hcd->usb_phy = NULL;
 202
 203        return ret;
 204}
 205
 206static int xhci_plat_probe(struct platform_device *pdev)
 207{
 208        const struct xhci_plat_priv *priv_match;
 209        const struct hc_driver  *driver;
 210        struct device           *sysdev, *tmpdev;
 211        struct xhci_hcd         *xhci;
 212        struct resource         *res;
 213        struct usb_hcd          *hcd;
 214        int                     ret;
 215        int                     irq;
 216        struct xhci_plat_priv   *priv = NULL;
 217
 218
 219        if (usb_disabled())
 220                return -ENODEV;
 221
 222        driver = &xhci_plat_hc_driver;
 223
 224        irq = platform_get_irq(pdev, 0);
 225        if (irq < 0)
 226                return irq;
 227
 228        /*
 229         * sysdev must point to a device that is known to the system firmware
 230         * or PCI hardware. We handle these three cases here:
 231         * 1. xhci_plat comes from firmware
 232         * 2. xhci_plat is child of a device from firmware (dwc3-plat)
 233         * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
 234         */
 235        for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
 236                if (is_of_node(sysdev->fwnode) ||
 237                        is_acpi_device_node(sysdev->fwnode))
 238                        break;
 239#ifdef CONFIG_PCI
 240                else if (sysdev->bus == &pci_bus_type)
 241                        break;
 242#endif
 243        }
 244
 245        if (!sysdev)
 246                sysdev = &pdev->dev;
 247
 248        /* Try to set 64-bit DMA first */
 249        if (WARN_ON(!sysdev->dma_mask))
 250                /* Platform did not initialize dma_mask */
 251                ret = dma_coerce_mask_and_coherent(sysdev,
 252                                                   DMA_BIT_MASK(64));
 253        else
 254                ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
 255
 256        /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
 257        if (ret) {
 258                ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
 259                if (ret)
 260                        return ret;
 261        }
 262
 263        /* Set the controller as wakeup capable */
 264        device_set_wakeup_capable(&pdev->dev, true);
 265
 266        pm_runtime_set_active(&pdev->dev);
 267        pm_runtime_enable(&pdev->dev);
 268        pm_runtime_get_noresume(&pdev->dev);
 269
 270        hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
 271                               dev_name(&pdev->dev), NULL);
 272        if (!hcd) {
 273                ret = -ENOMEM;
 274                goto disable_runtime;
 275        }
 276
 277        hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
 278        if (IS_ERR(hcd->regs)) {
 279                ret = PTR_ERR(hcd->regs);
 280                goto put_hcd;
 281        }
 282
 283        hcd->rsrc_start = res->start;
 284        hcd->rsrc_len = resource_size(res);
 285
 286        xhci = hcd_to_xhci(hcd);
 287
 288        /*
 289         * Not all platforms have clks so it is not an error if the
 290         * clock do not exist.
 291         */
 292        xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
 293        if (IS_ERR(xhci->reg_clk)) {
 294                ret = PTR_ERR(xhci->reg_clk);
 295                goto put_hcd;
 296        }
 297
 298        ret = clk_prepare_enable(xhci->reg_clk);
 299        if (ret)
 300                goto put_hcd;
 301
 302        xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
 303        if (IS_ERR(xhci->clk)) {
 304                ret = PTR_ERR(xhci->clk);
 305                goto disable_reg_clk;
 306        }
 307
 308        ret = clk_prepare_enable(xhci->clk);
 309        if (ret)
 310                goto disable_reg_clk;
 311
 312        if (pdev->dev.of_node)
 313                priv_match = of_device_get_match_data(&pdev->dev);
 314        else
 315                priv_match = dev_get_platdata(&pdev->dev);
 316
 317        if (priv_match) {
 318                priv = hcd_to_xhci_priv(hcd);
 319                /* Just copy data for now */
 320                *priv = *priv_match;
 321        }
 322
 323        device_set_wakeup_capable(&pdev->dev, true);
 324
 325        xhci->main_hcd = hcd;
 326        xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
 327                        dev_name(&pdev->dev), hcd);
 328        if (!xhci->shared_hcd) {
 329                ret = -ENOMEM;
 330                goto disable_clk;
 331        }
 332
 333        /* imod_interval is the interrupt moderation value in nanoseconds. */
 334        xhci->imod_interval = 40000;
 335
 336        /* Iterate over all parent nodes for finding quirks */
 337        for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
 338
 339                if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
 340                        xhci->quirks |= XHCI_HW_LPM_DISABLE;
 341
 342                if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
 343                        xhci->quirks |= XHCI_LPM_SUPPORT;
 344
 345                if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
 346                        xhci->quirks |= XHCI_BROKEN_PORT_PED;
 347
 348                device_property_read_u32(tmpdev, "imod-interval-ns",
 349                                         &xhci->imod_interval);
 350        }
 351
 352        hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
 353        if (IS_ERR(hcd->usb_phy)) {
 354                ret = PTR_ERR(hcd->usb_phy);
 355                if (ret == -EPROBE_DEFER)
 356                        goto put_usb3_hcd;
 357                hcd->usb_phy = NULL;
 358        } else {
 359                ret = usb_phy_init(hcd->usb_phy);
 360                if (ret)
 361                        goto put_usb3_hcd;
 362        }
 363
 364        hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
 365        xhci->shared_hcd->tpl_support = hcd->tpl_support;
 366        if (priv && (priv->quirks & XHCI_SKIP_PHY_INIT))
 367                hcd->skip_phy_initialization = 1;
 368
 369        ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
 370        if (ret)
 371                goto disable_usb_phy;
 372
 373        if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
 374                xhci->shared_hcd->can_do_streams = 1;
 375
 376        ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
 377        if (ret)
 378                goto dealloc_usb2_hcd;
 379
 380        ret = usb_otg_set_host(&pdev->dev, hcd, true);
 381        if (ret)
 382                goto dealloc_usb2_hcd;
 383
 384        device_enable_async_suspend(&pdev->dev);
 385        pm_runtime_put_noidle(&pdev->dev);
 386
 387        /*
 388         * Prevent runtime pm from being on as default, users should enable
 389         * runtime pm using power/control in sysfs.
 390         */
 391        pm_runtime_forbid(&pdev->dev);
 392
 393        return 0;
 394
 395
 396dealloc_usb2_hcd:
 397        usb_remove_hcd(hcd);
 398
 399disable_usb_phy:
 400        usb_phy_shutdown(hcd->usb_phy);
 401
 402put_usb3_hcd:
 403        usb_put_hcd(xhci->shared_hcd);
 404
 405disable_clk:
 406        clk_disable_unprepare(xhci->clk);
 407
 408disable_reg_clk:
 409        clk_disable_unprepare(xhci->reg_clk);
 410
 411put_hcd:
 412        usb_put_hcd(hcd);
 413
 414disable_runtime:
 415        pm_runtime_put_noidle(&pdev->dev);
 416        pm_runtime_disable(&pdev->dev);
 417
 418        return ret;
 419}
 420
 421static int xhci_plat_remove(struct platform_device *dev)
 422{
 423        struct usb_hcd  *hcd = platform_get_drvdata(dev);
 424        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 425        struct clk *clk = xhci->clk;
 426        struct clk *reg_clk = xhci->reg_clk;
 427        struct usb_hcd *shared_hcd = xhci->shared_hcd;
 428
 429        pm_runtime_get_sync(&dev->dev);
 430        xhci->xhc_state |= XHCI_STATE_REMOVING;
 431
 432        usb_remove_hcd(shared_hcd);
 433        xhci->shared_hcd = NULL;
 434        usb_phy_shutdown(hcd->usb_phy);
 435
 436        usb_otg_set_host(&dev->dev, hcd, false);
 437
 438        usb_remove_hcd(hcd);
 439        usb_put_hcd(shared_hcd);
 440
 441        clk_disable_unprepare(clk);
 442        clk_disable_unprepare(reg_clk);
 443        usb_put_hcd(hcd);
 444
 445        pm_runtime_disable(&dev->dev);
 446        pm_runtime_put_noidle(&dev->dev);
 447        pm_runtime_set_suspended(&dev->dev);
 448
 449        return 0;
 450}
 451
 452static int __maybe_unused xhci_plat_suspend(struct device *dev)
 453{
 454        struct usb_hcd  *hcd = dev_get_drvdata(dev);
 455        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 456        int ret;
 457
 458        ret = xhci_priv_suspend_quirk(hcd);
 459        if (ret)
 460                return ret;
 461        /*
 462         * xhci_suspend() needs `do_wakeup` to know whether host is allowed
 463         * to do wakeup during suspend.
 464         */
 465        return xhci_suspend(xhci, device_may_wakeup(dev));
 466}
 467
 468static int __maybe_unused xhci_plat_resume(struct device *dev)
 469{
 470        struct usb_hcd  *hcd = dev_get_drvdata(dev);
 471        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 472        int ret;
 473
 474        ret = xhci_priv_resume_quirk(hcd);
 475        if (ret)
 476                return ret;
 477
 478        ret = xhci_resume(xhci, 0);
 479        if (ret)
 480                return ret;
 481
 482        pm_runtime_disable(dev);
 483        pm_runtime_set_active(dev);
 484        pm_runtime_enable(dev);
 485
 486        return 0;
 487}
 488
 489static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
 490{
 491        struct usb_hcd  *hcd = dev_get_drvdata(dev);
 492        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 493        int ret;
 494
 495        ret = xhci_priv_suspend_quirk(hcd);
 496        if (ret)
 497                return ret;
 498
 499        return xhci_suspend(xhci, true);
 500}
 501
 502static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
 503{
 504        struct usb_hcd  *hcd = dev_get_drvdata(dev);
 505        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 506
 507        if (device_may_wakeup(&hcd->self.root_hub->dev))
 508                disable_irq_wake(hcd->irq);
 509
 510        return xhci_resume(xhci, 0);
 511}
 512
 513static const struct dev_pm_ops xhci_plat_pm_ops = {
 514        SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
 515
 516        SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
 517                           xhci_plat_runtime_resume,
 518                           NULL)
 519};
 520
 521#ifdef CONFIG_ACPI
 522static const struct acpi_device_id usb_xhci_acpi_match[] = {
 523        /* XHCI-compliant USB Controller */
 524        { "PNP0D10", },
 525        { }
 526};
 527MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
 528#endif
 529
 530static struct platform_driver usb_xhci_driver = {
 531        .probe  = xhci_plat_probe,
 532        .remove = xhci_plat_remove,
 533        .shutdown = usb_hcd_platform_shutdown,
 534        .driver = {
 535                .name = "xhci-hcd",
 536                .pm = &xhci_plat_pm_ops,
 537                .of_match_table = of_match_ptr(usb_xhci_of_match),
 538                .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
 539        },
 540};
 541MODULE_ALIAS("platform:xhci-hcd");
 542
 543static int __init xhci_plat_init(void)
 544{
 545        xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
 546        return platform_driver_register(&usb_xhci_driver);
 547}
 548module_init(xhci_plat_init);
 549
 550static void __exit xhci_plat_exit(void)
 551{
 552        platform_driver_unregister(&usb_xhci_driver);
 553}
 554module_exit(xhci_plat_exit);
 555
 556MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
 557MODULE_LICENSE("GPL");
 558