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