1 /* $OpenBSD: dwc2_hcd.c,v 1.26 2021/11/28 09:25:02 mglocker Exp $ */ 2 /* $NetBSD: dwc2_hcd.c,v 1.15 2014/11/24 10:14:14 skrll Exp $ */ 3 4 /* 5 * hcd.c - DesignWare HS OTG Controller host-mode routines 6 * 7 * Copyright (C) 2004-2013 Synopsys, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The names of the above-listed copyright holders may not be used 19 * to endorse or promote products derived from this software without 20 * specific prior written permission. 21 * 22 * ALTERNATIVELY, this software may be distributed under the terms of the 23 * GNU General Public License ("GPL") as published by the Free Software 24 * Foundation; either version 2 of the License, or (at your option) any 25 * later version. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * This file contains the core HCD code, and implements the Linux hc_driver 42 * API 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/malloc.h> 48 #include <sys/signal.h> 49 #include <sys/proc.h> 50 #include <sys/pool.h> 51 #include <sys/task.h> 52 53 #include <machine/bus.h> 54 55 #include <dev/usb/usb.h> 56 #include <dev/usb/usbdi.h> 57 #include <dev/usb/usbdivar.h> 58 #include <dev/usb/usb_mem.h> 59 60 #include <dev/usb/dwc2/dwc2.h> 61 #include <dev/usb/dwc2/dwc2var.h> 62 63 #include <dev/usb/dwc2/dwc2_core.h> 64 #include <dev/usb/dwc2/dwc2_hcd.h> 65 66 /** 67 * dwc2_dump_channel_info() - Prints the state of a host channel 68 * 69 * @hsotg: Programming view of DWC_otg controller 70 * @chan: Pointer to the channel to dump 71 * 72 * Must be called with interrupt disabled and spinlock held 73 * 74 * NOTE: This function will be removed once the peripheral controller code 75 * is integrated and the driver is stable 76 */ 77 #ifdef VERBOSE_DEBUG 78 STATIC void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg, 79 struct dwc2_host_chan *chan) 80 { 81 int num_channels = hsotg->core_params->host_channels; 82 struct dwc2_qh *qh; 83 u32 hcchar; 84 u32 hcsplt; 85 u32 hctsiz; 86 u32 hc_dma; 87 int i; 88 89 if (chan == NULL) 90 return; 91 92 hcchar = DWC2_READ_4(hsotg, HCCHAR(chan->hc_num)); 93 hcsplt = DWC2_READ_4(hsotg, HCSPLT(chan->hc_num)); 94 hctsiz = DWC2_READ_4(hsotg, HCTSIZ(chan->hc_num)); 95 hc_dma = DWC2_READ_4(hsotg, HCDMA(chan->hc_num)); 96 97 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan); 98 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n", 99 hcchar, hcsplt); 100 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n", 101 hctsiz, hc_dma); 102 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n", 103 chan->dev_addr, chan->ep_num, chan->ep_is_in); 104 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type); 105 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet); 106 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start); 107 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started); 108 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status); 109 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf); 110 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n", 111 (unsigned long)chan->xfer_dma); 112 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len); 113 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh); 114 dev_dbg(hsotg->dev, " NP inactive sched:\n"); 115 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive, 116 qh_list_entry) 117 dev_dbg(hsotg->dev, " %p\n", qh); 118 dev_dbg(hsotg->dev, " NP waiting sched:\n"); 119 list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting, 120 qh_list_entry) 121 dev_dbg(hsotg->dev, " %p\n", qh); 122 dev_dbg(hsotg->dev, " NP active sched:\n"); 123 list_for_each_entry(qh, &hsotg->non_periodic_sched_active, 124 qh_list_entry) 125 dev_dbg(hsotg->dev, " %p\n", qh); 126 dev_dbg(hsotg->dev, " Channels:\n"); 127 for (i = 0; i < num_channels; i++) { 128 struct dwc2_host_chan *ch = hsotg->hc_ptr_array[i]; 129 130 dev_dbg(hsotg->dev, " %2d: %p\n", i, ch); 131 } 132 } 133 #endif /* VERBOSE_DEBUG */ 134 135 /* 136 * Processes all the URBs in a single list of QHs. Completes them with 137 * -ETIMEDOUT and frees the QTD. 138 * 139 * Must be called with interrupt disabled and spinlock held 140 */ 141 STATIC void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg, 142 struct list_head *qh_list) 143 { 144 struct dwc2_qh *qh, *qh_tmp; 145 struct dwc2_qtd *qtd, *qtd_tmp; 146 147 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) { 148 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, 149 qtd_list_entry) { 150 dwc2_host_complete(hsotg, qtd, -ECONNRESET); 151 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh); 152 } 153 } 154 } 155 156 STATIC void dwc2_qh_list_free(struct dwc2_hsotg *hsotg, 157 struct list_head *qh_list) 158 { 159 struct dwc2_qtd *qtd, *qtd_tmp; 160 struct dwc2_qh *qh, *qh_tmp; 161 unsigned long flags; 162 163 if (!qh_list->next) 164 /* The list hasn't been initialized yet */ 165 return; 166 167 spin_lock_irqsave(&hsotg->lock, flags); 168 169 /* Ensure there are no QTDs or URBs left */ 170 dwc2_kill_urbs_in_qh_list(hsotg, qh_list); 171 172 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) { 173 dwc2_hcd_qh_unlink(hsotg, qh); 174 175 /* Free each QTD in the QH's QTD list */ 176 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, 177 qtd_list_entry) 178 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh); 179 180 spin_unlock_irqrestore(&hsotg->lock, flags); 181 dwc2_hcd_qh_free(hsotg, qh); 182 spin_lock_irqsave(&hsotg->lock, flags); 183 } 184 185 spin_unlock_irqrestore(&hsotg->lock, flags); 186 } 187 188 /* 189 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic 190 * and periodic schedules. The QTD associated with each URB is removed from 191 * the schedule and freed. This function may be called when a disconnect is 192 * detected or when the HCD is being stopped. 193 * 194 * Must be called with interrupt disabled and spinlock held 195 */ 196 STATIC void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg) 197 { 198 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive); 199 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting); 200 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active); 201 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive); 202 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready); 203 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned); 204 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued); 205 } 206 207 /** 208 * dwc2_hcd_start() - Starts the HCD when switching to Host mode 209 * 210 * @hsotg: Pointer to struct dwc2_hsotg 211 */ 212 void dwc2_hcd_start(struct dwc2_hsotg *hsotg) 213 { 214 u32 hprt0; 215 216 if (hsotg->op_state == OTG_STATE_B_HOST) { 217 /* 218 * Reset the port. During a HNP mode switch the reset 219 * needs to occur within 1ms and have a duration of at 220 * least 50ms. 221 */ 222 hprt0 = dwc2_read_hprt0(hsotg); 223 hprt0 |= HPRT0_RST; 224 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 225 } 226 227 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work, 228 msecs_to_jiffies(50)); 229 } 230 231 /* Must be called with interrupt disabled and spinlock held */ 232 STATIC void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg) 233 { 234 int num_channels = hsotg->core_params->host_channels; 235 struct dwc2_host_chan *channel; 236 u32 hcchar; 237 int i; 238 239 if (hsotg->core_params->dma_enable <= 0) { 240 /* Flush out any channel requests in slave mode */ 241 for (i = 0; i < num_channels; i++) { 242 channel = hsotg->hc_ptr_array[i]; 243 if (!list_empty(&channel->hc_list_entry)) 244 continue; 245 hcchar = DWC2_READ_4(hsotg, HCCHAR(i)); 246 if (hcchar & HCCHAR_CHENA) { 247 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR); 248 hcchar |= HCCHAR_CHDIS; 249 DWC2_WRITE_4(hsotg, HCCHAR(i), hcchar); 250 } 251 } 252 } 253 254 for (i = 0; i < num_channels; i++) { 255 channel = hsotg->hc_ptr_array[i]; 256 if (!list_empty(&channel->hc_list_entry)) 257 continue; 258 hcchar = DWC2_READ_4(hsotg, HCCHAR(i)); 259 if (hcchar & HCCHAR_CHENA) { 260 /* Halt the channel */ 261 hcchar |= HCCHAR_CHDIS; 262 DWC2_WRITE_4(hsotg, HCCHAR(i), hcchar); 263 } 264 265 dwc2_hc_cleanup(hsotg, channel); 266 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list); 267 /* 268 * Added for Descriptor DMA to prevent channel double cleanup in 269 * release_channel_ddma(), which is called from ep_disable when 270 * device disconnects 271 */ 272 channel->qh = NULL; 273 } 274 /* All channels have been freed, mark them available */ 275 if (hsotg->core_params->uframe_sched > 0) { 276 hsotg->available_host_channels = 277 hsotg->core_params->host_channels; 278 } else { 279 hsotg->non_periodic_channels = 0; 280 hsotg->periodic_channels = 0; 281 } 282 } 283 284 /** 285 * dwc2_hcd_connect() - Handles connect of the HCD 286 * 287 * @hsotg: Pointer to struct dwc2_hsotg 288 * 289 * Must be called with interrupt disabled and spinlock held 290 */ 291 void dwc2_hcd_connect(struct dwc2_hsotg *hsotg) 292 { 293 if (hsotg->lx_state != DWC2_L0) 294 usb_hcd_resume_root_hub(hsotg->priv); 295 296 hsotg->flags.b.port_connect_status_change = 1; 297 hsotg->flags.b.port_connect_status = 1; 298 } 299 300 /** 301 * dwc2_hcd_disconnect() - Handles disconnect of the HCD 302 * 303 * @hsotg: Pointer to struct dwc2_hsotg 304 * @force: If true, we won't try to reconnect even if we see device connected. 305 * 306 * Must be called with interrupt disabled and spinlock held 307 */ 308 void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force) 309 { 310 u32 intr; 311 u32 hprt0; 312 313 /* Set status flags for the hub driver */ 314 hsotg->flags.b.port_connect_status_change = 1; 315 hsotg->flags.b.port_connect_status = 0; 316 317 /* 318 * Shutdown any transfers in process by clearing the Tx FIFO Empty 319 * interrupt mask and status bits and disabling subsequent host 320 * channel interrupts. 321 */ 322 intr = DWC2_READ_4(hsotg, GINTMSK); 323 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT); 324 DWC2_WRITE_4(hsotg, GINTMSK, intr); 325 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT; 326 DWC2_WRITE_4(hsotg, GINTSTS, intr); 327 328 /* 329 * Turn off the vbus power only if the core has transitioned to device 330 * mode. If still in host mode, need to keep power on to detect a 331 * reconnection. 332 */ 333 if (dwc2_is_device_mode(hsotg)) { 334 if (hsotg->op_state != OTG_STATE_A_SUSPEND) { 335 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n"); 336 DWC2_WRITE_4(hsotg, HPRT0, 0); 337 } 338 339 dwc2_disable_host_interrupts(hsotg); 340 } 341 342 /* Respond with an error status to all URBs in the schedule */ 343 dwc2_kill_all_urbs(hsotg); 344 345 if (dwc2_is_host_mode(hsotg)) 346 /* Clean up any host channels that were in use */ 347 dwc2_hcd_cleanup_channels(hsotg); 348 349 dwc2_host_disconnect(hsotg); 350 351 dwc2_root_intr(hsotg->hsotg_sc); 352 353 /* 354 * Add an extra check here to see if we're actually connected but 355 * we don't have a detection interrupt pending. This can happen if: 356 * 1. hardware sees connect 357 * 2. hardware sees disconnect 358 * 3. hardware sees connect 359 * 4. dwc2_port_intr() - clears connect interrupt 360 * 5. dwc2_handle_common_intr() - calls here 361 * 362 * Without the extra check here we will end calling disconnect 363 * and won't get any future interrupts to handle the connect. 364 */ 365 if (!force) { 366 hprt0 = DWC2_READ_4(hsotg, HPRT0); 367 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS)) 368 dwc2_hcd_connect(hsotg); 369 } 370 } 371 372 /** 373 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup 374 * 375 * @hsotg: Pointer to struct dwc2_hsotg 376 */ 377 STATIC void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg) 378 { 379 if (hsotg->bus_suspended) { 380 hsotg->flags.b.port_suspend_change = 1; 381 usb_hcd_resume_root_hub(hsotg->priv); 382 } 383 384 if (hsotg->lx_state == DWC2_L1) 385 hsotg->flags.b.port_l1_change = 1; 386 387 dwc2_root_intr(hsotg->hsotg_sc); 388 } 389 390 /** 391 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner 392 * 393 * @hsotg: Pointer to struct dwc2_hsotg 394 * 395 * Must be called with interrupt disabled and spinlock held 396 */ 397 void dwc2_hcd_stop(struct dwc2_hsotg *hsotg) 398 { 399 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n"); 400 401 /* 402 * The root hub should be disconnected before this function is called. 403 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue) 404 * and the QH lists (via ..._hcd_endpoint_disable). 405 */ 406 407 /* Turn off all host-specific interrupts */ 408 dwc2_disable_host_interrupts(hsotg); 409 410 /* Turn off the vbus power */ 411 dev_dbg(hsotg->dev, "PortPower off\n"); 412 DWC2_WRITE_4(hsotg, HPRT0, 0); 413 } 414 415 /* Caller must hold driver lock */ 416 int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg, 417 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh, 418 struct dwc2_qtd *qtd) 419 { 420 u32 intr_mask; 421 int retval; 422 int dev_speed; 423 424 if (!hsotg->flags.b.port_connect_status) { 425 /* No longer connected */ 426 dev_err(hsotg->dev, "Not connected\n"); 427 return -ENODEV; 428 } 429 430 dev_speed = dwc2_host_get_speed(hsotg, urb->priv); 431 432 /* Some configurations cannot support LS traffic on a FS root port */ 433 if ((dev_speed == USB_SPEED_LOW) && 434 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) && 435 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) { 436 u32 hprt0 = DWC2_READ_4(hsotg, HPRT0); 437 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT; 438 439 if (prtspd == HPRT0_SPD_FULL_SPEED) { 440 dev_err(hsotg->dev, 441 "DWC OTG HCD URB Enqueue unsupported\n"); 442 return -ENODEV; 443 } 444 } 445 446 if (!qtd) 447 return -EINVAL; 448 449 memset(qtd, 0, sizeof(*qtd)); 450 451 dwc2_hcd_qtd_init(qtd, urb); 452 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh); 453 if (retval) { 454 dev_err(hsotg->dev, 455 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n", 456 retval); 457 return retval; 458 } 459 460 intr_mask = DWC2_READ_4(hsotg, GINTMSK); 461 if (!(intr_mask & GINTSTS_SOF)) { 462 enum dwc2_transaction_type tr_type; 463 464 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK && 465 !(qtd->urb->flags & URB_GIVEBACK_ASAP)) 466 /* 467 * Do not schedule SG transactions until qtd has 468 * URB_GIVEBACK_ASAP set 469 */ 470 return 0; 471 472 tr_type = dwc2_hcd_select_transactions(hsotg); 473 if (tr_type != DWC2_TRANSACTION_NONE) 474 dwc2_hcd_queue_transactions(hsotg, tr_type); 475 } 476 477 return 0; 478 } 479 480 /* Must be called with interrupt disabled and spinlock held */ 481 int 482 dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg, 483 struct dwc2_hcd_urb *urb) 484 { 485 struct dwc2_qh *qh; 486 struct dwc2_qtd *urb_qtd; 487 488 urb_qtd = urb->qtd; 489 if (!urb_qtd) { 490 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n"); 491 return -EINVAL; 492 } 493 494 qh = urb_qtd->qh; 495 if (!qh) { 496 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n"); 497 return -EINVAL; 498 } 499 500 urb->priv = NULL; 501 502 if (urb_qtd->in_process && qh->channel) { 503 #ifdef VERBOSE_DEBUG 504 dwc2_dump_channel_info(hsotg, qh->channel); 505 #endif 506 /* The QTD is in process (it has been assigned to a channel) */ 507 if (hsotg->flags.b.port_connect_status) 508 /* 509 * If still connected (i.e. in host mode), halt the 510 * channel so it can be used for other transfers. If 511 * no longer connected, the host registers can't be 512 * written to halt the channel since the core is in 513 * device mode. 514 */ 515 dwc2_hc_halt(hsotg, qh->channel, 516 DWC2_HC_XFER_URB_DEQUEUE); 517 } 518 519 /* 520 * Free the QTD and clean up the associated QH. Leave the QH in the 521 * schedule if it has any remaining QTDs. 522 */ 523 if (hsotg->core_params->dma_desc_enable <= 0) { 524 u8 in_process = urb_qtd->in_process; 525 526 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh); 527 if (in_process) { 528 dwc2_hcd_qh_deactivate(hsotg, qh, 0); 529 qh->channel = NULL; 530 } else if (list_empty(&qh->qtd_list)) { 531 dwc2_hcd_qh_unlink(hsotg, qh); 532 } 533 } else { 534 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh); 535 } 536 537 return 0; 538 } 539 540 541 /* 542 * Initializes dynamic portions of the DWC_otg HCD state 543 * 544 * Must be called with interrupt disabled and spinlock held 545 */ 546 void 547 dwc2_hcd_reinit(struct dwc2_hsotg *hsotg) 548 { 549 struct dwc2_host_chan *chan, *chan_tmp; 550 int num_channels; 551 int i; 552 553 hsotg->flags.d32 = 0; 554 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active; 555 556 if (hsotg->core_params->uframe_sched > 0) { 557 hsotg->available_host_channels = 558 hsotg->core_params->host_channels; 559 } else { 560 hsotg->non_periodic_channels = 0; 561 hsotg->periodic_channels = 0; 562 } 563 564 /* 565 * Put all channels in the free channel list and clean up channel 566 * states 567 */ 568 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list, 569 hc_list_entry) 570 list_del_init(&chan->hc_list_entry); 571 572 num_channels = hsotg->core_params->host_channels; 573 for (i = 0; i < num_channels; i++) { 574 chan = hsotg->hc_ptr_array[i]; 575 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list); 576 dwc2_hc_cleanup(hsotg, chan); 577 } 578 579 /* Initialize the DWC core for host mode operation */ 580 dwc2_core_host_init(hsotg); 581 } 582 583 STATIC void dwc2_hc_init_split(struct dwc2_hsotg *hsotg, 584 struct dwc2_host_chan *chan, 585 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb) 586 { 587 int hub_addr, hub_port; 588 589 chan->do_split = 1; 590 chan->xact_pos = qtd->isoc_split_pos; 591 chan->complete_split = qtd->complete_split; 592 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port); 593 chan->hub_addr = (u8)hub_addr; 594 chan->hub_port = (u8)hub_port; 595 } 596 597 STATIC void *dwc2_hc_init_xfer_data(struct dwc2_hsotg *hsotg, 598 struct dwc2_host_chan *chan, 599 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb) 600 { 601 if (hsotg->core_params->dma_enable > 0) { 602 chan->xfer_dma = DMAADDR(urb->usbdma, urb->actual_length); 603 604 /* For non-dword aligned case */ 605 if (hsotg->core_params->dma_desc_enable <= 0 && 606 (chan->xfer_dma & 0x3)) 607 return (u8 *)urb->buf + urb->actual_length; 608 } else { 609 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length; 610 } 611 612 return NULL; 613 } 614 615 STATIC void *dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg, 616 struct dwc2_host_chan *chan, 617 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb) 618 { 619 struct dwc2_hcd_iso_packet_desc *frame_desc; 620 void *bufptr = NULL; 621 622 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) { 623 case USB_ENDPOINT_XFER_CONTROL: 624 chan->ep_type = USB_ENDPOINT_XFER_CONTROL; 625 626 switch (qtd->control_phase) { 627 case DWC2_CONTROL_SETUP: 628 dev_vdbg(hsotg->dev, " Control setup transaction\n"); 629 chan->do_ping = 0; 630 chan->ep_is_in = 0; 631 chan->data_pid_start = DWC2_HC_PID_SETUP; 632 if (hsotg->core_params->dma_enable > 0) 633 chan->xfer_dma = urb->setup_dma; 634 else 635 chan->xfer_buf = urb->setup_packet; 636 chan->xfer_len = 8; 637 break; 638 639 case DWC2_CONTROL_DATA: 640 dev_vdbg(hsotg->dev, " Control data transaction\n"); 641 chan->data_pid_start = qtd->data_toggle; 642 bufptr = dwc2_hc_init_xfer_data(hsotg, chan, qtd, urb); 643 break; 644 645 case DWC2_CONTROL_STATUS: 646 /* 647 * Direction is opposite of data direction or IN if no 648 * data 649 */ 650 dev_vdbg(hsotg->dev, " Control status transaction\n"); 651 if (urb->length == 0) 652 chan->ep_is_in = 1; 653 else 654 chan->ep_is_in = 655 dwc2_hcd_is_pipe_out(&urb->pipe_info); 656 if (chan->ep_is_in) 657 chan->do_ping = 0; 658 chan->data_pid_start = DWC2_HC_PID_DATA1; 659 chan->xfer_len = 0; 660 if (hsotg->core_params->dma_enable > 0) 661 chan->xfer_dma = hsotg->status_buf_dma; 662 else 663 chan->xfer_buf = hsotg->status_buf; 664 break; 665 } 666 break; 667 668 case USB_ENDPOINT_XFER_BULK: 669 chan->ep_type = USB_ENDPOINT_XFER_BULK; 670 bufptr = dwc2_hc_init_xfer_data(hsotg, chan, qtd, urb); 671 break; 672 673 case USB_ENDPOINT_XFER_INT: 674 chan->ep_type = USB_ENDPOINT_XFER_INT; 675 bufptr = dwc2_hc_init_xfer_data(hsotg, chan, qtd, urb); 676 break; 677 678 case USB_ENDPOINT_XFER_ISOC: 679 chan->ep_type = USB_ENDPOINT_XFER_ISOC; 680 if (hsotg->core_params->dma_desc_enable > 0) 681 break; 682 683 frame_desc = &urb->iso_descs[qtd->isoc_frame_index]; 684 frame_desc->status = 0; 685 686 if (hsotg->core_params->dma_enable > 0) { 687 chan->xfer_dma = urb->dma; 688 chan->xfer_dma += frame_desc->offset + 689 qtd->isoc_split_offset; 690 } else { 691 chan->xfer_buf = urb->buf; 692 chan->xfer_buf += frame_desc->offset + 693 qtd->isoc_split_offset; 694 } 695 696 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset; 697 698 /* For non-dword aligned buffers */ 699 if (hsotg->core_params->dma_enable > 0 && 700 (chan->xfer_dma & 0x3)) 701 bufptr = (u8 *)urb->buf + frame_desc->offset + 702 qtd->isoc_split_offset; 703 704 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) { 705 if (chan->xfer_len <= 188) 706 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL; 707 else 708 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN; 709 } 710 break; 711 } 712 713 return bufptr; 714 } 715 716 STATIC int dwc2_hc_setup_align_buf(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh, 717 struct dwc2_host_chan *chan, 718 struct dwc2_hcd_urb *urb, void *bufptr) 719 { 720 u32 buf_size; 721 722 if (!qh->dw_align_buf) { 723 int err; 724 725 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC) 726 buf_size = hsotg->core_params->max_transfer_size; 727 else 728 /* 3072 = 3 max-size Isoc packets */ 729 buf_size = 3072; 730 731 qh->dw_align_buf = NULL; 732 qh->dw_align_buf_dma = 0; 733 err = usb_allocmem(&hsotg->hsotg_sc->sc_bus, buf_size, 0, 734 USB_DMA_COHERENT, &qh->dw_align_buf_usbdma); 735 if (!err) { 736 struct usb_dma *ud = &qh->dw_align_buf_usbdma; 737 738 qh->dw_align_buf = KERNADDR(ud, 0); 739 qh->dw_align_buf_dma = DMAADDR(ud, 0); 740 } 741 if (!qh->dw_align_buf) 742 return -ENOMEM; 743 qh->dw_align_buf_size = buf_size; 744 } 745 746 if (chan->xfer_len) { 747 dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", __func__); 748 void *usb_urb = urb->priv; 749 750 if (usb_urb) { 751 if (!chan->ep_is_in) { 752 memcpy(qh->dw_align_buf, bufptr, 753 chan->xfer_len); 754 } 755 } else { 756 dev_warn(hsotg->dev, "no URB in dwc2_urb\n"); 757 } 758 } 759 760 usb_syncmem(&qh->dw_align_buf_usbdma, 0, qh->dw_align_buf_size, 761 chan->ep_is_in ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 762 763 chan->align_buf = qh->dw_align_buf_dma; 764 return 0; 765 } 766 767 /** 768 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host 769 * channel and initializes the host channel to perform the transactions. The 770 * host channel is removed from the free list. 771 * 772 * @hsotg: The HCD state structure 773 * @qh: Transactions from the first QTD for this QH are selected and assigned 774 * to a free host channel 775 */ 776 STATIC int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 777 { 778 struct dwc2_host_chan *chan; 779 struct dwc2_hcd_urb *urb; 780 struct dwc2_qtd *qtd; 781 void *bufptr = NULL; 782 783 if (dbg_qh(qh)) 784 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh); 785 786 if (list_empty(&qh->qtd_list)) { 787 dev_dbg(hsotg->dev, "No QTDs in QH list\n"); 788 return -ENOMEM; 789 } 790 791 if (list_empty(&hsotg->free_hc_list)) { 792 dev_dbg(hsotg->dev, "No free channel to assign\n"); 793 return -ENOMEM; 794 } 795 796 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan, 797 hc_list_entry); 798 799 /* Remove host channel from free list */ 800 list_del_init(&chan->hc_list_entry); 801 802 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry); 803 urb = qtd->urb; 804 qh->channel = chan; 805 qtd->in_process = 1; 806 807 /* 808 * Use usb_pipedevice to determine device address. This address is 809 * 0 before the SET_ADDRESS command and the correct address afterward. 810 */ 811 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info); 812 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info); 813 chan->speed = qh->dev_speed; 814 chan->max_packet = dwc2_max_packet(qh->maxp); 815 816 chan->xfer_started = 0; 817 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS; 818 chan->error_state = (qtd->error_count > 0); 819 chan->halt_on_queue = 0; 820 chan->halt_pending = 0; 821 chan->requests = 0; 822 823 /* 824 * The following values may be modified in the transfer type section 825 * below. The xfer_len value may be reduced when the transfer is 826 * started to accommodate the max widths of the XferSize and PktCnt 827 * fields in the HCTSIZn register. 828 */ 829 830 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0); 831 if (chan->ep_is_in) 832 chan->do_ping = 0; 833 else 834 chan->do_ping = qh->ping_state; 835 836 chan->data_pid_start = qh->data_toggle; 837 chan->multi_count = 1; 838 839 if (urb->actual_length > urb->length && 840 !dwc2_hcd_is_pipe_in(&urb->pipe_info)) 841 urb->actual_length = urb->length; 842 843 chan->xfer_len = urb->length - urb->actual_length; 844 chan->xfer_count = 0; 845 846 /* Set the split attributes if required */ 847 if (qh->do_split) 848 dwc2_hc_init_split(hsotg, chan, qtd, urb); 849 else 850 chan->do_split = 0; 851 852 /* Set the transfer attributes */ 853 bufptr = dwc2_hc_init_xfer(hsotg, chan, qtd, urb); 854 855 /* Non DWORD-aligned buffer case */ 856 if (bufptr) { 857 dev_vdbg(hsotg->dev, "Non-aligned buffer\n"); 858 if (dwc2_hc_setup_align_buf(hsotg, qh, chan, urb, bufptr)) { 859 dev_err(hsotg->dev, 860 "%s: Failed to allocate memory to handle non-dword aligned buffer\n", 861 __func__); 862 /* Add channel back to free list */ 863 chan->align_buf = 0; 864 chan->multi_count = 0; 865 list_add_tail(&chan->hc_list_entry, 866 &hsotg->free_hc_list); 867 qtd->in_process = 0; 868 qh->channel = NULL; 869 return -ENOMEM; 870 } 871 } else { 872 chan->align_buf = 0; 873 } 874 875 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 876 chan->ep_type == USB_ENDPOINT_XFER_ISOC) 877 /* 878 * This value may be modified when the transfer is started 879 * to reflect the actual transfer length 880 */ 881 chan->multi_count = dwc2_hb_mult(qh->maxp); 882 883 if (hsotg->core_params->dma_desc_enable > 0) { 884 chan->desc_list_usbdma = qh->desc_list_usbdma; 885 chan->desc_list_addr = qh->desc_list_dma; 886 chan->desc_list_sz = qh->desc_list_sz; 887 } 888 889 dwc2_hc_init(hsotg, chan); 890 chan->qh = qh; 891 892 return 0; 893 } 894 895 /** 896 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer 897 * schedule and assigns them to available host channels. Called from the HCD 898 * interrupt handler functions. 899 * 900 * @hsotg: The HCD state structure 901 * 902 * Return: The types of new transactions that were assigned to host channels 903 */ 904 enum dwc2_transaction_type dwc2_hcd_select_transactions( 905 struct dwc2_hsotg *hsotg) 906 { 907 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE; 908 struct list_head *qh_ptr; 909 struct dwc2_qh *qh; 910 int num_channels; 911 912 #ifdef DWC2_DEBUG_SOF 913 dev_vdbg(hsotg->dev, " Select Transactions\n"); 914 #endif 915 916 /* Process entries in the periodic ready list */ 917 qh_ptr = hsotg->periodic_sched_ready.next; 918 while (qh_ptr != &hsotg->periodic_sched_ready) { 919 if (list_empty(&hsotg->free_hc_list)) 920 break; 921 if (hsotg->core_params->uframe_sched > 0) { 922 if (hsotg->available_host_channels <= 1) 923 break; 924 hsotg->available_host_channels--; 925 } 926 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry); 927 if (dwc2_assign_and_init_hc(hsotg, qh)) 928 break; 929 930 /* 931 * Move the QH from the periodic ready schedule to the 932 * periodic assigned schedule 933 */ 934 qh_ptr = qh_ptr->next; 935 list_move(&qh->qh_list_entry, &hsotg->periodic_sched_assigned); 936 ret_val = DWC2_TRANSACTION_PERIODIC; 937 } 938 939 /* 940 * Process entries in the inactive portion of the non-periodic 941 * schedule. Some free host channels may not be used if they are 942 * reserved for periodic transfers. 943 */ 944 num_channels = hsotg->core_params->host_channels; 945 qh_ptr = hsotg->non_periodic_sched_inactive.next; 946 while (qh_ptr != &hsotg->non_periodic_sched_inactive) { 947 if (hsotg->core_params->uframe_sched <= 0 && 948 hsotg->non_periodic_channels >= num_channels - 949 hsotg->periodic_channels) 950 break; 951 if (list_empty(&hsotg->free_hc_list)) 952 break; 953 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry); 954 955 /* 956 * Check to see if this is a NAK'd retransmit, in which case 957 * ignore for retransmission. We hold off on bulk/control 958 * retransmissions to reduce NAK interrupt overhead for 959 * cheeky devices that just hold off using NAKs. 960 */ 961 if (qh->nak_frame != 0xffff && 962 dwc2_full_frame_num(qh->nak_frame) == 963 dwc2_full_frame_num(dwc2_hcd_get_frame_number(hsotg))) { 964 qh_ptr = qh_ptr->next; 965 continue; 966 } else { 967 qh->nak_frame = 0xffff; 968 } 969 970 if (hsotg->core_params->uframe_sched > 0) { 971 if (hsotg->available_host_channels < 1) 972 break; 973 hsotg->available_host_channels--; 974 } 975 976 if (dwc2_assign_and_init_hc(hsotg, qh)) 977 break; 978 979 /* 980 * Move the QH from the non-periodic inactive schedule to the 981 * non-periodic active schedule 982 */ 983 qh_ptr = qh_ptr->next; 984 list_move(&qh->qh_list_entry, 985 &hsotg->non_periodic_sched_active); 986 987 if (ret_val == DWC2_TRANSACTION_NONE) 988 ret_val = DWC2_TRANSACTION_NON_PERIODIC; 989 else 990 ret_val = DWC2_TRANSACTION_ALL; 991 992 if (hsotg->core_params->uframe_sched <= 0) 993 hsotg->non_periodic_channels++; 994 } 995 996 return ret_val; 997 } 998 999 /** 1000 * dwc2_queue_transaction() - Attempts to queue a single transaction request for 1001 * a host channel associated with either a periodic or non-periodic transfer 1002 * 1003 * @hsotg: The HCD state structure 1004 * @chan: Host channel descriptor associated with either a periodic or 1005 * non-periodic transfer 1006 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO 1007 * for periodic transfers or the non-periodic Tx FIFO 1008 * for non-periodic transfers 1009 * 1010 * Return: 1 if a request is queued and more requests may be needed to 1011 * complete the transfer, 0 if no more requests are required for this 1012 * transfer, -1 if there is insufficient space in the Tx FIFO 1013 * 1014 * This function assumes that there is space available in the appropriate 1015 * request queue. For an OUT transfer or SETUP transaction in Slave mode, 1016 * it checks whether space is available in the appropriate Tx FIFO. 1017 * 1018 * Must be called with interrupt disabled and spinlock held 1019 */ 1020 STATIC int dwc2_queue_transaction(struct dwc2_hsotg *hsotg, 1021 struct dwc2_host_chan *chan, 1022 u16 fifo_dwords_avail) 1023 { 1024 int retval = 0; 1025 1026 if (chan->do_split) 1027 /* Put ourselves on the list to keep order straight */ 1028 list_move(&chan->split_order_list_entry, &hsotg->split_order); 1029 1030 if (hsotg->core_params->dma_enable > 0 && chan->qh) { 1031 if (hsotg->core_params->dma_desc_enable > 0) { 1032 if (!chan->xfer_started || 1033 chan->ep_type == USB_ENDPOINT_XFER_ISOC) { 1034 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh); 1035 chan->qh->ping_state = 0; 1036 } 1037 } else if (!chan->xfer_started) { 1038 dwc2_hc_start_transfer(hsotg, chan); 1039 chan->qh->ping_state = 0; 1040 } 1041 } else if (chan->halt_pending) { 1042 /* Don't queue a request if the channel has been halted */ 1043 } else if (chan->halt_on_queue) { 1044 dwc2_hc_halt(hsotg, chan, chan->halt_status); 1045 } else if (chan->do_ping) { 1046 if (!chan->xfer_started) 1047 dwc2_hc_start_transfer(hsotg, chan); 1048 } else if (!chan->ep_is_in || 1049 chan->data_pid_start == DWC2_HC_PID_SETUP) { 1050 if ((fifo_dwords_avail * 4) >= chan->max_packet) { 1051 if (!chan->xfer_started) { 1052 dwc2_hc_start_transfer(hsotg, chan); 1053 retval = 1; 1054 } else { 1055 retval = dwc2_hc_continue_transfer(hsotg, chan); 1056 } 1057 } else { 1058 retval = -1; 1059 } 1060 } else { 1061 if (!chan->xfer_started) { 1062 dwc2_hc_start_transfer(hsotg, chan); 1063 retval = 1; 1064 } else { 1065 retval = dwc2_hc_continue_transfer(hsotg, chan); 1066 } 1067 } 1068 1069 return retval; 1070 } 1071 1072 /* 1073 * Processes periodic channels for the next frame and queues transactions for 1074 * these channels to the DWC_otg controller. After queueing transactions, the 1075 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions 1076 * to queue as Periodic Tx FIFO or request queue space becomes available. 1077 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled. 1078 * 1079 * Must be called with interrupt disabled and spinlock held 1080 */ 1081 STATIC void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg) 1082 { 1083 struct list_head *qh_ptr; 1084 struct dwc2_qh *qh; 1085 u32 tx_status; 1086 u32 fspcavail; 1087 u32 gintmsk; 1088 int status; 1089 int no_queue_space = 0; 1090 int no_fifo_space = 0; 1091 u32 qspcavail; 1092 1093 if (dbg_perio()) 1094 dev_vdbg(hsotg->dev, "Queue periodic transactions\n"); 1095 1096 tx_status = DWC2_READ_4(hsotg, HPTXSTS); 1097 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 1098 TXSTS_QSPCAVAIL_SHIFT; 1099 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 1100 TXSTS_FSPCAVAIL_SHIFT; 1101 1102 if (dbg_perio()) { 1103 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n", 1104 qspcavail); 1105 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n", 1106 fspcavail); 1107 } 1108 1109 qh_ptr = hsotg->periodic_sched_assigned.next; 1110 while (qh_ptr != &hsotg->periodic_sched_assigned) { 1111 tx_status = DWC2_READ_4(hsotg, HPTXSTS); 1112 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 1113 TXSTS_QSPCAVAIL_SHIFT; 1114 if (qspcavail == 0) { 1115 no_queue_space = 1; 1116 break; 1117 } 1118 1119 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry); 1120 if (!qh->channel) { 1121 qh_ptr = qh_ptr->next; 1122 continue; 1123 } 1124 1125 /* Make sure EP's TT buffer is clean before queueing qtds */ 1126 if (qh->tt_buffer_dirty) { 1127 qh_ptr = qh_ptr->next; 1128 continue; 1129 } 1130 1131 /* 1132 * Set a flag if we're queuing high-bandwidth in slave mode. 1133 * The flag prevents any halts to get into the request queue in 1134 * the middle of multiple high-bandwidth packets getting queued. 1135 */ 1136 if (hsotg->core_params->dma_enable <= 0 && 1137 qh->channel->multi_count > 1) 1138 hsotg->queuing_high_bandwidth = 1; 1139 1140 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 1141 TXSTS_FSPCAVAIL_SHIFT; 1142 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail); 1143 if (status < 0) { 1144 no_fifo_space = 1; 1145 break; 1146 } 1147 1148 /* 1149 * In Slave mode, stay on the current transfer until there is 1150 * nothing more to do or the high-bandwidth request count is 1151 * reached. In DMA mode, only need to queue one request. The 1152 * controller automatically handles multiple packets for 1153 * high-bandwidth transfers. 1154 */ 1155 if (hsotg->core_params->dma_enable > 0 || status == 0 || 1156 qh->channel->requests == qh->channel->multi_count) { 1157 qh_ptr = qh_ptr->next; 1158 /* 1159 * Move the QH from the periodic assigned schedule to 1160 * the periodic queued schedule 1161 */ 1162 list_move(&qh->qh_list_entry, 1163 &hsotg->periodic_sched_queued); 1164 1165 /* done queuing high bandwidth */ 1166 hsotg->queuing_high_bandwidth = 0; 1167 } 1168 } 1169 1170 if (hsotg->core_params->dma_enable <= 0) { 1171 tx_status = DWC2_READ_4(hsotg, HPTXSTS); 1172 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 1173 TXSTS_QSPCAVAIL_SHIFT; 1174 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 1175 TXSTS_FSPCAVAIL_SHIFT; 1176 if (dbg_perio()) { 1177 dev_vdbg(hsotg->dev, 1178 " P Tx Req Queue Space Avail (after queue): %d\n", 1179 qspcavail); 1180 dev_vdbg(hsotg->dev, 1181 " P Tx FIFO Space Avail (after queue): %d\n", 1182 fspcavail); 1183 } 1184 1185 if (!list_empty(&hsotg->periodic_sched_assigned) || 1186 no_queue_space || no_fifo_space) { 1187 /* 1188 * May need to queue more transactions as the request 1189 * queue or Tx FIFO empties. Enable the periodic Tx 1190 * FIFO empty interrupt. (Always use the half-empty 1191 * level to ensure that new requests are loaded as 1192 * soon as possible.) 1193 */ 1194 gintmsk = DWC2_READ_4(hsotg, GINTMSK); 1195 gintmsk |= GINTSTS_PTXFEMP; 1196 DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 1197 } else { 1198 /* 1199 * Disable the Tx FIFO empty interrupt since there are 1200 * no more transactions that need to be queued right 1201 * now. This function is called from interrupt 1202 * handlers to queue more transactions as transfer 1203 * states change. 1204 */ 1205 gintmsk = DWC2_READ_4(hsotg, GINTMSK); 1206 gintmsk &= ~GINTSTS_PTXFEMP; 1207 DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 1208 } 1209 } 1210 } 1211 1212 /* 1213 * Processes active non-periodic channels and queues transactions for these 1214 * channels to the DWC_otg controller. After queueing transactions, the NP Tx 1215 * FIFO Empty interrupt is enabled if there are more transactions to queue as 1216 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx 1217 * FIFO Empty interrupt is disabled. 1218 * 1219 * Must be called with interrupt disabled and spinlock held 1220 */ 1221 STATIC void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg) 1222 { 1223 struct list_head *orig_qh_ptr; 1224 struct dwc2_qh *qh; 1225 u32 tx_status; 1226 u32 qspcavail; 1227 u32 fspcavail; 1228 u32 gintmsk; 1229 int status; 1230 int no_queue_space = 0; 1231 int no_fifo_space = 0; 1232 int more_to_do = 0; 1233 1234 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n"); 1235 1236 tx_status = DWC2_READ_4(hsotg, GNPTXSTS); 1237 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 1238 TXSTS_QSPCAVAIL_SHIFT; 1239 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 1240 TXSTS_FSPCAVAIL_SHIFT; 1241 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n", 1242 qspcavail); 1243 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n", 1244 fspcavail); 1245 1246 /* 1247 * Keep track of the starting point. Skip over the start-of-list 1248 * entry. 1249 */ 1250 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active) 1251 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next; 1252 orig_qh_ptr = hsotg->non_periodic_qh_ptr; 1253 1254 /* 1255 * Process once through the active list or until no more space is 1256 * available in the request queue or the Tx FIFO 1257 */ 1258 do { 1259 tx_status = DWC2_READ_4(hsotg, GNPTXSTS); 1260 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 1261 TXSTS_QSPCAVAIL_SHIFT; 1262 if (hsotg->core_params->dma_enable <= 0 && qspcavail == 0) { 1263 no_queue_space = 1; 1264 break; 1265 } 1266 1267 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh, 1268 qh_list_entry); 1269 if (!qh->channel) 1270 goto next; 1271 1272 /* Make sure EP's TT buffer is clean before queueing qtds */ 1273 if (qh->tt_buffer_dirty) 1274 goto next; 1275 1276 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 1277 TXSTS_FSPCAVAIL_SHIFT; 1278 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail); 1279 1280 if (status > 0) { 1281 more_to_do = 1; 1282 } else if (status < 0) { 1283 no_fifo_space = 1; 1284 break; 1285 } 1286 next: 1287 /* Advance to next QH, skipping start-of-list entry */ 1288 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next; 1289 if (hsotg->non_periodic_qh_ptr == 1290 &hsotg->non_periodic_sched_active) 1291 hsotg->non_periodic_qh_ptr = 1292 hsotg->non_periodic_qh_ptr->next; 1293 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr); 1294 1295 if (hsotg->core_params->dma_enable <= 0) { 1296 tx_status = DWC2_READ_4(hsotg, GNPTXSTS); 1297 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 1298 TXSTS_QSPCAVAIL_SHIFT; 1299 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 1300 TXSTS_FSPCAVAIL_SHIFT; 1301 dev_vdbg(hsotg->dev, 1302 " NP Tx Req Queue Space Avail (after queue): %d\n", 1303 qspcavail); 1304 dev_vdbg(hsotg->dev, 1305 " NP Tx FIFO Space Avail (after queue): %d\n", 1306 fspcavail); 1307 1308 if (more_to_do || no_queue_space || no_fifo_space) { 1309 /* 1310 * May need to queue more transactions as the request 1311 * queue or Tx FIFO empties. Enable the non-periodic 1312 * Tx FIFO empty interrupt. (Always use the half-empty 1313 * level to ensure that new requests are loaded as 1314 * soon as possible.) 1315 */ 1316 gintmsk = DWC2_READ_4(hsotg, GINTMSK); 1317 gintmsk |= GINTSTS_NPTXFEMP; 1318 DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 1319 } else { 1320 /* 1321 * Disable the Tx FIFO empty interrupt since there are 1322 * no more transactions that need to be queued right 1323 * now. This function is called from interrupt 1324 * handlers to queue more transactions as transfer 1325 * states change. 1326 */ 1327 gintmsk = DWC2_READ_4(hsotg, GINTMSK); 1328 gintmsk &= ~GINTSTS_NPTXFEMP; 1329 DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 1330 } 1331 } 1332 } 1333 1334 /** 1335 * dwc2_hcd_queue_transactions() - Processes the currently active host channels 1336 * and queues transactions for these channels to the DWC_otg controller. Called 1337 * from the HCD interrupt handler functions. 1338 * 1339 * @hsotg: The HCD state structure 1340 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic, 1341 * or both) 1342 * 1343 * Must be called with interrupt disabled and spinlock held 1344 */ 1345 void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg, 1346 enum dwc2_transaction_type tr_type) 1347 { 1348 #ifdef DWC2_DEBUG_SOF 1349 dev_vdbg(hsotg->dev, "Queue Transactions\n"); 1350 #endif 1351 /* Process host channels associated with periodic transfers */ 1352 if ((tr_type == DWC2_TRANSACTION_PERIODIC || 1353 tr_type == DWC2_TRANSACTION_ALL) && 1354 !list_empty(&hsotg->periodic_sched_assigned)) 1355 dwc2_process_periodic_channels(hsotg); 1356 1357 /* Process host channels associated with non-periodic transfers */ 1358 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC || 1359 tr_type == DWC2_TRANSACTION_ALL) { 1360 if (!list_empty(&hsotg->non_periodic_sched_active)) { 1361 dwc2_process_non_periodic_channels(hsotg); 1362 } else { 1363 /* 1364 * Ensure NP Tx FIFO empty interrupt is disabled when 1365 * there are no non-periodic transfers to process 1366 */ 1367 u32 gintmsk = DWC2_READ_4(hsotg, GINTMSK); 1368 1369 gintmsk &= ~GINTSTS_NPTXFEMP; 1370 DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 1371 } 1372 } 1373 } 1374 1375 1376 STATIC void dwc2_conn_id_status_change(void *data) 1377 { 1378 struct dwc2_hsotg *hsotg = data; 1379 u32 count = 0; 1380 u32 gotgctl; 1381 unsigned long flags; 1382 1383 dev_dbg(hsotg->dev, "%s()\n", __func__); 1384 1385 gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 1386 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl); 1387 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n", 1388 !!(gotgctl & GOTGCTL_CONID_B)); 1389 1390 /* B-Device connector (Device Mode) */ 1391 if (gotgctl & GOTGCTL_CONID_B) { 1392 /* Wait for switch to device mode */ 1393 dev_dbg(hsotg->dev, "connId B\n"); 1394 while (!dwc2_is_device_mode(hsotg)) { 1395 dev_info(hsotg->dev, 1396 "Waiting for Peripheral Mode, Mode=%s\n", 1397 dwc2_is_host_mode(hsotg) ? "Host" : 1398 "Peripheral"); 1399 usleep_range(20000, 40000); 1400 if (++count > 250) 1401 break; 1402 } 1403 if (count > 250) 1404 dev_err(hsotg->dev, 1405 "Connection id status change timed out\n"); 1406 hsotg->op_state = OTG_STATE_B_PERIPHERAL; 1407 dwc2_core_init(hsotg, false); 1408 dwc2_enable_global_interrupts(hsotg); 1409 spin_lock_irqsave(&hsotg->lock, flags); 1410 dwc2_hsotg_core_init_disconnected(hsotg, false); 1411 spin_unlock_irqrestore(&hsotg->lock, flags); 1412 dwc2_hsotg_core_connect(hsotg); 1413 } else { 1414 /* A-Device connector (Host Mode) */ 1415 dev_dbg(hsotg->dev, "connId A\n"); 1416 while (!dwc2_is_host_mode(hsotg)) { 1417 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n", 1418 dwc2_is_host_mode(hsotg) ? 1419 "Host" : "Peripheral"); 1420 usleep_range(20000, 40000); 1421 if (++count > 250) 1422 break; 1423 } 1424 if (count > 250) 1425 dev_err(hsotg->dev, 1426 "Connection id status change timed out\n"); 1427 hsotg->op_state = OTG_STATE_A_HOST; 1428 1429 /* Initialize the Core for Host mode */ 1430 dwc2_core_init(hsotg, false); 1431 dwc2_enable_global_interrupts(hsotg); 1432 dwc2_hcd_start(hsotg); 1433 } 1434 } 1435 1436 void dwc2_wakeup_detected(void *data) 1437 { 1438 struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)data; 1439 u32 hprt0; 1440 1441 dev_dbg(hsotg->dev, "%s()\n", __func__); 1442 1443 /* 1444 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms 1445 * so that OPT tests pass with all PHYs.) 1446 */ 1447 hprt0 = dwc2_read_hprt0(hsotg); 1448 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0); 1449 hprt0 &= ~HPRT0_RES; 1450 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1451 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n", 1452 DWC2_READ_4(hsotg, HPRT0)); 1453 1454 dwc2_hcd_rem_wakeup(hsotg); 1455 hsotg->bus_suspended = 0; 1456 1457 /* Change to L0 state */ 1458 hsotg->lx_state = DWC2_L0; 1459 } 1460 1461 /* Must NOT be called with interrupt disabled or spinlock held */ 1462 STATIC void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex) 1463 { 1464 unsigned long flags; 1465 u32 hprt0; 1466 u32 pcgctl; 1467 u32 gotgctl; 1468 1469 dev_dbg(hsotg->dev, "%s()\n", __func__); 1470 1471 spin_lock_irqsave(&hsotg->lock, flags); 1472 1473 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) { 1474 gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 1475 gotgctl |= GOTGCTL_HSTSETHNPEN; 1476 DWC2_WRITE_4(hsotg, GOTGCTL, gotgctl); 1477 hsotg->op_state = OTG_STATE_A_SUSPEND; 1478 } 1479 1480 hprt0 = dwc2_read_hprt0(hsotg); 1481 hprt0 |= HPRT0_SUSP; 1482 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1483 1484 hsotg->bus_suspended = 1; 1485 1486 /* 1487 * If hibernation is supported, Phy clock will be suspended 1488 * after registers are backuped. 1489 */ 1490 if (!hsotg->core_params->hibernation) { 1491 /* Suspend the Phy Clock */ 1492 pcgctl = DWC2_READ_4(hsotg, PCGCTL); 1493 pcgctl |= PCGCTL_STOPPCLK; 1494 DWC2_WRITE_4(hsotg, PCGCTL, pcgctl); 1495 udelay(10); 1496 } 1497 1498 /* For HNP the bus must be suspended for at least 200ms */ 1499 if (dwc2_host_is_b_hnp_enabled(hsotg)) { 1500 pcgctl = DWC2_READ_4(hsotg, PCGCTL); 1501 pcgctl &= ~PCGCTL_STOPPCLK; 1502 DWC2_WRITE_4(hsotg, PCGCTL, pcgctl); 1503 1504 spin_unlock_irqrestore(&hsotg->lock, flags); 1505 1506 usleep_range(200000, 250000); 1507 } else { 1508 spin_unlock_irqrestore(&hsotg->lock, flags); 1509 } 1510 } 1511 1512 /* Must NOT be called with interrupt disabled or spinlock held */ 1513 STATIC void dwc2_port_resume(struct dwc2_hsotg *hsotg) 1514 { 1515 struct dwc2_softc *sc = hsotg->hsotg_sc; 1516 unsigned long flags; 1517 u32 hprt0; 1518 u32 pcgctl; 1519 1520 spin_lock_irqsave(&hsotg->lock, flags); 1521 1522 /* 1523 * If hibernation is supported, Phy clock is already resumed 1524 * after registers restore. 1525 */ 1526 if (!hsotg->core_params->hibernation) { 1527 pcgctl = DWC2_READ_4(hsotg, PCGCTL); 1528 pcgctl &= ~PCGCTL_STOPPCLK; 1529 DWC2_WRITE_4(hsotg, PCGCTL, pcgctl); 1530 spin_unlock_irqrestore(&hsotg->lock, flags); 1531 usleep_range(20000, 40000); 1532 spin_lock_irqsave(&hsotg->lock, flags); 1533 } 1534 1535 hprt0 = dwc2_read_hprt0(hsotg); 1536 hprt0 |= HPRT0_RES; 1537 hprt0 &= ~HPRT0_SUSP; 1538 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1539 spin_unlock_irqrestore(&hsotg->lock, flags); 1540 1541 usb_delay_ms(&sc->sc_bus, USB_RESUME_TIMEOUT); 1542 1543 spin_lock_irqsave(&hsotg->lock, flags); 1544 hprt0 = dwc2_read_hprt0(hsotg); 1545 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP); 1546 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1547 hsotg->bus_suspended = 0; 1548 spin_unlock_irqrestore(&hsotg->lock, flags); 1549 } 1550 1551 /* Handles hub class-specific requests */ 1552 int 1553 dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq, 1554 u16 wvalue, u16 windex, char *buf, u16 wlength) 1555 { 1556 usb_hub_descriptor_t *hub_desc; 1557 usb_port_status_t ps; 1558 int retval = 0; 1559 u32 hprt0; 1560 u32 port_status; 1561 u32 speed; 1562 u32 pcgctl; 1563 1564 switch (typereq) { 1565 case ClearHubFeature: 1566 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue); 1567 1568 switch (wvalue) { 1569 case C_HUB_LOCAL_POWER: 1570 case C_HUB_OVER_CURRENT: 1571 /* Nothing required here */ 1572 break; 1573 1574 default: 1575 retval = -EINVAL; 1576 dev_err(hsotg->dev, 1577 "ClearHubFeature request %1xh unknown\n", 1578 wvalue); 1579 } 1580 break; 1581 1582 case ClearPortFeature: 1583 // if (wvalue != USB_PORT_FEAT_L1) 1584 if (!windex || windex > 1) 1585 goto error; 1586 switch (wvalue) { 1587 case USB_PORT_FEAT_ENABLE: 1588 dev_dbg(hsotg->dev, 1589 "ClearPortFeature USB_PORT_FEAT_ENABLE\n"); 1590 hprt0 = dwc2_read_hprt0(hsotg); 1591 hprt0 |= HPRT0_ENA; 1592 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1593 break; 1594 1595 case USB_PORT_FEAT_SUSPEND: 1596 dev_dbg(hsotg->dev, 1597 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n"); 1598 if (hsotg->bus_suspended) 1599 dwc2_port_resume(hsotg); 1600 break; 1601 1602 case USB_PORT_FEAT_POWER: 1603 dev_dbg(hsotg->dev, 1604 "ClearPortFeature USB_PORT_FEAT_POWER\n"); 1605 hprt0 = dwc2_read_hprt0(hsotg); 1606 hprt0 &= ~HPRT0_PWR; 1607 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1608 break; 1609 1610 case USB_PORT_FEAT_INDICATOR: 1611 dev_dbg(hsotg->dev, 1612 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n"); 1613 /* Port indicator not supported */ 1614 break; 1615 1616 case USB_PORT_FEAT_C_CONNECTION: 1617 /* 1618 * Clears driver's internal Connect Status Change flag 1619 */ 1620 dev_dbg(hsotg->dev, 1621 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n"); 1622 hsotg->flags.b.port_connect_status_change = 0; 1623 break; 1624 1625 case USB_PORT_FEAT_C_RESET: 1626 /* Clears driver's internal Port Reset Change flag */ 1627 dev_dbg(hsotg->dev, 1628 "ClearPortFeature USB_PORT_FEAT_C_RESET\n"); 1629 hsotg->flags.b.port_reset_change = 0; 1630 break; 1631 1632 case USB_PORT_FEAT_C_ENABLE: 1633 /* 1634 * Clears the driver's internal Port Enable/Disable 1635 * Change flag 1636 */ 1637 dev_dbg(hsotg->dev, 1638 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n"); 1639 hsotg->flags.b.port_enable_change = 0; 1640 break; 1641 1642 case USB_PORT_FEAT_C_SUSPEND: 1643 /* 1644 * Clears the driver's internal Port Suspend Change 1645 * flag, which is set when resume signaling on the host 1646 * port is complete 1647 */ 1648 dev_dbg(hsotg->dev, 1649 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n"); 1650 hsotg->flags.b.port_suspend_change = 0; 1651 break; 1652 1653 case USB_PORT_FEAT_C_PORT_L1: 1654 dev_dbg(hsotg->dev, 1655 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n"); 1656 hsotg->flags.b.port_l1_change = 0; 1657 break; 1658 1659 case USB_PORT_FEAT_C_OVER_CURRENT: 1660 dev_dbg(hsotg->dev, 1661 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n"); 1662 hsotg->flags.b.port_over_current_change = 0; 1663 break; 1664 1665 default: 1666 retval = -EINVAL; 1667 dev_err(hsotg->dev, 1668 "ClearPortFeature request %1xh unknown or unsupported\n", 1669 wvalue); 1670 } 1671 break; 1672 1673 case GetHubDescriptor: 1674 dev_dbg(hsotg->dev, "GetHubDescriptor\n"); 1675 hub_desc = (usb_hub_descriptor_t *)buf; 1676 hub_desc->bDescLength = 9; 1677 hub_desc->bDescriptorType = USB_DT_HUB; 1678 hub_desc->bNbrPorts = 1; 1679 USETW(hub_desc->wHubCharacteristics, HUB_CHAR_COMMON_LPSM | 1680 HUB_CHAR_INDV_PORT_OCPM); 1681 hub_desc->bPwrOn2PwrGood = 1; 1682 hub_desc->bHubContrCurrent = 0; 1683 hub_desc->DeviceRemovable[0] = 0; 1684 hub_desc->DeviceRemovable[1] = 0xff; 1685 break; 1686 1687 case GetHubStatus: 1688 dev_dbg(hsotg->dev, "GetHubStatus\n"); 1689 memset(buf, 0, 4); 1690 break; 1691 1692 case GetPortStatus: 1693 dev_vdbg(hsotg->dev, 1694 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex, 1695 hsotg->flags.d32); 1696 if (!windex || windex > 1) 1697 goto error; 1698 1699 port_status = 0; 1700 if (hsotg->flags.b.port_connect_status_change) 1701 port_status |= USB_PORT_STAT_C_CONNECTION; 1702 if (hsotg->flags.b.port_enable_change) 1703 port_status |= USB_PORT_STAT_C_ENABLE; 1704 if (hsotg->flags.b.port_suspend_change) 1705 port_status |= USB_PORT_STAT_C_SUSPEND; 1706 if (hsotg->flags.b.port_l1_change) 1707 port_status |= USB_PORT_STAT_C_L1; 1708 if (hsotg->flags.b.port_reset_change) 1709 port_status |= USB_PORT_STAT_C_RESET; 1710 if (hsotg->flags.b.port_over_current_change) { 1711 dev_warn(hsotg->dev, "Overcurrent change detected\n"); 1712 port_status |= USB_PORT_STAT_C_OVERCURRENT; 1713 } 1714 USETW(ps.wPortChange, port_status); 1715 1716 dev_vdbg(hsotg->dev, "wPortChange=%04x\n", port_status); 1717 if (!hsotg->flags.b.port_connect_status) { 1718 /* 1719 * The port is disconnected, which means the core is 1720 * either in device mode or it soon will be. Just 1721 * return 0's for the remainder of the port status 1722 * since the port register can't be read if the core 1723 * is in device mode. 1724 */ 1725 USETW(ps.wPortStatus, 0); 1726 memcpy(buf, &ps, sizeof(ps)); 1727 break; 1728 } 1729 1730 port_status = 0; 1731 hprt0 = DWC2_READ_4(hsotg, HPRT0); 1732 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0); 1733 1734 if (hprt0 & HPRT0_CONNSTS) 1735 port_status |= USB_PORT_STAT_CONNECTION; 1736 if (hprt0 & HPRT0_ENA) 1737 port_status |= USB_PORT_STAT_ENABLE; 1738 if (hprt0 & HPRT0_SUSP) 1739 port_status |= USB_PORT_STAT_SUSPEND; 1740 if (hprt0 & HPRT0_OVRCURRACT) 1741 port_status |= USB_PORT_STAT_OVERCURRENT; 1742 if (hprt0 & HPRT0_RST) 1743 port_status |= USB_PORT_STAT_RESET; 1744 if (hprt0 & HPRT0_PWR) 1745 port_status |= USB_PORT_STAT_POWER; 1746 1747 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT; 1748 if (speed == HPRT0_SPD_HIGH_SPEED) 1749 port_status |= USB_PORT_STAT_HIGH_SPEED; 1750 else if (speed == HPRT0_SPD_LOW_SPEED) 1751 port_status |= USB_PORT_STAT_LOW_SPEED; 1752 1753 if (hprt0 & HPRT0_TSTCTL_MASK) 1754 port_status |= USB_PORT_STAT_TEST; 1755 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */ 1756 USETW(ps.wPortStatus, port_status); 1757 1758 if (hsotg->core_params->dma_desc_fs_enable) { 1759 /* 1760 * Enable descriptor DMA only if a full speed 1761 * device is connected. 1762 */ 1763 if (hsotg->new_connection && 1764 ((port_status & 1765 (USB_PORT_STAT_CONNECTION | 1766 USB_PORT_STAT_HIGH_SPEED | 1767 USB_PORT_STAT_LOW_SPEED)) == 1768 USB_PORT_STAT_CONNECTION)) { 1769 u32 hcfg; 1770 1771 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n"); 1772 hsotg->core_params->dma_desc_enable = 1; 1773 hcfg = DWC2_READ_4(hsotg, HCFG); 1774 hcfg |= HCFG_DESCDMA; 1775 DWC2_WRITE_4(hsotg, HCFG, hcfg); 1776 hsotg->new_connection = false; 1777 } 1778 } 1779 dev_vdbg(hsotg->dev, "wPortStatus=%04x\n", port_status); 1780 memcpy(buf, &ps, sizeof(ps)); 1781 break; 1782 1783 case SetHubFeature: 1784 dev_dbg(hsotg->dev, "SetHubFeature\n"); 1785 /* No HUB features supported */ 1786 break; 1787 1788 case SetPortFeature: 1789 dev_dbg(hsotg->dev, "SetPortFeature\n"); 1790 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1)) 1791 goto error; 1792 1793 if (!hsotg->flags.b.port_connect_status) { 1794 /* 1795 * The port is disconnected, which means the core is 1796 * either in device mode or it soon will be. Just 1797 * return without doing anything since the port 1798 * register can't be written if the core is in device 1799 * mode. 1800 */ 1801 break; 1802 } 1803 1804 switch (wvalue) { 1805 case USB_PORT_FEAT_SUSPEND: 1806 dev_dbg(hsotg->dev, 1807 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n"); 1808 if (windex != hsotg->otg_port) 1809 goto error; 1810 dwc2_port_suspend(hsotg, windex); 1811 break; 1812 1813 case USB_PORT_FEAT_POWER: 1814 dev_dbg(hsotg->dev, 1815 "SetPortFeature - USB_PORT_FEAT_POWER\n"); 1816 hprt0 = dwc2_read_hprt0(hsotg); 1817 hprt0 |= HPRT0_PWR; 1818 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1819 break; 1820 1821 case USB_PORT_FEAT_RESET: 1822 hprt0 = dwc2_read_hprt0(hsotg); 1823 dev_dbg(hsotg->dev, 1824 "SetPortFeature - USB_PORT_FEAT_RESET\n"); 1825 pcgctl = DWC2_READ_4(hsotg, PCGCTL); 1826 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK); 1827 DWC2_WRITE_4(hsotg, PCGCTL, pcgctl); 1828 /* ??? Original driver does this */ 1829 DWC2_WRITE_4(hsotg, PCGCTL, 0); 1830 1831 hprt0 = dwc2_read_hprt0(hsotg); 1832 /* Clear suspend bit if resetting from suspend state */ 1833 hprt0 &= ~HPRT0_SUSP; 1834 1835 /* 1836 * When B-Host the Port reset bit is set in the Start 1837 * HCD Callback function, so that the reset is started 1838 * within 1ms of the HNP success interrupt 1839 */ 1840 if (!dwc2_hcd_is_b_host(hsotg)) { 1841 hprt0 |= HPRT0_PWR | HPRT0_RST; 1842 dev_dbg(hsotg->dev, 1843 "In host mode, hprt0=%08x\n", hprt0); 1844 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1845 } 1846 1847 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */ 1848 usleep_range(50000, 70000); 1849 hprt0 &= ~HPRT0_RST; 1850 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1851 hsotg->lx_state = DWC2_L0; /* Now back to On state */ 1852 break; 1853 1854 case USB_PORT_FEAT_INDICATOR: 1855 dev_dbg(hsotg->dev, 1856 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n"); 1857 /* Not supported */ 1858 break; 1859 1860 case USB_PORT_FEAT_TEST: 1861 hprt0 = dwc2_read_hprt0(hsotg); 1862 dev_dbg(hsotg->dev, 1863 "SetPortFeature - USB_PORT_FEAT_TEST\n"); 1864 hprt0 &= ~HPRT0_TSTCTL_MASK; 1865 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT; 1866 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 1867 break; 1868 1869 default: 1870 retval = -EINVAL; 1871 dev_err(hsotg->dev, 1872 "SetPortFeature %1xh unknown or unsupported\n", 1873 wvalue); 1874 break; 1875 } 1876 break; 1877 1878 default: 1879 error: 1880 retval = -EINVAL; 1881 dev_dbg(hsotg->dev, 1882 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n", 1883 typereq, windex, wvalue); 1884 break; 1885 } 1886 1887 return retval; 1888 } 1889 1890 int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg) 1891 { 1892 u32 hfnum = DWC2_READ_4(hsotg, HFNUM); 1893 1894 #ifdef DWC2_DEBUG_SOF 1895 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n", 1896 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT); 1897 #endif 1898 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT; 1899 } 1900 1901 int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg) 1902 { 1903 return hsotg->op_state == OTG_STATE_B_HOST; 1904 } 1905 1906 struct dwc2_hcd_urb * 1907 dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg, int iso_desc_count, 1908 gfp_t mem_flags) 1909 { 1910 struct dwc2_hcd_urb *urb; 1911 u32 size = sizeof(*urb) + iso_desc_count * 1912 sizeof(struct dwc2_hcd_iso_packet_desc); 1913 1914 urb = malloc(size, M_USBHC, M_ZERO | mem_flags); 1915 if (urb) 1916 urb->packet_count = iso_desc_count; 1917 return urb; 1918 } 1919 1920 void 1921 dwc2_hcd_urb_free(struct dwc2_hsotg *hsotg, struct dwc2_hcd_urb *urb, 1922 int iso_desc_count) 1923 { 1924 1925 u32 size = sizeof(*urb) + iso_desc_count * 1926 sizeof(struct dwc2_hcd_iso_packet_desc); 1927 1928 free(urb, M_USBHC, size); 1929 } 1930 1931 void 1932 dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg, struct dwc2_hcd_urb *urb, 1933 u8 dev_addr, u8 ep_num, u8 ep_type, u8 ep_dir, 1934 u16 mps) 1935 { 1936 if (dbg_perio() || 1937 ep_type == USB_ENDPOINT_XFER_BULK || 1938 ep_type == USB_ENDPOINT_XFER_CONTROL) 1939 dev_dbg(hsotg->dev, "urb=%p, xfer=%p\n", urb, urb->priv); 1940 urb->pipe_info.dev_addr = dev_addr; 1941 urb->pipe_info.ep_num = ep_num; 1942 urb->pipe_info.pipe_type = ep_type; 1943 urb->pipe_info.pipe_dir = ep_dir; 1944 urb->pipe_info.mps = mps; 1945 } 1946 1947 /* 1948 * NOTE: This function will be removed once the peripheral controller code 1949 * is integrated and the driver is stable 1950 */ 1951 void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg) 1952 { 1953 #ifdef DWC2_DEBUG 1954 struct dwc2_host_chan *chan; 1955 struct dwc2_hcd_urb *urb; 1956 struct dwc2_qtd *qtd; 1957 int num_channels; 1958 u32 np_tx_status; 1959 u32 p_tx_status; 1960 int i; 1961 1962 num_channels = hsotg->core_params->host_channels; 1963 dev_dbg(hsotg->dev, "\n"); 1964 dev_dbg(hsotg->dev, 1965 "************************************************************\n"); 1966 dev_dbg(hsotg->dev, "HCD State:\n"); 1967 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels); 1968 1969 for (i = 0; i < num_channels; i++) { 1970 chan = hsotg->hc_ptr_array[i]; 1971 dev_dbg(hsotg->dev, " Channel %d:\n", i); 1972 dev_dbg(hsotg->dev, 1973 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n", 1974 chan->dev_addr, chan->ep_num, chan->ep_is_in); 1975 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed); 1976 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type); 1977 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet); 1978 dev_dbg(hsotg->dev, " data_pid_start: %d\n", 1979 chan->data_pid_start); 1980 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count); 1981 dev_dbg(hsotg->dev, " xfer_started: %d\n", 1982 chan->xfer_started); 1983 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf); 1984 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n", 1985 (unsigned long)chan->xfer_dma); 1986 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len); 1987 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count); 1988 dev_dbg(hsotg->dev, " halt_on_queue: %d\n", 1989 chan->halt_on_queue); 1990 dev_dbg(hsotg->dev, " halt_pending: %d\n", 1991 chan->halt_pending); 1992 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status); 1993 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split); 1994 dev_dbg(hsotg->dev, " complete_split: %d\n", 1995 chan->complete_split); 1996 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr); 1997 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port); 1998 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos); 1999 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests); 2000 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh); 2001 2002 if (chan->xfer_started) { 2003 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", 2004 DWC2_READ_4(hsotg, HFNUM)); 2005 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", 2006 DWC2_READ_4(hsotg, HCCHAR(i))); 2007 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", 2008 DWC2_READ_4(hsotg, HCTSIZ(i))); 2009 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", 2010 DWC2_READ_4(hsotg, HCINT(i))); 2011 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", 2012 DWC2_READ_4(hsotg, HCINTMSK(i))); 2013 } 2014 2015 if (!(chan->xfer_started && chan->qh)) 2016 continue; 2017 2018 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) { 2019 if (!qtd->in_process) 2020 break; 2021 urb = qtd->urb; 2022 dev_dbg(hsotg->dev, " URB Info:\n"); 2023 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n", 2024 qtd, urb); 2025 if (urb) { 2026 dev_dbg(hsotg->dev, 2027 " Dev: %d, EP: %d %s\n", 2028 dwc2_hcd_get_dev_addr(&urb->pipe_info), 2029 dwc2_hcd_get_ep_num(&urb->pipe_info), 2030 dwc2_hcd_is_pipe_in(&urb->pipe_info) ? 2031 "IN" : "OUT"); 2032 dev_dbg(hsotg->dev, 2033 " Max packet size: %d\n", 2034 dwc2_hcd_get_mps(&urb->pipe_info)); 2035 dev_dbg(hsotg->dev, 2036 " transfer_buffer: %p\n", 2037 urb->buf); 2038 dev_dbg(hsotg->dev, 2039 " transfer_dma: %08lx\n", 2040 (unsigned long)urb->dma); 2041 dev_dbg(hsotg->dev, 2042 " transfer_buffer_length: %d\n", 2043 urb->length); 2044 dev_dbg(hsotg->dev, " actual_length: %d\n", 2045 urb->actual_length); 2046 } 2047 } 2048 } 2049 2050 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n", 2051 hsotg->non_periodic_channels); 2052 dev_dbg(hsotg->dev, " periodic_channels: %d\n", 2053 hsotg->periodic_channels); 2054 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs); 2055 np_tx_status = DWC2_READ_4(hsotg, GNPTXSTS); 2056 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n", 2057 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT); 2058 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n", 2059 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT); 2060 p_tx_status = DWC2_READ_4(hsotg, HPTXSTS); 2061 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n", 2062 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT); 2063 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n", 2064 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT); 2065 dwc2_hcd_dump_frrem(hsotg); 2066 2067 dwc2_dump_global_registers(hsotg); 2068 dwc2_dump_host_registers(hsotg); 2069 dev_dbg(hsotg->dev, 2070 "************************************************************\n"); 2071 dev_dbg(hsotg->dev, "\n"); 2072 #endif 2073 } 2074 2075 /* 2076 * NOTE: This function will be removed once the peripheral controller code 2077 * is integrated and the driver is stable 2078 */ 2079 void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg) 2080 { 2081 #ifdef DWC2_DUMP_FRREM 2082 dev_dbg(hsotg->dev, "Frame remaining at SOF:\n"); 2083 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2084 hsotg->frrem_samples, hsotg->frrem_accum, 2085 hsotg->frrem_samples > 0 ? 2086 hsotg->frrem_accum / hsotg->frrem_samples : 0); 2087 dev_dbg(hsotg->dev, "\n"); 2088 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n"); 2089 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2090 hsotg->hfnum_7_samples, 2091 hsotg->hfnum_7_frrem_accum, 2092 hsotg->hfnum_7_samples > 0 ? 2093 hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0); 2094 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n"); 2095 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2096 hsotg->hfnum_0_samples, 2097 hsotg->hfnum_0_frrem_accum, 2098 hsotg->hfnum_0_samples > 0 ? 2099 hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0); 2100 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n"); 2101 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2102 hsotg->hfnum_other_samples, 2103 hsotg->hfnum_other_frrem_accum, 2104 hsotg->hfnum_other_samples > 0 ? 2105 hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples : 2106 0); 2107 dev_dbg(hsotg->dev, "\n"); 2108 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n"); 2109 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2110 hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a, 2111 hsotg->hfnum_7_samples_a > 0 ? 2112 hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0); 2113 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n"); 2114 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2115 hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a, 2116 hsotg->hfnum_0_samples_a > 0 ? 2117 hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0); 2118 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n"); 2119 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2120 hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a, 2121 hsotg->hfnum_other_samples_a > 0 ? 2122 hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a 2123 : 0); 2124 dev_dbg(hsotg->dev, "\n"); 2125 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n"); 2126 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2127 hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b, 2128 hsotg->hfnum_7_samples_b > 0 ? 2129 hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0); 2130 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n"); 2131 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2132 hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b, 2133 (hsotg->hfnum_0_samples_b > 0) ? 2134 hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0); 2135 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n"); 2136 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n", 2137 hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b, 2138 (hsotg->hfnum_other_samples_b > 0) ? 2139 hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b 2140 : 0); 2141 #endif 2142 } 2143 2144 struct wrapper_priv_data { 2145 struct dwc2_hsotg *hsotg; 2146 }; 2147 2148 2149 void dwc2_host_start(struct dwc2_hsotg *hsotg) 2150 { 2151 // struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg); 2152 2153 // hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg); 2154 _dwc2_hcd_start(hsotg); 2155 } 2156 2157 void dwc2_host_disconnect(struct dwc2_hsotg *hsotg) 2158 { 2159 // struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg); 2160 2161 // hcd->self.is_b_host = 0; 2162 } 2163 2164 /* 2165 * Work queue function for starting the HCD when A-Cable is connected 2166 */ 2167 STATIC void dwc2_hcd_start_func(void *data) 2168 { 2169 struct dwc2_hsotg *hsotg = data; 2170 2171 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg); 2172 dwc2_host_start(hsotg); 2173 } 2174 2175 /* 2176 * Reset work queue function 2177 */ 2178 STATIC void dwc2_hcd_reset_func(void *data) 2179 { 2180 struct dwc2_hsotg *hsotg = data; 2181 unsigned long flags; 2182 u32 hprt0; 2183 2184 dev_dbg(hsotg->dev, "USB RESET function called\n"); 2185 2186 spin_lock_irqsave(&hsotg->lock, flags); 2187 2188 hprt0 = dwc2_read_hprt0(hsotg); 2189 hprt0 &= ~HPRT0_RST; 2190 DWC2_WRITE_4(hsotg, HPRT0, hprt0); 2191 hsotg->flags.b.port_reset_change = 1; 2192 2193 dwc2_root_intr(hsotg->hsotg_sc); 2194 2195 spin_unlock_irqrestore(&hsotg->lock, flags); 2196 } 2197 2198 /* 2199 * ========================================================================= 2200 * Linux HC Driver Functions 2201 * ========================================================================= 2202 */ 2203 2204 /* 2205 * Initializes the DWC_otg controller and its root hub and prepares it for host 2206 * mode operation. Activates the root port. Returns 0 on success and a negative 2207 * error code on failure. 2208 */ 2209 2210 /* 2211 * Frees secondary storage associated with the dwc2_hsotg structure contained 2212 * in the struct usb_hcd field 2213 */ 2214 STATIC void dwc2_hcd_free(struct dwc2_hsotg *hsotg) 2215 { 2216 u32 ahbcfg; 2217 u32 dctl; 2218 int i; 2219 2220 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n"); 2221 2222 /* Free memory for QH/QTD lists */ 2223 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive); 2224 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting); 2225 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active); 2226 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive); 2227 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready); 2228 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned); 2229 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued); 2230 2231 /* Free memory for the host channels */ 2232 for (i = 0; i < MAX_EPS_CHANNELS; i++) { 2233 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i]; 2234 2235 if (chan != NULL) { 2236 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n", 2237 i, chan); 2238 hsotg->hc_ptr_array[i] = NULL; 2239 free(chan, M_USBHC, sizeof(*chan)); 2240 } 2241 } 2242 2243 if (hsotg->core_params->dma_enable > 0) { 2244 if (hsotg->status_buf) { 2245 usb_freemem(&hsotg->hsotg_sc->sc_bus, 2246 &hsotg->status_buf_usbdma); 2247 hsotg->status_buf = NULL; 2248 } 2249 } else { 2250 free(hsotg->status_buf, M_USBHC, DWC2_HCD_STATUS_BUF_SIZE); 2251 hsotg->status_buf = NULL; 2252 } 2253 2254 ahbcfg = DWC2_READ_4(hsotg, GAHBCFG); 2255 2256 /* Disable all interrupts */ 2257 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN; 2258 DWC2_WRITE_4(hsotg, GAHBCFG, ahbcfg); 2259 DWC2_WRITE_4(hsotg, GINTMSK, 0); 2260 2261 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) { 2262 dctl = DWC2_READ_4(hsotg, DCTL); 2263 dctl |= DCTL_SFTDISCON; 2264 DWC2_WRITE_4(hsotg, DCTL, dctl); 2265 } 2266 2267 if (hsotg->wq_otg) { 2268 taskq_destroy(hsotg->wq_otg); 2269 } 2270 2271 free(hsotg->core_params, M_USBHC, sizeof(*hsotg->core_params)); 2272 hsotg->core_params = NULL; 2273 timeout_del(&hsotg->wkp_timer); 2274 } 2275 2276 STATIC void dwc2_hcd_release(struct dwc2_hsotg *hsotg) 2277 { 2278 /* Turn off all host-specific interrupts */ 2279 dwc2_disable_host_interrupts(hsotg); 2280 2281 dwc2_hcd_free(hsotg); 2282 } 2283 2284 /* 2285 * Initializes the HCD. This function allocates memory for and initializes the 2286 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the 2287 * USB bus with the core and calls the hc_driver->start() function. It returns 2288 * a negative error on failure. 2289 */ 2290 int dwc2_hcd_init(struct dwc2_hsotg *hsotg) 2291 { 2292 struct dwc2_host_chan *channel; 2293 int i, num_channels; 2294 int retval; 2295 2296 if (usb_disabled()) 2297 return -ENODEV; 2298 2299 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n"); 2300 2301 retval = -ENOMEM; 2302 2303 dev_dbg(hsotg->dev, "hcfg=%08x\n", DWC2_READ_4(hsotg, HCFG)); 2304 2305 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS 2306 hsotg->frame_num_array = malloc(sizeof(*hsotg->frame_num_array) * 2307 FRAME_NUM_ARRAY_SIZE, M_USBHC, 2308 M_ZERO | M_WAITOK); 2309 if (!hsotg->frame_num_array) 2310 goto error1; 2311 hsotg->last_frame_num_array = malloc( 2312 sizeof(*hsotg->last_frame_num_array) * 2313 FRAME_NUM_ARRAY_SIZE, M_USBHC, M_ZERO | M_WAITOK); 2314 if (!hsotg->last_frame_num_array) 2315 goto error1; 2316 hsotg->last_frame_num = HFNUM_MAX_FRNUM; 2317 #endif 2318 2319 spin_lock_init(&hsotg->lock); 2320 2321 /* 2322 * Disable the global interrupt until all the interrupt handlers are 2323 * installed 2324 */ 2325 dwc2_disable_global_interrupts(hsotg); 2326 2327 /* Initialize the DWC_otg core, and select the Phy type */ 2328 retval = dwc2_core_init(hsotg, true); 2329 if (retval) 2330 goto error2; 2331 2332 /* Create new workqueue and init work */ 2333 retval = -ENOMEM; 2334 hsotg->wq_otg = taskq_create("dwc2", 1, IPL_USB, 0); 2335 if (!hsotg->wq_otg) { 2336 dev_err(hsotg->dev, "Failed to create workqueue\n"); 2337 goto error2; 2338 } 2339 task_set(&hsotg->wf_otg, dwc2_conn_id_status_change, hsotg); 2340 2341 timeout_set(&hsotg->wkp_timer, dwc2_wakeup_detected, hsotg); 2342 2343 /* Initialize the non-periodic schedule */ 2344 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive); 2345 INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting); 2346 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active); 2347 2348 /* Initialize the periodic schedule */ 2349 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive); 2350 INIT_LIST_HEAD(&hsotg->periodic_sched_ready); 2351 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned); 2352 INIT_LIST_HEAD(&hsotg->periodic_sched_queued); 2353 2354 INIT_LIST_HEAD(&hsotg->split_order); 2355 2356 /* 2357 * Create a host channel descriptor for each host channel implemented 2358 * in the controller. Initialize the channel descriptor array. 2359 */ 2360 INIT_LIST_HEAD(&hsotg->free_hc_list); 2361 num_channels = hsotg->core_params->host_channels; 2362 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array)); 2363 2364 for (i = 0; i < num_channels; i++) { 2365 channel = malloc(sizeof(*channel), M_USBHC, M_ZERO | M_WAITOK); 2366 if (channel == NULL) 2367 goto error3; 2368 channel->hc_num = i; 2369 INIT_LIST_HEAD(&channel->split_order_list_entry); 2370 hsotg->hc_ptr_array[i] = channel; 2371 } 2372 2373 if (hsotg->core_params->uframe_sched > 0) 2374 dwc2_hcd_init_usecs(hsotg); 2375 2376 /* Initialize hsotg start work */ 2377 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func, hsotg); 2378 2379 /* Initialize port reset work */ 2380 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func, hsotg); 2381 2382 /* 2383 * Allocate space for storing data on status transactions. Normally no 2384 * data is sent, but this space acts as a bit bucket. This must be 2385 * done after usb_add_hcd since that function allocates the DMA buffer 2386 * pool. 2387 */ 2388 hsotg->status_buf = NULL; 2389 if (hsotg->core_params->dma_enable > 0) { 2390 int error = usb_allocmem(&hsotg->hsotg_sc->sc_bus, 2391 DWC2_HCD_STATUS_BUF_SIZE, 0, USB_DMA_COHERENT, 2392 &hsotg->status_buf_usbdma); 2393 if (!error) { 2394 hsotg->status_buf = KERNADDR(&hsotg->status_buf_usbdma, 0); 2395 hsotg->status_buf_dma = DMAADDR(&hsotg->status_buf_usbdma, 0); 2396 } 2397 } else 2398 hsotg->status_buf = malloc(DWC2_HCD_STATUS_BUF_SIZE, M_USBHC, 2399 M_ZERO | M_WAITOK); 2400 2401 /* retval is already -ENOMEM */ 2402 if (!hsotg->status_buf) 2403 goto error3; 2404 2405 hsotg->otg_port = 1; 2406 hsotg->frame_list = NULL; 2407 hsotg->frame_list_dma = 0; 2408 hsotg->periodic_qh_count = 0; 2409 2410 /* Initiate lx_state to L3 disconnected state */ 2411 hsotg->lx_state = DWC2_L3; 2412 2413 _dwc2_hcd_start(hsotg); 2414 2415 dwc2_hcd_dump_state(hsotg); 2416 2417 dwc2_enable_global_interrupts(hsotg); 2418 2419 return 0; 2420 2421 error3: 2422 dwc2_hcd_release(hsotg); 2423 error2: 2424 if (hsotg->core_params != NULL) 2425 free(hsotg->core_params, M_USBHC, sizeof(*hsotg->core_params)); 2426 2427 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS 2428 if (hsotg->last_frame_num_array != NULL) 2429 free(hsotg->last_frame_num_array, M_USBHC, 2430 sizeof(*hsotg->last_frame_num_array) * FRAME_NUM_ARRAY_SIZE); 2431 if (hsotg->frame_num_array != NULL) 2432 free(hsotg->frame_num_array, M_USBHC, 2433 sizeof(*hsotg->frame_num_array) * FRAME_NUM_ARRAY_SIZE); 2434 #endif 2435 2436 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval); 2437 return retval; 2438 } 2439 2440 /* 2441 * Removes the HCD. 2442 * Frees memory and resources associated with the HCD and deregisters the bus. 2443 */ 2444 void dwc2_hcd_remove(struct dwc2_hsotg *hsotg) 2445 { 2446 struct usb_hcd *hcd; 2447 2448 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n"); 2449 2450 hcd = dwc2_hsotg_to_hcd(hsotg); 2451 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd); 2452 2453 if (!hcd) { 2454 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n", 2455 __func__); 2456 return; 2457 } 2458 hsotg->priv = NULL; 2459 2460 dwc2_hcd_release(hsotg); 2461 2462 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS 2463 free(hsotg->last_frame_num_array, M_USBHC, sizeof(*hsotg->last_frame_num_array) * FRAME_NUM_ARRAY_SIZE); 2464 free(hsotg->frame_num_array, M_USBHC, sizeof(*hsotg->frame_num_array) * FRAME_NUM_ARRAY_SIZE); 2465 #endif 2466 } 2467