blob: 34de3bc42bac393a325b1421e02179ae149a3061 [file] [log] [blame]
huang lin365250e2014-08-06 16:43:43 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Rockchip Electronics
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
huang lin365250e2014-08-06 16:43:43 +080014 */
15
16#include <libpayload.h>
17#include <arch/cache.h>
18
19#include "dwc2.h"
20#include "dwc2_private.h"
21
22static void dummy(hci_t *controller)
23{
24}
25
26static void dwc2_reinit(hci_t *controller)
27{
28 dwc2_reg_t *reg = DWC2_REG(controller);
29 gusbcfg_t gusbcfg = { .d32 = 0 };
30 grstctl_t grstctl = { .d32 = 0 };
31 gintsts_t gintsts = { .d32 = 0 };
32 gahbcfg_t gahbcfg = { .d32 = 0 };
33 grxfsiz_t grxfsiz = { .d32 = 0 };
huang lin8b52c932014-12-04 18:25:47 +080034 ghwcfg3_t hwcfg3 = { .d32 = 0 };
huang lin365250e2014-08-06 16:43:43 +080035 hcintmsk_t hcintmsk = { .d32 = 0 };
Yunzhi Liaa336092015-06-19 17:09:04 +080036 gtxfsiz_t gnptxfsiz = { .d32 = 0 };
37 gtxfsiz_t hptxfsiz = { .d32 = 0 };
huang lin365250e2014-08-06 16:43:43 +080038
39 const int timeout = 10000;
huang lin8b52c932014-12-04 18:25:47 +080040 int i, fifo_blocks, tx_blocks;
huang lin365250e2014-08-06 16:43:43 +080041
42 /* Wait for AHB idle */
43 for (i = 0; i < timeout; i++) {
44 udelay(1);
45 grstctl.d32 = readl(&reg->core.grstctl);
46 if (grstctl.ahbidle)
47 break;
48 }
49 if (i == timeout)
50 fatal("DWC2 Init error AHB Idle\n");
51
52 /* Restart the Phy Clock */
53 writel(0x0, &reg->pcgr.pcgcctl);
54 /* Core soft reset */
55 grstctl.csftrst = 1;
56 writel(grstctl.d32, &reg->core.grstctl);
57 for (i = 0; i < timeout; i++) {
58 udelay(1);
59 grstctl.d32 = readl(&reg->core.grstctl);
60 if (!grstctl.csftrst)
61 break;
62 }
63 if (i == timeout)
64 fatal("DWC2 Init error reset fail\n");
65
66 /* Set 16bit PHY if & Force host mode */
67 gusbcfg.d32 = readl(&reg->core.gusbcfg);
68 gusbcfg.phyif = 1;
69 gusbcfg.forcehstmode = 1;
70 gusbcfg.forcedevmode = 0;
71 writel(gusbcfg.d32, &reg->core.gusbcfg);
72 /* Wait for force host mode effect, it may takes 100ms */
73 for (i = 0; i < timeout; i++) {
74 udelay(10);
75 gintsts.d32 = readl(&reg->core.gintsts);
76 if (gintsts.curmod)
77 break;
78 }
79 if (i == timeout)
80 fatal("DWC2 Init error force host mode fail\n");
81
82 /*
83 * Config FIFO
84 * The non-periodic tx fifo and rx fifo share one continuous
85 * piece of IP-internal SRAM.
86 */
huang lin8b52c932014-12-04 18:25:47 +080087
88 /*
89 * Read total data FIFO depth from HWCFG3
90 * this value is in terms of 32-bit words
91 */
92 hwcfg3.d32 = readl(&reg->core.ghwcfg3);
93 /*
94 * Reserve 2 spaces for the status entries of received packets
95 * and 2 spaces for bulk and control OUT endpoints. Calculate how
96 * many blocks can be alloted, assume largest packet size is 512.
Yunzhi Liaa336092015-06-19 17:09:04 +080097 * 16 locations reserved for periodic TX .
huang lin8b52c932014-12-04 18:25:47 +080098 */
Yunzhi Liaa336092015-06-19 17:09:04 +080099 fifo_blocks = (hwcfg3.dfifodepth - 4 - 16) / (512 / 4);
huang lin8b52c932014-12-04 18:25:47 +0800100 tx_blocks = fifo_blocks / 2;
101
102 grxfsiz.rxfdep = (fifo_blocks - tx_blocks) * (512 / 4) + 4;
huang lin365250e2014-08-06 16:43:43 +0800103 writel(grxfsiz.d32, &reg->core.grxfsiz);
Yunzhi Liaa336092015-06-19 17:09:04 +0800104 gnptxfsiz.txfstaddr = grxfsiz.rxfdep;
105 gnptxfsiz.txfdep = tx_blocks * (512 / 4);
huang lin365250e2014-08-06 16:43:43 +0800106 writel(gnptxfsiz.d32, &reg->core.gnptxfsiz);
Yunzhi Liaa336092015-06-19 17:09:04 +0800107 hptxfsiz.txfstaddr = gnptxfsiz.txfstaddr + gnptxfsiz.txfdep;
108 hptxfsiz.txfdep = 16;
109 writel(hptxfsiz.d32, &reg->core.hptxfsiz);
huang lin365250e2014-08-06 16:43:43 +0800110
111 /* Init host channels */
112 hcintmsk.xfercomp = 1;
113 hcintmsk.xacterr = 1;
114 hcintmsk.stall = 1;
115 hcintmsk.chhltd = 1;
116 hcintmsk.bblerr = 1;
117 for (i = 0; i < MAX_EPS_CHANNELS; i++)
118 writel(hcintmsk.d32, &reg->host.hchn[i].hcintmaskn);
119
120 /* Unmask interrupt and configure DMA mode */
121 gahbcfg.glblintrmsk = 1;
122 gahbcfg.hbstlen = DMA_BURST_INCR8;
123 gahbcfg.dmaen = 1;
124 writel(gahbcfg.d32, &reg->core.gahbcfg);
125
126 DWC2_INST(controller)->hprt0 = &reg->host.hprt;
127
128 usb_debug("DWC2 init finished!\n");
129}
130
131static void dwc2_shutdown(hci_t *controller)
132{
133 detach_controller(controller);
134 free(DWC2_INST(controller)->dma_buffer);
135 free(DWC2_INST(controller));
136 free(controller);
137}
138
Yunzhi Li49384262015-08-11 17:58:14 +0800139/* Test root port device connect status */
140static int dwc2_disconnected(hci_t *controller)
141{
142 dwc2_reg_t *reg = DWC2_REG(controller);
143 hprt_t hprt;
144
145 hprt.d32 = readl(&reg->host.hprt);
146 return !(hprt.prtena && hprt.prtconnsts);
147}
148
huang lin365250e2014-08-06 16:43:43 +0800149/*
150 * This function returns the actual transfer length when the transfer succeeded
151 * or an error code if the transfer failed
152 */
153static int
154wait_for_complete(endpoint_t *ep, uint32_t ch_num)
155{
156 hcint_t hcint;
157 hcchar_t hcchar;
158 hctsiz_t hctsiz;
159 dwc2_reg_t *reg = DWC2_REG(ep->dev->controller);
160 int timeout = 600000; /* time out after 600000 * 5us == 3s */
161
162 /*
163 * TODO: We should take care of up to three times of transfer error
164 * retry here, according to the USB 2.0 spec 4.5.2
165 */
166 do {
167 udelay(5);
168 hcint.d32 = readl(&reg->host.hchn[ch_num].hcintn);
169 hctsiz.d32 = readl(&reg->host.hchn[ch_num].hctsizn);
170
171 if (hcint.chhltd) {
172 writel(hcint.d32, &reg->host.hchn[ch_num].hcintn);
Yunzhi Liebd3da72015-07-02 15:28:11 +0800173 if (hcint.xfercomp || hcint.ack)
huang lin365250e2014-08-06 16:43:43 +0800174 return hctsiz.xfersize;
Yunzhi Liaa336092015-06-19 17:09:04 +0800175 else if (hcint.nak || hcint.frmovrun)
Yunzhi Liebd3da72015-07-02 15:28:11 +0800176 return -HCSTAT_NAK;
huang lin365250e2014-08-06 16:43:43 +0800177 else if (hcint.xacterr)
178 return -HCSTAT_XFERERR;
179 else if (hcint.bblerr)
180 return -HCSTAT_BABBLE;
181 else if (hcint.stall)
182 return -HCSTAT_STALL;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800183 else if (hcint.nyet)
184 return -HCSTAT_NYET;
huang lin365250e2014-08-06 16:43:43 +0800185 else
186 return -HCSTAT_UNKNOW;
187 }
huang lin365250e2014-08-06 16:43:43 +0800188
Yunzhi Li49384262015-08-11 17:58:14 +0800189 if (dwc2_disconnected(ep->dev->controller))
190 return -HCSTAT_DISCONNECTED;
191 } while (timeout--);
192 /* Release the channel when hit timeout condition */
huang lin365250e2014-08-06 16:43:43 +0800193 hcchar.d32 = readl(&reg->host.hchn[ch_num].hccharn);
194 if (hcchar.chen) {
195 /*
196 * Programming the HCCHARn register with the chdis and
197 * chena bits set to 1 at the same time to disable the
198 * channel and the core will generate a channel halted
199 * interrupt.
200 */
201 hcchar.chdis = 1;
202 writel(hcchar.d32, &reg->host.hchn[ch_num].hccharn);
203 do {
204 hcchar.d32 = readl(&reg->host.hchn[ch_num].hccharn);
205 } while (hcchar.chen);
206
207 }
208
209 /* Clear all pending interrupt flags */
210 hcint.d32 = ~0;
211 writel(hcint.d32, &reg->host.hchn[ch_num].hcintn);
212
213 return -HCSTAT_TIMEOUT;
214}
215
216static int
Yunzhi Liebd3da72015-07-02 15:28:11 +0800217dwc2_do_xfer(endpoint_t *ep, int size, int pid, ep_dir_t dir,
Yunzhi Li8b498892015-07-24 16:12:42 +0800218 uint32_t ch_num, u8 *data_buf, int *short_pkt)
huang lin365250e2014-08-06 16:43:43 +0800219{
220 uint32_t do_copy;
221 int ret;
222 uint32_t packet_cnt;
223 uint32_t packet_size;
224 uint32_t transferred = 0;
225 uint32_t inpkt_length;
226 hctsiz_t hctsiz = { .d32 = 0 };
227 hcchar_t hcchar = { .d32 = 0 };
228 void *aligned_buf;
229 dwc2_reg_t *reg = DWC2_REG(ep->dev->controller);
230
231 packet_size = ep->maxpacketsize;
232 packet_cnt = ALIGN_UP(size, packet_size) / packet_size;
233 inpkt_length = packet_cnt * packet_size;
234 /* At least 1 packet should be programed */
235 packet_cnt = (packet_cnt == 0) ? 1 : packet_cnt;
236
237 /*
238 * For an IN, this field is the buffer size that the application has
239 * reserved for the transfer. The application should program this field
240 * as integer multiple of the maximum packet size for IN transactions.
241 */
242 hctsiz.xfersize = (dir == EPDIR_OUT) ? size : inpkt_length;
243 hctsiz.pktcnt = packet_cnt;
244 hctsiz.pid = pid;
245
246 hcchar.mps = packet_size;
247 hcchar.epnum = ep->endpoint & 0xf;
248 hcchar.epdir = dir;
249 hcchar.eptype = ep->type;
250 hcchar.multicnt = 1;
251 hcchar.devaddr = ep->dev->address;
252 hcchar.chdis = 0;
253 hcchar.chen = 1;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800254 if (ep->dev->speed == LOW_SPEED)
255 hcchar.lspddev = 1;
huang lin365250e2014-08-06 16:43:43 +0800256
257 if (size > DMA_SIZE) {
258 usb_debug("Transfer too large: %d\n", size);
259 return -1;
260 }
261
262 /*
263 * Check the buffer address which should be 4-byte aligned and DMA
264 * coherent
265 */
266 do_copy = !dma_coherent(data_buf) || ((uintptr_t)data_buf & 0x3);
267 aligned_buf = do_copy ? DWC2_INST(ep->dev->controller)->dma_buffer :
268 data_buf;
269
270 if (do_copy && (dir == EPDIR_OUT))
271 memcpy(aligned_buf, data_buf, size);
272
Yunzhi Li87c9f0e2015-09-10 11:42:30 +0800273 if (dwc2_disconnected(ep->dev->controller))
274 return -HCSTAT_DISCONNECTED;
275
huang lin365250e2014-08-06 16:43:43 +0800276 writel(hctsiz.d32, &reg->host.hchn[ch_num].hctsizn);
Ionela Voinescufa143852015-02-02 15:35:44 +0000277 writel((uint32_t)virt_to_bus(aligned_buf),
278 &reg->host.hchn[ch_num].hcdman);
huang lin365250e2014-08-06 16:43:43 +0800279 writel(hcchar.d32, &reg->host.hchn[ch_num].hccharn);
280
281 ret = wait_for_complete(ep, ch_num);
282
283 if (ret >= 0) {
284 /* Calculate actual transferred length */
Julius Wernere8a8a002015-08-17 18:47:18 -0700285 transferred = (dir == EPDIR_IN) ? inpkt_length - ret : size;
huang lin365250e2014-08-06 16:43:43 +0800286
287 if (do_copy && (dir == EPDIR_IN))
288 memcpy(data_buf, aligned_buf, transferred);
Yunzhi Li8b498892015-07-24 16:12:42 +0800289
290 if ((short_pkt != NULL) && (dir == EPDIR_IN))
291 *short_pkt = (ret > 0) ? 1 : 0;
292
huang lin365250e2014-08-06 16:43:43 +0800293 }
294
295 /* Save data toggle */
296 hctsiz.d32 = readl(&reg->host.hchn[ch_num].hctsizn);
297 ep->toggle = hctsiz.pid;
298
299 if (ret < 0) {
300 usb_debug("%s Transfer stop code: %x\n", __func__, ret);
301 return ret;
302 }
303 return transferred;
304}
305
306static int
Yunzhi Liebd3da72015-07-02 15:28:11 +0800307dwc2_split_transfer(endpoint_t *ep, int size, int pid, ep_dir_t dir,
Yunzhi Li8b498892015-07-24 16:12:42 +0800308 uint32_t ch_num, u8 *data_buf, split_info_t *split,
309 int *short_pkt)
Yunzhi Liebd3da72015-07-02 15:28:11 +0800310{
311 dwc2_reg_t *reg = DWC2_REG(ep->dev->controller);
312 hfnum_t hfnum;
313 hcsplit_t hcsplit = { .d32 = 0 };
314 int ret, transferred = 0;
315
316 hcsplit.hubaddr = split->hubaddr;
317 hcsplit.prtaddr = split->hubport;
318 hcsplit.spltena = 1;
319 writel(hcsplit.d32, &reg->host.hchn[ch_num].hcspltn);
320
321 /* Wait for next frame boundary */
322 do {
323 hfnum.d32 = readl(&reg->host.hfnum);
Yunzhi Li49384262015-08-11 17:58:14 +0800324
Yunzhi Lia8810002015-08-28 09:43:44 +0800325 if (dwc2_disconnected(ep->dev->controller)) {
326 ret = -HCSTAT_DISCONNECTED;
327 goto out;
328 }
Yunzhi Liebd3da72015-07-02 15:28:11 +0800329 } while (hfnum.frnum % 8 != 0);
330
331 /* Handle Start-Split */
332 ret = dwc2_do_xfer(ep, dir == EPDIR_IN ? 0 : size, pid, dir, ch_num,
Yunzhi Li8b498892015-07-24 16:12:42 +0800333 data_buf, NULL);
Yunzhi Liebd3da72015-07-02 15:28:11 +0800334 if (ret < 0)
335 goto out;
336
337 hcsplit.spltena = 1;
338 hcsplit.compsplt = 1;
339 writel(hcsplit.d32, &reg->host.hchn[ch_num].hcspltn);
340 ep->toggle = pid;
341
342 if (dir == EPDIR_OUT)
343 transferred += ret;
344
345 /* Handle Complete-Split */
346 do {
347 ret = dwc2_do_xfer(ep, dir == EPDIR_OUT ? 0 : size, ep->toggle,
Yunzhi Li8b498892015-07-24 16:12:42 +0800348 dir, ch_num, data_buf, short_pkt);
Yunzhi Liebd3da72015-07-02 15:28:11 +0800349 } while (ret == -HCSTAT_NYET);
350
351 if (dir == EPDIR_IN)
352 transferred += ret;
353
354out:
355 /* Clear hcsplit reg */
356 hcsplit.spltena = 0;
357 hcsplit.compsplt = 0;
358 writel(hcsplit.d32, &reg->host.hchn[ch_num].hcspltn);
359
360 if (ret < 0)
361 return ret;
362
363 return transferred;
364}
365
366static int dwc2_need_split(usbdev_t *dev, split_info_t *split)
367{
368 if (dev->speed == HIGH_SPEED)
369 return 0;
370
371 if (closest_usb2_hub(dev, &split->hubaddr, &split->hubport))
372 return 0;
373
374 return 1;
375}
376
377static int
378dwc2_transfer(endpoint_t *ep, int size, int pid, ep_dir_t dir, uint32_t ch_num,
379 u8 *src, uint8_t skip_nak)
380{
381 split_info_t split;
Yunzhi Li8b498892015-07-24 16:12:42 +0800382 int ret, short_pkt, transferred = 0, timeout = 3000;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800383
384 ep->toggle = pid;
385
386 do {
Yunzhi Li8b498892015-07-24 16:12:42 +0800387 short_pkt = 0;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800388 if (dwc2_need_split(ep->dev, &split)) {
389nak_retry:
Yunzhi Li8b498892015-07-24 16:12:42 +0800390 ret = dwc2_split_transfer(ep, MIN(ep->maxpacketsize,
391 size), ep->toggle, dir, 0, src, &split,
392 &short_pkt);
Yunzhi Liebd3da72015-07-02 15:28:11 +0800393
394 /*
395 * dwc2_split_transfer() waits for the next FullSpeed
396 * frame boundary, so we have one try per millisecond.
397 * It's 3s timeout for each split transfer.
398 */
399 if (ret == -HCSTAT_NAK && !skip_nak && --timeout) {
400 udelay(500);
401 goto nak_retry;
402 }
403 } else {
Yunzhi Li8b498892015-07-24 16:12:42 +0800404 ret = dwc2_do_xfer(ep, MIN(DMA_SIZE, size), pid, dir, 0,
405 src, &short_pkt);
Yunzhi Liebd3da72015-07-02 15:28:11 +0800406 }
407
408 if (ret < 0)
409 return ret;
410
411 size -= ret;
412 src += ret;
413 transferred += ret;
Yunzhi Li8b498892015-07-24 16:12:42 +0800414
415 } while (size > 0 && !short_pkt);
Yunzhi Liebd3da72015-07-02 15:28:11 +0800416
417 return transferred;
418}
419
420static int
huang lin365250e2014-08-06 16:43:43 +0800421dwc2_bulk(endpoint_t *ep, int size, u8 *src, int finalize)
422{
423 ep_dir_t data_dir;
424
425 if (ep->direction == IN)
426 data_dir = EPDIR_IN;
427 else if (ep->direction == OUT)
428 data_dir = EPDIR_OUT;
429 else
430 return -1;
431
Yunzhi Liebd3da72015-07-02 15:28:11 +0800432 return dwc2_transfer(ep, size, ep->toggle, data_dir, 0, src, 0);
huang lin365250e2014-08-06 16:43:43 +0800433}
434
435static int
436dwc2_control(usbdev_t *dev, direction_t dir, int drlen, void *setup,
437 int dalen, u8 *src)
438{
439 int ret = 0;
huang lin365250e2014-08-06 16:43:43 +0800440 ep_dir_t data_dir;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800441 endpoint_t *ep = &dev->endpoints[0];
huang lin365250e2014-08-06 16:43:43 +0800442
443 if (dir == IN)
444 data_dir = EPDIR_IN;
445 else if (dir == OUT)
446 data_dir = EPDIR_OUT;
447 else
448 return -1;
449
450 /* Setup Phase */
Yunzhi Liebd3da72015-07-02 15:28:11 +0800451 if (dwc2_transfer(ep, drlen, PID_SETUP, EPDIR_OUT, 0, setup, 0) < 0)
huang lin365250e2014-08-06 16:43:43 +0800452 return -1;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800453
huang lin365250e2014-08-06 16:43:43 +0800454 /* Data Phase */
Yunzhi Liebd3da72015-07-02 15:28:11 +0800455 ep->toggle = PID_DATA1;
huang lin365250e2014-08-06 16:43:43 +0800456 if (dalen > 0) {
Yunzhi Liebd3da72015-07-02 15:28:11 +0800457 ret = dwc2_transfer(ep, dalen, ep->toggle, data_dir, 0, src, 0);
huang lin365250e2014-08-06 16:43:43 +0800458 if (ret < 0)
459 return -1;
460 }
Yunzhi Liebd3da72015-07-02 15:28:11 +0800461
huang lin365250e2014-08-06 16:43:43 +0800462 /* Status Phase */
Yunzhi Liebd3da72015-07-02 15:28:11 +0800463 if (dwc2_transfer(ep, 0, PID_DATA1, !data_dir, 0, NULL, 0) < 0)
huang lin365250e2014-08-06 16:43:43 +0800464 return -1;
465
466 return ret;
467}
468
Yunzhi Liaa336092015-06-19 17:09:04 +0800469static int
470dwc2_intr(endpoint_t *ep, int size, u8 *src)
471{
472 ep_dir_t data_dir;
473
474 if (ep->direction == IN)
475 data_dir = EPDIR_IN;
476 else if (ep->direction == OUT)
477 data_dir = EPDIR_OUT;
478 else
479 return -1;
480
Yunzhi Liebd3da72015-07-02 15:28:11 +0800481 return dwc2_transfer(ep, size, ep->toggle, data_dir, 0, src, 1);
Yunzhi Liaa336092015-06-19 17:09:04 +0800482}
483
484static u32 dwc2_intr_get_timestamp(intr_queue_t *q)
485{
486 hprt_t hprt;
487 hfnum_t hfnum;
488 hci_t *controller = q->endp->dev->controller;
489 dwc_ctrl_t *dwc2 = DWC2_INST(controller);
490 dwc2_reg_t *reg = DWC2_REG(controller);
491
492 hfnum.d32 = readl(&reg->host.hfnum);
493 hprt.d32 = readl(dwc2->hprt0);
494
495 /*
496 * hfnum.frnum increments when a new SOF is transmitted on
497 * the USB, and is reset to 0 when it reaches 16'h3FFF
498 */
499 switch (hprt.prtspd) {
500 case PRTSPD_HIGH:
501 /* 8 micro-frame per ms for high-speed */
502 return hfnum.frnum / 8;
503 case PRTSPD_FULL:
504 case PRTSPD_LOW:
505 default:
506 /* 1 micro-frame per ms for high-speed */
507 return hfnum.frnum / 1;
508 }
509}
510
511/* create and hook-up an intr queue into device schedule */
512static void *
513dwc2_create_intr_queue(endpoint_t *ep, const int reqsize,
514 const int reqcount, const int reqtiming)
515{
516 intr_queue_t *q = (intr_queue_t *)xzalloc(sizeof(intr_queue_t));
517
518 q->data = dma_memalign(4, reqsize);
519 q->endp = ep;
520 q->reqsize = reqsize;
521 q->reqtiming = reqtiming;
522
523 return q;
524}
525
526static void
527dwc2_destroy_intr_queue(endpoint_t *ep, void *_q)
528{
529 intr_queue_t *q = (intr_queue_t *)_q;
530
531 free(q->data);
532 free(q);
533}
534
535/*
536 * read one intr-packet from queue, if available. extend the queue for
537 * new input. Return NULL if nothing new available.
538 * Recommended use: while (data=poll_intr_queue(q)) process(data);
539 */
540static u8 *
541dwc2_poll_intr_queue(void *_q)
542{
543 intr_queue_t *q = (intr_queue_t *)_q;
544 int ret = 0;
545 u32 timestamp = dwc2_intr_get_timestamp(q);
546
547 /*
548 * If hfnum.frnum run overflow it will schedule
549 * an interrupt transfer immediately
550 */
551 if (timestamp - q->timestamp < q->reqtiming)
552 return NULL;
553
554 q->timestamp = timestamp;
555
556 ret = dwc2_intr(q->endp, q->reqsize, q->data);
557
558 if (ret > 0)
559 return q->data;
560 else
561 return NULL;
562}
563
huang lin365250e2014-08-06 16:43:43 +0800564hci_t *dwc2_init(void *bar)
565{
566 hci_t *controller = new_controller();
567 controller->instance = xzalloc(sizeof(dwc_ctrl_t));
568
569 DWC2_INST(controller)->dma_buffer = dma_malloc(DMA_SIZE);
570 if (!DWC2_INST(controller)->dma_buffer) {
571 usb_debug("Not enough DMA memory for DWC2 bounce buffer\n");
572 goto free_dwc2;
573 }
574
575 controller->type = DWC2;
576 controller->start = dummy;
577 controller->stop = dummy;
578 controller->reset = dummy;
579 controller->init = dwc2_reinit;
580 controller->shutdown = dwc2_shutdown;
581 controller->bulk = dwc2_bulk;
582 controller->control = dwc2_control;
583 controller->set_address = generic_set_address;
584 controller->finish_device_config = NULL;
585 controller->destroy_device = NULL;
Yunzhi Liaa336092015-06-19 17:09:04 +0800586 controller->create_intr_queue = dwc2_create_intr_queue;
587 controller->destroy_intr_queue = dwc2_destroy_intr_queue;
588 controller->poll_intr_queue = dwc2_poll_intr_queue;
huang lin365250e2014-08-06 16:43:43 +0800589 controller->reg_base = (uintptr_t)bar;
590 init_device_entry(controller, 0);
591
592 /* Init controller */
593 controller->init(controller);
594
595 /* Setup up root hub */
596 controller->devices[0]->controller = controller;
597 controller->devices[0]->init = dwc2_rh_init;
598 controller->devices[0]->init(controller->devices[0]);
599 return controller;
600
601free_dwc2:
602 detach_controller(controller);
603 free(DWC2_INST(controller));
604 free(controller);
605 return NULL;
606}