linux/drivers/usb/cdns3/core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Cadence USBSS DRD Driver.
   4 *
   5 * Copyright (C) 2018-2019 Cadence.
   6 * Copyright (C) 2017-2018 NXP
   7 * Copyright (C) 2019 Texas Instruments
   8 *
   9 * Author: Peter Chen <peter.chen@nxp.com>
  10 *         Pawel Laszczak <pawell@cadence.com>
  11 *         Roger Quadros <rogerq@ti.com>
  12 */
  13
  14#include <linux/dma-mapping.h>
  15#include <linux/module.h>
  16#include <linux/kernel.h>
  17#include <linux/platform_device.h>
  18#include <linux/interrupt.h>
  19#include <linux/io.h>
  20#include <linux/pm_runtime.h>
  21
  22#include "gadget.h"
  23#include "core.h"
  24#include "host-export.h"
  25#include "gadget-export.h"
  26#include "drd.h"
  27
  28static int cdns3_idle_init(struct cdns3 *cdns);
  29
  30static inline
  31struct cdns3_role_driver *cdns3_get_current_role_driver(struct cdns3 *cdns)
  32{
  33        WARN_ON(!cdns->roles[cdns->role]);
  34        return cdns->roles[cdns->role];
  35}
  36
  37static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
  38{
  39        int ret;
  40
  41        if (WARN_ON(role > USB_ROLE_DEVICE))
  42                return 0;
  43
  44        mutex_lock(&cdns->mutex);
  45        cdns->role = role;
  46        mutex_unlock(&cdns->mutex);
  47
  48        if (!cdns->roles[role])
  49                return -ENXIO;
  50
  51        if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
  52                return 0;
  53
  54        mutex_lock(&cdns->mutex);
  55        ret = cdns->roles[role]->start(cdns);
  56        if (!ret)
  57                cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
  58        mutex_unlock(&cdns->mutex);
  59
  60        return ret;
  61}
  62
  63static void cdns3_role_stop(struct cdns3 *cdns)
  64{
  65        enum usb_role role = cdns->role;
  66
  67        if (WARN_ON(role > USB_ROLE_DEVICE))
  68                return;
  69
  70        if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
  71                return;
  72
  73        mutex_lock(&cdns->mutex);
  74        cdns->roles[role]->stop(cdns);
  75        cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
  76        mutex_unlock(&cdns->mutex);
  77}
  78
  79static void cdns3_exit_roles(struct cdns3 *cdns)
  80{
  81        cdns3_role_stop(cdns);
  82        cdns3_drd_exit(cdns);
  83}
  84
  85static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns);
  86
  87/**
  88 * cdns3_core_init_role - initialize role of operation
  89 * @cdns: Pointer to cdns3 structure
  90 *
  91 * Returns 0 on success otherwise negative errno
  92 */
  93static int cdns3_core_init_role(struct cdns3 *cdns)
  94{
  95        struct device *dev = cdns->dev;
  96        enum usb_dr_mode best_dr_mode;
  97        enum usb_dr_mode dr_mode;
  98        int ret = 0;
  99
 100        dr_mode = usb_get_dr_mode(dev);
 101        cdns->role = USB_ROLE_NONE;
 102
 103        /*
 104         * If driver can't read mode by means of usb_get_dr_mode function then
 105         * chooses mode according with Kernel configuration. This setting
 106         * can be restricted later depending on strap pin configuration.
 107         */
 108        if (dr_mode == USB_DR_MODE_UNKNOWN) {
 109                if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
 110                    IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
 111                        dr_mode = USB_DR_MODE_OTG;
 112                else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
 113                        dr_mode = USB_DR_MODE_HOST;
 114                else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
 115                        dr_mode = USB_DR_MODE_PERIPHERAL;
 116        }
 117
 118        /*
 119         * At this point cdns->dr_mode contains strap configuration.
 120         * Driver try update this setting considering kernel configuration
 121         */
 122        best_dr_mode = cdns->dr_mode;
 123
 124        ret = cdns3_idle_init(cdns);
 125        if (ret)
 126                return ret;
 127
 128        if (dr_mode == USB_DR_MODE_OTG) {
 129                best_dr_mode = cdns->dr_mode;
 130        } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
 131                best_dr_mode = dr_mode;
 132        } else if (cdns->dr_mode != dr_mode) {
 133                dev_err(dev, "Incorrect DRD configuration\n");
 134                return -EINVAL;
 135        }
 136
 137        dr_mode = best_dr_mode;
 138
 139        if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
 140                ret = cdns3_host_init(cdns);
 141                if (ret) {
 142                        dev_err(dev, "Host initialization failed with %d\n",
 143                                ret);
 144                        goto err;
 145                }
 146        }
 147
 148        if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
 149                ret = cdns3_gadget_init(cdns);
 150                if (ret) {
 151                        dev_err(dev, "Device initialization failed with %d\n",
 152                                ret);
 153                        goto err;
 154                }
 155        }
 156
 157        cdns->dr_mode = dr_mode;
 158
 159        ret = cdns3_drd_update_mode(cdns);
 160        if (ret)
 161                goto err;
 162
 163        /* Initialize idle role to start with */
 164        ret = cdns3_role_start(cdns, USB_ROLE_NONE);
 165        if (ret)
 166                goto err;
 167
 168        switch (cdns->dr_mode) {
 169        case USB_DR_MODE_OTG:
 170                ret = cdns3_hw_role_switch(cdns);
 171                if (ret)
 172                        goto err;
 173                break;
 174        case USB_DR_MODE_PERIPHERAL:
 175                ret = cdns3_role_start(cdns, USB_ROLE_DEVICE);
 176                if (ret)
 177                        goto err;
 178                break;
 179        case USB_DR_MODE_HOST:
 180                ret = cdns3_role_start(cdns, USB_ROLE_HOST);
 181                if (ret)
 182                        goto err;
 183                break;
 184        default:
 185                ret = -EINVAL;
 186                goto err;
 187        }
 188
 189        return ret;
 190err:
 191        cdns3_exit_roles(cdns);
 192        return ret;
 193}
 194
 195/**
 196 * cdsn3_hw_role_state_machine  - role switch state machine based on hw events.
 197 * @cdns: Pointer to controller structure.
 198 *
 199 * Returns next role to be entered based on hw events.
 200 */
 201static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns)
 202{
 203        enum usb_role role;
 204        int id, vbus;
 205
 206        if (cdns->dr_mode != USB_DR_MODE_OTG)
 207                goto not_otg;
 208
 209        id = cdns3_get_id(cdns);
 210        vbus = cdns3_get_vbus(cdns);
 211
 212        /*
 213         * Role change state machine
 214         * Inputs: ID, VBUS
 215         * Previous state: cdns->role
 216         * Next state: role
 217         */
 218        role = cdns->role;
 219
 220        switch (role) {
 221        case USB_ROLE_NONE:
 222                /*
 223                 * Driver treats USB_ROLE_NONE synonymous to IDLE state from
 224                 * controller specification.
 225                 */
 226                if (!id)
 227                        role = USB_ROLE_HOST;
 228                else if (vbus)
 229                        role = USB_ROLE_DEVICE;
 230                break;
 231        case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
 232                if (id)
 233                        role = USB_ROLE_NONE;
 234                break;
 235        case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
 236                if (!vbus)
 237                        role = USB_ROLE_NONE;
 238                break;
 239        }
 240
 241        dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
 242
 243        return role;
 244
 245not_otg:
 246        if (cdns3_is_host(cdns))
 247                role = USB_ROLE_HOST;
 248        if (cdns3_is_device(cdns))
 249                role = USB_ROLE_DEVICE;
 250
 251        return role;
 252}
 253
 254static int cdns3_idle_role_start(struct cdns3 *cdns)
 255{
 256        return 0;
 257}
 258
 259static void cdns3_idle_role_stop(struct cdns3 *cdns)
 260{
 261        /* Program Lane swap and bring PHY out of RESET */
 262        phy_reset(cdns->usb3_phy);
 263}
 264
 265static int cdns3_idle_init(struct cdns3 *cdns)
 266{
 267        struct cdns3_role_driver *rdrv;
 268
 269        rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
 270        if (!rdrv)
 271                return -ENOMEM;
 272
 273        rdrv->start = cdns3_idle_role_start;
 274        rdrv->stop = cdns3_idle_role_stop;
 275        rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
 276        rdrv->suspend = NULL;
 277        rdrv->resume = NULL;
 278        rdrv->name = "idle";
 279
 280        cdns->roles[USB_ROLE_NONE] = rdrv;
 281
 282        return 0;
 283}
 284
 285/**
 286 * cdns3_hw_role_switch - switch roles based on HW state
 287 * @cdns3: controller
 288 */
 289int cdns3_hw_role_switch(struct cdns3 *cdns)
 290{
 291        enum usb_role real_role, current_role;
 292        int ret = 0;
 293
 294        /* Do nothing if role based on syfs. */
 295        if (cdns->role_override)
 296                return 0;
 297
 298        pm_runtime_get_sync(cdns->dev);
 299
 300        current_role = cdns->role;
 301        real_role = cdsn3_hw_role_state_machine(cdns);
 302
 303        /* Do nothing if nothing changed */
 304        if (current_role == real_role)
 305                goto exit;
 306
 307        cdns3_role_stop(cdns);
 308
 309        dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
 310
 311        ret = cdns3_role_start(cdns, real_role);
 312        if (ret) {
 313                /* Back to current role */
 314                dev_err(cdns->dev, "set %d has failed, back to %d\n",
 315                        real_role, current_role);
 316                ret = cdns3_role_start(cdns, current_role);
 317                if (ret)
 318                        dev_err(cdns->dev, "back to %d failed too\n",
 319                                current_role);
 320        }
 321exit:
 322        pm_runtime_put_sync(cdns->dev);
 323        return ret;
 324}
 325
 326/**
 327 * cdsn3_role_get - get current role of controller.
 328 *
 329 * @dev: Pointer to device structure
 330 *
 331 * Returns role
 332 */
 333static enum usb_role cdns3_role_get(struct usb_role_switch *sw)
 334{
 335        struct cdns3 *cdns = usb_role_switch_get_drvdata(sw);
 336
 337        return cdns->role;
 338}
 339
 340/**
 341 * cdns3_role_set - set current role of controller.
 342 *
 343 * @dev: pointer to device object
 344 * @role - the previous role
 345 * Handles below events:
 346 * - Role switch for dual-role devices
 347 * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices
 348 */
 349static int cdns3_role_set(struct usb_role_switch *sw, enum usb_role role)
 350{
 351        struct cdns3 *cdns = usb_role_switch_get_drvdata(sw);
 352        int ret = 0;
 353
 354        pm_runtime_get_sync(cdns->dev);
 355
 356        /*
 357         * FIXME: switch role framework should be extended to meet
 358         * requirements. Driver assumes that role can be controlled
 359         * by SW or HW. Temporary workaround is to use USB_ROLE_NONE to
 360         * switch from SW to HW control.
 361         *
 362         * For dr_mode == USB_DR_MODE_OTG:
 363         *      if user sets USB_ROLE_HOST or USB_ROLE_DEVICE then driver
 364         *      sets role_override flag and forces that role.
 365         *      if user sets USB_ROLE_NONE, driver clears role_override and lets
 366         *      HW state machine take over.
 367         *
 368         * For dr_mode != USB_DR_MODE_OTG:
 369         *      Assumptions:
 370         *      1. Restricted user control between NONE and dr_mode.
 371         *      2. Driver doesn't need to rely on role_override flag.
 372         *      3. Driver needs to ensure that HW state machine is never called
 373         *         if dr_mode != USB_DR_MODE_OTG.
 374         */
 375        if (role == USB_ROLE_NONE)
 376                cdns->role_override = 0;
 377        else
 378                cdns->role_override = 1;
 379
 380        /*
 381         * HW state might have changed so driver need to trigger
 382         * HW state machine if dr_mode == USB_DR_MODE_OTG.
 383         */
 384        if (!cdns->role_override && cdns->dr_mode == USB_DR_MODE_OTG) {
 385                cdns3_hw_role_switch(cdns);
 386                goto pm_put;
 387        }
 388
 389        if (cdns->role == role)
 390                goto pm_put;
 391
 392        if (cdns->dr_mode == USB_DR_MODE_HOST) {
 393                switch (role) {
 394                case USB_ROLE_NONE:
 395                case USB_ROLE_HOST:
 396                        break;
 397                default:
 398                        ret = -EPERM;
 399                        goto pm_put;
 400                }
 401        }
 402
 403        if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) {
 404                switch (role) {
 405                case USB_ROLE_NONE:
 406                case USB_ROLE_DEVICE:
 407                        break;
 408                default:
 409                        ret = -EPERM;
 410                        goto pm_put;
 411                }
 412        }
 413
 414        cdns3_role_stop(cdns);
 415        ret = cdns3_role_start(cdns, role);
 416        if (ret) {
 417                dev_err(cdns->dev, "set role %d has failed\n", role);
 418                ret = -EPERM;
 419        }
 420
 421pm_put:
 422        pm_runtime_put_sync(cdns->dev);
 423        return ret;
 424}
 425
 426/**
 427 * cdns3_probe - probe for cdns3 core device
 428 * @pdev: Pointer to cdns3 core platform device
 429 *
 430 * Returns 0 on success otherwise negative errno
 431 */
 432static int cdns3_probe(struct platform_device *pdev)
 433{
 434        struct usb_role_switch_desc sw_desc = { };
 435        struct device *dev = &pdev->dev;
 436        struct resource *res;
 437        struct cdns3 *cdns;
 438        void __iomem *regs;
 439        int ret;
 440
 441        ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
 442        if (ret) {
 443                dev_err(dev, "error setting dma mask: %d\n", ret);
 444                return -ENODEV;
 445        }
 446
 447        cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
 448        if (!cdns)
 449                return -ENOMEM;
 450
 451        cdns->dev = dev;
 452
 453        platform_set_drvdata(pdev, cdns);
 454
 455        res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
 456        if (!res) {
 457                dev_err(dev, "missing host IRQ\n");
 458                return -ENODEV;
 459        }
 460
 461        cdns->xhci_res[0] = *res;
 462
 463        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
 464        if (!res) {
 465                dev_err(dev, "couldn't get xhci resource\n");
 466                return -ENXIO;
 467        }
 468
 469        cdns->xhci_res[1] = *res;
 470
 471        cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
 472        if (cdns->dev_irq == -EPROBE_DEFER)
 473                return cdns->dev_irq;
 474
 475        if (cdns->dev_irq < 0)
 476                dev_err(dev, "couldn't get peripheral irq\n");
 477
 478        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dev");
 479        regs = devm_ioremap_resource(dev, res);
 480        if (IS_ERR(regs))
 481                return PTR_ERR(regs);
 482        cdns->dev_regs  = regs;
 483
 484        cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
 485        if (cdns->otg_irq == -EPROBE_DEFER)
 486                return cdns->otg_irq;
 487
 488        if (cdns->otg_irq < 0) {
 489                dev_err(dev, "couldn't get otg irq\n");
 490                return cdns->otg_irq;
 491        }
 492
 493        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
 494        if (!res) {
 495                dev_err(dev, "couldn't get otg resource\n");
 496                return -ENXIO;
 497        }
 498
 499        cdns->otg_res = *res;
 500
 501        mutex_init(&cdns->mutex);
 502
 503        cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
 504        if (IS_ERR(cdns->usb2_phy))
 505                return PTR_ERR(cdns->usb2_phy);
 506
 507        ret = phy_init(cdns->usb2_phy);
 508        if (ret)
 509                return ret;
 510
 511        cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
 512        if (IS_ERR(cdns->usb3_phy))
 513                return PTR_ERR(cdns->usb3_phy);
 514
 515        ret = phy_init(cdns->usb3_phy);
 516        if (ret)
 517                goto err1;
 518
 519        ret = phy_power_on(cdns->usb2_phy);
 520        if (ret)
 521                goto err2;
 522
 523        ret = phy_power_on(cdns->usb3_phy);
 524        if (ret)
 525                goto err3;
 526
 527        sw_desc.set = cdns3_role_set;
 528        sw_desc.get = cdns3_role_get;
 529        sw_desc.allow_userspace_control = true;
 530        sw_desc.driver_data = cdns;
 531
 532        cdns->role_sw = usb_role_switch_register(dev, &sw_desc);
 533        if (IS_ERR(cdns->role_sw)) {
 534                ret = PTR_ERR(cdns->role_sw);
 535                dev_warn(dev, "Unable to register Role Switch\n");
 536                goto err4;
 537        }
 538
 539        ret = cdns3_drd_init(cdns);
 540        if (ret)
 541                goto err5;
 542
 543        ret = cdns3_core_init_role(cdns);
 544        if (ret)
 545                goto err5;
 546
 547        device_set_wakeup_capable(dev, true);
 548        pm_runtime_set_active(dev);
 549        pm_runtime_enable(dev);
 550
 551        /*
 552         * The controller needs less time between bus and controller suspend,
 553         * and we also needs a small delay to avoid frequently entering low
 554         * power mode.
 555         */
 556        pm_runtime_set_autosuspend_delay(dev, 20);
 557        pm_runtime_mark_last_busy(dev);
 558        pm_runtime_use_autosuspend(dev);
 559        dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
 560
 561        return 0;
 562err5:
 563        cdns3_drd_exit(cdns);
 564        usb_role_switch_unregister(cdns->role_sw);
 565err4:
 566        phy_power_off(cdns->usb3_phy);
 567
 568err3:
 569        phy_power_off(cdns->usb2_phy);
 570err2:
 571        phy_exit(cdns->usb3_phy);
 572err1:
 573        phy_exit(cdns->usb2_phy);
 574
 575        return ret;
 576}
 577
 578/**
 579 * cdns3_remove - unbind drd driver and clean up
 580 * @pdev: Pointer to Linux platform device
 581 *
 582 * Returns 0 on success otherwise negative errno
 583 */
 584static int cdns3_remove(struct platform_device *pdev)
 585{
 586        struct cdns3 *cdns = platform_get_drvdata(pdev);
 587
 588        pm_runtime_get_sync(&pdev->dev);
 589        pm_runtime_disable(&pdev->dev);
 590        pm_runtime_put_noidle(&pdev->dev);
 591        cdns3_exit_roles(cdns);
 592        usb_role_switch_unregister(cdns->role_sw);
 593        phy_power_off(cdns->usb2_phy);
 594        phy_power_off(cdns->usb3_phy);
 595        phy_exit(cdns->usb2_phy);
 596        phy_exit(cdns->usb3_phy);
 597        return 0;
 598}
 599
 600#ifdef CONFIG_PM_SLEEP
 601
 602static int cdns3_suspend(struct device *dev)
 603{
 604        struct cdns3 *cdns = dev_get_drvdata(dev);
 605        unsigned long flags;
 606
 607        if (cdns->role == USB_ROLE_HOST)
 608                return 0;
 609
 610        if (pm_runtime_status_suspended(dev))
 611                pm_runtime_resume(dev);
 612
 613        if (cdns->roles[cdns->role]->suspend) {
 614                spin_lock_irqsave(&cdns->gadget_dev->lock, flags);
 615                cdns->roles[cdns->role]->suspend(cdns, false);
 616                spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags);
 617        }
 618
 619        return 0;
 620}
 621
 622static int cdns3_resume(struct device *dev)
 623{
 624        struct cdns3 *cdns = dev_get_drvdata(dev);
 625        unsigned long flags;
 626
 627        if (cdns->role == USB_ROLE_HOST)
 628                return 0;
 629
 630        if (cdns->roles[cdns->role]->resume) {
 631                spin_lock_irqsave(&cdns->gadget_dev->lock, flags);
 632                cdns->roles[cdns->role]->resume(cdns, false);
 633                spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags);
 634        }
 635
 636        pm_runtime_disable(dev);
 637        pm_runtime_set_active(dev);
 638        pm_runtime_enable(dev);
 639
 640        return 0;
 641}
 642#endif
 643
 644static const struct dev_pm_ops cdns3_pm_ops = {
 645        SET_SYSTEM_SLEEP_PM_OPS(cdns3_suspend, cdns3_resume)
 646};
 647
 648#ifdef CONFIG_OF
 649static const struct of_device_id of_cdns3_match[] = {
 650        { .compatible = "cdns,usb3" },
 651        { },
 652};
 653MODULE_DEVICE_TABLE(of, of_cdns3_match);
 654#endif
 655
 656static struct platform_driver cdns3_driver = {
 657        .probe          = cdns3_probe,
 658        .remove         = cdns3_remove,
 659        .driver         = {
 660                .name   = "cdns-usb3",
 661                .of_match_table = of_match_ptr(of_cdns3_match),
 662                .pm     = &cdns3_pm_ops,
 663        },
 664};
 665
 666module_platform_driver(cdns3_driver);
 667
 668MODULE_ALIAS("platform:cdns3");
 669MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
 670MODULE_LICENSE("GPL v2");
 671MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");
 672