1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* 3 * hcd.c - DesignWare HS OTG Controller host-mode routines 4 * 5 * Copyright (C) 2004-2013 Synopsys, Inc. 6 */ 7 8 /* 9 * This file contains the core HCD code, and implements the Linux hc_driver 10 * API 11 */ 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/spinlock.h> 15 #include <linux/interrupt.h> 16 #include <linux/platform_device.h> 17 #include <linux/dma-mapping.h> 18 #include <linux/delay.h> 19 #include <linux/io.h> 20 #include <linux/slab.h> 21 #include <linux/usb.h> 22 23 #include <linux/usb/hcd.h> 24 #include <linux/usb/ch11.h> 25 #include <linux/usb/of.h> 26 27 #include "core.h" 28 #include "hcd.h" 29 30 /* 31 * ========================================================================= 32 * Host Core Layer Functions 33 * ========================================================================= 34 */ 35 36 /** 37 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts, 38 * used in both device and host modes 39 * 40 * @hsotg: Programming view of the DWC_otg controller 41 */ 42 static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg) 43 { 44 u32 intmsk; 45 46 /* Clear any pending OTG Interrupts */ 47 dwc2_writel(hsotg, 0xffffffff, GOTGINT); 48 49 /* Clear any pending interrupts */ 50 dwc2_writel(hsotg, 0xffffffff, GINTSTS); 51 52 /* Enable the interrupts in the GINTMSK */ 53 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT; 54 55 if (!hsotg->params.host_dma) 56 intmsk |= GINTSTS_RXFLVL; 57 if (!hsotg->params.external_id_pin_ctl) 58 intmsk |= GINTSTS_CONIDSTSCHNG; 59 60 intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP | 61 GINTSTS_SESSREQINT; 62 63 if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm) 64 intmsk |= GINTSTS_LPMTRANRCVD; 65 66 dwc2_writel(hsotg, intmsk, GINTMSK); 67 } 68 69 static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg) 70 { 71 u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG); 72 73 switch (hsotg->hw_params.arch) { 74 case GHWCFG2_EXT_DMA_ARCH: 75 dev_err(hsotg->dev, "External DMA Mode not supported\n"); 76 return -EINVAL; 77 78 case GHWCFG2_INT_DMA_ARCH: 79 dev_dbg(hsotg->dev, "Internal DMA Mode\n"); 80 if (hsotg->params.ahbcfg != -1) { 81 ahbcfg &= GAHBCFG_CTRL_MASK; 82 ahbcfg |= hsotg->params.ahbcfg & 83 ~GAHBCFG_CTRL_MASK; 84 } 85 break; 86 87 case GHWCFG2_SLAVE_ONLY_ARCH: 88 default: 89 dev_dbg(hsotg->dev, "Slave Only Mode\n"); 90 break; 91 } 92 93 if (hsotg->params.host_dma) 94 ahbcfg |= GAHBCFG_DMA_EN; 95 else 96 hsotg->params.dma_desc_enable = false; 97 98 dwc2_writel(hsotg, ahbcfg, GAHBCFG); 99 100 return 0; 101 } 102 103 static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg) 104 { 105 u32 usbcfg; 106 107 usbcfg = dwc2_readl(hsotg, GUSBCFG); 108 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP); 109 110 switch (hsotg->hw_params.op_mode) { 111 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE: 112 if (hsotg->params.otg_caps.hnp_support && 113 hsotg->params.otg_caps.srp_support) 114 usbcfg |= GUSBCFG_HNPCAP; 115 fallthrough; 116 117 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE: 118 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE: 119 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST: 120 if (hsotg->params.otg_caps.srp_support) 121 usbcfg |= GUSBCFG_SRPCAP; 122 break; 123 124 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE: 125 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE: 126 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST: 127 default: 128 break; 129 } 130 131 dwc2_writel(hsotg, usbcfg, GUSBCFG); 132 } 133 134 static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg) 135 { 136 if (hsotg->vbus_supply) 137 return regulator_enable(hsotg->vbus_supply); 138 139 return 0; 140 } 141 142 static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg) 143 { 144 if (hsotg->vbus_supply) 145 return regulator_disable(hsotg->vbus_supply); 146 147 return 0; 148 } 149 150 /** 151 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts 152 * 153 * @hsotg: Programming view of DWC_otg controller 154 */ 155 static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg) 156 { 157 u32 intmsk; 158 159 dev_dbg(hsotg->dev, "%s()\n", __func__); 160 161 /* Disable all interrupts */ 162 dwc2_writel(hsotg, 0, GINTMSK); 163 dwc2_writel(hsotg, 0, HAINTMSK); 164 165 /* Enable the common interrupts */ 166 dwc2_enable_common_interrupts(hsotg); 167 168 /* Enable host mode interrupts without disturbing common interrupts */ 169 intmsk = dwc2_readl(hsotg, GINTMSK); 170 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT; 171 dwc2_writel(hsotg, intmsk, GINTMSK); 172 } 173 174 /** 175 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts 176 * 177 * @hsotg: Programming view of DWC_otg controller 178 */ 179 static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg) 180 { 181 u32 intmsk = dwc2_readl(hsotg, GINTMSK); 182 183 /* Disable host mode interrupts without disturbing common interrupts */ 184 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT | 185 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT); 186 dwc2_writel(hsotg, intmsk, GINTMSK); 187 } 188 189 /* 190 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size 191 * For system that have a total fifo depth that is smaller than the default 192 * RX + TX fifo size. 193 * 194 * @hsotg: Programming view of DWC_otg controller 195 */ 196 static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg) 197 { 198 struct dwc2_core_params *params = &hsotg->params; 199 struct dwc2_hw_params *hw = &hsotg->hw_params; 200 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size; 201 202 total_fifo_size = hw->total_fifo_size; 203 rxfsiz = params->host_rx_fifo_size; 204 nptxfsiz = params->host_nperio_tx_fifo_size; 205 ptxfsiz = params->host_perio_tx_fifo_size; 206 207 /* 208 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth 209 * allocation with support for high bandwidth endpoints. Synopsys 210 * defines MPS(Max Packet size) for a periodic EP=1024, and for 211 * non-periodic as 512. 212 */ 213 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) { 214 /* 215 * For Buffer DMA mode/Scatter Gather DMA mode 216 * 2 * ((Largest Packet size / 4) + 1 + 1) + n 217 * with n = number of host channel. 218 * 2 * ((1024/4) + 2) = 516 219 */ 220 rxfsiz = 516 + hw->host_channels; 221 222 /* 223 * min non-periodic tx fifo depth 224 * 2 * (largest non-periodic USB packet used / 4) 225 * 2 * (512/4) = 256 226 */ 227 nptxfsiz = 256; 228 229 /* 230 * min periodic tx fifo depth 231 * (largest packet size*MC)/4 232 * (1024 * 3)/4 = 768 233 */ 234 ptxfsiz = 768; 235 236 params->host_rx_fifo_size = rxfsiz; 237 params->host_nperio_tx_fifo_size = nptxfsiz; 238 params->host_perio_tx_fifo_size = ptxfsiz; 239 } 240 241 /* 242 * If the summation of RX, NPTX and PTX fifo sizes is still 243 * bigger than the total_fifo_size, then we have a problem. 244 * 245 * We won't be able to allocate as many endpoints. Right now, 246 * we're just printing an error message, but ideally this FIFO 247 * allocation algorithm would be improved in the future. 248 * 249 * FIXME improve this FIFO allocation algorithm. 250 */ 251 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz))) 252 dev_err(hsotg->dev, "invalid fifo sizes\n"); 253 } 254 255 static void dwc2_config_fifos(struct dwc2_hsotg *hsotg) 256 { 257 struct dwc2_core_params *params = &hsotg->params; 258 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz; 259 260 if (!params->enable_dynamic_fifo) 261 return; 262 263 dwc2_calculate_dynamic_fifo(hsotg); 264 265 /* Rx FIFO */ 266 grxfsiz = dwc2_readl(hsotg, GRXFSIZ); 267 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz); 268 grxfsiz &= ~GRXFSIZ_DEPTH_MASK; 269 grxfsiz |= params->host_rx_fifo_size << 270 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK; 271 dwc2_writel(hsotg, grxfsiz, GRXFSIZ); 272 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n", 273 dwc2_readl(hsotg, GRXFSIZ)); 274 275 /* Non-periodic Tx FIFO */ 276 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n", 277 dwc2_readl(hsotg, GNPTXFSIZ)); 278 nptxfsiz = params->host_nperio_tx_fifo_size << 279 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK; 280 nptxfsiz |= params->host_rx_fifo_size << 281 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK; 282 dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ); 283 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n", 284 dwc2_readl(hsotg, GNPTXFSIZ)); 285 286 /* Periodic Tx FIFO */ 287 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n", 288 dwc2_readl(hsotg, HPTXFSIZ)); 289 hptxfsiz = params->host_perio_tx_fifo_size << 290 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK; 291 hptxfsiz |= (params->host_rx_fifo_size + 292 params->host_nperio_tx_fifo_size) << 293 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK; 294 dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ); 295 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n", 296 dwc2_readl(hsotg, HPTXFSIZ)); 297 298 if (hsotg->params.en_multiple_tx_fifo && 299 hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) { 300 /* 301 * This feature was implemented in 2.91a version 302 * Global DFIFOCFG calculation for Host mode - 303 * include RxFIFO, NPTXFIFO and HPTXFIFO 304 */ 305 dfifocfg = dwc2_readl(hsotg, GDFIFOCFG); 306 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK; 307 dfifocfg |= (params->host_rx_fifo_size + 308 params->host_nperio_tx_fifo_size + 309 params->host_perio_tx_fifo_size) << 310 GDFIFOCFG_EPINFOBASE_SHIFT & 311 GDFIFOCFG_EPINFOBASE_MASK; 312 dwc2_writel(hsotg, dfifocfg, GDFIFOCFG); 313 } 314 } 315 316 /** 317 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for 318 * the HFIR register according to PHY type and speed 319 * 320 * @hsotg: Programming view of DWC_otg controller 321 * 322 * NOTE: The caller can modify the value of the HFIR register only after the 323 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort) 324 * has been set 325 */ 326 u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg) 327 { 328 u32 usbcfg; 329 u32 hprt0; 330 int clock = 60; /* default value */ 331 332 usbcfg = dwc2_readl(hsotg, GUSBCFG); 333 hprt0 = dwc2_readl(hsotg, HPRT0); 334 335 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) && 336 !(usbcfg & GUSBCFG_PHYIF16)) 337 clock = 60; 338 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type == 339 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI) 340 clock = 48; 341 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) && 342 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16)) 343 clock = 30; 344 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) && 345 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16)) 346 clock = 60; 347 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) && 348 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16)) 349 clock = 48; 350 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) && 351 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI) 352 clock = 48; 353 if ((usbcfg & GUSBCFG_PHYSEL) && 354 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) 355 clock = 48; 356 357 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED) 358 /* High speed case */ 359 return 125 * clock - 1; 360 361 /* FS/LS case */ 362 return 1000 * clock - 1; 363 } 364 365 /** 366 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination 367 * buffer 368 * 369 * @hsotg: Programming view of DWC_otg controller 370 * @dest: Destination buffer for the packet 371 * @bytes: Number of bytes to copy to the destination 372 */ 373 void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes) 374 { 375 u32 *data_buf = (u32 *)dest; 376 int word_count = (bytes + 3) / 4; 377 int i; 378 379 /* 380 * Todo: Account for the case where dest is not dword aligned. This 381 * requires reading data from the FIFO into a u32 temp buffer, then 382 * moving it into the data buffer. 383 */ 384 385 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes); 386 387 for (i = 0; i < word_count; i++, data_buf++) 388 *data_buf = dwc2_readl(hsotg, HCFIFO(0)); 389 } 390 391 /** 392 * dwc2_dump_channel_info() - Prints the state of a host channel 393 * 394 * @hsotg: Programming view of DWC_otg controller 395 * @chan: Pointer to the channel to dump 396 * 397 * Must be called with interrupt disabled and spinlock held 398 * 399 * NOTE: This function will be removed once the peripheral controller code 400 * is integrated and the driver is stable 401 */ 402 static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg, 403 struct dwc2_host_chan *chan) 404 { 405 #ifdef VERBOSE_DEBUG 406 int num_channels = hsotg->params.host_channels; 407 struct dwc2_qh *qh; 408 u32 hcchar; 409 u32 hcsplt; 410 u32 hctsiz; 411 u32 hc_dma; 412 int i; 413 414 if (!chan) 415 return; 416 417 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num)); 418 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num)); 419 hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num)); 420 hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num)); 421 422 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan); 423 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n", 424 hcchar, hcsplt); 425 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n", 426 hctsiz, hc_dma); 427 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n", 428 chan->dev_addr, chan->ep_num, chan->ep_is_in); 429 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type); 430 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet); 431 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start); 432 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started); 433 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status); 434 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf); 435 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n", 436 (unsigned long)chan->xfer_dma); 437 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len); 438 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh); 439 dev_dbg(hsotg->dev, " NP inactive sched:\n"); 440 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive, 441 qh_list_entry) 442 dev_dbg(hsotg->dev, " %p\n", qh); 443 dev_dbg(hsotg->dev, " NP waiting sched:\n"); 444 list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting, 445 qh_list_entry) 446 dev_dbg(hsotg->dev, " %p\n", qh); 447 dev_dbg(hsotg->dev, " NP active sched:\n"); 448 list_for_each_entry(qh, &hsotg->non_periodic_sched_active, 449 qh_list_entry) 450 dev_dbg(hsotg->dev, " %p\n", qh); 451 dev_dbg(hsotg->dev, " Channels:\n"); 452 for (i = 0; i < num_channels; i++) { 453 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i]; 454 455 dev_dbg(hsotg->dev, " %2d: %p\n", i, chan); 456 } 457 #endif /* VERBOSE_DEBUG */ 458 } 459 460 static int _dwc2_hcd_start(struct usb_hcd *hcd); 461 462 static void dwc2_host_start(struct dwc2_hsotg *hsotg) 463 { 464 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg); 465 466 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg); 467 _dwc2_hcd_start(hcd); 468 } 469 470 static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg) 471 { 472 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg); 473 474 hcd->self.is_b_host = 0; 475 } 476 477 static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, 478 int *hub_addr, int *hub_port) 479 { 480 struct urb *urb = context; 481 482 if (urb->dev->tt) 483 *hub_addr = urb->dev->tt->hub->devnum; 484 else 485 *hub_addr = 0; 486 *hub_port = urb->dev->ttport; 487 } 488 489 /* 490 * ========================================================================= 491 * Low Level Host Channel Access Functions 492 * ========================================================================= 493 */ 494 495 static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg, 496 struct dwc2_host_chan *chan) 497 { 498 u32 hcintmsk = HCINTMSK_CHHLTD; 499 500 switch (chan->ep_type) { 501 case USB_ENDPOINT_XFER_CONTROL: 502 case USB_ENDPOINT_XFER_BULK: 503 dev_vdbg(hsotg->dev, "control/bulk\n"); 504 hcintmsk |= HCINTMSK_XFERCOMPL; 505 hcintmsk |= HCINTMSK_STALL; 506 hcintmsk |= HCINTMSK_XACTERR; 507 hcintmsk |= HCINTMSK_DATATGLERR; 508 if (chan->ep_is_in) { 509 hcintmsk |= HCINTMSK_BBLERR; 510 } else { 511 hcintmsk |= HCINTMSK_NAK; 512 hcintmsk |= HCINTMSK_NYET; 513 if (chan->do_ping) 514 hcintmsk |= HCINTMSK_ACK; 515 } 516 517 if (chan->do_split) { 518 hcintmsk |= HCINTMSK_NAK; 519 if (chan->complete_split) 520 hcintmsk |= HCINTMSK_NYET; 521 else 522 hcintmsk |= HCINTMSK_ACK; 523 } 524 525 if (chan->error_state) 526 hcintmsk |= HCINTMSK_ACK; 527 break; 528 529 case USB_ENDPOINT_XFER_INT: 530 if (dbg_perio()) 531 dev_vdbg(hsotg->dev, "intr\n"); 532 hcintmsk |= HCINTMSK_XFERCOMPL; 533 hcintmsk |= HCINTMSK_NAK; 534 hcintmsk |= HCINTMSK_STALL; 535 hcintmsk |= HCINTMSK_XACTERR; 536 hcintmsk |= HCINTMSK_DATATGLERR; 537 hcintmsk |= HCINTMSK_FRMOVRUN; 538 539 if (chan->ep_is_in) 540 hcintmsk |= HCINTMSK_BBLERR; 541 if (chan->error_state) 542 hcintmsk |= HCINTMSK_ACK; 543 if (chan->do_split) { 544 if (chan->complete_split) 545 hcintmsk |= HCINTMSK_NYET; 546 else 547 hcintmsk |= HCINTMSK_ACK; 548 } 549 break; 550 551 case USB_ENDPOINT_XFER_ISOC: 552 if (dbg_perio()) 553 dev_vdbg(hsotg->dev, "isoc\n"); 554 hcintmsk |= HCINTMSK_XFERCOMPL; 555 hcintmsk |= HCINTMSK_FRMOVRUN; 556 hcintmsk |= HCINTMSK_ACK; 557 558 if (chan->ep_is_in) { 559 hcintmsk |= HCINTMSK_XACTERR; 560 hcintmsk |= HCINTMSK_BBLERR; 561 } 562 break; 563 default: 564 dev_err(hsotg->dev, "## Unknown EP type ##\n"); 565 break; 566 } 567 568 dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num)); 569 if (dbg_hc(chan)) 570 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk); 571 } 572 573 static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg, 574 struct dwc2_host_chan *chan) 575 { 576 u32 hcintmsk = HCINTMSK_CHHLTD; 577 578 /* 579 * For Descriptor DMA mode core halts the channel on AHB error. 580 * Interrupt is not required. 581 */ 582 if (!hsotg->params.dma_desc_enable) { 583 if (dbg_hc(chan)) 584 dev_vdbg(hsotg->dev, "desc DMA disabled\n"); 585 hcintmsk |= HCINTMSK_AHBERR; 586 } else { 587 if (dbg_hc(chan)) 588 dev_vdbg(hsotg->dev, "desc DMA enabled\n"); 589 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) 590 hcintmsk |= HCINTMSK_XFERCOMPL; 591 } 592 593 if (chan->error_state && !chan->do_split && 594 chan->ep_type != USB_ENDPOINT_XFER_ISOC) { 595 if (dbg_hc(chan)) 596 dev_vdbg(hsotg->dev, "setting ACK\n"); 597 hcintmsk |= HCINTMSK_ACK; 598 if (chan->ep_is_in) { 599 hcintmsk |= HCINTMSK_DATATGLERR; 600 if (chan->ep_type != USB_ENDPOINT_XFER_INT) 601 hcintmsk |= HCINTMSK_NAK; 602 } 603 } 604 605 dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num)); 606 if (dbg_hc(chan)) 607 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk); 608 } 609 610 static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg, 611 struct dwc2_host_chan *chan) 612 { 613 u32 intmsk; 614 615 if (hsotg->params.host_dma) { 616 if (dbg_hc(chan)) 617 dev_vdbg(hsotg->dev, "DMA enabled\n"); 618 dwc2_hc_enable_dma_ints(hsotg, chan); 619 } else { 620 if (dbg_hc(chan)) 621 dev_vdbg(hsotg->dev, "DMA disabled\n"); 622 dwc2_hc_enable_slave_ints(hsotg, chan); 623 } 624 625 /* Enable the top level host channel interrupt */ 626 intmsk = dwc2_readl(hsotg, HAINTMSK); 627 intmsk |= 1 << chan->hc_num; 628 dwc2_writel(hsotg, intmsk, HAINTMSK); 629 if (dbg_hc(chan)) 630 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk); 631 632 /* Make sure host channel interrupts are enabled */ 633 intmsk = dwc2_readl(hsotg, GINTMSK); 634 intmsk |= GINTSTS_HCHINT; 635 dwc2_writel(hsotg, intmsk, GINTMSK); 636 if (dbg_hc(chan)) 637 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk); 638 } 639 640 /** 641 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from 642 * a specific endpoint 643 * 644 * @hsotg: Programming view of DWC_otg controller 645 * @chan: Information needed to initialize the host channel 646 * 647 * The HCCHARn register is set up with the characteristics specified in chan. 648 * Host channel interrupts that may need to be serviced while this transfer is 649 * in progress are enabled. 650 */ 651 static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan) 652 { 653 u8 hc_num = chan->hc_num; 654 u32 hcintmsk; 655 u32 hcchar; 656 u32 hcsplt = 0; 657 658 if (dbg_hc(chan)) 659 dev_vdbg(hsotg->dev, "%s()\n", __func__); 660 661 /* Clear old interrupt conditions for this host channel */ 662 hcintmsk = 0xffffffff; 663 hcintmsk &= ~HCINTMSK_RESERVED14_31; 664 dwc2_writel(hsotg, hcintmsk, HCINT(hc_num)); 665 666 /* Enable channel interrupts required for this transfer */ 667 dwc2_hc_enable_ints(hsotg, chan); 668 669 /* 670 * Program the HCCHARn register with the endpoint characteristics for 671 * the current transfer 672 */ 673 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK; 674 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK; 675 if (chan->ep_is_in) 676 hcchar |= HCCHAR_EPDIR; 677 if (chan->speed == USB_SPEED_LOW) 678 hcchar |= HCCHAR_LSPDDEV; 679 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK; 680 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK; 681 dwc2_writel(hsotg, hcchar, HCCHAR(hc_num)); 682 if (dbg_hc(chan)) { 683 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n", 684 hc_num, hcchar); 685 686 dev_vdbg(hsotg->dev, "%s: Channel %d\n", 687 __func__, hc_num); 688 dev_vdbg(hsotg->dev, " Dev Addr: %d\n", 689 chan->dev_addr); 690 dev_vdbg(hsotg->dev, " Ep Num: %d\n", 691 chan->ep_num); 692 dev_vdbg(hsotg->dev, " Is In: %d\n", 693 chan->ep_is_in); 694 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n", 695 chan->speed == USB_SPEED_LOW); 696 dev_vdbg(hsotg->dev, " Ep Type: %d\n", 697 chan->ep_type); 698 dev_vdbg(hsotg->dev, " Max Pkt: %d\n", 699 chan->max_packet); 700 } 701 702 /* Program the HCSPLT register for SPLITs */ 703 if (chan->do_split) { 704 if (dbg_hc(chan)) 705 dev_vdbg(hsotg->dev, 706 "Programming HC %d with split --> %s\n", 707 hc_num, 708 chan->complete_split ? "CSPLIT" : "SSPLIT"); 709 if (chan->complete_split) 710 hcsplt |= HCSPLT_COMPSPLT; 711 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT & 712 HCSPLT_XACTPOS_MASK; 713 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT & 714 HCSPLT_HUBADDR_MASK; 715 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT & 716 HCSPLT_PRTADDR_MASK; 717 if (dbg_hc(chan)) { 718 dev_vdbg(hsotg->dev, " comp split %d\n", 719 chan->complete_split); 720 dev_vdbg(hsotg->dev, " xact pos %d\n", 721 chan->xact_pos); 722 dev_vdbg(hsotg->dev, " hub addr %d\n", 723 chan->hub_addr); 724 dev_vdbg(hsotg->dev, " hub port %d\n", 725 chan->hub_port); 726 dev_vdbg(hsotg->dev, " is_in %d\n", 727 chan->ep_is_in); 728 dev_vdbg(hsotg->dev, " Max Pkt %d\n", 729 chan->max_packet); 730 dev_vdbg(hsotg->dev, " xferlen %d\n", 731 chan->xfer_len); 732 } 733 } 734 735 dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num)); 736 } 737 738 /** 739 * dwc2_hc_halt() - Attempts to halt a host channel 740 * 741 * @hsotg: Controller register interface 742 * @chan: Host channel to halt 743 * @halt_status: Reason for halting the channel 744 * 745 * This function should only be called in Slave mode or to abort a transfer in 746 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the 747 * controller halts the channel when the transfer is complete or a condition 748 * occurs that requires application intervention. 749 * 750 * In slave mode, checks for a free request queue entry, then sets the Channel 751 * Enable and Channel Disable bits of the Host Channel Characteristics 752 * register of the specified channel to intiate the halt. If there is no free 753 * request queue entry, sets only the Channel Disable bit of the HCCHARn 754 * register to flush requests for this channel. In the latter case, sets a 755 * flag to indicate that the host channel needs to be halted when a request 756 * queue slot is open. 757 * 758 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the 759 * HCCHARn register. The controller ensures there is space in the request 760 * queue before submitting the halt request. 761 * 762 * Some time may elapse before the core flushes any posted requests for this 763 * host channel and halts. The Channel Halted interrupt handler completes the 764 * deactivation of the host channel. 765 */ 766 void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan, 767 enum dwc2_halt_status halt_status) 768 { 769 u32 nptxsts, hptxsts, hcchar; 770 771 if (dbg_hc(chan)) 772 dev_vdbg(hsotg->dev, "%s()\n", __func__); 773 774 /* 775 * In buffer DMA or external DMA mode channel can't be halted 776 * for non-split periodic channels. At the end of the next 777 * uframe/frame (in the worst case), the core generates a channel 778 * halted and disables the channel automatically. 779 */ 780 if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) || 781 hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) { 782 if (!chan->do_split && 783 (chan->ep_type == USB_ENDPOINT_XFER_ISOC || 784 chan->ep_type == USB_ENDPOINT_XFER_INT)) { 785 dev_err(hsotg->dev, "%s() Channel can't be halted\n", 786 __func__); 787 return; 788 } 789 } 790 791 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS) 792 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status); 793 794 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE || 795 halt_status == DWC2_HC_XFER_AHB_ERR) { 796 /* 797 * Disable all channel interrupts except Ch Halted. The QTD 798 * and QH state associated with this transfer has been cleared 799 * (in the case of URB_DEQUEUE), so the channel needs to be 800 * shut down carefully to prevent crashes. 801 */ 802 u32 hcintmsk = HCINTMSK_CHHLTD; 803 804 dev_vdbg(hsotg->dev, "dequeue/error\n"); 805 dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num)); 806 807 /* 808 * Make sure no other interrupts besides halt are currently 809 * pending. Handling another interrupt could cause a crash due 810 * to the QTD and QH state. 811 */ 812 dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num)); 813 814 /* 815 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR 816 * even if the channel was already halted for some other 817 * reason 818 */ 819 chan->halt_status = halt_status; 820 821 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num)); 822 if (!(hcchar & HCCHAR_CHENA)) { 823 /* 824 * The channel is either already halted or it hasn't 825 * started yet. In DMA mode, the transfer may halt if 826 * it finishes normally or a condition occurs that 827 * requires driver intervention. Don't want to halt 828 * the channel again. In either Slave or DMA mode, 829 * it's possible that the transfer has been assigned 830 * to a channel, but not started yet when an URB is 831 * dequeued. Don't want to halt a channel that hasn't 832 * started yet. 833 */ 834 return; 835 } 836 } 837 if (chan->halt_pending) { 838 /* 839 * A halt has already been issued for this channel. This might 840 * happen when a transfer is aborted by a higher level in 841 * the stack. 842 */ 843 dev_vdbg(hsotg->dev, 844 "*** %s: Channel %d, chan->halt_pending already set ***\n", 845 __func__, chan->hc_num); 846 return; 847 } 848 849 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num)); 850 851 /* No need to set the bit in DDMA for disabling the channel */ 852 /* TODO check it everywhere channel is disabled */ 853 if (!hsotg->params.dma_desc_enable) { 854 if (dbg_hc(chan)) 855 dev_vdbg(hsotg->dev, "desc DMA disabled\n"); 856 hcchar |= HCCHAR_CHENA; 857 } else { 858 if (dbg_hc(chan)) 859 dev_dbg(hsotg->dev, "desc DMA enabled\n"); 860 } 861 hcchar |= HCCHAR_CHDIS; 862 863 if (!hsotg->params.host_dma) { 864 if (dbg_hc(chan)) 865 dev_vdbg(hsotg->dev, "DMA not enabled\n"); 866 hcchar |= HCCHAR_CHENA; 867 868 /* Check for space in the request queue to issue the halt */ 869 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL || 870 chan->ep_type == USB_ENDPOINT_XFER_BULK) { 871 dev_vdbg(hsotg->dev, "control/bulk\n"); 872 nptxsts = dwc2_readl(hsotg, GNPTXSTS); 873 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) { 874 dev_vdbg(hsotg->dev, "Disabling channel\n"); 875 hcchar &= ~HCCHAR_CHENA; 876 } 877 } else { 878 if (dbg_perio()) 879 dev_vdbg(hsotg->dev, "isoc/intr\n"); 880 hptxsts = dwc2_readl(hsotg, HPTXSTS); 881 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 || 882 hsotg->queuing_high_bandwidth) { 883 if (dbg_perio()) 884 dev_vdbg(hsotg->dev, "Disabling channel\n"); 885 hcchar &= ~HCCHAR_CHENA; 886 } 887 } 888 } else { 889 if (dbg_hc(chan)) 890 dev_vdbg(hsotg->dev, "DMA enabled\n"); 891 } 892 893 dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num)); 894 chan->halt_status = halt_status; 895 896 if (hcchar & HCCHAR_CHENA) { 897 if (dbg_hc(chan)) 898 dev_vdbg(hsotg->dev, "Channel enabled\n"); 899 chan->halt_pending = 1; 900 chan->halt_on_queue = 0; 901 } else { 902 if (dbg_hc(chan)) 903 dev_vdbg(hsotg->dev, "Channel disabled\n"); 904 chan->halt_on_queue = 1; 905 } 906 907 if (dbg_hc(chan)) { 908 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__, 909 chan->hc_num); 910 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n", 911 hcchar); 912 dev_vdbg(hsotg->dev, " halt_pending: %d\n", 913 chan->halt_pending); 914 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n", 915 chan->halt_on_queue); 916 dev_vdbg(hsotg->dev, " halt_status: %d\n", 917 chan->halt_status); 918 } 919 } 920 921 /** 922 * dwc2_hc_cleanup() - Clears the transfer state for a host channel 923 * 924 * @hsotg: Programming view of DWC_otg controller 925 * @chan: Identifies the host channel to clean up 926 * 927 * This function is normally called after a transfer is done and the host 928 * channel is being released 929 */ 930 void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan) 931 { 932 u32 hcintmsk; 933 934 chan->xfer_started = 0; 935 936 list_del_init(&chan->split_order_list_entry); 937 938 /* 939 * Clear channel interrupt enables and any unhandled channel interrupt 940 * conditions 941 */ 942 dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num)); 943 hcintmsk = 0xffffffff; 944 hcintmsk &= ~HCINTMSK_RESERVED14_31; 945 dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num)); 946 } 947 948 /** 949 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in 950 * which frame a periodic transfer should occur 951 * 952 * @hsotg: Programming view of DWC_otg controller 953 * @chan: Identifies the host channel to set up and its properties 954 * @hcchar: Current value of the HCCHAR register for the specified host channel 955 * 956 * This function has no effect on non-periodic transfers 957 */ 958 static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg, 959 struct dwc2_host_chan *chan, u32 *hcchar) 960 { 961 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 962 chan->ep_type == USB_ENDPOINT_XFER_ISOC) { 963 int host_speed; 964 int xfer_ns; 965 int xfer_us; 966 int bytes_in_fifo; 967 u16 fifo_space; 968 u16 frame_number; 969 u16 wire_frame; 970 971 /* 972 * Try to figure out if we're an even or odd frame. If we set 973 * even and the current frame number is even the transfer 974 * will happen immediately. Similar if both are odd. If one is 975 * even and the other is odd then the transfer will happen when 976 * the frame number ticks. 977 * 978 * There's a bit of a balancing act to get this right. 979 * Sometimes we may want to send data in the current frame (AK 980 * right away). We might want to do this if the frame number 981 * _just_ ticked, but we might also want to do this in order 982 * to continue a split transaction that happened late in a 983 * microframe (so we didn't know to queue the next transfer 984 * until the frame number had ticked). The problem is that we 985 * need a lot of knowledge to know if there's actually still 986 * time to send things or if it would be better to wait until 987 * the next frame. 988 * 989 * We can look at how much time is left in the current frame 990 * and make a guess about whether we'll have time to transfer. 991 * We'll do that. 992 */ 993 994 /* Get speed host is running at */ 995 host_speed = (chan->speed != USB_SPEED_HIGH && 996 !chan->do_split) ? chan->speed : USB_SPEED_HIGH; 997 998 /* See how many bytes are in the periodic FIFO right now */ 999 fifo_space = (dwc2_readl(hsotg, HPTXSTS) & 1000 TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT; 1001 bytes_in_fifo = sizeof(u32) * 1002 (hsotg->params.host_perio_tx_fifo_size - 1003 fifo_space); 1004 1005 /* 1006 * Roughly estimate bus time for everything in the periodic 1007 * queue + our new transfer. This is "rough" because we're 1008 * using a function that makes takes into account IN/OUT 1009 * and INT/ISO and we're just slamming in one value for all 1010 * transfers. This should be an over-estimate and that should 1011 * be OK, but we can probably tighten it. 1012 */ 1013 xfer_ns = usb_calc_bus_time(host_speed, false, false, 1014 chan->xfer_len + bytes_in_fifo); 1015 xfer_us = NS_TO_US(xfer_ns); 1016 1017 /* See what frame number we'll be at by the time we finish */ 1018 frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us); 1019 1020 /* This is when we were scheduled to be on the wire */ 1021 wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1); 1022 1023 /* 1024 * If we'd finish _after_ the frame we're scheduled in then 1025 * it's hopeless. Just schedule right away and hope for the 1026 * best. Note that it _might_ be wise to call back into the 1027 * scheduler to pick a better frame, but this is better than 1028 * nothing. 1029 */ 1030 if (dwc2_frame_num_gt(frame_number, wire_frame)) { 1031 dwc2_sch_vdbg(hsotg, 1032 "QH=%p EO MISS fr=%04x=>%04x (%+d)\n", 1033 chan->qh, wire_frame, frame_number, 1034 dwc2_frame_num_dec(frame_number, 1035 wire_frame)); 1036 wire_frame = frame_number; 1037 1038 /* 1039 * We picked a different frame number; communicate this 1040 * back to the scheduler so it doesn't try to schedule 1041 * another in the same frame. 1042 * 1043 * Remember that next_active_frame is 1 before the wire 1044 * frame. 1045 */ 1046 chan->qh->next_active_frame = 1047 dwc2_frame_num_dec(frame_number, 1); 1048 } 1049 1050 if (wire_frame & 1) 1051 *hcchar |= HCCHAR_ODDFRM; 1052 else 1053 *hcchar &= ~HCCHAR_ODDFRM; 1054 } 1055 } 1056 1057 static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan) 1058 { 1059 /* Set up the initial PID for the transfer */ 1060 if (chan->speed == USB_SPEED_HIGH) { 1061 if (chan->ep_is_in) { 1062 if (chan->multi_count == 1) 1063 chan->data_pid_start = DWC2_HC_PID_DATA0; 1064 else if (chan->multi_count == 2) 1065 chan->data_pid_start = DWC2_HC_PID_DATA1; 1066 else 1067 chan->data_pid_start = DWC2_HC_PID_DATA2; 1068 } else { 1069 if (chan->multi_count == 1) 1070 chan->data_pid_start = DWC2_HC_PID_DATA0; 1071 else 1072 chan->data_pid_start = DWC2_HC_PID_MDATA; 1073 } 1074 } else { 1075 chan->data_pid_start = DWC2_HC_PID_DATA0; 1076 } 1077 } 1078 1079 /** 1080 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with 1081 * the Host Channel 1082 * 1083 * @hsotg: Programming view of DWC_otg controller 1084 * @chan: Information needed to initialize the host channel 1085 * 1086 * This function should only be called in Slave mode. For a channel associated 1087 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel 1088 * associated with a periodic EP, the periodic Tx FIFO is written. 1089 * 1090 * Upon return the xfer_buf and xfer_count fields in chan are incremented by 1091 * the number of bytes written to the Tx FIFO. 1092 */ 1093 static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg, 1094 struct dwc2_host_chan *chan) 1095 { 1096 u32 i; 1097 u32 remaining_count; 1098 u32 byte_count; 1099 u32 dword_count; 1100 u32 *data_buf = (u32 *)chan->xfer_buf; 1101 1102 if (dbg_hc(chan)) 1103 dev_vdbg(hsotg->dev, "%s()\n", __func__); 1104 1105 remaining_count = chan->xfer_len - chan->xfer_count; 1106 if (remaining_count > chan->max_packet) 1107 byte_count = chan->max_packet; 1108 else 1109 byte_count = remaining_count; 1110 1111 dword_count = (byte_count + 3) / 4; 1112 1113 if (((unsigned long)data_buf & 0x3) == 0) { 1114 /* xfer_buf is DWORD aligned */ 1115 for (i = 0; i < dword_count; i++, data_buf++) 1116 dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num)); 1117 } else { 1118 /* xfer_buf is not DWORD aligned */ 1119 for (i = 0; i < dword_count; i++, data_buf++) { 1120 u32 data = data_buf[0] | data_buf[1] << 8 | 1121 data_buf[2] << 16 | data_buf[3] << 24; 1122 dwc2_writel(hsotg, data, HCFIFO(chan->hc_num)); 1123 } 1124 } 1125 1126 chan->xfer_count += byte_count; 1127 chan->xfer_buf += byte_count; 1128 } 1129 1130 /** 1131 * dwc2_hc_do_ping() - Starts a PING transfer 1132 * 1133 * @hsotg: Programming view of DWC_otg controller 1134 * @chan: Information needed to initialize the host channel 1135 * 1136 * This function should only be called in Slave mode. The Do Ping bit is set in 1137 * the HCTSIZ register, then the channel is enabled. 1138 */ 1139 static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, 1140 struct dwc2_host_chan *chan) 1141 { 1142 u32 hcchar; 1143 u32 hctsiz; 1144 1145 if (dbg_hc(chan)) 1146 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__, 1147 chan->hc_num); 1148 1149 hctsiz = TSIZ_DOPNG; 1150 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT; 1151 dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num)); 1152 1153 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num)); 1154 hcchar |= HCCHAR_CHENA; 1155 hcchar &= ~HCCHAR_CHDIS; 1156 dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num)); 1157 } 1158 1159 /** 1160 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host 1161 * channel and starts the transfer 1162 * 1163 * @hsotg: Programming view of DWC_otg controller 1164 * @chan: Information needed to initialize the host channel. The xfer_len value 1165 * may be reduced to accommodate the max widths of the XferSize and 1166 * PktCnt fields in the HCTSIZn register. The multi_count value may be 1167 * changed to reflect the final xfer_len value. 1168 * 1169 * This function may be called in either Slave mode or DMA mode. In Slave mode, 1170 * the caller must ensure that there is sufficient space in the request queue 1171 * and Tx Data FIFO. 1172 * 1173 * For an OUT transfer in Slave mode, it loads a data packet into the 1174 * appropriate FIFO. If necessary, additional data packets are loaded in the 1175 * Host ISR. 1176 * 1177 * For an IN transfer in Slave mode, a data packet is requested. The data 1178 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary, 1179 * additional data packets are requested in the Host ISR. 1180 * 1181 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ 1182 * register along with a packet count of 1 and the channel is enabled. This 1183 * causes a single PING transaction to occur. Other fields in HCTSIZ are 1184 * simply set to 0 since no data transfer occurs in this case. 1185 * 1186 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with 1187 * all the information required to perform the subsequent data transfer. In 1188 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the 1189 * controller performs the entire PING protocol, then starts the data 1190 * transfer. 1191 */ 1192 static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg, 1193 struct dwc2_host_chan *chan) 1194 { 1195 u32 max_hc_xfer_size = hsotg->params.max_transfer_size; 1196 u16 max_hc_pkt_count = hsotg->params.max_packet_count; 1197 u32 hcchar; 1198 u32 hctsiz = 0; 1199 u16 num_packets; 1200 u32 ec_mc; 1201 1202 if (dbg_hc(chan)) 1203 dev_vdbg(hsotg->dev, "%s()\n", __func__); 1204 1205 if (chan->do_ping) { 1206 if (!hsotg->params.host_dma) { 1207 if (dbg_hc(chan)) 1208 dev_vdbg(hsotg->dev, "ping, no DMA\n"); 1209 dwc2_hc_do_ping(hsotg, chan); 1210 chan->xfer_started = 1; 1211 return; 1212 } 1213 1214 if (dbg_hc(chan)) 1215 dev_vdbg(hsotg->dev, "ping, DMA\n"); 1216 1217 hctsiz |= TSIZ_DOPNG; 1218 } 1219 1220 if (chan->do_split) { 1221 if (dbg_hc(chan)) 1222 dev_vdbg(hsotg->dev, "split\n"); 1223 num_packets = 1; 1224 1225 if (chan->complete_split && !chan->ep_is_in) 1226 /* 1227 * For CSPLIT OUT Transfer, set the size to 0 so the 1228 * core doesn't expect any data written to the FIFO 1229 */ 1230 chan->xfer_len = 0; 1231 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet) 1232 chan->xfer_len = chan->max_packet; 1233 else if (!chan->ep_is_in && chan->xfer_len > 188) 1234 chan->xfer_len = 188; 1235 1236 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT & 1237 TSIZ_XFERSIZE_MASK; 1238 1239 /* For split set ec_mc for immediate retries */ 1240 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 1241 chan->ep_type == USB_ENDPOINT_XFER_ISOC) 1242 ec_mc = 3; 1243 else 1244 ec_mc = 1; 1245 } else { 1246 if (dbg_hc(chan)) 1247 dev_vdbg(hsotg->dev, "no split\n"); 1248 /* 1249 * Ensure that the transfer length and packet count will fit 1250 * in the widths allocated for them in the HCTSIZn register 1251 */ 1252 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 1253 chan->ep_type == USB_ENDPOINT_XFER_ISOC) { 1254 /* 1255 * Make sure the transfer size is no larger than one 1256 * (micro)frame's worth of data. (A check was done 1257 * when the periodic transfer was accepted to ensure 1258 * that a (micro)frame's worth of data can be 1259 * programmed into a channel.) 1260 */ 1261 u32 max_periodic_len = 1262 chan->multi_count * chan->max_packet; 1263 1264 if (chan->xfer_len > max_periodic_len) 1265 chan->xfer_len = max_periodic_len; 1266 } else if (chan->xfer_len > max_hc_xfer_size) { 1267 /* 1268 * Make sure that xfer_len is a multiple of max packet 1269 * size 1270 */ 1271 chan->xfer_len = 1272 max_hc_xfer_size - chan->max_packet + 1; 1273 } 1274 1275 if (chan->xfer_len > 0) { 1276 num_packets = (chan->xfer_len + chan->max_packet - 1) / 1277 chan->max_packet; 1278 if (num_packets > max_hc_pkt_count) { 1279 num_packets = max_hc_pkt_count; 1280 chan->xfer_len = num_packets * chan->max_packet; 1281 } else if (chan->ep_is_in) { 1282 /* 1283 * Always program an integral # of max packets 1284 * for IN transfers. 1285 * Note: This assumes that the input buffer is 1286 * aligned and sized accordingly. 1287 */ 1288 chan->xfer_len = num_packets * chan->max_packet; 1289 } 1290 } else { 1291 /* Need 1 packet for transfer length of 0 */ 1292 num_packets = 1; 1293 } 1294 1295 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 1296 chan->ep_type == USB_ENDPOINT_XFER_ISOC) 1297 /* 1298 * Make sure that the multi_count field matches the 1299 * actual transfer length 1300 */ 1301 chan->multi_count = num_packets; 1302 1303 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) 1304 dwc2_set_pid_isoc(chan); 1305 1306 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT & 1307 TSIZ_XFERSIZE_MASK; 1308 1309 /* The ec_mc gets the multi_count for non-split */ 1310 ec_mc = chan->multi_count; 1311 } 1312 1313 chan->start_pkt_count = num_packets; 1314 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK; 1315 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT & 1316 TSIZ_SC_MC_PID_MASK; 1317 dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num)); 1318 if (dbg_hc(chan)) { 1319 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n", 1320 hctsiz, chan->hc_num); 1321 1322 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__, 1323 chan->hc_num); 1324 dev_vdbg(hsotg->dev, " Xfer Size: %d\n", 1325 (hctsiz & TSIZ_XFERSIZE_MASK) >> 1326 TSIZ_XFERSIZE_SHIFT); 1327 dev_vdbg(hsotg->dev, " Num Pkts: %d\n", 1328 (hctsiz & TSIZ_PKTCNT_MASK) >> 1329 TSIZ_PKTCNT_SHIFT); 1330 dev_vdbg(hsotg->dev, " Start PID: %d\n", 1331 (hctsiz & TSIZ_SC_MC_PID_MASK) >> 1332 TSIZ_SC_MC_PID_SHIFT); 1333 } 1334 1335 if (hsotg->params.host_dma) { 1336 dma_addr_t dma_addr; 1337 1338 if (chan->align_buf) { 1339 if (dbg_hc(chan)) 1340 dev_vdbg(hsotg->dev, "align_buf\n"); 1341 dma_addr = chan->align_buf; 1342 } else { 1343 dma_addr = chan->xfer_dma; 1344 } 1345 dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num)); 1346 1347 if (dbg_hc(chan)) 1348 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n", 1349 (unsigned long)dma_addr, chan->hc_num); 1350 } 1351 1352 /* Start the split */ 1353 if (chan->do_split) { 1354 u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num)); 1355 1356 hcsplt |= HCSPLT_SPLTENA; 1357 dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num)); 1358 } 1359 1360 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num)); 1361 hcchar &= ~HCCHAR_MULTICNT_MASK; 1362 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK; 1363 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar); 1364 1365 if (hcchar & HCCHAR_CHDIS) 1366 dev_warn(hsotg->dev, 1367 "%s: chdis set, channel %d, hcchar 0x%08x\n", 1368 __func__, chan->hc_num, hcchar); 1369 1370 /* Set host channel enable after all other setup is complete */ 1371 hcchar |= HCCHAR_CHENA; 1372 hcchar &= ~HCCHAR_CHDIS; 1373 1374 if (dbg_hc(chan)) 1375 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n", 1376 (hcchar & HCCHAR_MULTICNT_MASK) >> 1377 HCCHAR_MULTICNT_SHIFT); 1378 1379 dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num)); 1380 if (dbg_hc(chan)) 1381 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar, 1382 chan->hc_num); 1383 1384 chan->xfer_started = 1; 1385 chan->requests++; 1386 1387 if (!hsotg->params.host_dma && 1388 !chan->ep_is_in && chan->xfer_len > 0) 1389 /* Load OUT packet into the appropriate Tx FIFO */ 1390 dwc2_hc_write_packet(hsotg, chan); 1391 } 1392 1393 /** 1394 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a 1395 * host channel and starts the transfer in Descriptor DMA mode 1396 * 1397 * @hsotg: Programming view of DWC_otg controller 1398 * @chan: Information needed to initialize the host channel 1399 * 1400 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set. 1401 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field 1402 * with micro-frame bitmap. 1403 * 1404 * Initializes HCDMA register with descriptor list address and CTD value then 1405 * starts the transfer via enabling the channel. 1406 */ 1407 void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg, 1408 struct dwc2_host_chan *chan) 1409 { 1410 u32 hcchar; 1411 u32 hctsiz = 0; 1412 1413 if (chan->do_ping) 1414 hctsiz |= TSIZ_DOPNG; 1415 1416 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) 1417 dwc2_set_pid_isoc(chan); 1418 1419 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */ 1420 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT & 1421 TSIZ_SC_MC_PID_MASK; 1422 1423 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */ 1424 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK; 1425 1426 /* Non-zero only for high-speed interrupt endpoints */ 1427 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK; 1428 1429 if (dbg_hc(chan)) { 1430 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__, 1431 chan->hc_num); 1432 dev_vdbg(hsotg->dev, " Start PID: %d\n", 1433 chan->data_pid_start); 1434 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1); 1435 } 1436 1437 dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num)); 1438 1439 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr, 1440 chan->desc_list_sz, DMA_TO_DEVICE); 1441 1442 dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num)); 1443 1444 if (dbg_hc(chan)) 1445 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n", 1446 &chan->desc_list_addr, chan->hc_num); 1447 1448 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num)); 1449 hcchar &= ~HCCHAR_MULTICNT_MASK; 1450 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT & 1451 HCCHAR_MULTICNT_MASK; 1452 1453 if (hcchar & HCCHAR_CHDIS) 1454 dev_warn(hsotg->dev, 1455 "%s: chdis set, channel %d, hcchar 0x%08x\n", 1456 __func__, chan->hc_num, hcchar); 1457 1458 /* Set host channel enable after all other setup is complete */ 1459 hcchar |= HCCHAR_CHENA; 1460 hcchar &= ~HCCHAR_CHDIS; 1461 1462 if (dbg_hc(chan)) 1463 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n", 1464 (hcchar & HCCHAR_MULTICNT_MASK) >> 1465 HCCHAR_MULTICNT_SHIFT); 1466 1467 dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num)); 1468 if (dbg_hc(chan)) 1469 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar, 1470 chan->hc_num); 1471 1472 chan->xfer_started = 1; 1473 chan->requests++; 1474 } 1475 1476 /** 1477 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by 1478 * a previous call to dwc2_hc_start_transfer() 1479 * 1480 * @hsotg: Programming view of DWC_otg controller 1481 * @chan: Information needed to initialize the host channel 1482 * 1483 * The caller must ensure there is sufficient space in the request queue and Tx 1484 * Data FIFO. This function should only be called in Slave mode. In DMA mode, 1485 * the controller acts autonomously to complete transfers programmed to a host 1486 * channel. 1487 * 1488 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO 1489 * if there is any data remaining to be queued. For an IN transfer, another 1490 * data packet is always requested. For the SETUP phase of a control transfer, 1491 * this function does nothing. 1492 * 1493 * Return: 1 if a new request is queued, 0 if no more requests are required 1494 * for this transfer 1495 */ 1496 static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg, 1497 struct dwc2_host_chan *chan) 1498 { 1499 if (dbg_hc(chan)) 1500 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__, 1501 chan->hc_num); 1502 1503 if (chan->do_split) 1504 /* SPLITs always queue just once per channel */ 1505 return 0; 1506 1507 if (chan->data_pid_start == DWC2_HC_PID_SETUP) 1508 /* SETUPs are queued only once since they can't be NAK'd */ 1509 return 0; 1510 1511 if (chan->ep_is_in) { 1512 /* 1513 * Always queue another request for other IN transfers. If 1514 * back-to-back INs are issued and NAKs are received for both, 1515 * the driver may still be processing the first NAK when the 1516 * second NAK is received. When the interrupt handler clears 1517 * the NAK interrupt for the first NAK, the second NAK will 1518 * not be seen. So we can't depend on the NAK interrupt 1519 * handler to requeue a NAK'd request. Instead, IN requests 1520 * are issued each time this function is called. When the 1521 * transfer completes, the extra requests for the channel will 1522 * be flushed. 1523 */ 1524 u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num)); 1525 1526 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar); 1527 hcchar |= HCCHAR_CHENA; 1528 hcchar &= ~HCCHAR_CHDIS; 1529 if (dbg_hc(chan)) 1530 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n", 1531 hcchar); 1532 dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num)); 1533 chan->requests++; 1534 return 1; 1535 } 1536 1537 /* OUT transfers */ 1538 1539 if (chan->xfer_count < chan->xfer_len) { 1540 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 1541 chan->ep_type == USB_ENDPOINT_XFER_ISOC) { 1542 u32 hcchar = dwc2_readl(hsotg, 1543 HCCHAR(chan->hc_num)); 1544 1545 dwc2_hc_set_even_odd_frame(hsotg, chan, 1546 &hcchar); 1547 } 1548 1549 /* Load OUT packet into the appropriate Tx FIFO */ 1550 dwc2_hc_write_packet(hsotg, chan); 1551 chan->requests++; 1552 return 1; 1553 } 1554 1555 return 0; 1556 } 1557 1558 /* 1559 * ========================================================================= 1560 * HCD 1561 * ========================================================================= 1562 */ 1563 1564 /* 1565 * Processes all the URBs in a single list of QHs. Completes them with 1566 * -ETIMEDOUT and frees the QTD. 1567 * 1568 * Must be called with interrupt disabled and spinlock held 1569 */ 1570 static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg, 1571 struct list_head *qh_list) 1572 { 1573 struct dwc2_qh *qh, *qh_tmp; 1574 struct dwc2_qtd *qtd, *qtd_tmp; 1575 1576 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) { 1577 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, 1578 qtd_list_entry) { 1579 dwc2_host_complete(hsotg, qtd, -ECONNRESET); 1580 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh); 1581 } 1582 } 1583 } 1584 1585 static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg, 1586 struct list_head *qh_list) 1587 { 1588 struct dwc2_qtd *qtd, *qtd_tmp; 1589 struct dwc2_qh *qh, *qh_tmp; 1590 unsigned long flags; 1591 1592 if (!qh_list->next) 1593 /* The list hasn't been initialized yet */ 1594 return; 1595 1596 spin_lock_irqsave(&hsotg->lock, flags); 1597 1598 /* Ensure there are no QTDs or URBs left */ 1599 dwc2_kill_urbs_in_qh_list(hsotg, qh_list); 1600 1601 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) { 1602 dwc2_hcd_qh_unlink(hsotg, qh); 1603 1604 /* Free each QTD in the QH's QTD list */ 1605 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, 1606 qtd_list_entry) 1607 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh); 1608 1609 if (qh->channel && qh->channel->qh == qh) 1610 qh->channel->qh = NULL; 1611 1612 spin_unlock_irqrestore(&hsotg->lock, flags); 1613 dwc2_hcd_qh_free(hsotg, qh); 1614 spin_lock_irqsave(&hsotg->lock, flags); 1615 } 1616 1617 spin_unlock_irqrestore(&hsotg->lock, flags); 1618 } 1619 1620 /* 1621 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic 1622 * and periodic schedules. The QTD associated with each URB is removed from 1623 * the schedule and freed. This function may be called when a disconnect is 1624 * detected or when the HCD is being stopped. 1625 * 1626 * Must be called with interrupt disabled and spinlock held 1627 */ 1628 static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg) 1629 { 1630 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive); 1631 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting); 1632 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active); 1633 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive); 1634 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready); 1635 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned); 1636 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued); 1637 } 1638 1639 /** 1640 * dwc2_hcd_start() - Starts the HCD when switching to Host mode 1641 * 1642 * @hsotg: Pointer to struct dwc2_hsotg 1643 */ 1644 void dwc2_hcd_start(struct dwc2_hsotg *hsotg) 1645 { 1646 u32 hprt0; 1647 1648 if (hsotg->op_state == OTG_STATE_B_HOST) { 1649 /* 1650 * Reset the port. During a HNP mode switch the reset 1651 * needs to occur within 1ms and have a duration of at 1652 * least 50ms. 1653 */ 1654 hprt0 = dwc2_read_hprt0(hsotg); 1655 hprt0 |= HPRT0_RST; 1656 dwc2_writel(hsotg, hprt0, HPRT0); 1657 } 1658 1659 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work, 1660 msecs_to_jiffies(50)); 1661 } 1662 1663 /* Must be called with interrupt disabled and spinlock held */ 1664 static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg) 1665 { 1666 int num_channels = hsotg->params.host_channels; 1667 struct dwc2_host_chan *channel; 1668 u32 hcchar; 1669 int i; 1670 1671 if (!hsotg->params.host_dma) { 1672 /* Flush out any channel requests in slave mode */ 1673 for (i = 0; i < num_channels; i++) { 1674 channel = hsotg->hc_ptr_array[i]; 1675 if (!list_empty(&channel->hc_list_entry)) 1676 continue; 1677 hcchar = dwc2_readl(hsotg, HCCHAR(i)); 1678 if (hcchar & HCCHAR_CHENA) { 1679 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR); 1680 hcchar |= HCCHAR_CHDIS; 1681 dwc2_writel(hsotg, hcchar, HCCHAR(i)); 1682 } 1683 } 1684 } 1685 1686 for (i = 0; i < num_channels; i++) { 1687 channel = hsotg->hc_ptr_array[i]; 1688 if (!list_empty(&channel->hc_list_entry)) 1689 continue; 1690 hcchar = dwc2_readl(hsotg, HCCHAR(i)); 1691 if (hcchar & HCCHAR_CHENA) { 1692 /* Halt the channel */ 1693 hcchar |= HCCHAR_CHDIS; 1694 dwc2_writel(hsotg, hcchar, HCCHAR(i)); 1695 } 1696 1697 dwc2_hc_cleanup(hsotg, channel); 1698 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list); 1699 /* 1700 * Added for Descriptor DMA to prevent channel double cleanup in 1701 * release_channel_ddma(), which is called from ep_disable when 1702 * device disconnects 1703 */ 1704 channel->qh = NULL; 1705 } 1706 /* All channels have been freed, mark them available */ 1707 if (hsotg->params.uframe_sched) { 1708 hsotg->available_host_channels = 1709 hsotg->params.host_channels; 1710 } else { 1711 hsotg->non_periodic_channels = 0; 1712 hsotg->periodic_channels = 0; 1713 } 1714 } 1715 1716 /** 1717 * dwc2_hcd_connect() - Handles connect of the HCD 1718 * 1719 * @hsotg: Pointer to struct dwc2_hsotg 1720 * 1721 * Must be called with interrupt disabled and spinlock held 1722 */ 1723 void dwc2_hcd_connect(struct dwc2_hsotg *hsotg) 1724 { 1725 if (hsotg->lx_state != DWC2_L0) 1726 usb_hcd_resume_root_hub(hsotg->priv); 1727 1728 hsotg->flags.b.port_connect_status_change = 1; 1729 hsotg->flags.b.port_connect_status = 1; 1730 } 1731 1732 /** 1733 * dwc2_hcd_disconnect() - Handles disconnect of the HCD 1734 * 1735 * @hsotg: Pointer to struct dwc2_hsotg 1736 * @force: If true, we won't try to reconnect even if we see device connected. 1737 * 1738 * Must be called with interrupt disabled and spinlock held 1739 */ 1740 void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force) 1741 { 1742 u32 intr; 1743 u32 hprt0; 1744 1745 /* Set status flags for the hub driver */ 1746 hsotg->flags.b.port_connect_status_change = 1; 1747 hsotg->flags.b.port_connect_status = 0; 1748 1749 /* 1750 * Shutdown any transfers in process by clearing the Tx FIFO Empty 1751 * interrupt mask and status bits and disabling subsequent host 1752 * channel interrupts. 1753 */ 1754 intr = dwc2_readl(hsotg, GINTMSK); 1755 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT); 1756 dwc2_writel(hsotg, intr, GINTMSK); 1757 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT; 1758 dwc2_writel(hsotg, intr, GINTSTS); 1759 1760 /* 1761 * Turn off the vbus power only if the core has transitioned to device 1762 * mode. If still in host mode, need to keep power on to detect a 1763 * reconnection. 1764 */ 1765 if (dwc2_is_device_mode(hsotg)) { 1766 if (hsotg->op_state != OTG_STATE_A_SUSPEND) { 1767 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n"); 1768 dwc2_writel(hsotg, 0, HPRT0); 1769 } 1770 1771 dwc2_disable_host_interrupts(hsotg); 1772 } 1773 1774 /* Respond with an error status to all URBs in the schedule */ 1775 dwc2_kill_all_urbs(hsotg); 1776 1777 if (dwc2_is_host_mode(hsotg)) 1778 /* Clean up any host channels that were in use */ 1779 dwc2_hcd_cleanup_channels(hsotg); 1780 1781 dwc2_host_disconnect(hsotg); 1782 1783 /* 1784 * Add an extra check here to see if we're actually connected but 1785 * we don't have a detection interrupt pending. This can happen if: 1786 * 1. hardware sees connect 1787 * 2. hardware sees disconnect 1788 * 3. hardware sees connect 1789 * 4. dwc2_port_intr() - clears connect interrupt 1790 * 5. dwc2_handle_common_intr() - calls here 1791 * 1792 * Without the extra check here we will end calling disconnect 1793 * and won't get any future interrupts to handle the connect. 1794 */ 1795 if (!force) { 1796 hprt0 = dwc2_readl(hsotg, HPRT0); 1797 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS)) 1798 dwc2_hcd_connect(hsotg); 1799 } 1800 } 1801 1802 /** 1803 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup 1804 * 1805 * @hsotg: Pointer to struct dwc2_hsotg 1806 */ 1807 static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg) 1808 { 1809 if (hsotg->bus_suspended) { 1810 hsotg->flags.b.port_suspend_change = 1; 1811 usb_hcd_resume_root_hub(hsotg->priv); 1812 } 1813 1814 if (hsotg->lx_state == DWC2_L1) 1815 hsotg->flags.b.port_l1_change = 1; 1816 } 1817 1818 /** 1819 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner 1820 * 1821 * @hsotg: Pointer to struct dwc2_hsotg 1822 * 1823 * Must be called with interrupt disabled and spinlock held 1824 */ 1825 void dwc2_hcd_stop(struct dwc2_hsotg *hsotg) 1826 { 1827 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n"); 1828 1829 /* 1830 * The root hub should be disconnected before this function is called. 1831 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue) 1832 * and the QH lists (via ..._hcd_endpoint_disable). 1833 */ 1834 1835 /* Turn off all host-specific interrupts */ 1836 dwc2_disable_host_interrupts(hsotg); 1837 1838 /* Turn off the vbus power */ 1839 dev_dbg(hsotg->dev, "PortPower off\n"); 1840 dwc2_writel(hsotg, 0, HPRT0); 1841 } 1842 1843 /* Caller must hold driver lock */ 1844 static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg, 1845 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh, 1846 struct dwc2_qtd *qtd) 1847 { 1848 u32 intr_mask; 1849 int retval; 1850 int dev_speed; 1851 1852 if (!hsotg->flags.b.port_connect_status) { 1853 /* No longer connected */ 1854 dev_err(hsotg->dev, "Not connected\n"); 1855 return -ENODEV; 1856 } 1857 1858 dev_speed = dwc2_host_get_speed(hsotg, urb->priv); 1859 1860 /* Some configurations cannot support LS traffic on a FS root port */ 1861 if ((dev_speed == USB_SPEED_LOW) && 1862 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) && 1863 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) { 1864 u32 hprt0 = dwc2_readl(hsotg, HPRT0); 1865 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT; 1866 1867 if (prtspd == HPRT0_SPD_FULL_SPEED) 1868 return -ENODEV; 1869 } 1870 1871 if (!qtd) 1872 return -EINVAL; 1873 1874 dwc2_hcd_qtd_init(qtd, urb); 1875 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh); 1876 if (retval) { 1877 dev_err(hsotg->dev, 1878 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n", 1879 retval); 1880 return retval; 1881 } 1882 1883 intr_mask = dwc2_readl(hsotg, GINTMSK); 1884 if (!(intr_mask & GINTSTS_SOF)) { 1885 enum dwc2_transaction_type tr_type; 1886 1887 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK && 1888 !(qtd->urb->flags & URB_GIVEBACK_ASAP)) 1889 /* 1890 * Do not schedule SG transactions until qtd has 1891 * URB_GIVEBACK_ASAP set 1892 */ 1893 return 0; 1894 1895 tr_type = dwc2_hcd_select_transactions(hsotg); 1896 if (tr_type != DWC2_TRANSACTION_NONE) 1897 dwc2_hcd_queue_transactions(hsotg, tr_type); 1898 } 1899 1900 return 0; 1901 } 1902 1903 /* Must be called with interrupt disabled and spinlock held */ 1904 static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg, 1905 struct dwc2_hcd_urb *urb) 1906 { 1907 struct dwc2_qh *qh; 1908 struct dwc2_qtd *urb_qtd; 1909 1910 urb_qtd = urb->qtd; 1911 if (!urb_qtd) { 1912 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n"); 1913 return -EINVAL; 1914 } 1915 1916 qh = urb_qtd->qh; 1917 if (!qh) { 1918 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n"); 1919 return -EINVAL; 1920 } 1921 1922 urb->priv = NULL; 1923 1924 if (urb_qtd->in_process && qh->channel) { 1925 dwc2_dump_channel_info(hsotg, qh->channel); 1926 1927 /* The QTD is in process (it has been assigned to a channel) */ 1928 if (hsotg->flags.b.port_connect_status) 1929 /* 1930 * If still connected (i.e. in host mode), halt the 1931 * channel so it can be used for other transfers. If 1932 * no longer connected, the host registers can't be 1933 * written to halt the channel since the core is in 1934 * device mode. 1935 */ 1936 dwc2_hc_halt(hsotg, qh->channel, 1937 DWC2_HC_XFER_URB_DEQUEUE); 1938 } 1939 1940 /* 1941 * Free the QTD and clean up the associated QH. Leave the QH in the 1942 * schedule if it has any remaining QTDs. 1943 */ 1944 if (!hsotg->params.dma_desc_enable) { 1945 u8 in_process = urb_qtd->in_process; 1946 1947 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh); 1948 if (in_process) { 1949 dwc2_hcd_qh_deactivate(hsotg, qh, 0); 1950 qh->channel = NULL; 1951 } else if (list_empty(&qh->qtd_list)) { 1952 dwc2_hcd_qh_unlink(hsotg, qh); 1953 } 1954 } else { 1955 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh); 1956 } 1957 1958 return 0; 1959 } 1960 1961 /* Must NOT be called with interrupt disabled or spinlock held */ 1962 static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg, 1963 struct usb_host_endpoint *ep, int retry) 1964 { 1965 struct dwc2_qtd *qtd, *qtd_tmp; 1966 struct dwc2_qh *qh; 1967 unsigned long flags; 1968 int rc; 1969 1970 spin_lock_irqsave(&hsotg->lock, flags); 1971 1972 qh = ep->hcpriv; 1973 if (!qh) { 1974 rc = -EINVAL; 1975 goto err; 1976 } 1977 1978 while (!list_empty(&qh->qtd_list) && retry--) { 1979 if (retry == 0) { 1980 dev_err(hsotg->dev, 1981 "## timeout in dwc2_hcd_endpoint_disable() ##\n"); 1982 rc = -EBUSY; 1983 goto err; 1984 } 1985 1986 spin_unlock_irqrestore(&hsotg->lock, flags); 1987 msleep(20); 1988 spin_lock_irqsave(&hsotg->lock, flags); 1989 qh = ep->hcpriv; 1990 if (!qh) { 1991 rc = -EINVAL; 1992 goto err; 1993 } 1994 } 1995 1996 /* 1997 * The hub driver still has a pointer to the endpoint. Wait until it 1998 * has called _dwc2_hcd_clear_tt_buffer_complete(). 1999 */ 2000 while (qh->tt_buffer_dirty) { 2001 spin_unlock_irqrestore(&hsotg->lock, flags); 2002 msleep(20); 2003 spin_lock_irqsave(&hsotg->lock, flags); 2004 qh = ep->hcpriv; 2005 if (!qh) { 2006 rc = -EINVAL; 2007 goto err; 2008 } 2009 } 2010 2011 dwc2_hcd_qh_unlink(hsotg, qh); 2012 2013 /* Free each QTD in the QH's QTD list */ 2014 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) 2015 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh); 2016 2017 ep->hcpriv = NULL; 2018 2019 if (qh->channel && qh->channel->qh == qh) 2020 qh->channel->qh = NULL; 2021 2022 spin_unlock_irqrestore(&hsotg->lock, flags); 2023 2024 dwc2_hcd_qh_free(hsotg, qh); 2025 2026 return 0; 2027 2028 err: 2029 ep->hcpriv = NULL; 2030 spin_unlock_irqrestore(&hsotg->lock, flags); 2031 2032 return rc; 2033 } 2034 2035 /* Must be called with interrupt disabled and spinlock held */ 2036 static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg, 2037 struct usb_host_endpoint *ep) 2038 { 2039 struct dwc2_qh *qh = ep->hcpriv; 2040 2041 if (!qh) 2042 return -EINVAL; 2043 2044 qh->data_toggle = DWC2_HC_PID_DATA0; 2045 2046 return 0; 2047 } 2048 2049 /** 2050 * dwc2_core_init() - Initializes the DWC_otg controller registers and 2051 * prepares the core for device mode or host mode operation 2052 * 2053 * @hsotg: Programming view of the DWC_otg controller 2054 * @initial_setup: If true then this is the first init for this instance. 2055 */ 2056 int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup) 2057 { 2058 u32 usbcfg, otgctl; 2059 int retval; 2060 2061 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg); 2062 2063 usbcfg = dwc2_readl(hsotg, GUSBCFG); 2064 2065 /* Set ULPI External VBUS bit if needed */ 2066 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV; 2067 if (hsotg->params.phy_ulpi_ext_vbus) 2068 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV; 2069 2070 /* Set external TS Dline pulsing bit if needed */ 2071 usbcfg &= ~GUSBCFG_TERMSELDLPULSE; 2072 if (hsotg->params.ts_dline) 2073 usbcfg |= GUSBCFG_TERMSELDLPULSE; 2074 2075 dwc2_writel(hsotg, usbcfg, GUSBCFG); 2076 2077 /* 2078 * Reset the Controller 2079 * 2080 * We only need to reset the controller if this is a re-init. 2081 * For the first init we know for sure that earlier code reset us (it 2082 * needed to in order to properly detect various parameters). 2083 */ 2084 if (!initial_setup) { 2085 retval = dwc2_core_reset(hsotg, false); 2086 if (retval) { 2087 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n", 2088 __func__); 2089 return retval; 2090 } 2091 } 2092 2093 /* 2094 * This needs to happen in FS mode before any other programming occurs 2095 */ 2096 retval = dwc2_phy_init(hsotg, initial_setup); 2097 if (retval) 2098 return retval; 2099 2100 /* Program the GAHBCFG Register */ 2101 retval = dwc2_gahbcfg_init(hsotg); 2102 if (retval) 2103 return retval; 2104 2105 /* Program the GUSBCFG register */ 2106 dwc2_gusbcfg_init(hsotg); 2107 2108 /* Program the GOTGCTL register */ 2109 otgctl = dwc2_readl(hsotg, GOTGCTL); 2110 otgctl &= ~GOTGCTL_OTGVER; 2111 dwc2_writel(hsotg, otgctl, GOTGCTL); 2112 2113 /* Clear the SRP success bit for FS-I2c */ 2114 hsotg->srp_success = 0; 2115 2116 /* Enable common interrupts */ 2117 dwc2_enable_common_interrupts(hsotg); 2118 2119 /* 2120 * Do device or host initialization based on mode during PCD and 2121 * HCD initialization 2122 */ 2123 if (dwc2_is_host_mode(hsotg)) { 2124 dev_dbg(hsotg->dev, "Host Mode\n"); 2125 hsotg->op_state = OTG_STATE_A_HOST; 2126 } else { 2127 dev_dbg(hsotg->dev, "Device Mode\n"); 2128 hsotg->op_state = OTG_STATE_B_PERIPHERAL; 2129 } 2130 2131 return 0; 2132 } 2133 2134 /** 2135 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for 2136 * Host mode 2137 * 2138 * @hsotg: Programming view of DWC_otg controller 2139 * 2140 * This function flushes the Tx and Rx FIFOs and flushes any entries in the 2141 * request queues. Host channels are reset to ensure that they are ready for 2142 * performing transfers. 2143 */ 2144 static void dwc2_core_host_init(struct dwc2_hsotg *hsotg) 2145 { 2146 u32 hcfg, hfir, otgctl, usbcfg; 2147 2148 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg); 2149 2150 /* Set HS/FS Timeout Calibration to 7 (max available value). 2151 * The number of PHY clocks that the application programs in 2152 * this field is added to the high/full speed interpacket timeout 2153 * duration in the core to account for any additional delays 2154 * introduced by the PHY. This can be required, because the delay 2155 * introduced by the PHY in generating the linestate condition 2156 * can vary from one PHY to another. 2157 */ 2158 usbcfg = dwc2_readl(hsotg, GUSBCFG); 2159 usbcfg |= GUSBCFG_TOUTCAL(7); 2160 dwc2_writel(hsotg, usbcfg, GUSBCFG); 2161 2162 /* Restart the Phy Clock */ 2163 dwc2_writel(hsotg, 0, PCGCTL); 2164 2165 /* Initialize Host Configuration Register */ 2166 dwc2_init_fs_ls_pclk_sel(hsotg); 2167 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL || 2168 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) { 2169 hcfg = dwc2_readl(hsotg, HCFG); 2170 hcfg |= HCFG_FSLSSUPP; 2171 dwc2_writel(hsotg, hcfg, HCFG); 2172 } 2173 2174 /* 2175 * This bit allows dynamic reloading of the HFIR register during 2176 * runtime. This bit needs to be programmed during initial configuration 2177 * and its value must not be changed during runtime. 2178 */ 2179 if (hsotg->params.reload_ctl) { 2180 hfir = dwc2_readl(hsotg, HFIR); 2181 hfir |= HFIR_RLDCTRL; 2182 dwc2_writel(hsotg, hfir, HFIR); 2183 } 2184 2185 if (hsotg->params.dma_desc_enable) { 2186 u32 op_mode = hsotg->hw_params.op_mode; 2187 2188 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a || 2189 !hsotg->hw_params.dma_desc_enable || 2190 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE || 2191 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE || 2192 op_mode == GHWCFG2_OP_MODE_UNDEFINED) { 2193 dev_err(hsotg->dev, 2194 "Hardware does not support descriptor DMA mode -\n"); 2195 dev_err(hsotg->dev, 2196 "falling back to buffer DMA mode.\n"); 2197 hsotg->params.dma_desc_enable = false; 2198 } else { 2199 hcfg = dwc2_readl(hsotg, HCFG); 2200 hcfg |= HCFG_DESCDMA; 2201 dwc2_writel(hsotg, hcfg, HCFG); 2202 } 2203 } 2204 2205 /* Configure data FIFO sizes */ 2206 dwc2_config_fifos(hsotg); 2207 2208 /* TODO - check this */ 2209 /* Clear Host Set HNP Enable in the OTG Control Register */ 2210 otgctl = dwc2_readl(hsotg, GOTGCTL); 2211 otgctl &= ~GOTGCTL_HSTSETHNPEN; 2212 dwc2_writel(hsotg, otgctl, GOTGCTL); 2213 2214 /* Make sure the FIFOs are flushed */ 2215 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */); 2216 dwc2_flush_rx_fifo(hsotg); 2217 2218 /* Clear Host Set HNP Enable in the OTG Control Register */ 2219 otgctl = dwc2_readl(hsotg, GOTGCTL); 2220 otgctl &= ~GOTGCTL_HSTSETHNPEN; 2221 dwc2_writel(hsotg, otgctl, GOTGCTL); 2222 2223 if (!hsotg->params.dma_desc_enable) { 2224 int num_channels, i; 2225 u32 hcchar; 2226 2227 /* Flush out any leftover queued requests */ 2228 num_channels = hsotg->params.host_channels; 2229 for (i = 0; i < num_channels; i++) { 2230 hcchar = dwc2_readl(hsotg, HCCHAR(i)); 2231 if (hcchar & HCCHAR_CHENA) { 2232 hcchar &= ~HCCHAR_CHENA; 2233 hcchar |= HCCHAR_CHDIS; 2234 hcchar &= ~HCCHAR_EPDIR; 2235 dwc2_writel(hsotg, hcchar, HCCHAR(i)); 2236 } 2237 } 2238 2239 /* Halt all channels to put them into a known state */ 2240 for (i = 0; i < num_channels; i++) { 2241 hcchar = dwc2_readl(hsotg, HCCHAR(i)); 2242 if (hcchar & HCCHAR_CHENA) { 2243 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS; 2244 hcchar &= ~HCCHAR_EPDIR; 2245 dwc2_writel(hsotg, hcchar, HCCHAR(i)); 2246 dev_dbg(hsotg->dev, "%s: Halt channel %d\n", 2247 __func__, i); 2248 2249 if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i), 2250 HCCHAR_CHENA, 2251 1000)) { 2252 dev_warn(hsotg->dev, 2253 "Unable to clear enable on channel %d\n", 2254 i); 2255 } 2256 } 2257 } 2258 } 2259 2260 /* Enable ACG feature in host mode, if supported */ 2261 dwc2_enable_acg(hsotg); 2262 2263 /* Turn on the vbus power */ 2264 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state); 2265 if (hsotg->op_state == OTG_STATE_A_HOST) { 2266 u32 hprt0 = dwc2_read_hprt0(hsotg); 2267 2268 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n", 2269 !!(hprt0 & HPRT0_PWR)); 2270 if (!(hprt0 & HPRT0_PWR)) { 2271 hprt0 |= HPRT0_PWR; 2272 dwc2_writel(hsotg, hprt0, HPRT0); 2273 } 2274 } 2275 2276 dwc2_enable_host_interrupts(hsotg); 2277 } 2278 2279 /* 2280 * Initializes dynamic portions of the DWC_otg HCD state 2281 * 2282 * Must be called with interrupt disabled and spinlock held 2283 */ 2284 static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg) 2285 { 2286 struct dwc2_host_chan *chan, *chan_tmp; 2287 int num_channels; 2288 int i; 2289 2290 hsotg->flags.d32 = 0; 2291 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active; 2292 2293 if (hsotg->params.uframe_sched) { 2294 hsotg->available_host_channels = 2295 hsotg->params.host_channels; 2296 } else { 2297 hsotg->non_periodic_channels = 0; 2298 hsotg->periodic_channels = 0; 2299 } 2300 2301 /* 2302 * Put all channels in the free channel list and clean up channel 2303 * states 2304 */ 2305 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list, 2306 hc_list_entry) 2307 list_del_init(&chan->hc_list_entry); 2308 2309 num_channels = hsotg->params.host_channels; 2310 for (i = 0; i < num_channels; i++) { 2311 chan = hsotg->hc_ptr_array[i]; 2312 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list); 2313 dwc2_hc_cleanup(hsotg, chan); 2314 } 2315 2316 /* Initialize the DWC core for host mode operation */ 2317 dwc2_core_host_init(hsotg); 2318 } 2319 2320 static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg, 2321 struct dwc2_host_chan *chan, 2322 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb) 2323 { 2324 int hub_addr, hub_port; 2325 2326 chan->do_split = 1; 2327 chan->xact_pos = qtd->isoc_split_pos; 2328 chan->complete_split = qtd->complete_split; 2329 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port); 2330 chan->hub_addr = (u8)hub_addr; 2331 chan->hub_port = (u8)hub_port; 2332 } 2333 2334 static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg, 2335 struct dwc2_host_chan *chan, 2336 struct dwc2_qtd *qtd) 2337 { 2338 struct dwc2_hcd_urb *urb = qtd->urb; 2339 struct dwc2_hcd_iso_packet_desc *frame_desc; 2340 2341 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) { 2342 case USB_ENDPOINT_XFER_CONTROL: 2343 chan->ep_type = USB_ENDPOINT_XFER_CONTROL; 2344 2345 switch (qtd->control_phase) { 2346 case DWC2_CONTROL_SETUP: 2347 dev_vdbg(hsotg->dev, " Control setup transaction\n"); 2348 chan->do_ping = 0; 2349 chan->ep_is_in = 0; 2350 chan->data_pid_start = DWC2_HC_PID_SETUP; 2351 if (hsotg->params.host_dma) 2352 chan->xfer_dma = urb->setup_dma; 2353 else 2354 chan->xfer_buf = urb->setup_packet; 2355 chan->xfer_len = 8; 2356 break; 2357 2358 case DWC2_CONTROL_DATA: 2359 dev_vdbg(hsotg->dev, " Control data transaction\n"); 2360 chan->data_pid_start = qtd->data_toggle; 2361 break; 2362 2363 case DWC2_CONTROL_STATUS: 2364 /* 2365 * Direction is opposite of data direction or IN if no 2366 * data 2367 */ 2368 dev_vdbg(hsotg->dev, " Control status transaction\n"); 2369 if (urb->length == 0) 2370 chan->ep_is_in = 1; 2371 else 2372 chan->ep_is_in = 2373 dwc2_hcd_is_pipe_out(&urb->pipe_info); 2374 if (chan->ep_is_in) 2375 chan->do_ping = 0; 2376 chan->data_pid_start = DWC2_HC_PID_DATA1; 2377 chan->xfer_len = 0; 2378 if (hsotg->params.host_dma) 2379 chan->xfer_dma = hsotg->status_buf_dma; 2380 else 2381 chan->xfer_buf = hsotg->status_buf; 2382 break; 2383 } 2384 break; 2385 2386 case USB_ENDPOINT_XFER_BULK: 2387 chan->ep_type = USB_ENDPOINT_XFER_BULK; 2388 break; 2389 2390 case USB_ENDPOINT_XFER_INT: 2391 chan->ep_type = USB_ENDPOINT_XFER_INT; 2392 break; 2393 2394 case USB_ENDPOINT_XFER_ISOC: 2395 chan->ep_type = USB_ENDPOINT_XFER_ISOC; 2396 if (hsotg->params.dma_desc_enable) 2397 break; 2398 2399 frame_desc = &urb->iso_descs[qtd->isoc_frame_index]; 2400 frame_desc->status = 0; 2401 2402 if (hsotg->params.host_dma) { 2403 chan->xfer_dma = urb->dma; 2404 chan->xfer_dma += frame_desc->offset + 2405 qtd->isoc_split_offset; 2406 } else { 2407 chan->xfer_buf = urb->buf; 2408 chan->xfer_buf += frame_desc->offset + 2409 qtd->isoc_split_offset; 2410 } 2411 2412 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset; 2413 2414 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) { 2415 if (chan->xfer_len <= 188) 2416 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL; 2417 else 2418 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN; 2419 } 2420 break; 2421 } 2422 } 2423 2424 static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg, 2425 struct dwc2_qh *qh, 2426 struct dwc2_host_chan *chan) 2427 { 2428 if (!hsotg->unaligned_cache || 2429 chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE) 2430 return -ENOMEM; 2431 2432 if (!qh->dw_align_buf) { 2433 qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache, 2434 GFP_ATOMIC | GFP_DMA); 2435 if (!qh->dw_align_buf) 2436 return -ENOMEM; 2437 } 2438 2439 qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf, 2440 DWC2_KMEM_UNALIGNED_BUF_SIZE, 2441 DMA_FROM_DEVICE); 2442 2443 if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) { 2444 dev_err(hsotg->dev, "can't map align_buf\n"); 2445 chan->align_buf = 0; 2446 return -EINVAL; 2447 } 2448 2449 chan->align_buf = qh->dw_align_buf_dma; 2450 return 0; 2451 } 2452 2453 #define DWC2_USB_DMA_ALIGN 4 2454 2455 static void dwc2_free_dma_aligned_buffer(struct urb *urb) 2456 { 2457 void *stored_xfer_buffer; 2458 size_t length; 2459 2460 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER)) 2461 return; 2462 2463 /* Restore urb->transfer_buffer from the end of the allocated area */ 2464 memcpy(&stored_xfer_buffer, 2465 PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length, 2466 dma_get_cache_alignment()), 2467 sizeof(urb->transfer_buffer)); 2468 2469 if (usb_urb_dir_in(urb)) { 2470 if (usb_pipeisoc(urb->pipe)) 2471 length = urb->transfer_buffer_length; 2472 else 2473 length = urb->actual_length; 2474 2475 memcpy(stored_xfer_buffer, urb->transfer_buffer, length); 2476 } 2477 kfree(urb->transfer_buffer); 2478 urb->transfer_buffer = stored_xfer_buffer; 2479 2480 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER; 2481 } 2482 2483 static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) 2484 { 2485 void *kmalloc_ptr; 2486 size_t kmalloc_size; 2487 2488 if (urb->num_sgs || urb->sg || 2489 urb->transfer_buffer_length == 0 || 2490 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1))) 2491 return 0; 2492 2493 /* 2494 * Allocate a buffer with enough padding for original transfer_buffer 2495 * pointer. This allocation is guaranteed to be aligned properly for 2496 * DMA 2497 */ 2498 kmalloc_size = urb->transfer_buffer_length + 2499 (dma_get_cache_alignment() - 1) + 2500 sizeof(urb->transfer_buffer); 2501 2502 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags); 2503 if (!kmalloc_ptr) 2504 return -ENOMEM; 2505 2506 /* 2507 * Position value of original urb->transfer_buffer pointer to the end 2508 * of allocation for later referencing 2509 */ 2510 memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length, 2511 dma_get_cache_alignment()), 2512 &urb->transfer_buffer, sizeof(urb->transfer_buffer)); 2513 2514 if (usb_urb_dir_out(urb)) 2515 memcpy(kmalloc_ptr, urb->transfer_buffer, 2516 urb->transfer_buffer_length); 2517 urb->transfer_buffer = kmalloc_ptr; 2518 2519 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER; 2520 2521 return 0; 2522 } 2523 2524 static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, 2525 gfp_t mem_flags) 2526 { 2527 int ret; 2528 2529 /* We assume setup_dma is always aligned; warn if not */ 2530 WARN_ON_ONCE(urb->setup_dma && 2531 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1))); 2532 2533 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags); 2534 if (ret) 2535 return ret; 2536 2537 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); 2538 if (ret) 2539 dwc2_free_dma_aligned_buffer(urb); 2540 2541 return ret; 2542 } 2543 2544 static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) 2545 { 2546 usb_hcd_unmap_urb_for_dma(hcd, urb); 2547 dwc2_free_dma_aligned_buffer(urb); 2548 } 2549 2550 /** 2551 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host 2552 * channel and initializes the host channel to perform the transactions. The 2553 * host channel is removed from the free list. 2554 * 2555 * @hsotg: The HCD state structure 2556 * @qh: Transactions from the first QTD for this QH are selected and assigned 2557 * to a free host channel 2558 */ 2559 static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 2560 { 2561 struct dwc2_host_chan *chan; 2562 struct dwc2_hcd_urb *urb; 2563 struct dwc2_qtd *qtd; 2564 2565 if (dbg_qh(qh)) 2566 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh); 2567 2568 if (list_empty(&qh->qtd_list)) { 2569 dev_dbg(hsotg->dev, "No QTDs in QH list\n"); 2570 return -ENOMEM; 2571 } 2572 2573 if (list_empty(&hsotg->free_hc_list)) { 2574 dev_dbg(hsotg->dev, "No free channel to assign\n"); 2575 return -ENOMEM; 2576 } 2577 2578 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan, 2579 hc_list_entry); 2580 2581 /* Remove host channel from free list */ 2582 list_del_init(&chan->hc_list_entry); 2583 2584 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry); 2585 urb = qtd->urb; 2586 qh->channel = chan; 2587 qtd->in_process = 1; 2588 2589 /* 2590 * Use usb_pipedevice to determine device address. This address is 2591 * 0 before the SET_ADDRESS command and the correct address afterward. 2592 */ 2593 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info); 2594 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info); 2595 chan->speed = qh->dev_speed; 2596 chan->max_packet = qh->maxp; 2597 2598 chan->xfer_started = 0; 2599 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS; 2600 chan->error_state = (qtd->error_count > 0); 2601 chan->halt_on_queue = 0; 2602 chan->halt_pending = 0; 2603 chan->requests = 0; 2604 2605 /* 2606 * The following values may be modified in the transfer type section 2607 * below. The xfer_len value may be reduced when the transfer is 2608 * started to accommodate the max widths of the XferSize and PktCnt 2609 * fields in the HCTSIZn register. 2610 */ 2611 2612 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0); 2613 if (chan->ep_is_in) 2614 chan->do_ping = 0; 2615 else 2616 chan->do_ping = qh->ping_state; 2617 2618 chan->data_pid_start = qh->data_toggle; 2619 chan->multi_count = 1; 2620 2621 if (urb->actual_length > urb->length && 2622 !dwc2_hcd_is_pipe_in(&urb->pipe_info)) 2623 urb->actual_length = urb->length; 2624 2625 if (hsotg->params.host_dma) 2626 chan->xfer_dma = urb->dma + urb->actual_length; 2627 else 2628 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length; 2629 2630 chan->xfer_len = urb->length - urb->actual_length; 2631 chan->xfer_count = 0; 2632 2633 /* Set the split attributes if required */ 2634 if (qh->do_split) 2635 dwc2_hc_init_split(hsotg, chan, qtd, urb); 2636 else 2637 chan->do_split = 0; 2638 2639 /* Set the transfer attributes */ 2640 dwc2_hc_init_xfer(hsotg, chan, qtd); 2641 2642 /* For non-dword aligned buffers */ 2643 if (hsotg->params.host_dma && qh->do_split && 2644 chan->ep_is_in && (chan->xfer_dma & 0x3)) { 2645 dev_vdbg(hsotg->dev, "Non-aligned buffer\n"); 2646 if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) { 2647 dev_err(hsotg->dev, 2648 "Failed to allocate memory to handle non-aligned buffer\n"); 2649 /* Add channel back to free list */ 2650 chan->align_buf = 0; 2651 chan->multi_count = 0; 2652 list_add_tail(&chan->hc_list_entry, 2653 &hsotg->free_hc_list); 2654 qtd->in_process = 0; 2655 qh->channel = NULL; 2656 return -ENOMEM; 2657 } 2658 } else { 2659 /* 2660 * We assume that DMA is always aligned in non-split 2661 * case or split out case. Warn if not. 2662 */ 2663 WARN_ON_ONCE(hsotg->params.host_dma && 2664 (chan->xfer_dma & 0x3)); 2665 chan->align_buf = 0; 2666 } 2667 2668 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 2669 chan->ep_type == USB_ENDPOINT_XFER_ISOC) 2670 /* 2671 * This value may be modified when the transfer is started 2672 * to reflect the actual transfer length 2673 */ 2674 chan->multi_count = qh->maxp_mult; 2675 2676 if (hsotg->params.dma_desc_enable) { 2677 chan->desc_list_addr = qh->desc_list_dma; 2678 chan->desc_list_sz = qh->desc_list_sz; 2679 } 2680 2681 dwc2_hc_init(hsotg, chan); 2682 chan->qh = qh; 2683 2684 return 0; 2685 } 2686 2687 /** 2688 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer 2689 * schedule and assigns them to available host channels. Called from the HCD 2690 * interrupt handler functions. 2691 * 2692 * @hsotg: The HCD state structure 2693 * 2694 * Return: The types of new transactions that were assigned to host channels 2695 */ 2696 enum dwc2_transaction_type dwc2_hcd_select_transactions( 2697 struct dwc2_hsotg *hsotg) 2698 { 2699 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE; 2700 struct list_head *qh_ptr; 2701 struct dwc2_qh *qh; 2702 int num_channels; 2703 2704 #ifdef DWC2_DEBUG_SOF 2705 dev_vdbg(hsotg->dev, " Select Transactions\n"); 2706 #endif 2707 2708 /* Process entries in the periodic ready list */ 2709 qh_ptr = hsotg->periodic_sched_ready.next; 2710 while (qh_ptr != &hsotg->periodic_sched_ready) { 2711 if (list_empty(&hsotg->free_hc_list)) 2712 break; 2713 if (hsotg->params.uframe_sched) { 2714 if (hsotg->available_host_channels <= 1) 2715 break; 2716 hsotg->available_host_channels--; 2717 } 2718 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry); 2719 if (dwc2_assign_and_init_hc(hsotg, qh)) { 2720 if (hsotg->params.uframe_sched) 2721 hsotg->available_host_channels++; 2722 break; 2723 } 2724 2725 /* 2726 * Move the QH from the periodic ready schedule to the 2727 * periodic assigned schedule 2728 */ 2729 qh_ptr = qh_ptr->next; 2730 list_move_tail(&qh->qh_list_entry, 2731 &hsotg->periodic_sched_assigned); 2732 ret_val = DWC2_TRANSACTION_PERIODIC; 2733 } 2734 2735 /* 2736 * Process entries in the inactive portion of the non-periodic 2737 * schedule. Some free host channels may not be used if they are 2738 * reserved for periodic transfers. 2739 */ 2740 num_channels = hsotg->params.host_channels; 2741 qh_ptr = hsotg->non_periodic_sched_inactive.next; 2742 while (qh_ptr != &hsotg->non_periodic_sched_inactive) { 2743 if (!hsotg->params.uframe_sched && 2744 hsotg->non_periodic_channels >= num_channels - 2745 hsotg->periodic_channels) 2746 break; 2747 if (list_empty(&hsotg->free_hc_list)) 2748 break; 2749 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry); 2750 if (hsotg->params.uframe_sched) { 2751 if (hsotg->available_host_channels < 1) 2752 break; 2753 hsotg->available_host_channels--; 2754 } 2755 2756 if (dwc2_assign_and_init_hc(hsotg, qh)) { 2757 if (hsotg->params.uframe_sched) 2758 hsotg->available_host_channels++; 2759 break; 2760 } 2761 2762 /* 2763 * Move the QH from the non-periodic inactive schedule to the 2764 * non-periodic active schedule 2765 */ 2766 qh_ptr = qh_ptr->next; 2767 list_move_tail(&qh->qh_list_entry, 2768 &hsotg->non_periodic_sched_active); 2769 2770 if (ret_val == DWC2_TRANSACTION_NONE) 2771 ret_val = DWC2_TRANSACTION_NON_PERIODIC; 2772 else 2773 ret_val = DWC2_TRANSACTION_ALL; 2774 2775 if (!hsotg->params.uframe_sched) 2776 hsotg->non_periodic_channels++; 2777 } 2778 2779 return ret_val; 2780 } 2781 2782 /** 2783 * dwc2_queue_transaction() - Attempts to queue a single transaction request for 2784 * a host channel associated with either a periodic or non-periodic transfer 2785 * 2786 * @hsotg: The HCD state structure 2787 * @chan: Host channel descriptor associated with either a periodic or 2788 * non-periodic transfer 2789 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO 2790 * for periodic transfers or the non-periodic Tx FIFO 2791 * for non-periodic transfers 2792 * 2793 * Return: 1 if a request is queued and more requests may be needed to 2794 * complete the transfer, 0 if no more requests are required for this 2795 * transfer, -1 if there is insufficient space in the Tx FIFO 2796 * 2797 * This function assumes that there is space available in the appropriate 2798 * request queue. For an OUT transfer or SETUP transaction in Slave mode, 2799 * it checks whether space is available in the appropriate Tx FIFO. 2800 * 2801 * Must be called with interrupt disabled and spinlock held 2802 */ 2803 static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg, 2804 struct dwc2_host_chan *chan, 2805 u16 fifo_dwords_avail) 2806 { 2807 int retval = 0; 2808 2809 if (chan->do_split) 2810 /* Put ourselves on the list to keep order straight */ 2811 list_move_tail(&chan->split_order_list_entry, 2812 &hsotg->split_order); 2813 2814 if (hsotg->params.host_dma && chan->qh) { 2815 if (hsotg->params.dma_desc_enable) { 2816 if (!chan->xfer_started || 2817 chan->ep_type == USB_ENDPOINT_XFER_ISOC) { 2818 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh); 2819 chan->qh->ping_state = 0; 2820 } 2821 } else if (!chan->xfer_started) { 2822 dwc2_hc_start_transfer(hsotg, chan); 2823 chan->qh->ping_state = 0; 2824 } 2825 } else if (chan->halt_pending) { 2826 /* Don't queue a request if the channel has been halted */ 2827 } else if (chan->halt_on_queue) { 2828 dwc2_hc_halt(hsotg, chan, chan->halt_status); 2829 } else if (chan->do_ping) { 2830 if (!chan->xfer_started) 2831 dwc2_hc_start_transfer(hsotg, chan); 2832 } else if (!chan->ep_is_in || 2833 chan->data_pid_start == DWC2_HC_PID_SETUP) { 2834 if ((fifo_dwords_avail * 4) >= chan->max_packet) { 2835 if (!chan->xfer_started) { 2836 dwc2_hc_start_transfer(hsotg, chan); 2837 retval = 1; 2838 } else { 2839 retval = dwc2_hc_continue_transfer(hsotg, chan); 2840 } 2841 } else { 2842 retval = -1; 2843 } 2844 } else { 2845 if (!chan->xfer_started) { 2846 dwc2_hc_start_transfer(hsotg, chan); 2847 retval = 1; 2848 } else { 2849 retval = dwc2_hc_continue_transfer(hsotg, chan); 2850 } 2851 } 2852 2853 return retval; 2854 } 2855 2856 /* 2857 * Processes periodic channels for the next frame and queues transactions for 2858 * these channels to the DWC_otg controller. After queueing transactions, the 2859 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions 2860 * to queue as Periodic Tx FIFO or request queue space becomes available. 2861 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled. 2862 * 2863 * Must be called with interrupt disabled and spinlock held 2864 */ 2865 static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg) 2866 { 2867 struct list_head *qh_ptr; 2868 struct dwc2_qh *qh; 2869 u32 tx_status; 2870 u32 fspcavail; 2871 u32 gintmsk; 2872 int status; 2873 bool no_queue_space = false; 2874 bool no_fifo_space = false; 2875 u32 qspcavail; 2876 2877 /* If empty list then just adjust interrupt enables */ 2878 if (list_empty(&hsotg->periodic_sched_assigned)) 2879 goto exit; 2880 2881 if (dbg_perio()) 2882 dev_vdbg(hsotg->dev, "Queue periodic transactions\n"); 2883 2884 tx_status = dwc2_readl(hsotg, HPTXSTS); 2885 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 2886 TXSTS_QSPCAVAIL_SHIFT; 2887 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 2888 TXSTS_FSPCAVAIL_SHIFT; 2889 2890 if (dbg_perio()) { 2891 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n", 2892 qspcavail); 2893 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n", 2894 fspcavail); 2895 } 2896 2897 qh_ptr = hsotg->periodic_sched_assigned.next; 2898 while (qh_ptr != &hsotg->periodic_sched_assigned) { 2899 tx_status = dwc2_readl(hsotg, HPTXSTS); 2900 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 2901 TXSTS_QSPCAVAIL_SHIFT; 2902 if (qspcavail == 0) { 2903 no_queue_space = true; 2904 break; 2905 } 2906 2907 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry); 2908 if (!qh->channel) { 2909 qh_ptr = qh_ptr->next; 2910 continue; 2911 } 2912 2913 /* Make sure EP's TT buffer is clean before queueing qtds */ 2914 if (qh->tt_buffer_dirty) { 2915 qh_ptr = qh_ptr->next; 2916 continue; 2917 } 2918 2919 /* 2920 * Set a flag if we're queuing high-bandwidth in slave mode. 2921 * The flag prevents any halts to get into the request queue in 2922 * the middle of multiple high-bandwidth packets getting queued. 2923 */ 2924 if (!hsotg->params.host_dma && 2925 qh->channel->multi_count > 1) 2926 hsotg->queuing_high_bandwidth = 1; 2927 2928 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 2929 TXSTS_FSPCAVAIL_SHIFT; 2930 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail); 2931 if (status < 0) { 2932 no_fifo_space = true; 2933 break; 2934 } 2935 2936 /* 2937 * In Slave mode, stay on the current transfer until there is 2938 * nothing more to do or the high-bandwidth request count is 2939 * reached. In DMA mode, only need to queue one request. The 2940 * controller automatically handles multiple packets for 2941 * high-bandwidth transfers. 2942 */ 2943 if (hsotg->params.host_dma || status == 0 || 2944 qh->channel->requests == qh->channel->multi_count) { 2945 qh_ptr = qh_ptr->next; 2946 /* 2947 * Move the QH from the periodic assigned schedule to 2948 * the periodic queued schedule 2949 */ 2950 list_move_tail(&qh->qh_list_entry, 2951 &hsotg->periodic_sched_queued); 2952 2953 /* done queuing high bandwidth */ 2954 hsotg->queuing_high_bandwidth = 0; 2955 } 2956 } 2957 2958 exit: 2959 if (no_queue_space || no_fifo_space || 2960 (!hsotg->params.host_dma && 2961 !list_empty(&hsotg->periodic_sched_assigned))) { 2962 /* 2963 * May need to queue more transactions as the request 2964 * queue or Tx FIFO empties. Enable the periodic Tx 2965 * FIFO empty interrupt. (Always use the half-empty 2966 * level to ensure that new requests are loaded as 2967 * soon as possible.) 2968 */ 2969 gintmsk = dwc2_readl(hsotg, GINTMSK); 2970 if (!(gintmsk & GINTSTS_PTXFEMP)) { 2971 gintmsk |= GINTSTS_PTXFEMP; 2972 dwc2_writel(hsotg, gintmsk, GINTMSK); 2973 } 2974 } else { 2975 /* 2976 * Disable the Tx FIFO empty interrupt since there are 2977 * no more transactions that need to be queued right 2978 * now. This function is called from interrupt 2979 * handlers to queue more transactions as transfer 2980 * states change. 2981 */ 2982 gintmsk = dwc2_readl(hsotg, GINTMSK); 2983 if (gintmsk & GINTSTS_PTXFEMP) { 2984 gintmsk &= ~GINTSTS_PTXFEMP; 2985 dwc2_writel(hsotg, gintmsk, GINTMSK); 2986 } 2987 } 2988 } 2989 2990 /* 2991 * Processes active non-periodic channels and queues transactions for these 2992 * channels to the DWC_otg controller. After queueing transactions, the NP Tx 2993 * FIFO Empty interrupt is enabled if there are more transactions to queue as 2994 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx 2995 * FIFO Empty interrupt is disabled. 2996 * 2997 * Must be called with interrupt disabled and spinlock held 2998 */ 2999 static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg) 3000 { 3001 struct list_head *orig_qh_ptr; 3002 struct dwc2_qh *qh; 3003 u32 tx_status; 3004 u32 qspcavail; 3005 u32 fspcavail; 3006 u32 gintmsk; 3007 int status; 3008 int no_queue_space = 0; 3009 int no_fifo_space = 0; 3010 int more_to_do = 0; 3011 3012 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n"); 3013 3014 tx_status = dwc2_readl(hsotg, GNPTXSTS); 3015 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 3016 TXSTS_QSPCAVAIL_SHIFT; 3017 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 3018 TXSTS_FSPCAVAIL_SHIFT; 3019 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n", 3020 qspcavail); 3021 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n", 3022 fspcavail); 3023 3024 /* 3025 * Keep track of the starting point. Skip over the start-of-list 3026 * entry. 3027 */ 3028 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active) 3029 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next; 3030 orig_qh_ptr = hsotg->non_periodic_qh_ptr; 3031 3032 /* 3033 * Process once through the active list or until no more space is 3034 * available in the request queue or the Tx FIFO 3035 */ 3036 do { 3037 tx_status = dwc2_readl(hsotg, GNPTXSTS); 3038 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 3039 TXSTS_QSPCAVAIL_SHIFT; 3040 if (!hsotg->params.host_dma && qspcavail == 0) { 3041 no_queue_space = 1; 3042 break; 3043 } 3044 3045 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh, 3046 qh_list_entry); 3047 if (!qh->channel) 3048 goto next; 3049 3050 /* Make sure EP's TT buffer is clean before queueing qtds */ 3051 if (qh->tt_buffer_dirty) 3052 goto next; 3053 3054 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 3055 TXSTS_FSPCAVAIL_SHIFT; 3056 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail); 3057 3058 if (status > 0) { 3059 more_to_do = 1; 3060 } else if (status < 0) { 3061 no_fifo_space = 1; 3062 break; 3063 } 3064 next: 3065 /* Advance to next QH, skipping start-of-list entry */ 3066 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next; 3067 if (hsotg->non_periodic_qh_ptr == 3068 &hsotg->non_periodic_sched_active) 3069 hsotg->non_periodic_qh_ptr = 3070 hsotg->non_periodic_qh_ptr->next; 3071 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr); 3072 3073 if (!hsotg->params.host_dma) { 3074 tx_status = dwc2_readl(hsotg, GNPTXSTS); 3075 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >> 3076 TXSTS_QSPCAVAIL_SHIFT; 3077 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >> 3078 TXSTS_FSPCAVAIL_SHIFT; 3079 dev_vdbg(hsotg->dev, 3080 " NP Tx Req Queue Space Avail (after queue): %d\n", 3081 qspcavail); 3082 dev_vdbg(hsotg->dev, 3083 " NP Tx FIFO Space Avail (after queue): %d\n", 3084 fspcavail); 3085 3086 if (more_to_do || no_queue_space || no_fifo_space) { 3087 /* 3088 * May need to queue more transactions as the request 3089 * queue or Tx FIFO empties. Enable the non-periodic 3090 * Tx FIFO empty interrupt. (Always use the half-empty 3091 * level to ensure that new requests are loaded as 3092 * soon as possible.) 3093 */ 3094 gintmsk = dwc2_readl(hsotg, GINTMSK); 3095 gintmsk |= GINTSTS_NPTXFEMP; 3096 dwc2_writel(hsotg, gintmsk, GINTMSK); 3097 } else { 3098 /* 3099 * Disable the Tx FIFO empty interrupt since there are 3100 * no more transactions that need to be queued right 3101 * now. This function is called from interrupt 3102 * handlers to queue more transactions as transfer 3103 * states change. 3104 */ 3105 gintmsk = dwc2_readl(hsotg, GINTMSK); 3106 gintmsk &= ~GINTSTS_NPTXFEMP; 3107 dwc2_writel(hsotg, gintmsk, GINTMSK); 3108 } 3109 } 3110 } 3111 3112 /** 3113 * dwc2_hcd_queue_transactions() - Processes the currently active host channels 3114 * and queues transactions for these channels to the DWC_otg controller. Called 3115 * from the HCD interrupt handler functions. 3116 * 3117 * @hsotg: The HCD state structure 3118 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic, 3119 * or both) 3120 * 3121 * Must be called with interrupt disabled and spinlock held 3122 */ 3123 void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg, 3124 enum dwc2_transaction_type tr_type) 3125 { 3126 #ifdef DWC2_DEBUG_SOF 3127 dev_vdbg(hsotg->dev, "Queue Transactions\n"); 3128 #endif 3129 /* Process host channels associated with periodic transfers */ 3130 if (tr_type == DWC2_TRANSACTION_PERIODIC || 3131 tr_type == DWC2_TRANSACTION_ALL) 3132 dwc2_process_periodic_channels(hsotg); 3133 3134 /* Process host channels associated with non-periodic transfers */ 3135 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC || 3136 tr_type == DWC2_TRANSACTION_ALL) { 3137 if (!list_empty(&hsotg->non_periodic_sched_active)) { 3138 dwc2_process_non_periodic_channels(hsotg); 3139 } else { 3140 /* 3141 * Ensure NP Tx FIFO empty interrupt is disabled when 3142 * there are no non-periodic transfers to process 3143 */ 3144 u32 gintmsk = dwc2_readl(hsotg, GINTMSK); 3145 3146 gintmsk &= ~GINTSTS_NPTXFEMP; 3147 dwc2_writel(hsotg, gintmsk, GINTMSK); 3148 } 3149 } 3150 } 3151 3152 static void dwc2_conn_id_status_change(struct work_struct *work) 3153 { 3154 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg, 3155 wf_otg); 3156 u32 count = 0; 3157 u32 gotgctl; 3158 unsigned long flags; 3159 3160 dev_dbg(hsotg->dev, "%s()\n", __func__); 3161 3162 gotgctl = dwc2_readl(hsotg, GOTGCTL); 3163 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl); 3164 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n", 3165 !!(gotgctl & GOTGCTL_CONID_B)); 3166 3167 /* B-Device connector (Device Mode) */ 3168 if (gotgctl & GOTGCTL_CONID_B) { 3169 dwc2_vbus_supply_exit(hsotg); 3170 /* Wait for switch to device mode */ 3171 dev_dbg(hsotg->dev, "connId B\n"); 3172 if (hsotg->bus_suspended) { 3173 dev_info(hsotg->dev, 3174 "Do port resume before switching to device mode\n"); 3175 dwc2_port_resume(hsotg); 3176 } 3177 while (!dwc2_is_device_mode(hsotg)) { 3178 dev_info(hsotg->dev, 3179 "Waiting for Peripheral Mode, Mode=%s\n", 3180 dwc2_is_host_mode(hsotg) ? "Host" : 3181 "Peripheral"); 3182 msleep(20); 3183 /* 3184 * Sometimes the initial GOTGCTRL read is wrong, so 3185 * check it again and jump to host mode if that was 3186 * the case. 3187 */ 3188 gotgctl = dwc2_readl(hsotg, GOTGCTL); 3189 if (!(gotgctl & GOTGCTL_CONID_B)) 3190 goto host; 3191 if (++count > 250) 3192 break; 3193 } 3194 if (count > 250) 3195 dev_err(hsotg->dev, 3196 "Connection id status change timed out\n"); 3197 3198 /* 3199 * Exit Partial Power Down without restoring registers. 3200 * No need to check the return value as registers 3201 * are not being restored. 3202 */ 3203 if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2) 3204 dwc2_exit_partial_power_down(hsotg, 0, false); 3205 3206 hsotg->op_state = OTG_STATE_B_PERIPHERAL; 3207 dwc2_core_init(hsotg, false); 3208 dwc2_enable_global_interrupts(hsotg); 3209 spin_lock_irqsave(&hsotg->lock, flags); 3210 dwc2_hsotg_core_init_disconnected(hsotg, false); 3211 spin_unlock_irqrestore(&hsotg->lock, flags); 3212 /* Enable ACG feature in device mode,if supported */ 3213 dwc2_enable_acg(hsotg); 3214 dwc2_hsotg_core_connect(hsotg); 3215 } else { 3216 host: 3217 /* A-Device connector (Host Mode) */ 3218 dev_dbg(hsotg->dev, "connId A\n"); 3219 while (!dwc2_is_host_mode(hsotg)) { 3220 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n", 3221 dwc2_is_host_mode(hsotg) ? 3222 "Host" : "Peripheral"); 3223 msleep(20); 3224 if (++count > 250) 3225 break; 3226 } 3227 if (count > 250) 3228 dev_err(hsotg->dev, 3229 "Connection id status change timed out\n"); 3230 3231 spin_lock_irqsave(&hsotg->lock, flags); 3232 dwc2_hsotg_disconnect(hsotg); 3233 spin_unlock_irqrestore(&hsotg->lock, flags); 3234 3235 hsotg->op_state = OTG_STATE_A_HOST; 3236 /* Initialize the Core for Host mode */ 3237 dwc2_core_init(hsotg, false); 3238 dwc2_enable_global_interrupts(hsotg); 3239 dwc2_hcd_start(hsotg); 3240 } 3241 } 3242 3243 static void dwc2_wakeup_detected(struct timer_list *t) 3244 { 3245 struct dwc2_hsotg *hsotg = timer_container_of(hsotg, t, wkp_timer); 3246 u32 hprt0; 3247 3248 dev_dbg(hsotg->dev, "%s()\n", __func__); 3249 3250 /* 3251 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms 3252 * so that OPT tests pass with all PHYs.) 3253 */ 3254 hprt0 = dwc2_read_hprt0(hsotg); 3255 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0); 3256 hprt0 &= ~HPRT0_RES; 3257 dwc2_writel(hsotg, hprt0, HPRT0); 3258 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n", 3259 dwc2_readl(hsotg, HPRT0)); 3260 3261 dwc2_hcd_rem_wakeup(hsotg); 3262 hsotg->bus_suspended = false; 3263 3264 /* Change to L0 state */ 3265 hsotg->lx_state = DWC2_L0; 3266 } 3267 3268 static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg) 3269 { 3270 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg); 3271 3272 return hcd->self.b_hnp_enable; 3273 } 3274 3275 /** 3276 * dwc2_port_suspend() - Put controller in suspend mode for host. 3277 * 3278 * @hsotg: Programming view of the DWC_otg controller 3279 * @windex: The control request wIndex field 3280 * 3281 * Return: non-zero if failed to enter suspend mode for host. 3282 * 3283 * This function is for entering Host mode suspend. 3284 * Must NOT be called with interrupt disabled or spinlock held. 3285 */ 3286 int dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex) 3287 { 3288 unsigned long flags; 3289 u32 pcgctl; 3290 u32 gotgctl; 3291 int ret = 0; 3292 3293 dev_dbg(hsotg->dev, "%s()\n", __func__); 3294 3295 spin_lock_irqsave(&hsotg->lock, flags); 3296 3297 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) { 3298 gotgctl = dwc2_readl(hsotg, GOTGCTL); 3299 gotgctl |= GOTGCTL_HSTSETHNPEN; 3300 dwc2_writel(hsotg, gotgctl, GOTGCTL); 3301 hsotg->op_state = OTG_STATE_A_SUSPEND; 3302 } 3303 3304 switch (hsotg->params.power_down) { 3305 case DWC2_POWER_DOWN_PARAM_PARTIAL: 3306 ret = dwc2_enter_partial_power_down(hsotg); 3307 if (ret) 3308 dev_err(hsotg->dev, 3309 "enter partial_power_down failed.\n"); 3310 break; 3311 case DWC2_POWER_DOWN_PARAM_HIBERNATION: 3312 /* 3313 * Perform spin unlock and lock because in 3314 * "dwc2_host_enter_hibernation()" function there is a spinlock 3315 * logic which prevents servicing of any IRQ during entering 3316 * hibernation. 3317 */ 3318 spin_unlock_irqrestore(&hsotg->lock, flags); 3319 ret = dwc2_enter_hibernation(hsotg, 1); 3320 if (ret) 3321 dev_err(hsotg->dev, "enter hibernation failed.\n"); 3322 spin_lock_irqsave(&hsotg->lock, flags); 3323 break; 3324 case DWC2_POWER_DOWN_PARAM_NONE: 3325 /* 3326 * If not hibernation nor partial power down are supported, 3327 * clock gating is used to save power. 3328 */ 3329 if (!hsotg->params.no_clock_gating) 3330 dwc2_host_enter_clock_gating(hsotg); 3331 break; 3332 } 3333 3334 /* For HNP the bus must be suspended for at least 200ms */ 3335 if (dwc2_host_is_b_hnp_enabled(hsotg)) { 3336 pcgctl = dwc2_readl(hsotg, PCGCTL); 3337 pcgctl &= ~PCGCTL_STOPPCLK; 3338 dwc2_writel(hsotg, pcgctl, PCGCTL); 3339 3340 spin_unlock_irqrestore(&hsotg->lock, flags); 3341 3342 msleep(200); 3343 } else { 3344 spin_unlock_irqrestore(&hsotg->lock, flags); 3345 } 3346 3347 return ret; 3348 } 3349 3350 /** 3351 * dwc2_port_resume() - Exit controller from suspend mode for host. 3352 * 3353 * @hsotg: Programming view of the DWC_otg controller 3354 * 3355 * Return: non-zero if failed to exit suspend mode for host. 3356 * 3357 * This function is for exiting Host mode suspend. 3358 * Must NOT be called with interrupt disabled or spinlock held. 3359 */ 3360 int dwc2_port_resume(struct dwc2_hsotg *hsotg) 3361 { 3362 unsigned long flags; 3363 int ret = 0; 3364 3365 spin_lock_irqsave(&hsotg->lock, flags); 3366 3367 switch (hsotg->params.power_down) { 3368 case DWC2_POWER_DOWN_PARAM_PARTIAL: 3369 ret = dwc2_exit_partial_power_down(hsotg, 0, true); 3370 if (ret) 3371 dev_err(hsotg->dev, 3372 "exit partial_power_down failed.\n"); 3373 break; 3374 case DWC2_POWER_DOWN_PARAM_HIBERNATION: 3375 /* Exit host hibernation. */ 3376 ret = dwc2_exit_hibernation(hsotg, 0, 0, 1); 3377 if (ret) 3378 dev_err(hsotg->dev, "exit hibernation failed.\n"); 3379 break; 3380 case DWC2_POWER_DOWN_PARAM_NONE: 3381 /* 3382 * If not hibernation nor partial power down are supported, 3383 * port resume is done using the clock gating programming flow. 3384 */ 3385 spin_unlock_irqrestore(&hsotg->lock, flags); 3386 dwc2_host_exit_clock_gating(hsotg, 0); 3387 spin_lock_irqsave(&hsotg->lock, flags); 3388 break; 3389 } 3390 3391 spin_unlock_irqrestore(&hsotg->lock, flags); 3392 3393 return ret; 3394 } 3395 3396 /* Handles hub class-specific requests */ 3397 static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq, 3398 u16 wvalue, u16 windex, char *buf, u16 wlength) 3399 { 3400 struct usb_hub_descriptor *hub_desc; 3401 int retval = 0; 3402 u32 hprt0; 3403 u32 port_status; 3404 u32 speed; 3405 u32 pcgctl; 3406 u32 pwr; 3407 3408 switch (typereq) { 3409 case ClearHubFeature: 3410 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue); 3411 3412 switch (wvalue) { 3413 case C_HUB_LOCAL_POWER: 3414 case C_HUB_OVER_CURRENT: 3415 /* Nothing required here */ 3416 break; 3417 3418 default: 3419 retval = -EINVAL; 3420 dev_err(hsotg->dev, 3421 "ClearHubFeature request %1xh unknown\n", 3422 wvalue); 3423 } 3424 break; 3425 3426 case ClearPortFeature: 3427 if (wvalue != USB_PORT_FEAT_L1) 3428 if (!windex || windex > 1) 3429 goto error; 3430 switch (wvalue) { 3431 case USB_PORT_FEAT_ENABLE: 3432 dev_dbg(hsotg->dev, 3433 "ClearPortFeature USB_PORT_FEAT_ENABLE\n"); 3434 hprt0 = dwc2_read_hprt0(hsotg); 3435 hprt0 |= HPRT0_ENA; 3436 dwc2_writel(hsotg, hprt0, HPRT0); 3437 break; 3438 3439 case USB_PORT_FEAT_SUSPEND: 3440 dev_dbg(hsotg->dev, 3441 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n"); 3442 3443 if (hsotg->bus_suspended) 3444 retval = dwc2_port_resume(hsotg); 3445 break; 3446 3447 case USB_PORT_FEAT_POWER: 3448 dev_dbg(hsotg->dev, 3449 "ClearPortFeature USB_PORT_FEAT_POWER\n"); 3450 hprt0 = dwc2_read_hprt0(hsotg); 3451 pwr = hprt0 & HPRT0_PWR; 3452 hprt0 &= ~HPRT0_PWR; 3453 dwc2_writel(hsotg, hprt0, HPRT0); 3454 if (pwr) 3455 dwc2_vbus_supply_exit(hsotg); 3456 break; 3457 3458 case USB_PORT_FEAT_INDICATOR: 3459 dev_dbg(hsotg->dev, 3460 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n"); 3461 /* Port indicator not supported */ 3462 break; 3463 3464 case USB_PORT_FEAT_C_CONNECTION: 3465 /* 3466 * Clears driver's internal Connect Status Change flag 3467 */ 3468 dev_dbg(hsotg->dev, 3469 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n"); 3470 hsotg->flags.b.port_connect_status_change = 0; 3471 break; 3472 3473 case USB_PORT_FEAT_C_RESET: 3474 /* Clears driver's internal Port Reset Change flag */ 3475 dev_dbg(hsotg->dev, 3476 "ClearPortFeature USB_PORT_FEAT_C_RESET\n"); 3477 hsotg->flags.b.port_reset_change = 0; 3478 break; 3479 3480 case USB_PORT_FEAT_C_ENABLE: 3481 /* 3482 * Clears the driver's internal Port Enable/Disable 3483 * Change flag 3484 */ 3485 dev_dbg(hsotg->dev, 3486 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n"); 3487 hsotg->flags.b.port_enable_change = 0; 3488 break; 3489 3490 case USB_PORT_FEAT_C_SUSPEND: 3491 /* 3492 * Clears the driver's internal Port Suspend Change 3493 * flag, which is set when resume signaling on the host 3494 * port is complete 3495 */ 3496 dev_dbg(hsotg->dev, 3497 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n"); 3498 hsotg->flags.b.port_suspend_change = 0; 3499 break; 3500 3501 case USB_PORT_FEAT_C_PORT_L1: 3502 dev_dbg(hsotg->dev, 3503 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n"); 3504 hsotg->flags.b.port_l1_change = 0; 3505 break; 3506 3507 case USB_PORT_FEAT_C_OVER_CURRENT: 3508 dev_dbg(hsotg->dev, 3509 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n"); 3510 hsotg->flags.b.port_over_current_change = 0; 3511 break; 3512 3513 default: 3514 retval = -EINVAL; 3515 dev_err(hsotg->dev, 3516 "ClearPortFeature request %1xh unknown or unsupported\n", 3517 wvalue); 3518 } 3519 break; 3520 3521 case GetHubDescriptor: 3522 dev_dbg(hsotg->dev, "GetHubDescriptor\n"); 3523 hub_desc = (struct usb_hub_descriptor *)buf; 3524 hub_desc->bDescLength = 9; 3525 hub_desc->bDescriptorType = USB_DT_HUB; 3526 hub_desc->bNbrPorts = 1; 3527 hub_desc->wHubCharacteristics = 3528 cpu_to_le16(HUB_CHAR_COMMON_LPSM | 3529 HUB_CHAR_INDV_PORT_OCPM); 3530 hub_desc->bPwrOn2PwrGood = 1; 3531 hub_desc->bHubContrCurrent = 0; 3532 hub_desc->u.hs.DeviceRemovable[0] = 0; 3533 hub_desc->u.hs.DeviceRemovable[1] = 0xff; 3534 break; 3535 3536 case GetHubStatus: 3537 dev_dbg(hsotg->dev, "GetHubStatus\n"); 3538 memset(buf, 0, 4); 3539 break; 3540 3541 case GetPortStatus: 3542 dev_vdbg(hsotg->dev, 3543 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex, 3544 hsotg->flags.d32); 3545 if (!windex || windex > 1) 3546 goto error; 3547 3548 port_status = 0; 3549 if (hsotg->flags.b.port_connect_status_change) 3550 port_status |= USB_PORT_STAT_C_CONNECTION << 16; 3551 if (hsotg->flags.b.port_enable_change) 3552 port_status |= USB_PORT_STAT_C_ENABLE << 16; 3553 if (hsotg->flags.b.port_suspend_change) 3554 port_status |= USB_PORT_STAT_C_SUSPEND << 16; 3555 if (hsotg->flags.b.port_l1_change) 3556 port_status |= USB_PORT_STAT_C_L1 << 16; 3557 if (hsotg->flags.b.port_reset_change) 3558 port_status |= USB_PORT_STAT_C_RESET << 16; 3559 if (hsotg->flags.b.port_over_current_change) { 3560 dev_warn(hsotg->dev, "Overcurrent change detected\n"); 3561 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16; 3562 } 3563 3564 if (dwc2_is_device_mode(hsotg)) { 3565 /* 3566 * Just return 0's for the remainder of the port status 3567 * since the port register can't be read if the core 3568 * is in device mode. 3569 */ 3570 *(__le32 *)buf = cpu_to_le32(port_status); 3571 break; 3572 } 3573 3574 hprt0 = dwc2_readl(hsotg, HPRT0); 3575 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0); 3576 3577 if (hprt0 & HPRT0_CONNSTS) 3578 port_status |= USB_PORT_STAT_CONNECTION; 3579 if (hprt0 & HPRT0_ENA) 3580 port_status |= USB_PORT_STAT_ENABLE; 3581 if (hprt0 & HPRT0_SUSP) 3582 port_status |= USB_PORT_STAT_SUSPEND; 3583 if (hprt0 & HPRT0_OVRCURRACT) 3584 port_status |= USB_PORT_STAT_OVERCURRENT; 3585 if (hprt0 & HPRT0_RST) 3586 port_status |= USB_PORT_STAT_RESET; 3587 if (hprt0 & HPRT0_PWR) 3588 port_status |= USB_PORT_STAT_POWER; 3589 3590 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT; 3591 if (speed == HPRT0_SPD_HIGH_SPEED) 3592 port_status |= USB_PORT_STAT_HIGH_SPEED; 3593 else if (speed == HPRT0_SPD_LOW_SPEED) 3594 port_status |= USB_PORT_STAT_LOW_SPEED; 3595 3596 if (hprt0 & HPRT0_TSTCTL_MASK) 3597 port_status |= USB_PORT_STAT_TEST; 3598 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */ 3599 3600 if (hsotg->params.dma_desc_fs_enable) { 3601 /* 3602 * Enable descriptor DMA only if a full speed 3603 * device is connected. 3604 */ 3605 if (hsotg->new_connection && 3606 ((port_status & 3607 (USB_PORT_STAT_CONNECTION | 3608 USB_PORT_STAT_HIGH_SPEED | 3609 USB_PORT_STAT_LOW_SPEED)) == 3610 USB_PORT_STAT_CONNECTION)) { 3611 u32 hcfg; 3612 3613 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n"); 3614 hsotg->params.dma_desc_enable = true; 3615 hcfg = dwc2_readl(hsotg, HCFG); 3616 hcfg |= HCFG_DESCDMA; 3617 dwc2_writel(hsotg, hcfg, HCFG); 3618 hsotg->new_connection = false; 3619 } 3620 } 3621 3622 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status); 3623 *(__le32 *)buf = cpu_to_le32(port_status); 3624 break; 3625 3626 case SetHubFeature: 3627 dev_dbg(hsotg->dev, "SetHubFeature\n"); 3628 /* No HUB features supported */ 3629 break; 3630 3631 case SetPortFeature: 3632 dev_dbg(hsotg->dev, "SetPortFeature\n"); 3633 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1)) 3634 goto error; 3635 3636 if (dwc2_is_device_mode(hsotg)) { 3637 /* 3638 * Just return 0's for the remainder of the port status 3639 * since the port register can't be read if the core 3640 * is in device mode. 3641 */ 3642 break; 3643 } 3644 3645 switch (wvalue) { 3646 case USB_PORT_FEAT_SUSPEND: 3647 dev_dbg(hsotg->dev, 3648 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n"); 3649 if (windex != hsotg->otg_port) 3650 goto error; 3651 if (!hsotg->bus_suspended) 3652 retval = dwc2_port_suspend(hsotg, windex); 3653 break; 3654 3655 case USB_PORT_FEAT_POWER: 3656 dev_dbg(hsotg->dev, 3657 "SetPortFeature - USB_PORT_FEAT_POWER\n"); 3658 hprt0 = dwc2_read_hprt0(hsotg); 3659 pwr = hprt0 & HPRT0_PWR; 3660 hprt0 |= HPRT0_PWR; 3661 dwc2_writel(hsotg, hprt0, HPRT0); 3662 if (!pwr) 3663 dwc2_vbus_supply_init(hsotg); 3664 break; 3665 3666 case USB_PORT_FEAT_RESET: 3667 dev_dbg(hsotg->dev, 3668 "SetPortFeature - USB_PORT_FEAT_RESET\n"); 3669 3670 hprt0 = dwc2_read_hprt0(hsotg); 3671 3672 if (hsotg->hibernated) { 3673 retval = dwc2_exit_hibernation(hsotg, 0, 1, 1); 3674 if (retval) 3675 dev_err(hsotg->dev, 3676 "exit hibernation failed\n"); 3677 } 3678 3679 if (hsotg->in_ppd) { 3680 retval = dwc2_exit_partial_power_down(hsotg, 1, 3681 true); 3682 if (retval) 3683 dev_err(hsotg->dev, 3684 "exit partial_power_down failed\n"); 3685 } 3686 3687 if (hsotg->params.power_down == 3688 DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended) 3689 dwc2_host_exit_clock_gating(hsotg, 0); 3690 3691 pcgctl = dwc2_readl(hsotg, PCGCTL); 3692 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK); 3693 dwc2_writel(hsotg, pcgctl, PCGCTL); 3694 /* ??? Original driver does this */ 3695 dwc2_writel(hsotg, 0, PCGCTL); 3696 3697 hprt0 = dwc2_read_hprt0(hsotg); 3698 pwr = hprt0 & HPRT0_PWR; 3699 /* Clear suspend bit if resetting from suspend state */ 3700 hprt0 &= ~HPRT0_SUSP; 3701 3702 /* 3703 * When B-Host the Port reset bit is set in the Start 3704 * HCD Callback function, so that the reset is started 3705 * within 1ms of the HNP success interrupt 3706 */ 3707 if (!dwc2_hcd_is_b_host(hsotg)) { 3708 hprt0 |= HPRT0_PWR | HPRT0_RST; 3709 dev_dbg(hsotg->dev, 3710 "In host mode, hprt0=%08x\n", hprt0); 3711 dwc2_writel(hsotg, hprt0, HPRT0); 3712 if (!pwr) 3713 dwc2_vbus_supply_init(hsotg); 3714 } 3715 3716 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */ 3717 msleep(50); 3718 hprt0 &= ~HPRT0_RST; 3719 dwc2_writel(hsotg, hprt0, HPRT0); 3720 hsotg->lx_state = DWC2_L0; /* Now back to On state */ 3721 break; 3722 3723 case USB_PORT_FEAT_INDICATOR: 3724 dev_dbg(hsotg->dev, 3725 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n"); 3726 /* Not supported */ 3727 break; 3728 3729 case USB_PORT_FEAT_TEST: 3730 hprt0 = dwc2_read_hprt0(hsotg); 3731 dev_dbg(hsotg->dev, 3732 "SetPortFeature - USB_PORT_FEAT_TEST\n"); 3733 hprt0 &= ~HPRT0_TSTCTL_MASK; 3734 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT; 3735 dwc2_writel(hsotg, hprt0, HPRT0); 3736 break; 3737 3738 default: 3739 retval = -EINVAL; 3740 dev_err(hsotg->dev, 3741 "SetPortFeature %1xh unknown or unsupported\n", 3742 wvalue); 3743 break; 3744 } 3745 break; 3746 3747 default: 3748 error: 3749 retval = -EINVAL; 3750 dev_dbg(hsotg->dev, 3751 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n", 3752 typereq, windex, wvalue); 3753 break; 3754 } 3755 3756 return retval; 3757 } 3758 3759 static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port) 3760 { 3761 int retval; 3762 3763 if (port != 1) 3764 return -EINVAL; 3765 3766 retval = (hsotg->flags.b.port_connect_status_change || 3767 hsotg->flags.b.port_reset_change || 3768 hsotg->flags.b.port_enable_change || 3769 hsotg->flags.b.port_suspend_change || 3770 hsotg->flags.b.port_over_current_change); 3771 3772 if (retval) { 3773 dev_dbg(hsotg->dev, 3774 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n"); 3775 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n", 3776 hsotg->flags.b.port_connect_status_change); 3777 dev_dbg(hsotg->dev, " port_reset_change: %d\n", 3778 hsotg->flags.b.port_reset_change); 3779 dev_dbg(hsotg->dev, " port_enable_change: %d\n", 3780 hsotg->flags.b.port_enable_change); 3781 dev_dbg(hsotg->dev, " port_suspend_change: %d\n", 3782 hsotg->flags.b.port_suspend_change); 3783 dev_dbg(hsotg->dev, " port_over_current_change: %d\n", 3784 hsotg->flags.b.port_over_current_change); 3785 } 3786 3787 return retval; 3788 } 3789 3790 int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg) 3791 { 3792 u32 hfnum = dwc2_readl(hsotg, HFNUM); 3793 3794 #ifdef DWC2_DEBUG_SOF 3795 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n", 3796 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT); 3797 #endif 3798 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT; 3799 } 3800 3801 int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us) 3802 { 3803 u32 hprt = dwc2_readl(hsotg, HPRT0); 3804 u32 hfir = dwc2_readl(hsotg, HFIR); 3805 u32 hfnum = dwc2_readl(hsotg, HFNUM); 3806 unsigned int us_per_frame; 3807 unsigned int frame_number; 3808 unsigned int remaining; 3809 unsigned int interval; 3810 unsigned int phy_clks; 3811 3812 /* High speed has 125 us per (micro) frame; others are 1 ms per */ 3813 us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125; 3814 3815 /* Extract fields */ 3816 frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT; 3817 remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT; 3818 interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT; 3819 3820 /* 3821 * Number of phy clocks since the last tick of the frame number after 3822 * "us" has passed. 3823 */ 3824 phy_clks = (interval - remaining) + 3825 DIV_ROUND_UP(interval * us, us_per_frame); 3826 3827 return dwc2_frame_num_inc(frame_number, phy_clks / interval); 3828 } 3829 3830 int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg) 3831 { 3832 return hsotg->op_state == OTG_STATE_B_HOST; 3833 } 3834 3835 static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg, 3836 int iso_desc_count, 3837 gfp_t mem_flags) 3838 { 3839 struct dwc2_hcd_urb *urb; 3840 3841 urb = kzalloc_flex(*urb, iso_descs, iso_desc_count, mem_flags); 3842 if (urb) 3843 urb->packet_count = iso_desc_count; 3844 return urb; 3845 } 3846 3847 static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg, 3848 struct dwc2_hcd_urb *urb, u8 dev_addr, 3849 u8 ep_num, u8 ep_type, u8 ep_dir, 3850 u16 maxp, u16 maxp_mult) 3851 { 3852 if (dbg_perio() || 3853 ep_type == USB_ENDPOINT_XFER_BULK || 3854 ep_type == USB_ENDPOINT_XFER_CONTROL) 3855 dev_vdbg(hsotg->dev, 3856 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n", 3857 dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult); 3858 urb->pipe_info.dev_addr = dev_addr; 3859 urb->pipe_info.ep_num = ep_num; 3860 urb->pipe_info.pipe_type = ep_type; 3861 urb->pipe_info.pipe_dir = ep_dir; 3862 urb->pipe_info.maxp = maxp; 3863 urb->pipe_info.maxp_mult = maxp_mult; 3864 } 3865 3866 /* 3867 * NOTE: This function will be removed once the peripheral controller code 3868 * is integrated and the driver is stable 3869 */ 3870 void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg) 3871 { 3872 #ifdef DEBUG 3873 struct dwc2_host_chan *chan; 3874 struct dwc2_hcd_urb *urb; 3875 struct dwc2_qtd *qtd; 3876 int num_channels; 3877 u32 np_tx_status; 3878 u32 p_tx_status; 3879 int i; 3880 3881 num_channels = hsotg->params.host_channels; 3882 dev_dbg(hsotg->dev, "\n"); 3883 dev_dbg(hsotg->dev, 3884 "************************************************************\n"); 3885 dev_dbg(hsotg->dev, "HCD State:\n"); 3886 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels); 3887 3888 for (i = 0; i < num_channels; i++) { 3889 chan = hsotg->hc_ptr_array[i]; 3890 dev_dbg(hsotg->dev, " Channel %d:\n", i); 3891 dev_dbg(hsotg->dev, 3892 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n", 3893 chan->dev_addr, chan->ep_num, chan->ep_is_in); 3894 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed); 3895 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type); 3896 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet); 3897 dev_dbg(hsotg->dev, " data_pid_start: %d\n", 3898 chan->data_pid_start); 3899 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count); 3900 dev_dbg(hsotg->dev, " xfer_started: %d\n", 3901 chan->xfer_started); 3902 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf); 3903 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n", 3904 (unsigned long)chan->xfer_dma); 3905 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len); 3906 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count); 3907 dev_dbg(hsotg->dev, " halt_on_queue: %d\n", 3908 chan->halt_on_queue); 3909 dev_dbg(hsotg->dev, " halt_pending: %d\n", 3910 chan->halt_pending); 3911 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status); 3912 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split); 3913 dev_dbg(hsotg->dev, " complete_split: %d\n", 3914 chan->complete_split); 3915 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr); 3916 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port); 3917 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos); 3918 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests); 3919 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh); 3920 3921 if (chan->xfer_started) { 3922 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk; 3923 3924 hfnum = dwc2_readl(hsotg, HFNUM); 3925 hcchar = dwc2_readl(hsotg, HCCHAR(i)); 3926 hctsiz = dwc2_readl(hsotg, HCTSIZ(i)); 3927 hcint = dwc2_readl(hsotg, HCINT(i)); 3928 hcintmsk = dwc2_readl(hsotg, HCINTMSK(i)); 3929 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum); 3930 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar); 3931 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz); 3932 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint); 3933 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk); 3934 } 3935 3936 if (!(chan->xfer_started && chan->qh)) 3937 continue; 3938 3939 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) { 3940 if (!qtd->in_process) 3941 break; 3942 urb = qtd->urb; 3943 dev_dbg(hsotg->dev, " URB Info:\n"); 3944 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n", 3945 qtd, urb); 3946 if (urb) { 3947 dev_dbg(hsotg->dev, 3948 " Dev: %d, EP: %d %s\n", 3949 dwc2_hcd_get_dev_addr(&urb->pipe_info), 3950 dwc2_hcd_get_ep_num(&urb->pipe_info), 3951 dwc2_hcd_is_pipe_in(&urb->pipe_info) ? 3952 "IN" : "OUT"); 3953 dev_dbg(hsotg->dev, 3954 " Max packet size: %d (%d mult)\n", 3955 dwc2_hcd_get_maxp(&urb->pipe_info), 3956 dwc2_hcd_get_maxp_mult(&urb->pipe_info)); 3957 dev_dbg(hsotg->dev, 3958 " transfer_buffer: %p\n", 3959 urb->buf); 3960 dev_dbg(hsotg->dev, 3961 " transfer_dma: %08lx\n", 3962 (unsigned long)urb->dma); 3963 dev_dbg(hsotg->dev, 3964 " transfer_buffer_length: %d\n", 3965 urb->length); 3966 dev_dbg(hsotg->dev, " actual_length: %d\n", 3967 urb->actual_length); 3968 } 3969 } 3970 } 3971 3972 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n", 3973 hsotg->non_periodic_channels); 3974 dev_dbg(hsotg->dev, " periodic_channels: %d\n", 3975 hsotg->periodic_channels); 3976 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs); 3977 np_tx_status = dwc2_readl(hsotg, GNPTXSTS); 3978 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n", 3979 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT); 3980 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n", 3981 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT); 3982 p_tx_status = dwc2_readl(hsotg, HPTXSTS); 3983 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n", 3984 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT); 3985 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n", 3986 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT); 3987 dwc2_dump_global_registers(hsotg); 3988 dwc2_dump_host_registers(hsotg); 3989 dev_dbg(hsotg->dev, 3990 "************************************************************\n"); 3991 dev_dbg(hsotg->dev, "\n"); 3992 #endif 3993 } 3994 3995 struct wrapper_priv_data { 3996 struct dwc2_hsotg *hsotg; 3997 }; 3998 3999 /* Gets the dwc2_hsotg from a usb_hcd */ 4000 static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd) 4001 { 4002 struct wrapper_priv_data *p; 4003 4004 p = (struct wrapper_priv_data *)&hcd->hcd_priv; 4005 return p->hsotg; 4006 } 4007 4008 /** 4009 * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context 4010 * 4011 * This will get the dwc2_tt structure (and ttport) associated with the given 4012 * context (which is really just a struct urb pointer). 4013 * 4014 * The first time this is called for a given TT we allocate memory for our 4015 * structure. When everyone is done and has called dwc2_host_put_tt_info() 4016 * then the refcount for the structure will go to 0 and we'll free it. 4017 * 4018 * @hsotg: The HCD state structure for the DWC OTG controller. 4019 * @context: The priv pointer from a struct dwc2_hcd_urb. 4020 * @mem_flags: Flags for allocating memory. 4021 * @ttport: We'll return this device's port number here. That's used to 4022 * reference into the bitmap if we're on a multi_tt hub. 4023 * 4024 * Return: a pointer to a struct dwc2_tt. Don't forget to call 4025 * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure. 4026 */ 4027 4028 struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context, 4029 gfp_t mem_flags, int *ttport) 4030 { 4031 struct urb *urb = context; 4032 struct dwc2_tt *dwc_tt = NULL; 4033 4034 if (urb->dev->tt) { 4035 *ttport = urb->dev->ttport; 4036 4037 dwc_tt = urb->dev->tt->hcpriv; 4038 if (!dwc_tt) { 4039 size_t bitmap_size; 4040 4041 /* 4042 * For single_tt we need one schedule. For multi_tt 4043 * we need one per port. 4044 */ 4045 bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP * 4046 sizeof(dwc_tt->periodic_bitmaps[0]); 4047 if (urb->dev->tt->multi) 4048 bitmap_size *= urb->dev->tt->hub->maxchild; 4049 4050 dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size, 4051 mem_flags); 4052 if (!dwc_tt) 4053 return NULL; 4054 4055 dwc_tt->usb_tt = urb->dev->tt; 4056 dwc_tt->usb_tt->hcpriv = dwc_tt; 4057 } 4058 4059 dwc_tt->refcount++; 4060 } 4061 4062 return dwc_tt; 4063 } 4064 4065 /** 4066 * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info() 4067 * 4068 * Frees resources allocated by dwc2_host_get_tt_info() if all current holders 4069 * of the structure are done. 4070 * 4071 * It's OK to call this with NULL. 4072 * 4073 * @hsotg: The HCD state structure for the DWC OTG controller. 4074 * @dwc_tt: The pointer returned by dwc2_host_get_tt_info. 4075 */ 4076 void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt) 4077 { 4078 /* Model kfree and make put of NULL a no-op */ 4079 if (!dwc_tt) 4080 return; 4081 4082 WARN_ON(dwc_tt->refcount < 1); 4083 4084 dwc_tt->refcount--; 4085 if (!dwc_tt->refcount) { 4086 dwc_tt->usb_tt->hcpriv = NULL; 4087 kfree(dwc_tt); 4088 } 4089 } 4090 4091 int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context) 4092 { 4093 struct urb *urb = context; 4094 4095 return urb->dev->speed; 4096 } 4097 4098 static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw, 4099 struct urb *urb) 4100 { 4101 struct usb_bus *bus = hcd_to_bus(hcd); 4102 4103 if (urb->interval) 4104 bus->bandwidth_allocated += bw / urb->interval; 4105 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) 4106 bus->bandwidth_isoc_reqs++; 4107 else 4108 bus->bandwidth_int_reqs++; 4109 } 4110 4111 static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw, 4112 struct urb *urb) 4113 { 4114 struct usb_bus *bus = hcd_to_bus(hcd); 4115 4116 if (urb->interval) 4117 bus->bandwidth_allocated -= bw / urb->interval; 4118 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) 4119 bus->bandwidth_isoc_reqs--; 4120 else 4121 bus->bandwidth_int_reqs--; 4122 } 4123 4124 /* 4125 * Sets the final status of an URB and returns it to the upper layer. Any 4126 * required cleanup of the URB is performed. 4127 * 4128 * Must be called with interrupt disabled and spinlock held 4129 */ 4130 void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd, 4131 int status) 4132 { 4133 struct urb *urb; 4134 int i; 4135 4136 if (!qtd) { 4137 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__); 4138 return; 4139 } 4140 4141 if (!qtd->urb) { 4142 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__); 4143 return; 4144 } 4145 4146 urb = qtd->urb->priv; 4147 if (!urb) { 4148 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__); 4149 return; 4150 } 4151 4152 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb); 4153 4154 if (dbg_urb(urb)) 4155 dev_vdbg(hsotg->dev, 4156 "%s: urb %p device %d ep %d-%s status %d actual %d\n", 4157 __func__, urb, usb_pipedevice(urb->pipe), 4158 usb_pipeendpoint(urb->pipe), 4159 usb_pipein(urb->pipe) ? "IN" : "OUT", status, 4160 urb->actual_length); 4161 4162 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { 4163 if (!hsotg->params.dma_desc_enable) 4164 urb->start_frame = qtd->qh->start_active_frame; 4165 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb); 4166 for (i = 0; i < urb->number_of_packets; ++i) { 4167 urb->iso_frame_desc[i].actual_length = 4168 dwc2_hcd_urb_get_iso_desc_actual_length( 4169 qtd->urb, i); 4170 urb->iso_frame_desc[i].status = 4171 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i); 4172 } 4173 } 4174 4175 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) { 4176 for (i = 0; i < urb->number_of_packets; i++) 4177 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n", 4178 i, urb->iso_frame_desc[i].status); 4179 } 4180 4181 urb->status = status; 4182 if (!status) { 4183 if ((urb->transfer_flags & URB_SHORT_NOT_OK) && 4184 urb->actual_length < urb->transfer_buffer_length) 4185 urb->status = -EREMOTEIO; 4186 } 4187 4188 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS || 4189 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) { 4190 struct usb_host_endpoint *ep = urb->ep; 4191 4192 if (ep) 4193 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg), 4194 dwc2_hcd_get_ep_bandwidth(hsotg, ep), 4195 urb); 4196 } 4197 4198 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb); 4199 urb->hcpriv = NULL; 4200 kfree(qtd->urb); 4201 qtd->urb = NULL; 4202 4203 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status); 4204 } 4205 4206 /* 4207 * Work queue function for starting the HCD when A-Cable is connected 4208 */ 4209 static void dwc2_hcd_start_func(struct work_struct *work) 4210 { 4211 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg, 4212 start_work.work); 4213 4214 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg); 4215 dwc2_host_start(hsotg); 4216 } 4217 4218 /* 4219 * Reset work queue function 4220 */ 4221 static void dwc2_hcd_reset_func(struct work_struct *work) 4222 { 4223 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg, 4224 reset_work.work); 4225 unsigned long flags; 4226 u32 hprt0; 4227 4228 dev_dbg(hsotg->dev, "USB RESET function called\n"); 4229 4230 spin_lock_irqsave(&hsotg->lock, flags); 4231 4232 hprt0 = dwc2_read_hprt0(hsotg); 4233 hprt0 &= ~HPRT0_RST; 4234 dwc2_writel(hsotg, hprt0, HPRT0); 4235 hsotg->flags.b.port_reset_change = 1; 4236 4237 spin_unlock_irqrestore(&hsotg->lock, flags); 4238 } 4239 4240 static void dwc2_hcd_phy_reset_func(struct work_struct *work) 4241 { 4242 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg, 4243 phy_reset_work); 4244 int ret; 4245 4246 ret = phy_reset(hsotg->phy); 4247 if (ret) 4248 dev_warn(hsotg->dev, "PHY reset failed\n"); 4249 } 4250 4251 /* 4252 * ========================================================================= 4253 * Linux HC Driver Functions 4254 * ========================================================================= 4255 */ 4256 4257 /* 4258 * Initializes the DWC_otg controller and its root hub and prepares it for host 4259 * mode operation. Activates the root port. Returns 0 on success and a negative 4260 * error code on failure. 4261 */ 4262 static int _dwc2_hcd_start(struct usb_hcd *hcd) 4263 { 4264 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4265 struct usb_bus *bus = hcd_to_bus(hcd); 4266 unsigned long flags; 4267 u32 hprt0; 4268 int ret; 4269 4270 dev_dbg(hsotg->dev, "DWC OTG HCD START\n"); 4271 4272 spin_lock_irqsave(&hsotg->lock, flags); 4273 hsotg->lx_state = DWC2_L0; 4274 hcd->state = HC_STATE_RUNNING; 4275 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4276 4277 if (dwc2_is_device_mode(hsotg)) { 4278 spin_unlock_irqrestore(&hsotg->lock, flags); 4279 return 0; /* why 0 ?? */ 4280 } 4281 4282 dwc2_hcd_reinit(hsotg); 4283 4284 hprt0 = dwc2_read_hprt0(hsotg); 4285 /* Has vbus power been turned on in dwc2_core_host_init ? */ 4286 if (hprt0 & HPRT0_PWR) { 4287 /* Enable external vbus supply before resuming root hub */ 4288 spin_unlock_irqrestore(&hsotg->lock, flags); 4289 ret = dwc2_vbus_supply_init(hsotg); 4290 if (ret) 4291 return ret; 4292 spin_lock_irqsave(&hsotg->lock, flags); 4293 } 4294 4295 /* Initialize and connect root hub if one is not already attached */ 4296 if (bus->root_hub) { 4297 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n"); 4298 /* Inform the HUB driver to resume */ 4299 usb_hcd_resume_root_hub(hcd); 4300 } 4301 4302 spin_unlock_irqrestore(&hsotg->lock, flags); 4303 4304 return 0; 4305 } 4306 4307 /* 4308 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are 4309 * stopped. 4310 */ 4311 static void _dwc2_hcd_stop(struct usb_hcd *hcd) 4312 { 4313 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4314 unsigned long flags; 4315 u32 hprt0; 4316 4317 /* Turn off all host-specific interrupts */ 4318 dwc2_disable_host_interrupts(hsotg); 4319 4320 /* Wait for interrupt processing to finish */ 4321 synchronize_irq(hcd->irq); 4322 4323 spin_lock_irqsave(&hsotg->lock, flags); 4324 hprt0 = dwc2_read_hprt0(hsotg); 4325 /* Ensure hcd is disconnected */ 4326 dwc2_hcd_disconnect(hsotg, true); 4327 dwc2_hcd_stop(hsotg); 4328 hsotg->lx_state = DWC2_L3; 4329 hcd->state = HC_STATE_HALT; 4330 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4331 spin_unlock_irqrestore(&hsotg->lock, flags); 4332 4333 /* keep balanced supply init/exit by checking HPRT0_PWR */ 4334 if (hprt0 & HPRT0_PWR) 4335 dwc2_vbus_supply_exit(hsotg); 4336 4337 usleep_range(1000, 3000); 4338 } 4339 4340 static int _dwc2_hcd_suspend(struct usb_hcd *hcd) 4341 { 4342 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4343 unsigned long flags; 4344 int ret = 0; 4345 4346 spin_lock_irqsave(&hsotg->lock, flags); 4347 4348 if (dwc2_is_device_mode(hsotg)) 4349 goto unlock; 4350 4351 if (hsotg->lx_state != DWC2_L0) 4352 goto unlock; 4353 4354 if (!HCD_HW_ACCESSIBLE(hcd)) 4355 goto unlock; 4356 4357 if (hsotg->op_state == OTG_STATE_B_PERIPHERAL) 4358 goto unlock; 4359 4360 if (hsotg->bus_suspended) 4361 goto skip_power_saving; 4362 4363 if (!(dwc2_read_hprt0(hsotg) & HPRT0_CONNSTS)) 4364 goto skip_power_saving; 4365 4366 switch (hsotg->params.power_down) { 4367 case DWC2_POWER_DOWN_PARAM_PARTIAL: 4368 /* Enter partial_power_down */ 4369 ret = dwc2_enter_partial_power_down(hsotg); 4370 if (ret) 4371 dev_err(hsotg->dev, 4372 "enter partial_power_down failed\n"); 4373 /* After entering suspend, hardware is not accessible */ 4374 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4375 break; 4376 case DWC2_POWER_DOWN_PARAM_HIBERNATION: 4377 /* Enter hibernation */ 4378 spin_unlock_irqrestore(&hsotg->lock, flags); 4379 ret = dwc2_enter_hibernation(hsotg, 1); 4380 if (ret) 4381 dev_err(hsotg->dev, "enter hibernation failed\n"); 4382 spin_lock_irqsave(&hsotg->lock, flags); 4383 4384 /* After entering suspend, hardware is not accessible */ 4385 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4386 break; 4387 case DWC2_POWER_DOWN_PARAM_NONE: 4388 /* 4389 * If not hibernation nor partial power down are supported, 4390 * clock gating is used to save power. 4391 */ 4392 if (!hsotg->params.no_clock_gating) { 4393 dwc2_host_enter_clock_gating(hsotg); 4394 4395 /* After entering suspend, hardware is not accessible */ 4396 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4397 } 4398 break; 4399 default: 4400 goto skip_power_saving; 4401 } 4402 4403 spin_unlock_irqrestore(&hsotg->lock, flags); 4404 dwc2_vbus_supply_exit(hsotg); 4405 spin_lock_irqsave(&hsotg->lock, flags); 4406 4407 /* Ask phy to be suspended */ 4408 if (!IS_ERR_OR_NULL(hsotg->uphy)) { 4409 spin_unlock_irqrestore(&hsotg->lock, flags); 4410 usb_phy_set_suspend(hsotg->uphy, true); 4411 spin_lock_irqsave(&hsotg->lock, flags); 4412 } 4413 4414 skip_power_saving: 4415 hsotg->lx_state = DWC2_L2; 4416 unlock: 4417 spin_unlock_irqrestore(&hsotg->lock, flags); 4418 4419 return ret; 4420 } 4421 4422 static int _dwc2_hcd_resume(struct usb_hcd *hcd) 4423 { 4424 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4425 unsigned long flags; 4426 u32 hprt0; 4427 int ret = 0; 4428 4429 spin_lock_irqsave(&hsotg->lock, flags); 4430 4431 if (dwc2_is_device_mode(hsotg)) 4432 goto unlock; 4433 4434 if (hsotg->lx_state != DWC2_L2) 4435 goto unlock; 4436 4437 hprt0 = dwc2_read_hprt0(hsotg); 4438 4439 /* 4440 * Added port connection status checking which prevents exiting from 4441 * Partial Power Down mode from _dwc2_hcd_resume() if not in Partial 4442 * Power Down mode. 4443 */ 4444 if (hprt0 & HPRT0_CONNSTS) { 4445 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4446 hsotg->lx_state = DWC2_L0; 4447 goto unlock; 4448 } 4449 4450 switch (hsotg->params.power_down) { 4451 case DWC2_POWER_DOWN_PARAM_PARTIAL: 4452 ret = dwc2_exit_partial_power_down(hsotg, 0, true); 4453 if (ret) 4454 dev_err(hsotg->dev, 4455 "exit partial_power_down failed\n"); 4456 /* 4457 * Set HW accessible bit before powering on the controller 4458 * since an interrupt may rise. 4459 */ 4460 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4461 break; 4462 case DWC2_POWER_DOWN_PARAM_HIBERNATION: 4463 ret = dwc2_exit_hibernation(hsotg, 0, 0, 1); 4464 if (ret) 4465 dev_err(hsotg->dev, "exit hibernation failed.\n"); 4466 4467 /* 4468 * Set HW accessible bit before powering on the controller 4469 * since an interrupt may rise. 4470 */ 4471 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4472 break; 4473 case DWC2_POWER_DOWN_PARAM_NONE: 4474 /* 4475 * If not hibernation nor partial power down are supported, 4476 * port resume is done using the clock gating programming flow. 4477 */ 4478 spin_unlock_irqrestore(&hsotg->lock, flags); 4479 dwc2_host_exit_clock_gating(hsotg, 0); 4480 4481 /* 4482 * Initialize the Core for Host mode, as after system resume 4483 * the global interrupts are disabled. 4484 */ 4485 dwc2_core_init(hsotg, false); 4486 dwc2_enable_global_interrupts(hsotg); 4487 dwc2_hcd_reinit(hsotg); 4488 spin_lock_irqsave(&hsotg->lock, flags); 4489 4490 /* 4491 * Set HW accessible bit before powering on the controller 4492 * since an interrupt may rise. 4493 */ 4494 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 4495 break; 4496 default: 4497 hsotg->lx_state = DWC2_L0; 4498 goto unlock; 4499 } 4500 4501 /* Change Root port status, as port status change occurred after resume.*/ 4502 hsotg->flags.b.port_suspend_change = 1; 4503 4504 /* 4505 * Enable power if not already done. 4506 * This must not be spinlocked since duration 4507 * of this call is unknown. 4508 */ 4509 if (!IS_ERR_OR_NULL(hsotg->uphy)) { 4510 spin_unlock_irqrestore(&hsotg->lock, flags); 4511 usb_phy_set_suspend(hsotg->uphy, false); 4512 spin_lock_irqsave(&hsotg->lock, flags); 4513 } 4514 4515 /* Enable external vbus supply after resuming the port. */ 4516 spin_unlock_irqrestore(&hsotg->lock, flags); 4517 dwc2_vbus_supply_init(hsotg); 4518 4519 /* Wait for controller to correctly update D+/D- level */ 4520 usleep_range(3000, 5000); 4521 spin_lock_irqsave(&hsotg->lock, flags); 4522 4523 /* 4524 * Clear Port Enable and Port Status changes. 4525 * Enable Port Power. 4526 */ 4527 dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET | 4528 HPRT0_ENACHG, HPRT0); 4529 4530 /* Wait for controller to detect Port Connect */ 4531 spin_unlock_irqrestore(&hsotg->lock, flags); 4532 usleep_range(5000, 7000); 4533 spin_lock_irqsave(&hsotg->lock, flags); 4534 unlock: 4535 spin_unlock_irqrestore(&hsotg->lock, flags); 4536 4537 return ret; 4538 } 4539 4540 /* Returns the current frame number */ 4541 static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd) 4542 { 4543 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4544 4545 return dwc2_hcd_get_frame_number(hsotg); 4546 } 4547 4548 static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb, 4549 char *fn_name) 4550 { 4551 #ifdef VERBOSE_DEBUG 4552 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4553 char *pipetype = NULL; 4554 char *speed = NULL; 4555 4556 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb); 4557 dev_vdbg(hsotg->dev, " Device address: %d\n", 4558 usb_pipedevice(urb->pipe)); 4559 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n", 4560 usb_pipeendpoint(urb->pipe), 4561 usb_pipein(urb->pipe) ? "IN" : "OUT"); 4562 4563 switch (usb_pipetype(urb->pipe)) { 4564 case PIPE_CONTROL: 4565 pipetype = "CONTROL"; 4566 break; 4567 case PIPE_BULK: 4568 pipetype = "BULK"; 4569 break; 4570 case PIPE_INTERRUPT: 4571 pipetype = "INTERRUPT"; 4572 break; 4573 case PIPE_ISOCHRONOUS: 4574 pipetype = "ISOCHRONOUS"; 4575 break; 4576 } 4577 4578 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype, 4579 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ? 4580 "IN" : "OUT"); 4581 4582 switch (urb->dev->speed) { 4583 case USB_SPEED_HIGH: 4584 speed = "HIGH"; 4585 break; 4586 case USB_SPEED_FULL: 4587 speed = "FULL"; 4588 break; 4589 case USB_SPEED_LOW: 4590 speed = "LOW"; 4591 break; 4592 default: 4593 speed = "UNKNOWN"; 4594 break; 4595 } 4596 4597 dev_vdbg(hsotg->dev, " Speed: %s\n", speed); 4598 dev_vdbg(hsotg->dev, " Max packet size: %d (%d mult)\n", 4599 usb_endpoint_maxp(&urb->ep->desc), 4600 usb_endpoint_maxp_mult(&urb->ep->desc)); 4601 4602 dev_vdbg(hsotg->dev, " Data buffer length: %d\n", 4603 urb->transfer_buffer_length); 4604 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n", 4605 urb->transfer_buffer, (unsigned long)urb->transfer_dma); 4606 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n", 4607 urb->setup_packet, (unsigned long)urb->setup_dma); 4608 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval); 4609 4610 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { 4611 int i; 4612 4613 for (i = 0; i < urb->number_of_packets; i++) { 4614 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i); 4615 dev_vdbg(hsotg->dev, " offset: %d, length %d\n", 4616 urb->iso_frame_desc[i].offset, 4617 urb->iso_frame_desc[i].length); 4618 } 4619 } 4620 #endif 4621 } 4622 4623 /* 4624 * Starts processing a USB transfer request specified by a USB Request Block 4625 * (URB). mem_flags indicates the type of memory allocation to use while 4626 * processing this URB. 4627 */ 4628 static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, 4629 gfp_t mem_flags) 4630 { 4631 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4632 struct usb_host_endpoint *ep = urb->ep; 4633 struct dwc2_hcd_urb *dwc2_urb; 4634 int i; 4635 int retval; 4636 int alloc_bandwidth = 0; 4637 u8 ep_type = 0; 4638 u32 tflags = 0; 4639 void *buf; 4640 unsigned long flags; 4641 struct dwc2_qh *qh; 4642 bool qh_allocated = false; 4643 struct dwc2_qtd *qtd; 4644 struct dwc2_gregs_backup *gr; 4645 4646 gr = &hsotg->gr_backup; 4647 4648 if (dbg_urb(urb)) { 4649 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n"); 4650 dwc2_dump_urb_info(hcd, urb, "urb_enqueue"); 4651 } 4652 4653 if (hsotg->hibernated) { 4654 if (gr->gotgctl & GOTGCTL_CURMODE_HOST) 4655 retval = dwc2_exit_hibernation(hsotg, 0, 0, 1); 4656 else 4657 retval = dwc2_exit_hibernation(hsotg, 0, 0, 0); 4658 4659 if (retval) 4660 dev_err(hsotg->dev, 4661 "exit hibernation failed.\n"); 4662 } 4663 4664 if (hsotg->in_ppd) { 4665 retval = dwc2_exit_partial_power_down(hsotg, 0, true); 4666 if (retval) 4667 dev_err(hsotg->dev, 4668 "exit partial_power_down failed\n"); 4669 } 4670 4671 if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE && 4672 hsotg->bus_suspended && !hsotg->params.no_clock_gating) { 4673 if (dwc2_is_device_mode(hsotg)) 4674 dwc2_gadget_exit_clock_gating(hsotg, 0); 4675 else 4676 dwc2_host_exit_clock_gating(hsotg, 0); 4677 } 4678 4679 if (!ep) 4680 return -EINVAL; 4681 4682 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS || 4683 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) { 4684 spin_lock_irqsave(&hsotg->lock, flags); 4685 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep)) 4686 alloc_bandwidth = 1; 4687 spin_unlock_irqrestore(&hsotg->lock, flags); 4688 } 4689 4690 switch (usb_pipetype(urb->pipe)) { 4691 case PIPE_CONTROL: 4692 ep_type = USB_ENDPOINT_XFER_CONTROL; 4693 break; 4694 case PIPE_ISOCHRONOUS: 4695 ep_type = USB_ENDPOINT_XFER_ISOC; 4696 break; 4697 case PIPE_BULK: 4698 ep_type = USB_ENDPOINT_XFER_BULK; 4699 break; 4700 case PIPE_INTERRUPT: 4701 ep_type = USB_ENDPOINT_XFER_INT; 4702 break; 4703 } 4704 4705 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets, 4706 mem_flags); 4707 if (!dwc2_urb) 4708 return -ENOMEM; 4709 4710 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe), 4711 usb_pipeendpoint(urb->pipe), ep_type, 4712 usb_pipein(urb->pipe), 4713 usb_endpoint_maxp(&ep->desc), 4714 usb_endpoint_maxp_mult(&ep->desc)); 4715 4716 buf = urb->transfer_buffer; 4717 4718 if (hcd_uses_dma(hcd)) { 4719 if (!buf && (urb->transfer_dma & 3)) { 4720 dev_err(hsotg->dev, 4721 "%s: unaligned transfer with no transfer_buffer", 4722 __func__); 4723 retval = -EINVAL; 4724 goto fail0; 4725 } 4726 } 4727 4728 if (!(urb->transfer_flags & URB_NO_INTERRUPT)) 4729 tflags |= URB_GIVEBACK_ASAP; 4730 if (urb->transfer_flags & URB_ZERO_PACKET) 4731 tflags |= URB_SEND_ZERO_PACKET; 4732 4733 dwc2_urb->priv = urb; 4734 dwc2_urb->buf = buf; 4735 dwc2_urb->dma = urb->transfer_dma; 4736 dwc2_urb->length = urb->transfer_buffer_length; 4737 dwc2_urb->setup_packet = urb->setup_packet; 4738 dwc2_urb->setup_dma = urb->setup_dma; 4739 dwc2_urb->flags = tflags; 4740 dwc2_urb->interval = urb->interval; 4741 dwc2_urb->status = -EINPROGRESS; 4742 4743 for (i = 0; i < urb->number_of_packets; ++i) 4744 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i, 4745 urb->iso_frame_desc[i].offset, 4746 urb->iso_frame_desc[i].length); 4747 4748 urb->hcpriv = dwc2_urb; 4749 qh = (struct dwc2_qh *)ep->hcpriv; 4750 /* Create QH for the endpoint if it doesn't exist */ 4751 if (!qh) { 4752 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags); 4753 if (!qh) { 4754 retval = -ENOMEM; 4755 goto fail0; 4756 } 4757 ep->hcpriv = qh; 4758 qh_allocated = true; 4759 } 4760 4761 qtd = kzalloc_obj(*qtd, mem_flags); 4762 if (!qtd) { 4763 retval = -ENOMEM; 4764 goto fail1; 4765 } 4766 4767 spin_lock_irqsave(&hsotg->lock, flags); 4768 retval = usb_hcd_link_urb_to_ep(hcd, urb); 4769 if (retval) 4770 goto fail2; 4771 4772 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd); 4773 if (retval) 4774 goto fail3; 4775 4776 if (alloc_bandwidth) { 4777 dwc2_allocate_bus_bandwidth(hcd, 4778 dwc2_hcd_get_ep_bandwidth(hsotg, ep), 4779 urb); 4780 } 4781 4782 spin_unlock_irqrestore(&hsotg->lock, flags); 4783 4784 return 0; 4785 4786 fail3: 4787 dwc2_urb->priv = NULL; 4788 usb_hcd_unlink_urb_from_ep(hcd, urb); 4789 if (qh_allocated && qh->channel && qh->channel->qh == qh) 4790 qh->channel->qh = NULL; 4791 fail2: 4792 urb->hcpriv = NULL; 4793 spin_unlock_irqrestore(&hsotg->lock, flags); 4794 kfree(qtd); 4795 fail1: 4796 if (qh_allocated) { 4797 struct dwc2_qtd *qtd2, *qtd2_tmp; 4798 4799 ep->hcpriv = NULL; 4800 dwc2_hcd_qh_unlink(hsotg, qh); 4801 /* Free each QTD in the QH's QTD list */ 4802 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list, 4803 qtd_list_entry) 4804 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh); 4805 dwc2_hcd_qh_free(hsotg, qh); 4806 } 4807 fail0: 4808 kfree(dwc2_urb); 4809 4810 return retval; 4811 } 4812 4813 /* 4814 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success. 4815 */ 4816 static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, 4817 int status) 4818 { 4819 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4820 int rc; 4821 unsigned long flags; 4822 int urb_status; 4823 4824 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n"); 4825 dwc2_dump_urb_info(hcd, urb, "urb_dequeue"); 4826 4827 spin_lock_irqsave(&hsotg->lock, flags); 4828 4829 rc = usb_hcd_check_unlink_urb(hcd, urb, status); 4830 if (rc) 4831 goto out; 4832 4833 if (!urb->hcpriv) { 4834 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n"); 4835 goto out; 4836 } 4837 4838 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv); 4839 4840 usb_hcd_unlink_urb_from_ep(hcd, urb); 4841 4842 kfree(urb->hcpriv); 4843 urb->hcpriv = NULL; 4844 4845 /* Higher layer software sets URB status */ 4846 spin_unlock(&hsotg->lock); 4847 urb_status = urb->status; 4848 usb_hcd_giveback_urb(hcd, urb, status); 4849 spin_lock(&hsotg->lock); 4850 4851 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n"); 4852 dev_dbg(hsotg->dev, " urb->status = %d\n", urb_status); 4853 out: 4854 spin_unlock_irqrestore(&hsotg->lock, flags); 4855 4856 return rc; 4857 } 4858 4859 /* 4860 * Frees resources in the DWC_otg controller related to a given endpoint. Also 4861 * clears state in the HCD related to the endpoint. Any URBs for the endpoint 4862 * must already be dequeued. 4863 */ 4864 static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd, 4865 struct usb_host_endpoint *ep) 4866 { 4867 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4868 4869 dev_dbg(hsotg->dev, 4870 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n", 4871 ep->desc.bEndpointAddress, ep->hcpriv); 4872 dwc2_hcd_endpoint_disable(hsotg, ep, 250); 4873 } 4874 4875 /* 4876 * Resets endpoint specific parameter values, in current version used to reset 4877 * the data toggle (as a WA). This function can be called from usb_clear_halt 4878 * routine. 4879 */ 4880 static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd, 4881 struct usb_host_endpoint *ep) 4882 { 4883 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4884 unsigned long flags; 4885 4886 dev_dbg(hsotg->dev, 4887 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n", 4888 ep->desc.bEndpointAddress); 4889 4890 spin_lock_irqsave(&hsotg->lock, flags); 4891 dwc2_hcd_endpoint_reset(hsotg, ep); 4892 spin_unlock_irqrestore(&hsotg->lock, flags); 4893 } 4894 4895 /* 4896 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if 4897 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid 4898 * interrupt. 4899 * 4900 * This function is called by the USB core when an interrupt occurs 4901 */ 4902 static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd) 4903 { 4904 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4905 4906 return dwc2_handle_hcd_intr(hsotg); 4907 } 4908 4909 /* 4910 * Creates Status Change bitmap for the root hub and root port. The bitmap is 4911 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1 4912 * is the status change indicator for the single root port. Returns 1 if either 4913 * change indicator is 1, otherwise returns 0. 4914 */ 4915 static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf) 4916 { 4917 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4918 4919 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1; 4920 return buf[0] != 0; 4921 } 4922 4923 /* Handles hub class-specific requests */ 4924 static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue, 4925 u16 windex, char *buf, u16 wlength) 4926 { 4927 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq, 4928 wvalue, windex, buf, wlength); 4929 return retval; 4930 } 4931 4932 /* Handles hub TT buffer clear completions */ 4933 static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd, 4934 struct usb_host_endpoint *ep) 4935 { 4936 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4937 struct dwc2_qh *qh; 4938 unsigned long flags; 4939 4940 qh = ep->hcpriv; 4941 if (!qh) 4942 return; 4943 4944 spin_lock_irqsave(&hsotg->lock, flags); 4945 qh->tt_buffer_dirty = 0; 4946 4947 if (hsotg->flags.b.port_connect_status) 4948 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL); 4949 4950 spin_unlock_irqrestore(&hsotg->lock, flags); 4951 } 4952 4953 /* 4954 * HPRT0_SPD_HIGH_SPEED: high speed 4955 * HPRT0_SPD_FULL_SPEED: full speed 4956 */ 4957 static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed) 4958 { 4959 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4960 4961 if (hsotg->params.speed == speed) 4962 return; 4963 4964 hsotg->params.speed = speed; 4965 queue_work(hsotg->wq_otg, &hsotg->wf_otg); 4966 } 4967 4968 static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev) 4969 { 4970 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4971 4972 if (!hsotg->params.change_speed_quirk) 4973 return; 4974 4975 /* 4976 * On removal, set speed to default high-speed. 4977 */ 4978 if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN && 4979 udev->parent->speed < USB_SPEED_HIGH) { 4980 dev_info(hsotg->dev, "Set speed to default high-speed\n"); 4981 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED); 4982 } 4983 } 4984 4985 static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev) 4986 { 4987 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd); 4988 4989 if (!hsotg->params.change_speed_quirk) 4990 return 0; 4991 4992 if (udev->speed == USB_SPEED_HIGH) { 4993 dev_info(hsotg->dev, "Set speed to high-speed\n"); 4994 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED); 4995 } else if ((udev->speed == USB_SPEED_FULL || 4996 udev->speed == USB_SPEED_LOW)) { 4997 /* 4998 * Change speed setting to full-speed if there's 4999 * a full-speed or low-speed device plugged in. 5000 */ 5001 dev_info(hsotg->dev, "Set speed to full-speed\n"); 5002 dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED); 5003 } 5004 5005 return 0; 5006 } 5007 5008 static struct hc_driver dwc2_hc_driver = { 5009 .description = "dwc2_hsotg", 5010 .product_desc = "DWC OTG Controller", 5011 .hcd_priv_size = sizeof(struct wrapper_priv_data), 5012 5013 .irq = _dwc2_hcd_irq, 5014 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH, 5015 5016 .start = _dwc2_hcd_start, 5017 .stop = _dwc2_hcd_stop, 5018 .urb_enqueue = _dwc2_hcd_urb_enqueue, 5019 .urb_dequeue = _dwc2_hcd_urb_dequeue, 5020 .endpoint_disable = _dwc2_hcd_endpoint_disable, 5021 .endpoint_reset = _dwc2_hcd_endpoint_reset, 5022 .get_frame_number = _dwc2_hcd_get_frame_number, 5023 5024 .hub_status_data = _dwc2_hcd_hub_status_data, 5025 .hub_control = _dwc2_hcd_hub_control, 5026 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete, 5027 5028 .bus_suspend = _dwc2_hcd_suspend, 5029 .bus_resume = _dwc2_hcd_resume, 5030 5031 .map_urb_for_dma = dwc2_map_urb_for_dma, 5032 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma, 5033 }; 5034 5035 /* 5036 * Frees secondary storage associated with the dwc2_hsotg structure contained 5037 * in the struct usb_hcd field 5038 */ 5039 static void dwc2_hcd_free(struct dwc2_hsotg *hsotg) 5040 { 5041 u32 ahbcfg; 5042 u32 dctl; 5043 int i; 5044 5045 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n"); 5046 5047 /* Free memory for QH/QTD lists */ 5048 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive); 5049 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting); 5050 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active); 5051 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive); 5052 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready); 5053 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned); 5054 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued); 5055 5056 /* Free memory for the host channels */ 5057 for (i = 0; i < MAX_EPS_CHANNELS; i++) { 5058 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i]; 5059 5060 if (chan) { 5061 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n", 5062 i, chan); 5063 hsotg->hc_ptr_array[i] = NULL; 5064 kfree(chan); 5065 } 5066 } 5067 5068 if (hsotg->params.host_dma) { 5069 if (hsotg->status_buf) { 5070 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE, 5071 hsotg->status_buf, 5072 hsotg->status_buf_dma); 5073 hsotg->status_buf = NULL; 5074 } 5075 } else { 5076 kfree(hsotg->status_buf); 5077 hsotg->status_buf = NULL; 5078 } 5079 5080 ahbcfg = dwc2_readl(hsotg, GAHBCFG); 5081 5082 /* Disable all interrupts */ 5083 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN; 5084 dwc2_writel(hsotg, ahbcfg, GAHBCFG); 5085 dwc2_writel(hsotg, 0, GINTMSK); 5086 5087 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) { 5088 dctl = dwc2_readl(hsotg, DCTL); 5089 dctl |= DCTL_SFTDISCON; 5090 dwc2_writel(hsotg, dctl, DCTL); 5091 } 5092 5093 if (hsotg->wq_otg) { 5094 if (!cancel_work_sync(&hsotg->wf_otg)) 5095 flush_workqueue(hsotg->wq_otg); 5096 destroy_workqueue(hsotg->wq_otg); 5097 } 5098 5099 cancel_work_sync(&hsotg->phy_reset_work); 5100 } 5101 5102 static void dwc2_hcd_release(struct dwc2_hsotg *hsotg) 5103 { 5104 /* Turn off all host-specific interrupts */ 5105 dwc2_disable_host_interrupts(hsotg); 5106 timer_shutdown_sync(&hsotg->wkp_timer); 5107 5108 dwc2_hcd_free(hsotg); 5109 } 5110 5111 /* 5112 * Initializes the HCD. This function allocates memory for and initializes the 5113 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the 5114 * USB bus with the core and calls the hc_driver->start() function. It returns 5115 * a negative error on failure. 5116 */ 5117 int dwc2_hcd_init(struct dwc2_hsotg *hsotg) 5118 { 5119 struct platform_device *pdev = to_platform_device(hsotg->dev); 5120 struct resource *res; 5121 struct usb_hcd *hcd; 5122 struct dwc2_host_chan *channel; 5123 u32 hcfg; 5124 int i, num_channels; 5125 int retval; 5126 5127 if (usb_disabled()) 5128 return -ENODEV; 5129 5130 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n"); 5131 5132 retval = -ENOMEM; 5133 5134 hcfg = dwc2_readl(hsotg, HCFG); 5135 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg); 5136 5137 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS 5138 hsotg->frame_num_array = kzalloc_objs(*hsotg->frame_num_array, 5139 FRAME_NUM_ARRAY_SIZE); 5140 if (!hsotg->frame_num_array) 5141 goto error1; 5142 hsotg->last_frame_num_array = 5143 kzalloc_objs(*hsotg->last_frame_num_array, FRAME_NUM_ARRAY_SIZE); 5144 if (!hsotg->last_frame_num_array) 5145 goto error1; 5146 #endif 5147 hsotg->last_frame_num = HFNUM_MAX_FRNUM; 5148 5149 /* Check if the bus driver or platform code has setup a dma_mask */ 5150 if (hsotg->params.host_dma && 5151 !hsotg->dev->dma_mask) { 5152 dev_warn(hsotg->dev, 5153 "dma_mask not set, disabling DMA\n"); 5154 hsotg->params.host_dma = false; 5155 hsotg->params.dma_desc_enable = false; 5156 } 5157 5158 /* Set device flags indicating whether the HCD supports DMA */ 5159 if (hsotg->params.host_dma) { 5160 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0) 5161 dev_warn(hsotg->dev, "can't set DMA mask\n"); 5162 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0) 5163 dev_warn(hsotg->dev, "can't set coherent DMA mask\n"); 5164 } 5165 5166 if (hsotg->params.change_speed_quirk) { 5167 dwc2_hc_driver.free_dev = dwc2_free_dev; 5168 dwc2_hc_driver.reset_device = dwc2_reset_device; 5169 } 5170 5171 if (hsotg->params.host_dma) 5172 dwc2_hc_driver.flags |= HCD_DMA; 5173 5174 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev)); 5175 if (!hcd) 5176 goto error1; 5177 5178 hcd->has_tt = 1; 5179 5180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 5181 if (!res) { 5182 retval = -EINVAL; 5183 goto error2; 5184 } 5185 hcd->rsrc_start = res->start; 5186 hcd->rsrc_len = resource_size(res); 5187 5188 ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg; 5189 hsotg->priv = hcd; 5190 5191 /* 5192 * Disable the global interrupt until all the interrupt handlers are 5193 * installed 5194 */ 5195 dwc2_disable_global_interrupts(hsotg); 5196 5197 /* Initialize the DWC_otg core, and select the Phy type */ 5198 retval = dwc2_core_init(hsotg, true); 5199 if (retval) 5200 goto error2; 5201 5202 /* Create new workqueue and init work */ 5203 retval = -ENOMEM; 5204 hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0); 5205 if (!hsotg->wq_otg) { 5206 dev_err(hsotg->dev, "Failed to create workqueue\n"); 5207 goto error2; 5208 } 5209 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change); 5210 5211 timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0); 5212 5213 /* Initialize the non-periodic schedule */ 5214 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive); 5215 INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting); 5216 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active); 5217 5218 /* Initialize the periodic schedule */ 5219 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive); 5220 INIT_LIST_HEAD(&hsotg->periodic_sched_ready); 5221 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned); 5222 INIT_LIST_HEAD(&hsotg->periodic_sched_queued); 5223 5224 INIT_LIST_HEAD(&hsotg->split_order); 5225 5226 /* 5227 * Create a host channel descriptor for each host channel implemented 5228 * in the controller. Initialize the channel descriptor array. 5229 */ 5230 INIT_LIST_HEAD(&hsotg->free_hc_list); 5231 num_channels = hsotg->params.host_channels; 5232 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array)); 5233 5234 for (i = 0; i < num_channels; i++) { 5235 channel = kzalloc_obj(*channel); 5236 if (!channel) 5237 goto error3; 5238 channel->hc_num = i; 5239 INIT_LIST_HEAD(&channel->split_order_list_entry); 5240 hsotg->hc_ptr_array[i] = channel; 5241 } 5242 5243 /* Initialize work */ 5244 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func); 5245 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func); 5246 INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func); 5247 5248 /* 5249 * Allocate space for storing data on status transactions. Normally no 5250 * data is sent, but this space acts as a bit bucket. This must be 5251 * done after usb_add_hcd since that function allocates the DMA buffer 5252 * pool. 5253 */ 5254 if (hsotg->params.host_dma) 5255 hsotg->status_buf = dma_alloc_coherent(hsotg->dev, 5256 DWC2_HCD_STATUS_BUF_SIZE, 5257 &hsotg->status_buf_dma, GFP_KERNEL); 5258 else 5259 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE, 5260 GFP_KERNEL); 5261 5262 if (!hsotg->status_buf) 5263 goto error3; 5264 5265 /* 5266 * Create kmem caches to handle descriptor buffers in descriptor 5267 * DMA mode. 5268 * Alignment must be set to 512 bytes. 5269 */ 5270 if (hsotg->params.dma_desc_enable || 5271 hsotg->params.dma_desc_fs_enable) { 5272 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc", 5273 sizeof(struct dwc2_dma_desc) * 5274 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA, 5275 NULL); 5276 if (!hsotg->desc_gen_cache) { 5277 dev_err(hsotg->dev, 5278 "unable to create dwc2 generic desc cache\n"); 5279 5280 /* 5281 * Disable descriptor dma mode since it will not be 5282 * usable. 5283 */ 5284 hsotg->params.dma_desc_enable = false; 5285 hsotg->params.dma_desc_fs_enable = false; 5286 } 5287 5288 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc", 5289 sizeof(struct dwc2_dma_desc) * 5290 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL); 5291 if (!hsotg->desc_hsisoc_cache) { 5292 dev_err(hsotg->dev, 5293 "unable to create dwc2 hs isoc desc cache\n"); 5294 5295 kmem_cache_destroy(hsotg->desc_gen_cache); 5296 5297 /* 5298 * Disable descriptor dma mode since it will not be 5299 * usable. 5300 */ 5301 hsotg->params.dma_desc_enable = false; 5302 hsotg->params.dma_desc_fs_enable = false; 5303 } 5304 } 5305 5306 if (hsotg->params.host_dma) { 5307 /* 5308 * Create kmem caches to handle non-aligned buffer 5309 * in Buffer DMA mode. 5310 */ 5311 hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma", 5312 DWC2_KMEM_UNALIGNED_BUF_SIZE, 4, 5313 SLAB_CACHE_DMA, NULL); 5314 if (!hsotg->unaligned_cache) 5315 dev_err(hsotg->dev, 5316 "unable to create dwc2 unaligned cache\n"); 5317 } 5318 5319 hsotg->otg_port = 1; 5320 hsotg->frame_list = NULL; 5321 hsotg->frame_list_dma = 0; 5322 hsotg->periodic_qh_count = 0; 5323 5324 /* Initiate lx_state to L3 disconnected state */ 5325 hsotg->lx_state = DWC2_L3; 5326 5327 hcd->self.otg_port = hsotg->otg_port; 5328 5329 /* Don't support SG list at this point */ 5330 hcd->self.sg_tablesize = 0; 5331 5332 hcd->tpl_support = of_usb_host_tpl_support(hsotg->dev->of_node); 5333 5334 if (!IS_ERR_OR_NULL(hsotg->uphy)) 5335 otg_set_host(hsotg->uphy->otg, &hcd->self); 5336 5337 /* 5338 * Finish generic HCD initialization and start the HCD. This function 5339 * allocates the DMA buffer pool, registers the USB bus, requests the 5340 * IRQ line, and calls hcd_start method. 5341 */ 5342 retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED); 5343 if (retval < 0) 5344 goto error4; 5345 5346 device_wakeup_enable(hcd->self.controller); 5347 5348 dwc2_hcd_dump_state(hsotg); 5349 5350 dwc2_enable_global_interrupts(hsotg); 5351 5352 return 0; 5353 5354 error4: 5355 kmem_cache_destroy(hsotg->unaligned_cache); 5356 kmem_cache_destroy(hsotg->desc_hsisoc_cache); 5357 kmem_cache_destroy(hsotg->desc_gen_cache); 5358 error3: 5359 dwc2_hcd_release(hsotg); 5360 error2: 5361 usb_put_hcd(hcd); 5362 error1: 5363 5364 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS 5365 kfree(hsotg->last_frame_num_array); 5366 kfree(hsotg->frame_num_array); 5367 #endif 5368 5369 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval); 5370 return retval; 5371 } 5372 5373 /* 5374 * Removes the HCD. 5375 * Frees memory and resources associated with the HCD and deregisters the bus. 5376 */ 5377 void dwc2_hcd_remove(struct dwc2_hsotg *hsotg) 5378 { 5379 struct usb_hcd *hcd; 5380 5381 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n"); 5382 5383 hcd = dwc2_hsotg_to_hcd(hsotg); 5384 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd); 5385 5386 if (!hcd) { 5387 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n", 5388 __func__); 5389 return; 5390 } 5391 5392 if (!IS_ERR_OR_NULL(hsotg->uphy)) 5393 otg_set_host(hsotg->uphy->otg, NULL); 5394 5395 usb_remove_hcd(hcd); 5396 hsotg->priv = NULL; 5397 5398 kmem_cache_destroy(hsotg->unaligned_cache); 5399 kmem_cache_destroy(hsotg->desc_hsisoc_cache); 5400 kmem_cache_destroy(hsotg->desc_gen_cache); 5401 5402 dwc2_hcd_release(hsotg); 5403 usb_put_hcd(hcd); 5404 5405 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS 5406 kfree(hsotg->last_frame_num_array); 5407 kfree(hsotg->frame_num_array); 5408 #endif 5409 } 5410 5411 /** 5412 * dwc2_backup_host_registers() - Backup controller host registers. 5413 * When suspending usb bus, registers needs to be backuped 5414 * if controller power is disabled once suspended. 5415 * 5416 * @hsotg: Programming view of the DWC_otg controller 5417 */ 5418 int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg) 5419 { 5420 struct dwc2_hregs_backup *hr; 5421 int i; 5422 5423 dev_dbg(hsotg->dev, "%s\n", __func__); 5424 5425 /* Backup Host regs */ 5426 hr = &hsotg->hr_backup; 5427 hr->hcfg = dwc2_readl(hsotg, HCFG); 5428 hr->hflbaddr = dwc2_readl(hsotg, HFLBADDR); 5429 hr->haintmsk = dwc2_readl(hsotg, HAINTMSK); 5430 for (i = 0; i < hsotg->params.host_channels; ++i) { 5431 hr->hcchar[i] = dwc2_readl(hsotg, HCCHAR(i)); 5432 hr->hcsplt[i] = dwc2_readl(hsotg, HCSPLT(i)); 5433 hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i)); 5434 hr->hctsiz[i] = dwc2_readl(hsotg, HCTSIZ(i)); 5435 hr->hcidma[i] = dwc2_readl(hsotg, HCDMA(i)); 5436 hr->hcidmab[i] = dwc2_readl(hsotg, HCDMAB(i)); 5437 } 5438 5439 hr->hprt0 = dwc2_read_hprt0(hsotg); 5440 hr->hfir = dwc2_readl(hsotg, HFIR); 5441 hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ); 5442 hr->valid = true; 5443 5444 return 0; 5445 } 5446 5447 /** 5448 * dwc2_restore_host_registers() - Restore controller host registers. 5449 * When resuming usb bus, device registers needs to be restored 5450 * if controller power were disabled. 5451 * 5452 * @hsotg: Programming view of the DWC_otg controller 5453 */ 5454 int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg) 5455 { 5456 struct dwc2_hregs_backup *hr; 5457 int i; 5458 5459 dev_dbg(hsotg->dev, "%s\n", __func__); 5460 5461 /* Restore host regs */ 5462 hr = &hsotg->hr_backup; 5463 if (!hr->valid) { 5464 dev_err(hsotg->dev, "%s: no host registers to restore\n", 5465 __func__); 5466 return -EINVAL; 5467 } 5468 hr->valid = false; 5469 5470 dwc2_writel(hsotg, hr->hcfg, HCFG); 5471 dwc2_writel(hsotg, hr->hflbaddr, HFLBADDR); 5472 dwc2_writel(hsotg, hr->haintmsk, HAINTMSK); 5473 5474 for (i = 0; i < hsotg->params.host_channels; ++i) { 5475 dwc2_writel(hsotg, hr->hcchar[i], HCCHAR(i)); 5476 dwc2_writel(hsotg, hr->hcsplt[i], HCSPLT(i)); 5477 dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i)); 5478 dwc2_writel(hsotg, hr->hctsiz[i], HCTSIZ(i)); 5479 dwc2_writel(hsotg, hr->hcidma[i], HCDMA(i)); 5480 dwc2_writel(hsotg, hr->hcidmab[i], HCDMAB(i)); 5481 } 5482 5483 dwc2_writel(hsotg, hr->hprt0, HPRT0); 5484 dwc2_writel(hsotg, hr->hfir, HFIR); 5485 dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ); 5486 hsotg->frame_number = 0; 5487 5488 return 0; 5489 } 5490 5491 int dwc2_host_backup_critical_registers(struct dwc2_hsotg *hsotg) 5492 { 5493 int ret; 5494 5495 /* Backup all registers */ 5496 ret = dwc2_backup_global_registers(hsotg); 5497 if (ret) { 5498 dev_err(hsotg->dev, "%s: failed to backup global registers\n", 5499 __func__); 5500 return ret; 5501 } 5502 5503 ret = dwc2_backup_host_registers(hsotg); 5504 if (ret) { 5505 dev_err(hsotg->dev, "%s: failed to backup host registers\n", 5506 __func__); 5507 return ret; 5508 } 5509 5510 return 0; 5511 } 5512 5513 int dwc2_host_restore_critical_registers(struct dwc2_hsotg *hsotg) 5514 { 5515 int ret; 5516 5517 ret = dwc2_restore_global_registers(hsotg); 5518 if (ret) { 5519 dev_err(hsotg->dev, "%s: failed to restore registers\n", 5520 __func__); 5521 return ret; 5522 } 5523 5524 ret = dwc2_restore_host_registers(hsotg); 5525 if (ret) { 5526 dev_err(hsotg->dev, "%s: failed to restore host registers\n", 5527 __func__); 5528 return ret; 5529 } 5530 5531 return 0; 5532 } 5533 5534 /** 5535 * dwc2_host_enter_hibernation() - Put controller in Hibernation. 5536 * 5537 * @hsotg: Programming view of the DWC_otg controller 5538 */ 5539 int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg) 5540 { 5541 unsigned long flags; 5542 int ret = 0; 5543 u32 hprt0; 5544 u32 pcgcctl; 5545 u32 gusbcfg; 5546 u32 gpwrdn; 5547 5548 dev_dbg(hsotg->dev, "Preparing host for hibernation\n"); 5549 ret = dwc2_host_backup_critical_registers(hsotg); 5550 if (ret) 5551 return ret; 5552 5553 /* Enter USB Suspend Mode */ 5554 hprt0 = dwc2_readl(hsotg, HPRT0); 5555 hprt0 |= HPRT0_SUSP; 5556 hprt0 &= ~HPRT0_ENA; 5557 dwc2_writel(hsotg, hprt0, HPRT0); 5558 5559 /* Wait for the HPRT0.PrtSusp register field to be set */ 5560 if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000)) 5561 dev_warn(hsotg->dev, "Suspend wasn't generated\n"); 5562 5563 /* 5564 * We need to disable interrupts to prevent servicing of any IRQ 5565 * during going to hibernation 5566 */ 5567 spin_lock_irqsave(&hsotg->lock, flags); 5568 hsotg->lx_state = DWC2_L2; 5569 5570 gusbcfg = dwc2_readl(hsotg, GUSBCFG); 5571 if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) { 5572 /* ULPI interface */ 5573 udelay(10); 5574 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5575 gpwrdn |= GPWRDN_ULPI_LATCH_EN_DURING_HIB_ENTRY; 5576 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5577 udelay(10); 5578 /* Suspend the Phy Clock */ 5579 pcgcctl = dwc2_readl(hsotg, PCGCTL); 5580 pcgcctl |= PCGCTL_STOPPCLK; 5581 dwc2_writel(hsotg, pcgcctl, PCGCTL); 5582 udelay(10); 5583 5584 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5585 gpwrdn |= GPWRDN_PMUACTV; 5586 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5587 udelay(10); 5588 } else { 5589 /* UTMI+ Interface */ 5590 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5591 gpwrdn |= GPWRDN_PMUACTV; 5592 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5593 udelay(10); 5594 5595 pcgcctl = dwc2_readl(hsotg, PCGCTL); 5596 pcgcctl |= PCGCTL_STOPPCLK; 5597 dwc2_writel(hsotg, pcgcctl, PCGCTL); 5598 udelay(10); 5599 } 5600 5601 /* Enable interrupts from wake up logic */ 5602 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5603 gpwrdn |= GPWRDN_PMUINTSEL; 5604 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5605 udelay(10); 5606 5607 /* Unmask host mode interrupts in GPWRDN */ 5608 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5609 gpwrdn |= GPWRDN_DISCONN_DET_MSK; 5610 gpwrdn |= GPWRDN_LNSTSCHG_MSK; 5611 gpwrdn |= GPWRDN_STS_CHGINT_MSK; 5612 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5613 udelay(10); 5614 5615 /* Enable Power Down Clamp */ 5616 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5617 gpwrdn |= GPWRDN_PWRDNCLMP; 5618 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5619 udelay(10); 5620 5621 /* Switch off VDD */ 5622 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5623 gpwrdn |= GPWRDN_PWRDNSWTCH; 5624 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5625 5626 hsotg->hibernated = 1; 5627 hsotg->bus_suspended = 1; 5628 dev_dbg(hsotg->dev, "Host hibernation completed\n"); 5629 spin_unlock_irqrestore(&hsotg->lock, flags); 5630 return ret; 5631 } 5632 5633 /* 5634 * dwc2_host_exit_hibernation() 5635 * 5636 * @hsotg: Programming view of the DWC_otg controller 5637 * @rem_wakeup: indicates whether resume is initiated by Device or Host. 5638 * @param reset: indicates whether resume is initiated by Reset. 5639 * 5640 * Return: non-zero if failed to enter to hibernation. 5641 * 5642 * This function is for exiting from Host mode hibernation by 5643 * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup. 5644 */ 5645 int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup, 5646 int reset) 5647 { 5648 u32 gpwrdn; 5649 u32 hprt0; 5650 int ret = 0; 5651 struct dwc2_gregs_backup *gr; 5652 struct dwc2_hregs_backup *hr; 5653 5654 gr = &hsotg->gr_backup; 5655 hr = &hsotg->hr_backup; 5656 5657 dev_dbg(hsotg->dev, 5658 "%s: called with rem_wakeup = %d reset = %d\n", 5659 __func__, rem_wakeup, reset); 5660 5661 dwc2_hib_restore_common(hsotg, rem_wakeup, 1); 5662 hsotg->hibernated = 0; 5663 5664 /* 5665 * This step is not described in functional spec but if not wait for 5666 * this delay, mismatch interrupts occurred because just after restore 5667 * core is in Device mode(gintsts.curmode == 0) 5668 */ 5669 mdelay(100); 5670 5671 /* Clear all pending interrupts */ 5672 dwc2_writel(hsotg, 0xffffffff, GINTSTS); 5673 5674 /* De-assert Restore */ 5675 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5676 gpwrdn &= ~GPWRDN_RESTORE; 5677 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5678 udelay(10); 5679 5680 /* Restore GUSBCFG, HCFG */ 5681 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG); 5682 dwc2_writel(hsotg, hr->hcfg, HCFG); 5683 5684 /* Reset ULPI latch */ 5685 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5686 gpwrdn &= ~GPWRDN_ULPI_LATCH_EN_DURING_HIB_ENTRY; 5687 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5688 5689 /* De-assert Wakeup Logic */ 5690 if (!(rem_wakeup && hsotg->hw_params.snpsid >= DWC2_CORE_REV_4_30a)) { 5691 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5692 gpwrdn &= ~GPWRDN_PMUACTV; 5693 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5694 udelay(10); 5695 } 5696 5697 hprt0 = hr->hprt0; 5698 hprt0 |= HPRT0_PWR; 5699 hprt0 &= ~HPRT0_ENA; 5700 hprt0 &= ~HPRT0_SUSP; 5701 dwc2_writel(hsotg, hprt0, HPRT0); 5702 5703 hprt0 = hr->hprt0; 5704 hprt0 |= HPRT0_PWR; 5705 hprt0 &= ~HPRT0_ENA; 5706 hprt0 &= ~HPRT0_SUSP; 5707 5708 if (reset) { 5709 hprt0 |= HPRT0_RST; 5710 dwc2_writel(hsotg, hprt0, HPRT0); 5711 5712 /* Wait for Resume time and then program HPRT again */ 5713 mdelay(60); 5714 hprt0 &= ~HPRT0_RST; 5715 dwc2_writel(hsotg, hprt0, HPRT0); 5716 } else { 5717 hprt0 |= HPRT0_RES; 5718 dwc2_writel(hsotg, hprt0, HPRT0); 5719 5720 /* De-assert Wakeup Logic */ 5721 if ((rem_wakeup && hsotg->hw_params.snpsid >= DWC2_CORE_REV_4_30a)) { 5722 gpwrdn = dwc2_readl(hsotg, GPWRDN); 5723 gpwrdn &= ~GPWRDN_PMUACTV; 5724 dwc2_writel(hsotg, gpwrdn, GPWRDN); 5725 udelay(10); 5726 } 5727 /* Wait for Resume time and then program HPRT again */ 5728 mdelay(100); 5729 hprt0 &= ~HPRT0_RES; 5730 dwc2_writel(hsotg, hprt0, HPRT0); 5731 } 5732 /* Clear all interrupt status */ 5733 hprt0 = dwc2_readl(hsotg, HPRT0); 5734 hprt0 |= HPRT0_CONNDET; 5735 hprt0 |= HPRT0_ENACHG; 5736 hprt0 &= ~HPRT0_ENA; 5737 dwc2_writel(hsotg, hprt0, HPRT0); 5738 5739 hprt0 = dwc2_readl(hsotg, HPRT0); 5740 5741 /* Clear all pending interrupts */ 5742 dwc2_writel(hsotg, 0xffffffff, GINTSTS); 5743 5744 /* Restore global registers */ 5745 ret = dwc2_host_restore_critical_registers(hsotg); 5746 if (ret) 5747 return ret; 5748 5749 if (rem_wakeup) { 5750 dwc2_hcd_rem_wakeup(hsotg); 5751 /* 5752 * Change "port_connect_status_change" flag to re-enumerate, 5753 * because after exit from hibernation port connection status 5754 * is not detected. 5755 */ 5756 hsotg->flags.b.port_connect_status_change = 1; 5757 } 5758 5759 hsotg->hibernated = 0; 5760 hsotg->bus_suspended = 0; 5761 hsotg->lx_state = DWC2_L0; 5762 dev_dbg(hsotg->dev, "Host hibernation restore complete\n"); 5763 return ret; 5764 } 5765 5766 bool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2) 5767 { 5768 struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub; 5769 5770 /* If the controller isn't allowed to wakeup then we can power off. */ 5771 if (!device_may_wakeup(dwc2->dev)) 5772 return true; 5773 5774 /* 5775 * We don't want to power off the PHY if something under the 5776 * root hub has wakeup enabled. 5777 */ 5778 if (usb_wakeup_enabled_descendants(root_hub)) 5779 return false; 5780 5781 /* No reason to keep the PHY powered, so allow poweroff */ 5782 return true; 5783 } 5784 5785 /** 5786 * dwc2_host_enter_partial_power_down() - Put controller in partial 5787 * power down. 5788 * 5789 * @hsotg: Programming view of the DWC_otg controller 5790 * 5791 * Return: non-zero if failed to enter host partial power down. 5792 * 5793 * This function is for entering Host mode partial power down. 5794 */ 5795 int dwc2_host_enter_partial_power_down(struct dwc2_hsotg *hsotg) 5796 { 5797 u32 pcgcctl; 5798 u32 hprt0; 5799 int ret = 0; 5800 5801 dev_dbg(hsotg->dev, "Entering host partial power down started.\n"); 5802 5803 /* Put this port in suspend mode. */ 5804 hprt0 = dwc2_read_hprt0(hsotg); 5805 hprt0 |= HPRT0_SUSP; 5806 dwc2_writel(hsotg, hprt0, HPRT0); 5807 udelay(5); 5808 5809 /* Wait for the HPRT0.PrtSusp register field to be set */ 5810 if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000)) 5811 dev_warn(hsotg->dev, "Suspend wasn't generated\n"); 5812 5813 /* Backup all registers */ 5814 ret = dwc2_host_backup_critical_registers(hsotg); 5815 if (ret) 5816 return ret; 5817 5818 /* 5819 * Clear any pending interrupts since dwc2 will not be able to 5820 * clear them after entering partial_power_down. 5821 */ 5822 dwc2_writel(hsotg, 0xffffffff, GINTSTS); 5823 5824 /* Put the controller in low power state */ 5825 pcgcctl = dwc2_readl(hsotg, PCGCTL); 5826 5827 pcgcctl |= PCGCTL_PWRCLMP; 5828 dwc2_writel(hsotg, pcgcctl, PCGCTL); 5829 udelay(5); 5830 5831 pcgcctl |= PCGCTL_RSTPDWNMODULE; 5832 dwc2_writel(hsotg, pcgcctl, PCGCTL); 5833 udelay(5); 5834 5835 pcgcctl |= PCGCTL_STOPPCLK; 5836 dwc2_writel(hsotg, pcgcctl, PCGCTL); 5837 5838 /* Set in_ppd flag to 1 as here core enters suspend. */ 5839 hsotg->in_ppd = 1; 5840 hsotg->lx_state = DWC2_L2; 5841 hsotg->bus_suspended = true; 5842 5843 dev_dbg(hsotg->dev, "Entering host partial power down completed.\n"); 5844 5845 return ret; 5846 } 5847 5848 /* 5849 * dwc2_host_exit_partial_power_down() - Exit controller from host partial 5850 * power down. 5851 * 5852 * @hsotg: Programming view of the DWC_otg controller 5853 * @rem_wakeup: indicates whether resume is initiated by Reset. 5854 * @restore: indicates whether need to restore the registers or not. 5855 * 5856 * Return: non-zero if failed to exit host partial power down. 5857 * 5858 * This function is for exiting from Host mode partial power down. 5859 */ 5860 int dwc2_host_exit_partial_power_down(struct dwc2_hsotg *hsotg, 5861 int rem_wakeup, bool restore) 5862 { 5863 u32 pcgcctl; 5864 int ret = 0; 5865 u32 hprt0; 5866 5867 dev_dbg(hsotg->dev, "Exiting host partial power down started.\n"); 5868 5869 pcgcctl = dwc2_readl(hsotg, PCGCTL); 5870 pcgcctl &= ~PCGCTL_STOPPCLK; 5871 dwc2_writel(hsotg, pcgcctl, PCGCTL); 5872 udelay(5); 5873 5874 pcgcctl = dwc2_readl(hsotg, PCGCTL); 5875 pcgcctl &= ~PCGCTL_PWRCLMP; 5876 dwc2_writel(hsotg, pcgcctl, PCGCTL); 5877 udelay(5); 5878 5879 pcgcctl = dwc2_readl(hsotg, PCGCTL); 5880 pcgcctl &= ~PCGCTL_RSTPDWNMODULE; 5881 dwc2_writel(hsotg, pcgcctl, PCGCTL); 5882 5883 udelay(100); 5884 if (restore) { 5885 ret = dwc2_host_restore_critical_registers(hsotg); 5886 if (ret) 5887 return ret; 5888 } 5889 5890 /* Drive resume signaling and exit suspend mode on the port. */ 5891 hprt0 = dwc2_read_hprt0(hsotg); 5892 hprt0 |= HPRT0_RES; 5893 hprt0 &= ~HPRT0_SUSP; 5894 dwc2_writel(hsotg, hprt0, HPRT0); 5895 udelay(5); 5896 5897 if (!rem_wakeup) { 5898 /* Stop driveing resume signaling on the port. */ 5899 hprt0 = dwc2_read_hprt0(hsotg); 5900 hprt0 &= ~HPRT0_RES; 5901 dwc2_writel(hsotg, hprt0, HPRT0); 5902 5903 hsotg->bus_suspended = false; 5904 } else { 5905 /* Turn on the port power bit. */ 5906 hprt0 = dwc2_read_hprt0(hsotg); 5907 hprt0 |= HPRT0_PWR; 5908 dwc2_writel(hsotg, hprt0, HPRT0); 5909 5910 /* Connect hcd. */ 5911 dwc2_hcd_connect(hsotg); 5912 5913 mod_timer(&hsotg->wkp_timer, 5914 jiffies + msecs_to_jiffies(71)); 5915 } 5916 5917 /* Set lx_state to and in_ppd to 0 as here core exits from suspend. */ 5918 hsotg->in_ppd = 0; 5919 hsotg->lx_state = DWC2_L0; 5920 5921 dev_dbg(hsotg->dev, "Exiting host partial power down completed.\n"); 5922 return ret; 5923 } 5924 5925 /** 5926 * dwc2_host_enter_clock_gating() - Put controller in clock gating. 5927 * 5928 * @hsotg: Programming view of the DWC_otg controller 5929 * 5930 * This function is for entering Host mode clock gating. 5931 */ 5932 void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg) 5933 { 5934 u32 hprt0; 5935 u32 pcgctl; 5936 5937 dev_dbg(hsotg->dev, "Entering host clock gating.\n"); 5938 5939 /* Put this port in suspend mode. */ 5940 hprt0 = dwc2_read_hprt0(hsotg); 5941 hprt0 |= HPRT0_SUSP; 5942 dwc2_writel(hsotg, hprt0, HPRT0); 5943 5944 /* Set the Phy Clock bit as suspend is received. */ 5945 pcgctl = dwc2_readl(hsotg, PCGCTL); 5946 pcgctl |= PCGCTL_STOPPCLK; 5947 dwc2_writel(hsotg, pcgctl, PCGCTL); 5948 udelay(5); 5949 5950 /* Set the Gate hclk as suspend is received. */ 5951 pcgctl = dwc2_readl(hsotg, PCGCTL); 5952 pcgctl |= PCGCTL_GATEHCLK; 5953 dwc2_writel(hsotg, pcgctl, PCGCTL); 5954 udelay(5); 5955 5956 hsotg->bus_suspended = true; 5957 hsotg->lx_state = DWC2_L2; 5958 } 5959 5960 /** 5961 * dwc2_host_exit_clock_gating() - Exit controller from clock gating. 5962 * 5963 * @hsotg: Programming view of the DWC_otg controller 5964 * @rem_wakeup: indicates whether resume is initiated by remote wakeup 5965 * 5966 * This function is for exiting Host mode clock gating. 5967 */ 5968 void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup) 5969 { 5970 u32 hprt0; 5971 u32 pcgctl; 5972 5973 dev_dbg(hsotg->dev, "Exiting host clock gating.\n"); 5974 5975 /* Clear the Gate hclk. */ 5976 pcgctl = dwc2_readl(hsotg, PCGCTL); 5977 pcgctl &= ~PCGCTL_GATEHCLK; 5978 dwc2_writel(hsotg, pcgctl, PCGCTL); 5979 udelay(5); 5980 5981 /* Phy Clock bit. */ 5982 pcgctl = dwc2_readl(hsotg, PCGCTL); 5983 pcgctl &= ~PCGCTL_STOPPCLK; 5984 dwc2_writel(hsotg, pcgctl, PCGCTL); 5985 udelay(5); 5986 5987 /* Drive resume signaling and exit suspend mode on the port. */ 5988 hprt0 = dwc2_read_hprt0(hsotg); 5989 hprt0 |= HPRT0_RES; 5990 hprt0 &= ~HPRT0_SUSP; 5991 dwc2_writel(hsotg, hprt0, HPRT0); 5992 udelay(5); 5993 5994 if (!rem_wakeup) { 5995 /* In case of port resume need to wait for 40 ms */ 5996 msleep(USB_RESUME_TIMEOUT); 5997 5998 /* Stop driveing resume signaling on the port. */ 5999 hprt0 = dwc2_read_hprt0(hsotg); 6000 hprt0 &= ~HPRT0_RES; 6001 dwc2_writel(hsotg, hprt0, HPRT0); 6002 6003 hsotg->bus_suspended = false; 6004 hsotg->lx_state = DWC2_L0; 6005 } else { 6006 mod_timer(&hsotg->wkp_timer, 6007 jiffies + msecs_to_jiffies(71)); 6008 } 6009 } 6010