blob: a3979e51acdda85c5086b77a00092480684c2619 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
huang lin365250e2014-08-06 16:43:43 +080018 */
19
20#include <libpayload.h>
21#include <arch/cache.h>
22
23#include "dwc2.h"
24#include "dwc2_private.h"
25
26static void dummy(hci_t *controller)
27{
28}
29
30static void dwc2_reinit(hci_t *controller)
31{
32 dwc2_reg_t *reg = DWC2_REG(controller);
33 gusbcfg_t gusbcfg = { .d32 = 0 };
34 grstctl_t grstctl = { .d32 = 0 };
35 gintsts_t gintsts = { .d32 = 0 };
36 gahbcfg_t gahbcfg = { .d32 = 0 };
37 grxfsiz_t grxfsiz = { .d32 = 0 };
huang lin8b52c932014-12-04 18:25:47 +080038 ghwcfg3_t hwcfg3 = { .d32 = 0 };
huang lin365250e2014-08-06 16:43:43 +080039 hcintmsk_t hcintmsk = { .d32 = 0 };
Yunzhi Liaa336092015-06-19 17:09:04 +080040 gtxfsiz_t gnptxfsiz = { .d32 = 0 };
41 gtxfsiz_t hptxfsiz = { .d32 = 0 };
huang lin365250e2014-08-06 16:43:43 +080042
43 const int timeout = 10000;
huang lin8b52c932014-12-04 18:25:47 +080044 int i, fifo_blocks, tx_blocks;
huang lin365250e2014-08-06 16:43:43 +080045
46 /* Wait for AHB idle */
47 for (i = 0; i < timeout; i++) {
48 udelay(1);
49 grstctl.d32 = readl(&reg->core.grstctl);
50 if (grstctl.ahbidle)
51 break;
52 }
53 if (i == timeout)
54 fatal("DWC2 Init error AHB Idle\n");
55
56 /* Restart the Phy Clock */
57 writel(0x0, &reg->pcgr.pcgcctl);
58 /* Core soft reset */
59 grstctl.csftrst = 1;
60 writel(grstctl.d32, &reg->core.grstctl);
61 for (i = 0; i < timeout; i++) {
62 udelay(1);
63 grstctl.d32 = readl(&reg->core.grstctl);
64 if (!grstctl.csftrst)
65 break;
66 }
67 if (i == timeout)
68 fatal("DWC2 Init error reset fail\n");
69
70 /* Set 16bit PHY if & Force host mode */
71 gusbcfg.d32 = readl(&reg->core.gusbcfg);
72 gusbcfg.phyif = 1;
73 gusbcfg.forcehstmode = 1;
74 gusbcfg.forcedevmode = 0;
75 writel(gusbcfg.d32, &reg->core.gusbcfg);
76 /* Wait for force host mode effect, it may takes 100ms */
77 for (i = 0; i < timeout; i++) {
78 udelay(10);
79 gintsts.d32 = readl(&reg->core.gintsts);
80 if (gintsts.curmod)
81 break;
82 }
83 if (i == timeout)
84 fatal("DWC2 Init error force host mode fail\n");
85
86 /*
87 * Config FIFO
88 * The non-periodic tx fifo and rx fifo share one continuous
89 * piece of IP-internal SRAM.
90 */
huang lin8b52c932014-12-04 18:25:47 +080091
92 /*
93 * Read total data FIFO depth from HWCFG3
94 * this value is in terms of 32-bit words
95 */
96 hwcfg3.d32 = readl(&reg->core.ghwcfg3);
97 /*
98 * Reserve 2 spaces for the status entries of received packets
99 * and 2 spaces for bulk and control OUT endpoints. Calculate how
100 * many blocks can be alloted, assume largest packet size is 512.
Yunzhi Liaa336092015-06-19 17:09:04 +0800101 * 16 locations reserved for periodic TX .
huang lin8b52c932014-12-04 18:25:47 +0800102 */
Yunzhi Liaa336092015-06-19 17:09:04 +0800103 fifo_blocks = (hwcfg3.dfifodepth - 4 - 16) / (512 / 4);
huang lin8b52c932014-12-04 18:25:47 +0800104 tx_blocks = fifo_blocks / 2;
105
106 grxfsiz.rxfdep = (fifo_blocks - tx_blocks) * (512 / 4) + 4;
huang lin365250e2014-08-06 16:43:43 +0800107 writel(grxfsiz.d32, &reg->core.grxfsiz);
Yunzhi Liaa336092015-06-19 17:09:04 +0800108 gnptxfsiz.txfstaddr = grxfsiz.rxfdep;
109 gnptxfsiz.txfdep = tx_blocks * (512 / 4);
huang lin365250e2014-08-06 16:43:43 +0800110 writel(gnptxfsiz.d32, &reg->core.gnptxfsiz);
Yunzhi Liaa336092015-06-19 17:09:04 +0800111 hptxfsiz.txfstaddr = gnptxfsiz.txfstaddr + gnptxfsiz.txfdep;
112 hptxfsiz.txfdep = 16;
113 writel(hptxfsiz.d32, &reg->core.hptxfsiz);
huang lin365250e2014-08-06 16:43:43 +0800114
115 /* Init host channels */
116 hcintmsk.xfercomp = 1;
117 hcintmsk.xacterr = 1;
118 hcintmsk.stall = 1;
119 hcintmsk.chhltd = 1;
120 hcintmsk.bblerr = 1;
121 for (i = 0; i < MAX_EPS_CHANNELS; i++)
122 writel(hcintmsk.d32, &reg->host.hchn[i].hcintmaskn);
123
124 /* Unmask interrupt and configure DMA mode */
125 gahbcfg.glblintrmsk = 1;
126 gahbcfg.hbstlen = DMA_BURST_INCR8;
127 gahbcfg.dmaen = 1;
128 writel(gahbcfg.d32, &reg->core.gahbcfg);
129
130 DWC2_INST(controller)->hprt0 = &reg->host.hprt;
131
132 usb_debug("DWC2 init finished!\n");
133}
134
135static void dwc2_shutdown(hci_t *controller)
136{
137 detach_controller(controller);
138 free(DWC2_INST(controller)->dma_buffer);
139 free(DWC2_INST(controller));
140 free(controller);
141}
142
143/*
144 * This function returns the actual transfer length when the transfer succeeded
145 * or an error code if the transfer failed
146 */
147static int
148wait_for_complete(endpoint_t *ep, uint32_t ch_num)
149{
150 hcint_t hcint;
151 hcchar_t hcchar;
152 hctsiz_t hctsiz;
153 dwc2_reg_t *reg = DWC2_REG(ep->dev->controller);
154 int timeout = 600000; /* time out after 600000 * 5us == 3s */
155
156 /*
157 * TODO: We should take care of up to three times of transfer error
158 * retry here, according to the USB 2.0 spec 4.5.2
159 */
160 do {
161 udelay(5);
162 hcint.d32 = readl(&reg->host.hchn[ch_num].hcintn);
163 hctsiz.d32 = readl(&reg->host.hchn[ch_num].hctsizn);
164
165 if (hcint.chhltd) {
166 writel(hcint.d32, &reg->host.hchn[ch_num].hcintn);
Yunzhi Liebd3da72015-07-02 15:28:11 +0800167 if (hcint.xfercomp || hcint.ack)
huang lin365250e2014-08-06 16:43:43 +0800168 return hctsiz.xfersize;
Yunzhi Liaa336092015-06-19 17:09:04 +0800169 else if (hcint.nak || hcint.frmovrun)
Yunzhi Liebd3da72015-07-02 15:28:11 +0800170 return -HCSTAT_NAK;
huang lin365250e2014-08-06 16:43:43 +0800171 else if (hcint.xacterr)
172 return -HCSTAT_XFERERR;
173 else if (hcint.bblerr)
174 return -HCSTAT_BABBLE;
175 else if (hcint.stall)
176 return -HCSTAT_STALL;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800177 else if (hcint.ack)
178 return -HCSTAT_ACK;
179 else if (hcint.nyet)
180 return -HCSTAT_NYET;
huang lin365250e2014-08-06 16:43:43 +0800181 else
182 return -HCSTAT_UNKNOW;
183 }
184 } while (timeout--);
185
186 /* Release the channel on timeout */
187 hcchar.d32 = readl(&reg->host.hchn[ch_num].hccharn);
188 if (hcchar.chen) {
189 /*
190 * Programming the HCCHARn register with the chdis and
191 * chena bits set to 1 at the same time to disable the
192 * channel and the core will generate a channel halted
193 * interrupt.
194 */
195 hcchar.chdis = 1;
196 writel(hcchar.d32, &reg->host.hchn[ch_num].hccharn);
197 do {
198 hcchar.d32 = readl(&reg->host.hchn[ch_num].hccharn);
199 } while (hcchar.chen);
200
201 }
202
203 /* Clear all pending interrupt flags */
204 hcint.d32 = ~0;
205 writel(hcint.d32, &reg->host.hchn[ch_num].hcintn);
206
207 return -HCSTAT_TIMEOUT;
208}
209
210static int
Yunzhi Liebd3da72015-07-02 15:28:11 +0800211dwc2_do_xfer(endpoint_t *ep, int size, int pid, ep_dir_t dir,
huang lin365250e2014-08-06 16:43:43 +0800212 uint32_t ch_num, u8 *data_buf)
213{
214 uint32_t do_copy;
215 int ret;
216 uint32_t packet_cnt;
217 uint32_t packet_size;
218 uint32_t transferred = 0;
219 uint32_t inpkt_length;
220 hctsiz_t hctsiz = { .d32 = 0 };
221 hcchar_t hcchar = { .d32 = 0 };
222 void *aligned_buf;
223 dwc2_reg_t *reg = DWC2_REG(ep->dev->controller);
224
225 packet_size = ep->maxpacketsize;
226 packet_cnt = ALIGN_UP(size, packet_size) / packet_size;
227 inpkt_length = packet_cnt * packet_size;
228 /* At least 1 packet should be programed */
229 packet_cnt = (packet_cnt == 0) ? 1 : packet_cnt;
230
231 /*
232 * For an IN, this field is the buffer size that the application has
233 * reserved for the transfer. The application should program this field
234 * as integer multiple of the maximum packet size for IN transactions.
235 */
236 hctsiz.xfersize = (dir == EPDIR_OUT) ? size : inpkt_length;
237 hctsiz.pktcnt = packet_cnt;
238 hctsiz.pid = pid;
239
240 hcchar.mps = packet_size;
241 hcchar.epnum = ep->endpoint & 0xf;
242 hcchar.epdir = dir;
243 hcchar.eptype = ep->type;
244 hcchar.multicnt = 1;
245 hcchar.devaddr = ep->dev->address;
246 hcchar.chdis = 0;
247 hcchar.chen = 1;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800248 if (ep->dev->speed == LOW_SPEED)
249 hcchar.lspddev = 1;
huang lin365250e2014-08-06 16:43:43 +0800250
251 if (size > DMA_SIZE) {
252 usb_debug("Transfer too large: %d\n", size);
253 return -1;
254 }
255
256 /*
257 * Check the buffer address which should be 4-byte aligned and DMA
258 * coherent
259 */
260 do_copy = !dma_coherent(data_buf) || ((uintptr_t)data_buf & 0x3);
261 aligned_buf = do_copy ? DWC2_INST(ep->dev->controller)->dma_buffer :
262 data_buf;
263
264 if (do_copy && (dir == EPDIR_OUT))
265 memcpy(aligned_buf, data_buf, size);
266
267 writel(hctsiz.d32, &reg->host.hchn[ch_num].hctsizn);
Ionela Voinescufa143852015-02-02 15:35:44 +0000268 writel((uint32_t)virt_to_bus(aligned_buf),
269 &reg->host.hchn[ch_num].hcdman);
huang lin365250e2014-08-06 16:43:43 +0800270 writel(hcchar.d32, &reg->host.hchn[ch_num].hccharn);
271
272 ret = wait_for_complete(ep, ch_num);
273
274 if (ret >= 0) {
275 /* Calculate actual transferred length */
276 transferred = (dir == EPDIR_IN) ? inpkt_length - ret : ret;
277
278 if (do_copy && (dir == EPDIR_IN))
279 memcpy(data_buf, aligned_buf, transferred);
280 }
281
282 /* Save data toggle */
283 hctsiz.d32 = readl(&reg->host.hchn[ch_num].hctsizn);
284 ep->toggle = hctsiz.pid;
285
286 if (ret < 0) {
287 usb_debug("%s Transfer stop code: %x\n", __func__, ret);
288 return ret;
289 }
290 return transferred;
291}
292
293static int
Yunzhi Liebd3da72015-07-02 15:28:11 +0800294dwc2_split_transfer(endpoint_t *ep, int size, int pid, ep_dir_t dir,
295 uint32_t ch_num, u8 *data_buf, split_info_t *split)
296{
297 dwc2_reg_t *reg = DWC2_REG(ep->dev->controller);
298 hfnum_t hfnum;
299 hcsplit_t hcsplit = { .d32 = 0 };
300 int ret, transferred = 0;
301
302 hcsplit.hubaddr = split->hubaddr;
303 hcsplit.prtaddr = split->hubport;
304 hcsplit.spltena = 1;
305 writel(hcsplit.d32, &reg->host.hchn[ch_num].hcspltn);
306
307 /* Wait for next frame boundary */
308 do {
309 hfnum.d32 = readl(&reg->host.hfnum);
310 } while (hfnum.frnum % 8 != 0);
311
312 /* Handle Start-Split */
313 ret = dwc2_do_xfer(ep, dir == EPDIR_IN ? 0 : size, pid, dir, ch_num,
314 data_buf);
315 if (ret < 0)
316 goto out;
317
318 hcsplit.spltena = 1;
319 hcsplit.compsplt = 1;
320 writel(hcsplit.d32, &reg->host.hchn[ch_num].hcspltn);
321 ep->toggle = pid;
322
323 if (dir == EPDIR_OUT)
324 transferred += ret;
325
326 /* Handle Complete-Split */
327 do {
328 ret = dwc2_do_xfer(ep, dir == EPDIR_OUT ? 0 : size, ep->toggle,
329 dir, ch_num, data_buf);
330 } while (ret == -HCSTAT_NYET);
331
332 if (dir == EPDIR_IN)
333 transferred += ret;
334
335out:
336 /* Clear hcsplit reg */
337 hcsplit.spltena = 0;
338 hcsplit.compsplt = 0;
339 writel(hcsplit.d32, &reg->host.hchn[ch_num].hcspltn);
340
341 if (ret < 0)
342 return ret;
343
344 return transferred;
345}
346
347static int dwc2_need_split(usbdev_t *dev, split_info_t *split)
348{
349 if (dev->speed == HIGH_SPEED)
350 return 0;
351
352 if (closest_usb2_hub(dev, &split->hubaddr, &split->hubport))
353 return 0;
354
355 return 1;
356}
357
358static int
359dwc2_transfer(endpoint_t *ep, int size, int pid, ep_dir_t dir, uint32_t ch_num,
360 u8 *src, uint8_t skip_nak)
361{
362 split_info_t split;
363 int ret, transferred = 0, timeout = 3000;
364
365 ep->toggle = pid;
366
367 do {
368 if (dwc2_need_split(ep->dev, &split)) {
369nak_retry:
370 ret = dwc2_split_transfer(ep, size, ep->toggle, dir, 0,
371 src, &split);
372
373 /*
374 * dwc2_split_transfer() waits for the next FullSpeed
375 * frame boundary, so we have one try per millisecond.
376 * It's 3s timeout for each split transfer.
377 */
378 if (ret == -HCSTAT_NAK && !skip_nak && --timeout) {
379 udelay(500);
380 goto nak_retry;
381 }
382 } else {
383 ret = dwc2_do_xfer(ep, size, pid, dir, 0, src);
384 }
385
386 if (ret < 0)
387 return ret;
388
389 size -= ret;
390 src += ret;
391 transferred += ret;
392 } while (size > 0);
393
394 return transferred;
395}
396
397static int
huang lin365250e2014-08-06 16:43:43 +0800398dwc2_bulk(endpoint_t *ep, int size, u8 *src, int finalize)
399{
400 ep_dir_t data_dir;
401
402 if (ep->direction == IN)
403 data_dir = EPDIR_IN;
404 else if (ep->direction == OUT)
405 data_dir = EPDIR_OUT;
406 else
407 return -1;
408
Yunzhi Liebd3da72015-07-02 15:28:11 +0800409 return dwc2_transfer(ep, size, ep->toggle, data_dir, 0, src, 0);
huang lin365250e2014-08-06 16:43:43 +0800410}
411
412static int
413dwc2_control(usbdev_t *dev, direction_t dir, int drlen, void *setup,
414 int dalen, u8 *src)
415{
416 int ret = 0;
huang lin365250e2014-08-06 16:43:43 +0800417 ep_dir_t data_dir;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800418 endpoint_t *ep = &dev->endpoints[0];
huang lin365250e2014-08-06 16:43:43 +0800419
420 if (dir == IN)
421 data_dir = EPDIR_IN;
422 else if (dir == OUT)
423 data_dir = EPDIR_OUT;
424 else
425 return -1;
426
427 /* Setup Phase */
Yunzhi Liebd3da72015-07-02 15:28:11 +0800428 if (dwc2_transfer(ep, drlen, PID_SETUP, EPDIR_OUT, 0, setup, 0) < 0)
huang lin365250e2014-08-06 16:43:43 +0800429 return -1;
Yunzhi Liebd3da72015-07-02 15:28:11 +0800430
huang lin365250e2014-08-06 16:43:43 +0800431 /* Data Phase */
Yunzhi Liebd3da72015-07-02 15:28:11 +0800432 ep->toggle = PID_DATA1;
huang lin365250e2014-08-06 16:43:43 +0800433 if (dalen > 0) {
Yunzhi Liebd3da72015-07-02 15:28:11 +0800434 ret = dwc2_transfer(ep, dalen, ep->toggle, data_dir, 0, src, 0);
huang lin365250e2014-08-06 16:43:43 +0800435 if (ret < 0)
436 return -1;
437 }
Yunzhi Liebd3da72015-07-02 15:28:11 +0800438
huang lin365250e2014-08-06 16:43:43 +0800439 /* Status Phase */
Yunzhi Liebd3da72015-07-02 15:28:11 +0800440 if (dwc2_transfer(ep, 0, PID_DATA1, !data_dir, 0, NULL, 0) < 0)
huang lin365250e2014-08-06 16:43:43 +0800441 return -1;
442
443 return ret;
444}
445
Yunzhi Liaa336092015-06-19 17:09:04 +0800446static int
447dwc2_intr(endpoint_t *ep, int size, u8 *src)
448{
449 ep_dir_t data_dir;
450
451 if (ep->direction == IN)
452 data_dir = EPDIR_IN;
453 else if (ep->direction == OUT)
454 data_dir = EPDIR_OUT;
455 else
456 return -1;
457
Yunzhi Liebd3da72015-07-02 15:28:11 +0800458 return dwc2_transfer(ep, size, ep->toggle, data_dir, 0, src, 1);
Yunzhi Liaa336092015-06-19 17:09:04 +0800459}
460
461static u32 dwc2_intr_get_timestamp(intr_queue_t *q)
462{
463 hprt_t hprt;
464 hfnum_t hfnum;
465 hci_t *controller = q->endp->dev->controller;
466 dwc_ctrl_t *dwc2 = DWC2_INST(controller);
467 dwc2_reg_t *reg = DWC2_REG(controller);
468
469 hfnum.d32 = readl(&reg->host.hfnum);
470 hprt.d32 = readl(dwc2->hprt0);
471
472 /*
473 * hfnum.frnum increments when a new SOF is transmitted on
474 * the USB, and is reset to 0 when it reaches 16'h3FFF
475 */
476 switch (hprt.prtspd) {
477 case PRTSPD_HIGH:
478 /* 8 micro-frame per ms for high-speed */
479 return hfnum.frnum / 8;
480 case PRTSPD_FULL:
481 case PRTSPD_LOW:
482 default:
483 /* 1 micro-frame per ms for high-speed */
484 return hfnum.frnum / 1;
485 }
486}
487
488/* create and hook-up an intr queue into device schedule */
489static void *
490dwc2_create_intr_queue(endpoint_t *ep, const int reqsize,
491 const int reqcount, const int reqtiming)
492{
493 intr_queue_t *q = (intr_queue_t *)xzalloc(sizeof(intr_queue_t));
494
495 q->data = dma_memalign(4, reqsize);
496 q->endp = ep;
497 q->reqsize = reqsize;
498 q->reqtiming = reqtiming;
499
500 return q;
501}
502
503static void
504dwc2_destroy_intr_queue(endpoint_t *ep, void *_q)
505{
506 intr_queue_t *q = (intr_queue_t *)_q;
507
508 free(q->data);
509 free(q);
510}
511
512/*
513 * read one intr-packet from queue, if available. extend the queue for
514 * new input. Return NULL if nothing new available.
515 * Recommended use: while (data=poll_intr_queue(q)) process(data);
516 */
517static u8 *
518dwc2_poll_intr_queue(void *_q)
519{
520 intr_queue_t *q = (intr_queue_t *)_q;
521 int ret = 0;
522 u32 timestamp = dwc2_intr_get_timestamp(q);
523
524 /*
525 * If hfnum.frnum run overflow it will schedule
526 * an interrupt transfer immediately
527 */
528 if (timestamp - q->timestamp < q->reqtiming)
529 return NULL;
530
531 q->timestamp = timestamp;
532
533 ret = dwc2_intr(q->endp, q->reqsize, q->data);
534
535 if (ret > 0)
536 return q->data;
537 else
538 return NULL;
539}
540
huang lin365250e2014-08-06 16:43:43 +0800541hci_t *dwc2_init(void *bar)
542{
543 hci_t *controller = new_controller();
544 controller->instance = xzalloc(sizeof(dwc_ctrl_t));
545
546 DWC2_INST(controller)->dma_buffer = dma_malloc(DMA_SIZE);
547 if (!DWC2_INST(controller)->dma_buffer) {
548 usb_debug("Not enough DMA memory for DWC2 bounce buffer\n");
549 goto free_dwc2;
550 }
551
552 controller->type = DWC2;
553 controller->start = dummy;
554 controller->stop = dummy;
555 controller->reset = dummy;
556 controller->init = dwc2_reinit;
557 controller->shutdown = dwc2_shutdown;
558 controller->bulk = dwc2_bulk;
559 controller->control = dwc2_control;
560 controller->set_address = generic_set_address;
561 controller->finish_device_config = NULL;
562 controller->destroy_device = NULL;
Yunzhi Liaa336092015-06-19 17:09:04 +0800563 controller->create_intr_queue = dwc2_create_intr_queue;
564 controller->destroy_intr_queue = dwc2_destroy_intr_queue;
565 controller->poll_intr_queue = dwc2_poll_intr_queue;
huang lin365250e2014-08-06 16:43:43 +0800566 controller->reg_base = (uintptr_t)bar;
567 init_device_entry(controller, 0);
568
569 /* Init controller */
570 controller->init(controller);
571
572 /* Setup up root hub */
573 controller->devices[0]->controller = controller;
574 controller->devices[0]->init = dwc2_rh_init;
575 controller->devices[0]->init(controller->devices[0]);
576 return controller;
577
578free_dwc2:
579 detach_controller(controller);
580 free(DWC2_INST(controller));
581 free(controller);
582 return NULL;
583}