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 28 static int cdns3_idle_init(struct cdns3 *cdns); 29 30 static inline 31 struct 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 37 static 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 63 static 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 79 static void cdns3_exit_roles(struct cdns3 *cdns) 80 { 81 cdns3_role_stop(cdns); 82 cdns3_drd_exit(cdns); 83 } 84 85 static 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 */ 93 static 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_UNKNOWN: 170 case USB_DR_MODE_OTG: 171 ret = cdns3_hw_role_switch(cdns); 172 if (ret) 173 goto err; 174 break; 175 case USB_DR_MODE_PERIPHERAL: 176 ret = cdns3_role_start(cdns, USB_ROLE_DEVICE); 177 if (ret) 178 goto err; 179 break; 180 case USB_DR_MODE_HOST: 181 ret = cdns3_role_start(cdns, USB_ROLE_HOST); 182 if (ret) 183 goto err; 184 break; 185 } 186 187 return ret; 188 err: 189 cdns3_exit_roles(cdns); 190 return ret; 191 } 192 193 /** 194 * cdsn3_hw_role_state_machine - role switch state machine based on hw events. 195 * @cdns: Pointer to controller structure. 196 * 197 * Returns next role to be entered based on hw events. 198 */ 199 static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns) 200 { 201 enum usb_role role; 202 int id, vbus; 203 204 if (cdns->dr_mode != USB_DR_MODE_OTG) 205 goto not_otg; 206 207 id = cdns3_get_id(cdns); 208 vbus = cdns3_get_vbus(cdns); 209 210 /* 211 * Role change state machine 212 * Inputs: ID, VBUS 213 * Previous state: cdns->role 214 * Next state: role 215 */ 216 role = cdns->role; 217 218 switch (role) { 219 case USB_ROLE_NONE: 220 /* 221 * Driver treats USB_ROLE_NONE synonymous to IDLE state from 222 * controller specification. 223 */ 224 if (!id) 225 role = USB_ROLE_HOST; 226 else if (vbus) 227 role = USB_ROLE_DEVICE; 228 break; 229 case USB_ROLE_HOST: /* from HOST, we can only change to NONE */ 230 if (id) 231 role = USB_ROLE_NONE; 232 break; 233 case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/ 234 if (!vbus) 235 role = USB_ROLE_NONE; 236 break; 237 } 238 239 dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role); 240 241 return role; 242 243 not_otg: 244 if (cdns3_is_host(cdns)) 245 role = USB_ROLE_HOST; 246 if (cdns3_is_device(cdns)) 247 role = USB_ROLE_DEVICE; 248 249 return role; 250 } 251 252 static int cdns3_idle_role_start(struct cdns3 *cdns) 253 { 254 return 0; 255 } 256 257 static void cdns3_idle_role_stop(struct cdns3 *cdns) 258 { 259 /* Program Lane swap and bring PHY out of RESET */ 260 phy_reset(cdns->usb3_phy); 261 } 262 263 static int cdns3_idle_init(struct cdns3 *cdns) 264 { 265 struct cdns3_role_driver *rdrv; 266 267 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL); 268 if (!rdrv) 269 return -ENOMEM; 270 271 rdrv->start = cdns3_idle_role_start; 272 rdrv->stop = cdns3_idle_role_stop; 273 rdrv->state = CDNS3_ROLE_STATE_INACTIVE; 274 rdrv->suspend = NULL; 275 rdrv->resume = NULL; 276 rdrv->name = "idle"; 277 278 cdns->roles[USB_ROLE_NONE] = rdrv; 279 280 return 0; 281 } 282 283 /** 284 * cdns3_hw_role_switch - switch roles based on HW state 285 * @cdns3: controller 286 */ 287 int cdns3_hw_role_switch(struct cdns3 *cdns) 288 { 289 enum usb_role real_role, current_role; 290 int ret = 0; 291 292 /* Do nothing if role based on syfs. */ 293 if (cdns->role_override) 294 return 0; 295 296 pm_runtime_get_sync(cdns->dev); 297 298 current_role = cdns->role; 299 real_role = cdsn3_hw_role_state_machine(cdns); 300 301 /* Do nothing if nothing changed */ 302 if (current_role == real_role) 303 goto exit; 304 305 cdns3_role_stop(cdns); 306 307 dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role); 308 309 ret = cdns3_role_start(cdns, real_role); 310 if (ret) { 311 /* Back to current role */ 312 dev_err(cdns->dev, "set %d has failed, back to %d\n", 313 real_role, current_role); 314 ret = cdns3_role_start(cdns, current_role); 315 if (ret) 316 dev_err(cdns->dev, "back to %d failed too\n", 317 current_role); 318 } 319 exit: 320 pm_runtime_put_sync(cdns->dev); 321 return ret; 322 } 323 324 /** 325 * cdsn3_role_get - get current role of controller. 326 * 327 * @dev: Pointer to device structure 328 * 329 * Returns role 330 */ 331 static enum usb_role cdns3_role_get(struct device *dev) 332 { 333 struct cdns3 *cdns = dev_get_drvdata(dev); 334 335 return cdns->role; 336 } 337 338 /** 339 * cdns3_role_set - set current role of controller. 340 * 341 * @dev: pointer to device object 342 * @role - the previous role 343 * Handles below events: 344 * - Role switch for dual-role devices 345 * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices 346 */ 347 static int cdns3_role_set(struct device *dev, enum usb_role role) 348 { 349 struct cdns3 *cdns = dev_get_drvdata(dev); 350 int ret = 0; 351 352 pm_runtime_get_sync(cdns->dev); 353 354 /* 355 * FIXME: switch role framework should be extended to meet 356 * requirements. Driver assumes that role can be controlled 357 * by SW or HW. Temporary workaround is to use USB_ROLE_NONE to 358 * switch from SW to HW control. 359 * 360 * For dr_mode == USB_DR_MODE_OTG: 361 * if user sets USB_ROLE_HOST or USB_ROLE_DEVICE then driver 362 * sets role_override flag and forces that role. 363 * if user sets USB_ROLE_NONE, driver clears role_override and lets 364 * HW state machine take over. 365 * 366 * For dr_mode != USB_DR_MODE_OTG: 367 * Assumptions: 368 * 1. Restricted user control between NONE and dr_mode. 369 * 2. Driver doesn't need to rely on role_override flag. 370 * 3. Driver needs to ensure that HW state machine is never called 371 * if dr_mode != USB_DR_MODE_OTG. 372 */ 373 if (role == USB_ROLE_NONE) 374 cdns->role_override = 0; 375 else 376 cdns->role_override = 1; 377 378 /* 379 * HW state might have changed so driver need to trigger 380 * HW state machine if dr_mode == USB_DR_MODE_OTG. 381 */ 382 if (!cdns->role_override && cdns->dr_mode == USB_DR_MODE_OTG) { 383 cdns3_hw_role_switch(cdns); 384 goto pm_put; 385 } 386 387 if (cdns->role == role) 388 goto pm_put; 389 390 if (cdns->dr_mode == USB_DR_MODE_HOST) { 391 switch (role) { 392 case USB_ROLE_NONE: 393 case USB_ROLE_HOST: 394 break; 395 default: 396 ret = -EPERM; 397 goto pm_put; 398 } 399 } 400 401 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) { 402 switch (role) { 403 case USB_ROLE_NONE: 404 case USB_ROLE_DEVICE: 405 break; 406 default: 407 ret = -EPERM; 408 goto pm_put; 409 } 410 } 411 412 cdns3_role_stop(cdns); 413 ret = cdns3_role_start(cdns, role); 414 if (ret) { 415 dev_err(cdns->dev, "set role %d has failed\n", role); 416 ret = -EPERM; 417 } 418 419 pm_put: 420 pm_runtime_put_sync(cdns->dev); 421 return ret; 422 } 423 424 static const struct usb_role_switch_desc cdns3_switch_desc = { 425 .set = cdns3_role_set, 426 .get = cdns3_role_get, 427 .allow_userspace_control = true, 428 }; 429 430 /** 431 * cdns3_probe - probe for cdns3 core device 432 * @pdev: Pointer to cdns3 core platform device 433 * 434 * Returns 0 on success otherwise negative errno 435 */ 436 static int cdns3_probe(struct platform_device *pdev) 437 { 438 struct device *dev = &pdev->dev; 439 struct resource *res; 440 struct cdns3 *cdns; 441 void __iomem *regs; 442 int ret; 443 444 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); 445 if (ret) { 446 dev_err(dev, "error setting dma mask: %d\n", ret); 447 return -ENODEV; 448 } 449 450 cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL); 451 if (!cdns) 452 return -ENOMEM; 453 454 cdns->dev = dev; 455 456 platform_set_drvdata(pdev, cdns); 457 458 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host"); 459 if (!res) { 460 dev_err(dev, "missing host IRQ\n"); 461 return -ENODEV; 462 } 463 464 cdns->xhci_res[0] = *res; 465 466 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci"); 467 if (!res) { 468 dev_err(dev, "couldn't get xhci resource\n"); 469 return -ENXIO; 470 } 471 472 cdns->xhci_res[1] = *res; 473 474 cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral"); 475 if (cdns->dev_irq == -EPROBE_DEFER) 476 return cdns->dev_irq; 477 478 if (cdns->dev_irq < 0) 479 dev_err(dev, "couldn't get peripheral irq\n"); 480 481 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dev"); 482 regs = devm_ioremap_resource(dev, res); 483 if (IS_ERR(regs)) 484 return PTR_ERR(regs); 485 cdns->dev_regs = regs; 486 487 cdns->otg_irq = platform_get_irq_byname(pdev, "otg"); 488 if (cdns->otg_irq == -EPROBE_DEFER) 489 return cdns->otg_irq; 490 491 if (cdns->otg_irq < 0) { 492 dev_err(dev, "couldn't get otg irq\n"); 493 return cdns->otg_irq; 494 } 495 496 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg"); 497 if (!res) { 498 dev_err(dev, "couldn't get otg resource\n"); 499 return -ENXIO; 500 } 501 502 cdns->otg_res = *res; 503 504 mutex_init(&cdns->mutex); 505 506 cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy"); 507 if (IS_ERR(cdns->usb2_phy)) 508 return PTR_ERR(cdns->usb2_phy); 509 510 ret = phy_init(cdns->usb2_phy); 511 if (ret) 512 return ret; 513 514 cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy"); 515 if (IS_ERR(cdns->usb3_phy)) 516 return PTR_ERR(cdns->usb3_phy); 517 518 ret = phy_init(cdns->usb3_phy); 519 if (ret) 520 goto err1; 521 522 ret = phy_power_on(cdns->usb2_phy); 523 if (ret) 524 goto err2; 525 526 ret = phy_power_on(cdns->usb3_phy); 527 if (ret) 528 goto err3; 529 530 cdns->role_sw = usb_role_switch_register(dev, &cdns3_switch_desc); 531 if (IS_ERR(cdns->role_sw)) { 532 ret = PTR_ERR(cdns->role_sw); 533 dev_warn(dev, "Unable to register Role Switch\n"); 534 goto err4; 535 } 536 537 ret = cdns3_drd_init(cdns); 538 if (ret) 539 goto err5; 540 541 ret = cdns3_core_init_role(cdns); 542 if (ret) 543 goto err5; 544 545 device_set_wakeup_capable(dev, true); 546 pm_runtime_set_active(dev); 547 pm_runtime_enable(dev); 548 549 /* 550 * The controller needs less time between bus and controller suspend, 551 * and we also needs a small delay to avoid frequently entering low 552 * power mode. 553 */ 554 pm_runtime_set_autosuspend_delay(dev, 20); 555 pm_runtime_mark_last_busy(dev); 556 pm_runtime_use_autosuspend(dev); 557 dev_dbg(dev, "Cadence USB3 core: probe succeed\n"); 558 559 return 0; 560 err5: 561 cdns3_drd_exit(cdns); 562 usb_role_switch_unregister(cdns->role_sw); 563 err4: 564 phy_power_off(cdns->usb3_phy); 565 566 err3: 567 phy_power_off(cdns->usb2_phy); 568 err2: 569 phy_exit(cdns->usb3_phy); 570 err1: 571 phy_exit(cdns->usb2_phy); 572 573 return ret; 574 } 575 576 /** 577 * cdns3_remove - unbind drd driver and clean up 578 * @pdev: Pointer to Linux platform device 579 * 580 * Returns 0 on success otherwise negative errno 581 */ 582 static int cdns3_remove(struct platform_device *pdev) 583 { 584 struct cdns3 *cdns = platform_get_drvdata(pdev); 585 586 pm_runtime_get_sync(&pdev->dev); 587 pm_runtime_disable(&pdev->dev); 588 pm_runtime_put_noidle(&pdev->dev); 589 cdns3_exit_roles(cdns); 590 usb_role_switch_unregister(cdns->role_sw); 591 phy_power_off(cdns->usb2_phy); 592 phy_power_off(cdns->usb3_phy); 593 phy_exit(cdns->usb2_phy); 594 phy_exit(cdns->usb3_phy); 595 return 0; 596 } 597 598 #ifdef CONFIG_PM_SLEEP 599 600 static int cdns3_suspend(struct device *dev) 601 { 602 struct cdns3 *cdns = dev_get_drvdata(dev); 603 unsigned long flags; 604 605 if (cdns->role == USB_ROLE_HOST) 606 return 0; 607 608 if (pm_runtime_status_suspended(dev)) 609 pm_runtime_resume(dev); 610 611 if (cdns->roles[cdns->role]->suspend) { 612 spin_lock_irqsave(&cdns->gadget_dev->lock, flags); 613 cdns->roles[cdns->role]->suspend(cdns, false); 614 spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags); 615 } 616 617 return 0; 618 } 619 620 static int cdns3_resume(struct device *dev) 621 { 622 struct cdns3 *cdns = dev_get_drvdata(dev); 623 unsigned long flags; 624 625 if (cdns->role == USB_ROLE_HOST) 626 return 0; 627 628 if (cdns->roles[cdns->role]->resume) { 629 spin_lock_irqsave(&cdns->gadget_dev->lock, flags); 630 cdns->roles[cdns->role]->resume(cdns, false); 631 spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags); 632 } 633 634 pm_runtime_disable(dev); 635 pm_runtime_set_active(dev); 636 pm_runtime_enable(dev); 637 638 return 0; 639 } 640 #endif 641 642 static const struct dev_pm_ops cdns3_pm_ops = { 643 SET_SYSTEM_SLEEP_PM_OPS(cdns3_suspend, cdns3_resume) 644 }; 645 646 #ifdef CONFIG_OF 647 static const struct of_device_id of_cdns3_match[] = { 648 { .compatible = "cdns,usb3" }, 649 { }, 650 }; 651 MODULE_DEVICE_TABLE(of, of_cdns3_match); 652 #endif 653 654 static struct platform_driver cdns3_driver = { 655 .probe = cdns3_probe, 656 .remove = cdns3_remove, 657 .driver = { 658 .name = "cdns-usb3", 659 .of_match_table = of_match_ptr(of_cdns3_match), 660 .pm = &cdns3_pm_ops, 661 }, 662 }; 663 664 module_platform_driver(cdns3_driver); 665 666 MODULE_ALIAS("platform:cdns3"); 667 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>"); 668 MODULE_LICENSE("GPL v2"); 669 MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver"); 670