bdc_udc.c (15609B)
1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * bdc_udc.c - BRCM BDC USB3.0 device controller gagdet ops 4 * 5 * Copyright (C) 2014 Broadcom Corporation 6 * 7 * Author: Ashwini Pahuja 8 * 9 * Based on drivers under drivers/usb/gadget/udc/ 10 */ 11#include <linux/module.h> 12#include <linux/pci.h> 13#include <linux/dma-mapping.h> 14#include <linux/kernel.h> 15#include <linux/delay.h> 16#include <linux/ioport.h> 17#include <linux/sched.h> 18#include <linux/slab.h> 19#include <linux/errno.h> 20#include <linux/init.h> 21#include <linux/timer.h> 22#include <linux/list.h> 23#include <linux/interrupt.h> 24#include <linux/moduleparam.h> 25#include <linux/device.h> 26#include <linux/usb/ch9.h> 27#include <linux/usb/gadget.h> 28#include <linux/usb/otg.h> 29#include <linux/pm.h> 30#include <linux/io.h> 31#include <linux/irq.h> 32#include <asm/unaligned.h> 33#include <linux/platform_device.h> 34 35#include "bdc.h" 36#include "bdc_ep.h" 37#include "bdc_cmd.h" 38#include "bdc_dbg.h" 39 40static const struct usb_gadget_ops bdc_gadget_ops; 41 42static const char * const conn_speed_str[] = { 43 "Not connected", 44 "Full Speed", 45 "Low Speed", 46 "High Speed", 47 "Super Speed", 48}; 49 50/* EP0 initial descripror */ 51static struct usb_endpoint_descriptor bdc_gadget_ep0_desc = { 52 .bLength = USB_DT_ENDPOINT_SIZE, 53 .bDescriptorType = USB_DT_ENDPOINT, 54 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 55 .bEndpointAddress = 0, 56 .wMaxPacketSize = cpu_to_le16(EP0_MAX_PKT_SIZE), 57}; 58 59/* Advance the srr dqp maintained by SW */ 60static void srr_dqp_index_advc(struct bdc *bdc, u32 srr_num) 61{ 62 struct srr *srr; 63 64 srr = &bdc->srr; 65 dev_dbg_ratelimited(bdc->dev, "srr->dqp_index:%d\n", srr->dqp_index); 66 srr->dqp_index++; 67 /* rollback to 0 if we are past the last */ 68 if (srr->dqp_index == NUM_SR_ENTRIES) 69 srr->dqp_index = 0; 70} 71 72/* connect sr */ 73static void bdc_uspc_connected(struct bdc *bdc) 74{ 75 u32 speed, temp; 76 u32 usppms; 77 int ret; 78 79 temp = bdc_readl(bdc->regs, BDC_USPC); 80 speed = BDC_PSP(temp); 81 dev_dbg(bdc->dev, "%s speed=%x\n", __func__, speed); 82 switch (speed) { 83 case BDC_SPEED_SS: 84 bdc_gadget_ep0_desc.wMaxPacketSize = 85 cpu_to_le16(EP0_MAX_PKT_SIZE); 86 bdc->gadget.ep0->maxpacket = EP0_MAX_PKT_SIZE; 87 bdc->gadget.speed = USB_SPEED_SUPER; 88 /* Enable U1T in SS mode */ 89 usppms = bdc_readl(bdc->regs, BDC_USPPMS); 90 usppms &= ~BDC_U1T(0xff); 91 usppms |= BDC_U1T(U1_TIMEOUT); 92 usppms |= BDC_PORT_W1S; 93 bdc_writel(bdc->regs, BDC_USPPMS, usppms); 94 break; 95 96 case BDC_SPEED_HS: 97 bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 98 bdc->gadget.ep0->maxpacket = 64; 99 bdc->gadget.speed = USB_SPEED_HIGH; 100 break; 101 102 case BDC_SPEED_FS: 103 bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 104 bdc->gadget.ep0->maxpacket = 64; 105 bdc->gadget.speed = USB_SPEED_FULL; 106 break; 107 108 case BDC_SPEED_LS: 109 bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); 110 bdc->gadget.ep0->maxpacket = 8; 111 bdc->gadget.speed = USB_SPEED_LOW; 112 break; 113 default: 114 dev_err(bdc->dev, "UNDEFINED SPEED\n"); 115 return; 116 } 117 dev_dbg(bdc->dev, "connected at %s\n", conn_speed_str[speed]); 118 /* Now we know the speed, configure ep0 */ 119 bdc->bdc_ep_array[1]->desc = &bdc_gadget_ep0_desc; 120 ret = bdc_config_ep(bdc, bdc->bdc_ep_array[1]); 121 if (ret) 122 dev_err(bdc->dev, "EP0 config failed\n"); 123 bdc->bdc_ep_array[1]->usb_ep.desc = &bdc_gadget_ep0_desc; 124 bdc->bdc_ep_array[1]->flags |= BDC_EP_ENABLED; 125 usb_gadget_set_state(&bdc->gadget, USB_STATE_DEFAULT); 126} 127 128/* device got disconnected */ 129static void bdc_uspc_disconnected(struct bdc *bdc, bool reinit) 130{ 131 struct bdc_ep *ep; 132 133 dev_dbg(bdc->dev, "%s\n", __func__); 134 /* 135 * Only stop ep0 from here, rest of the endpoints will be disabled 136 * from gadget_disconnect 137 */ 138 ep = bdc->bdc_ep_array[1]; 139 if (ep && (ep->flags & BDC_EP_ENABLED)) 140 /* if enabled then stop and remove requests */ 141 bdc_ep_disable(ep); 142 143 if (bdc->gadget_driver && bdc->gadget_driver->disconnect) { 144 spin_unlock(&bdc->lock); 145 bdc->gadget_driver->disconnect(&bdc->gadget); 146 spin_lock(&bdc->lock); 147 } 148 /* Set Unknown speed */ 149 bdc->gadget.speed = USB_SPEED_UNKNOWN; 150 bdc->devstatus &= DEVSTATUS_CLEAR; 151 bdc->delayed_status = false; 152 bdc->reinit = reinit; 153 bdc->test_mode = false; 154} 155 156/* TNotify wkaeup timer */ 157static void bdc_func_wake_timer(struct work_struct *work) 158{ 159 struct bdc *bdc = container_of(work, struct bdc, func_wake_notify.work); 160 unsigned long flags; 161 162 dev_dbg(bdc->dev, "%s\n", __func__); 163 spin_lock_irqsave(&bdc->lock, flags); 164 /* 165 * Check if host has started transferring on endpoints 166 * FUNC_WAKE_ISSUED is cleared when transfer has started after resume 167 */ 168 if (bdc->devstatus & FUNC_WAKE_ISSUED) { 169 dev_dbg(bdc->dev, "FUNC_WAKE_ISSUED FLAG IS STILL SET\n"); 170 /* flag is still set, so again send func wake */ 171 bdc_function_wake_fh(bdc, 0); 172 schedule_delayed_work(&bdc->func_wake_notify, 173 msecs_to_jiffies(BDC_TNOTIFY)); 174 } 175 spin_unlock_irqrestore(&bdc->lock, flags); 176} 177 178/* handler for Link state change condition */ 179static void handle_link_state_change(struct bdc *bdc, u32 uspc) 180{ 181 u32 link_state; 182 183 dev_dbg(bdc->dev, "Link state change"); 184 link_state = BDC_PST(uspc); 185 switch (link_state) { 186 case BDC_LINK_STATE_U3: 187 if ((bdc->gadget.speed != USB_SPEED_UNKNOWN) && 188 bdc->gadget_driver->suspend) { 189 dev_dbg(bdc->dev, "Entered Suspend mode\n"); 190 spin_unlock(&bdc->lock); 191 bdc->devstatus |= DEVICE_SUSPENDED; 192 bdc->gadget_driver->suspend(&bdc->gadget); 193 spin_lock(&bdc->lock); 194 } 195 break; 196 case BDC_LINK_STATE_U0: 197 if (bdc->devstatus & REMOTE_WAKEUP_ISSUED) { 198 bdc->devstatus &= ~REMOTE_WAKEUP_ISSUED; 199 if (bdc->gadget.speed == USB_SPEED_SUPER) { 200 bdc_function_wake_fh(bdc, 0); 201 bdc->devstatus |= FUNC_WAKE_ISSUED; 202 /* 203 * Start a Notification timer and check if the 204 * Host transferred anything on any of the EPs, 205 * if not then send function wake again every 206 * TNotification secs until host initiates 207 * transfer to BDC, USB3 spec Table 8.13 208 */ 209 schedule_delayed_work( 210 &bdc->func_wake_notify, 211 msecs_to_jiffies(BDC_TNOTIFY)); 212 dev_dbg(bdc->dev, "sched func_wake_notify\n"); 213 } 214 } 215 break; 216 217 case BDC_LINK_STATE_RESUME: 218 dev_dbg(bdc->dev, "Resumed from Suspend\n"); 219 if (bdc->devstatus & DEVICE_SUSPENDED) { 220 bdc->gadget_driver->resume(&bdc->gadget); 221 bdc->devstatus &= ~DEVICE_SUSPENDED; 222 } 223 break; 224 default: 225 dev_dbg(bdc->dev, "link state:%d\n", link_state); 226 } 227} 228 229/* something changes on upstream port, handle it here */ 230void bdc_sr_uspc(struct bdc *bdc, struct bdc_sr *sreport) 231{ 232 u32 clear_flags = 0; 233 u32 uspc; 234 bool connected = false; 235 bool disconn = false; 236 237 uspc = bdc_readl(bdc->regs, BDC_USPC); 238 dev_dbg(bdc->dev, "%s uspc=0x%08x\n", __func__, uspc); 239 240 /* Port connect changed */ 241 if (uspc & BDC_PCC) { 242 /* Vbus not present, and not connected to Downstream port */ 243 if ((uspc & BDC_VBC) && !(uspc & BDC_VBS) && !(uspc & BDC_PCS)) 244 disconn = true; 245 else if ((uspc & BDC_PCS) && !BDC_PST(uspc)) 246 connected = true; 247 clear_flags |= BDC_PCC; 248 } 249 250 /* Change in VBus and VBus is present */ 251 if ((uspc & BDC_VBC) && (uspc & BDC_VBS)) { 252 if (bdc->pullup) { 253 dev_dbg(bdc->dev, "Do a softconnect\n"); 254 /* Attached state, do a softconnect */ 255 bdc_softconn(bdc); 256 usb_gadget_set_state(&bdc->gadget, USB_STATE_POWERED); 257 } 258 clear_flags |= BDC_VBC; 259 } else if ((uspc & BDC_PRS) || (uspc & BDC_PRC) || disconn) { 260 /* Hot reset, warm reset, 2.0 bus reset or disconn */ 261 dev_dbg(bdc->dev, "Port reset or disconn\n"); 262 bdc_uspc_disconnected(bdc, disconn); 263 clear_flags |= BDC_PRC; 264 } else if ((uspc & BDC_PSC) && (uspc & BDC_PCS)) { 265 /* Change in Link state */ 266 handle_link_state_change(bdc, uspc); 267 clear_flags |= BDC_PSC; 268 } 269 270 /* 271 * In SS we might not have PRC bit set before connection, but in 2.0 272 * the PRC bit is set before connection, so moving this condition out 273 * of bus reset to handle both SS/2.0 speeds. 274 */ 275 if (connected) { 276 /* This is the connect event for U0/L0 */ 277 dev_dbg(bdc->dev, "Connected\n"); 278 bdc_uspc_connected(bdc); 279 bdc->devstatus &= ~(DEVICE_SUSPENDED); 280 } 281 uspc = bdc_readl(bdc->regs, BDC_USPC); 282 uspc &= (~BDC_USPSC_RW); 283 dev_dbg(bdc->dev, "uspc=%x\n", uspc); 284 bdc_writel(bdc->regs, BDC_USPC, clear_flags); 285} 286 287/* Main interrupt handler for bdc */ 288static irqreturn_t bdc_udc_interrupt(int irq, void *_bdc) 289{ 290 u32 eqp_index, dqp_index, sr_type, srr_int; 291 struct bdc_sr *sreport; 292 struct bdc *bdc = _bdc; 293 u32 status; 294 int ret; 295 296 spin_lock(&bdc->lock); 297 status = bdc_readl(bdc->regs, BDC_BDCSC); 298 if (!(status & BDC_GIP)) { 299 spin_unlock(&bdc->lock); 300 return IRQ_NONE; 301 } 302 srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0)); 303 /* Check if the SRR IP bit it set? */ 304 if (!(srr_int & BDC_SRR_IP)) { 305 dev_warn(bdc->dev, "Global irq pending but SRR IP is 0\n"); 306 spin_unlock(&bdc->lock); 307 return IRQ_NONE; 308 } 309 eqp_index = BDC_SRR_EPI(srr_int); 310 dqp_index = BDC_SRR_DPI(srr_int); 311 dev_dbg(bdc->dev, 312 "%s eqp_index=%d dqp_index=%d srr.dqp_index=%d\n\n", 313 __func__, eqp_index, dqp_index, bdc->srr.dqp_index); 314 315 /* check for ring empty condition */ 316 if (eqp_index == dqp_index) { 317 dev_dbg(bdc->dev, "SRR empty?\n"); 318 spin_unlock(&bdc->lock); 319 return IRQ_HANDLED; 320 } 321 322 while (bdc->srr.dqp_index != eqp_index) { 323 sreport = &bdc->srr.sr_bds[bdc->srr.dqp_index]; 324 /* sreport is read before using it */ 325 rmb(); 326 sr_type = le32_to_cpu(sreport->offset[3]) & BD_TYPE_BITMASK; 327 dev_dbg_ratelimited(bdc->dev, "sr_type=%d\n", sr_type); 328 switch (sr_type) { 329 case SR_XSF: 330 bdc->sr_handler[0](bdc, sreport); 331 break; 332 333 case SR_USPC: 334 bdc->sr_handler[1](bdc, sreport); 335 break; 336 default: 337 dev_warn(bdc->dev, "SR:%d not handled\n", sr_type); 338 } 339 /* Advance the srr dqp index */ 340 srr_dqp_index_advc(bdc, 0); 341 } 342 /* update the hw dequeue pointer */ 343 srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0)); 344 srr_int &= ~BDC_SRR_DPI_MASK; 345 srr_int &= ~(BDC_SRR_RWS|BDC_SRR_RST|BDC_SRR_ISR); 346 srr_int |= ((bdc->srr.dqp_index) << 16); 347 srr_int |= BDC_SRR_IP; 348 bdc_writel(bdc->regs, BDC_SRRINT(0), srr_int); 349 srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0)); 350 if (bdc->reinit) { 351 ret = bdc_reinit(bdc); 352 if (ret) 353 dev_err(bdc->dev, "err in bdc reinit\n"); 354 } 355 356 spin_unlock(&bdc->lock); 357 358 return IRQ_HANDLED; 359} 360 361/* Gadget ops */ 362static int bdc_udc_start(struct usb_gadget *gadget, 363 struct usb_gadget_driver *driver) 364{ 365 struct bdc *bdc = gadget_to_bdc(gadget); 366 unsigned long flags; 367 int ret = 0; 368 369 dev_dbg(bdc->dev, "%s()\n", __func__); 370 spin_lock_irqsave(&bdc->lock, flags); 371 if (bdc->gadget_driver) { 372 dev_err(bdc->dev, "%s is already bound to %s\n", 373 bdc->gadget.name, 374 bdc->gadget_driver->driver.name); 375 ret = -EBUSY; 376 goto err; 377 } 378 /* 379 * Run the controller from here and when BDC is connected to 380 * Host then driver will receive a USPC SR with VBUS present 381 * and then driver will do a softconnect. 382 */ 383 ret = bdc_run(bdc); 384 if (ret) { 385 dev_err(bdc->dev, "%s bdc run fail\n", __func__); 386 goto err; 387 } 388 bdc->gadget_driver = driver; 389 bdc->gadget.dev.driver = &driver->driver; 390err: 391 spin_unlock_irqrestore(&bdc->lock, flags); 392 393 return ret; 394} 395 396static int bdc_udc_stop(struct usb_gadget *gadget) 397{ 398 struct bdc *bdc = gadget_to_bdc(gadget); 399 unsigned long flags; 400 401 dev_dbg(bdc->dev, "%s()\n", __func__); 402 spin_lock_irqsave(&bdc->lock, flags); 403 bdc_stop(bdc); 404 bdc->gadget_driver = NULL; 405 bdc->gadget.dev.driver = NULL; 406 spin_unlock_irqrestore(&bdc->lock, flags); 407 408 return 0; 409} 410 411static int bdc_udc_pullup(struct usb_gadget *gadget, int is_on) 412{ 413 struct bdc *bdc = gadget_to_bdc(gadget); 414 unsigned long flags; 415 u32 uspc; 416 417 dev_dbg(bdc->dev, "%s() is_on:%d\n", __func__, is_on); 418 if (!gadget) 419 return -EINVAL; 420 421 spin_lock_irqsave(&bdc->lock, flags); 422 if (!is_on) { 423 bdc_softdisconn(bdc); 424 bdc->pullup = false; 425 } else { 426 /* 427 * For a self powered device, we need to wait till we receive 428 * a VBUS change and Vbus present event, then if pullup flag 429 * is set, then only we present the Termintation. 430 */ 431 bdc->pullup = true; 432 /* 433 * Check if BDC is already connected to Host i.e Vbus=1, 434 * if yes, then present TERM now, this is typical for bus 435 * powered devices. 436 */ 437 uspc = bdc_readl(bdc->regs, BDC_USPC); 438 if (uspc & BDC_VBS) 439 bdc_softconn(bdc); 440 } 441 spin_unlock_irqrestore(&bdc->lock, flags); 442 443 return 0; 444} 445 446static int bdc_udc_set_selfpowered(struct usb_gadget *gadget, 447 int is_self) 448{ 449 struct bdc *bdc = gadget_to_bdc(gadget); 450 unsigned long flags; 451 452 dev_dbg(bdc->dev, "%s()\n", __func__); 453 gadget->is_selfpowered = (is_self != 0); 454 spin_lock_irqsave(&bdc->lock, flags); 455 if (!is_self) 456 bdc->devstatus |= 1 << USB_DEVICE_SELF_POWERED; 457 else 458 bdc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED); 459 460 spin_unlock_irqrestore(&bdc->lock, flags); 461 462 return 0; 463} 464 465static int bdc_udc_wakeup(struct usb_gadget *gadget) 466{ 467 struct bdc *bdc = gadget_to_bdc(gadget); 468 unsigned long flags; 469 u8 link_state; 470 u32 uspc; 471 int ret = 0; 472 473 dev_dbg(bdc->dev, 474 "%s() bdc->devstatus=%08x\n", 475 __func__, bdc->devstatus); 476 477 if (!(bdc->devstatus & REMOTE_WAKE_ENABLE)) 478 return -EOPNOTSUPP; 479 480 spin_lock_irqsave(&bdc->lock, flags); 481 uspc = bdc_readl(bdc->regs, BDC_USPC); 482 link_state = BDC_PST(uspc); 483 dev_dbg(bdc->dev, "link_state =%d portsc=%x", link_state, uspc); 484 if (link_state != BDC_LINK_STATE_U3) { 485 dev_warn(bdc->dev, 486 "can't wakeup from link state %d\n", 487 link_state); 488 ret = -EINVAL; 489 goto out; 490 } 491 if (bdc->gadget.speed == USB_SPEED_SUPER) 492 bdc->devstatus |= REMOTE_WAKEUP_ISSUED; 493 494 uspc &= ~BDC_PST_MASK; 495 uspc &= (~BDC_USPSC_RW); 496 uspc |= BDC_PST(BDC_LINK_STATE_U0); 497 uspc |= BDC_SWS; 498 bdc_writel(bdc->regs, BDC_USPC, uspc); 499 uspc = bdc_readl(bdc->regs, BDC_USPC); 500 link_state = BDC_PST(uspc); 501 dev_dbg(bdc->dev, "link_state =%d portsc=%x", link_state, uspc); 502out: 503 spin_unlock_irqrestore(&bdc->lock, flags); 504 505 return ret; 506} 507 508static const struct usb_gadget_ops bdc_gadget_ops = { 509 .wakeup = bdc_udc_wakeup, 510 .set_selfpowered = bdc_udc_set_selfpowered, 511 .pullup = bdc_udc_pullup, 512 .udc_start = bdc_udc_start, 513 .udc_stop = bdc_udc_stop, 514}; 515 516/* Init the gadget interface and register the udc */ 517int bdc_udc_init(struct bdc *bdc) 518{ 519 u32 temp; 520 int ret; 521 522 dev_dbg(bdc->dev, "%s()\n", __func__); 523 bdc->gadget.ops = &bdc_gadget_ops; 524 bdc->gadget.max_speed = USB_SPEED_SUPER; 525 bdc->gadget.speed = USB_SPEED_UNKNOWN; 526 bdc->gadget.dev.parent = bdc->dev; 527 528 bdc->gadget.sg_supported = false; 529 530 531 bdc->gadget.name = BRCM_BDC_NAME; 532 ret = devm_request_irq(bdc->dev, bdc->irq, bdc_udc_interrupt, 533 IRQF_SHARED, BRCM_BDC_NAME, bdc); 534 if (ret) { 535 dev_err(bdc->dev, 536 "failed to request irq #%d %d\n", 537 bdc->irq, ret); 538 return ret; 539 } 540 541 ret = bdc_init_ep(bdc); 542 if (ret) { 543 dev_err(bdc->dev, "bdc init ep fail: %d\n", ret); 544 return ret; 545 } 546 547 ret = usb_add_gadget_udc(bdc->dev, &bdc->gadget); 548 if (ret) { 549 dev_err(bdc->dev, "failed to register udc\n"); 550 goto err0; 551 } 552 usb_gadget_set_state(&bdc->gadget, USB_STATE_NOTATTACHED); 553 bdc->bdc_ep_array[1]->desc = &bdc_gadget_ep0_desc; 554 /* 555 * Allocate bd list for ep0 only, ep0 will be enabled on connect 556 * status report when the speed is known 557 */ 558 ret = bdc_ep_enable(bdc->bdc_ep_array[1]); 559 if (ret) { 560 dev_err(bdc->dev, "fail to enable %s\n", 561 bdc->bdc_ep_array[1]->name); 562 goto err1; 563 } 564 INIT_DELAYED_WORK(&bdc->func_wake_notify, bdc_func_wake_timer); 565 /* Enable Interrupts */ 566 temp = bdc_readl(bdc->regs, BDC_BDCSC); 567 temp |= BDC_GIE; 568 bdc_writel(bdc->regs, BDC_BDCSC, temp); 569 return 0; 570err1: 571 usb_del_gadget_udc(&bdc->gadget); 572err0: 573 bdc_free_ep(bdc); 574 575 return ret; 576} 577 578void bdc_udc_exit(struct bdc *bdc) 579{ 580 unsigned long flags; 581 582 dev_dbg(bdc->dev, "%s()\n", __func__); 583 spin_lock_irqsave(&bdc->lock, flags); 584 bdc_ep_disable(bdc->bdc_ep_array[1]); 585 spin_unlock_irqrestore(&bdc->lock, flags); 586 587 usb_del_gadget_udc(&bdc->gadget); 588 bdc_free_ep(bdc); 589}