3 * Copyright (C) 2014 Rockchip Electronics
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <libpayload.h>
16 #include <arch/cache.h>
19 #include "dwc2_private.h"
21 static void dummy(hci_t
*controller
)
25 static void dwc2_reinit(hci_t
*controller
)
27 dwc2_reg_t
*reg
= DWC2_REG(controller
);
28 gusbcfg_t gusbcfg
= { .d32
= 0 };
29 grstctl_t grstctl
= { .d32
= 0 };
30 gintsts_t gintsts
= { .d32
= 0 };
31 gahbcfg_t gahbcfg
= { .d32
= 0 };
32 grxfsiz_t grxfsiz
= { .d32
= 0 };
33 ghwcfg3_t hwcfg3
= { .d32
= 0 };
34 hcintmsk_t hcintmsk
= { .d32
= 0 };
35 gtxfsiz_t gnptxfsiz
= { .d32
= 0 };
36 gtxfsiz_t hptxfsiz
= { .d32
= 0 };
38 const int timeout
= 10000;
39 int i
, fifo_blocks
, tx_blocks
;
41 /* Wait for AHB idle */
42 for (i
= 0; i
< timeout
; i
++) {
44 grstctl
.d32
= readl(®
->core
.grstctl
);
49 fatal("DWC2 Init error AHB Idle\n");
51 /* Restart the Phy Clock */
52 writel(0x0, ®
->pcgr
.pcgcctl
);
55 writel(grstctl
.d32
, ®
->core
.grstctl
);
56 for (i
= 0; i
< timeout
; i
++) {
58 grstctl
.d32
= readl(®
->core
.grstctl
);
63 fatal("DWC2 Init error reset fail\n");
65 /* Set 16bit PHY if & Force host mode */
66 gusbcfg
.d32
= readl(®
->core
.gusbcfg
);
68 gusbcfg
.forcehstmode
= 1;
69 gusbcfg
.forcedevmode
= 0;
70 writel(gusbcfg
.d32
, ®
->core
.gusbcfg
);
71 /* Wait for force host mode effect, it may takes 100ms */
72 for (i
= 0; i
< timeout
; i
++) {
74 gintsts
.d32
= readl(®
->core
.gintsts
);
79 fatal("DWC2 Init error force host mode fail\n");
83 * The non-periodic tx fifo and rx fifo share one continuous
84 * piece of IP-internal SRAM.
88 * Read total data FIFO depth from HWCFG3
89 * this value is in terms of 32-bit words
91 hwcfg3
.d32
= readl(®
->core
.ghwcfg3
);
93 * Reserve 2 spaces for the status entries of received packets
94 * and 2 spaces for bulk and control OUT endpoints. Calculate how
95 * many blocks can be alloted, assume largest packet size is 512.
96 * 16 locations reserved for periodic TX .
98 fifo_blocks
= (hwcfg3
.dfifodepth
- 4 - 16) / (512 / 4);
99 tx_blocks
= fifo_blocks
/ 2;
101 grxfsiz
.rxfdep
= (fifo_blocks
- tx_blocks
) * (512 / 4) + 4;
102 writel(grxfsiz
.d32
, ®
->core
.grxfsiz
);
103 gnptxfsiz
.txfstaddr
= grxfsiz
.rxfdep
;
104 gnptxfsiz
.txfdep
= tx_blocks
* (512 / 4);
105 writel(gnptxfsiz
.d32
, ®
->core
.gnptxfsiz
);
106 hptxfsiz
.txfstaddr
= gnptxfsiz
.txfstaddr
+ gnptxfsiz
.txfdep
;
107 hptxfsiz
.txfdep
= 16;
108 writel(hptxfsiz
.d32
, ®
->core
.hptxfsiz
);
110 /* Init host channels */
111 hcintmsk
.xfercomp
= 1;
112 hcintmsk
.xacterr
= 1;
116 for (i
= 0; i
< MAX_EPS_CHANNELS
; i
++)
117 writel(hcintmsk
.d32
, ®
->host
.hchn
[i
].hcintmaskn
);
119 /* Unmask interrupt and configure DMA mode */
120 gahbcfg
.glblintrmsk
= 1;
121 gahbcfg
.hbstlen
= DMA_BURST_INCR8
;
123 writel(gahbcfg
.d32
, ®
->core
.gahbcfg
);
125 DWC2_INST(controller
)->hprt0
= ®
->host
.hprt
;
127 usb_debug("DWC2 init finished!\n");
130 static void dwc2_shutdown(hci_t
*controller
)
132 detach_controller(controller
);
133 free(DWC2_INST(controller
)->dma_buffer
);
134 free(DWC2_INST(controller
));
138 /* Test root port device connect status */
139 static int dwc2_disconnected(hci_t
*controller
)
141 dwc2_reg_t
*reg
= DWC2_REG(controller
);
144 hprt
.d32
= readl(®
->host
.hprt
);
145 return !(hprt
.prtena
&& hprt
.prtconnsts
);
148 #define DWC2_SLEEP_TIME_US 5
151 * This function returns the actual transfer length when the transfer succeeded
152 * or an error code if the transfer failed
155 wait_for_complete(endpoint_t
*ep
, uint32_t ch_num
)
160 dwc2_reg_t
*reg
= DWC2_REG(ep
->dev
->controller
);
161 int timeout
= USB_MAX_PROCESSING_TIME_US
/ DWC2_SLEEP_TIME_US
;
164 * TODO: We should take care of up to three times of transfer error
165 * retry here, according to the USB 2.0 spec 4.5.2
168 udelay(DWC2_SLEEP_TIME_US
);
169 hcint
.d32
= readl(®
->host
.hchn
[ch_num
].hcintn
);
170 hctsiz
.d32
= readl(®
->host
.hchn
[ch_num
].hctsizn
);
173 writel(hcint
.d32
, ®
->host
.hchn
[ch_num
].hcintn
);
174 if (hcint
.xfercomp
|| hcint
.ack
)
175 return hctsiz
.xfersize
;
176 else if (hcint
.nak
|| hcint
.frmovrun
)
178 else if (hcint
.xacterr
)
179 return -HCSTAT_XFERERR
;
180 else if (hcint
.bblerr
)
181 return -HCSTAT_BABBLE
;
182 else if (hcint
.stall
)
183 return -HCSTAT_STALL
;
187 return -HCSTAT_UNKNOW
;
190 if (dwc2_disconnected(ep
->dev
->controller
))
191 return -HCSTAT_DISCONNECTED
;
193 /* Release the channel when hit timeout condition */
194 hcchar
.d32
= readl(®
->host
.hchn
[ch_num
].hccharn
);
197 * Programming the HCCHARn register with the chdis and
198 * chena bits set to 1 at the same time to disable the
199 * channel and the core will generate a channel halted
203 writel(hcchar
.d32
, ®
->host
.hchn
[ch_num
].hccharn
);
205 hcchar
.d32
= readl(®
->host
.hchn
[ch_num
].hccharn
);
206 } while (hcchar
.chen
);
210 /* Clear all pending interrupt flags */
212 writel(hcint
.d32
, ®
->host
.hchn
[ch_num
].hcintn
);
214 return -HCSTAT_TIMEOUT
;
218 dwc2_do_xfer(endpoint_t
*ep
, int size
, int pid
, ep_dir_t dir
,
219 uint32_t ch_num
, u8
*data_buf
, int *short_pkt
)
224 uint32_t packet_size
;
225 uint32_t transferred
= 0;
226 uint32_t inpkt_length
;
227 hctsiz_t hctsiz
= { .d32
= 0 };
228 hcchar_t hcchar
= { .d32
= 0 };
230 dwc2_reg_t
*reg
= DWC2_REG(ep
->dev
->controller
);
232 packet_size
= ep
->maxpacketsize
;
233 packet_cnt
= ALIGN_UP(size
, packet_size
) / packet_size
;
234 inpkt_length
= packet_cnt
* packet_size
;
235 /* At least 1 packet should be programmed */
236 packet_cnt
= (packet_cnt
== 0) ? 1 : packet_cnt
;
239 * For an IN, this field is the buffer size that the application has
240 * reserved for the transfer. The application should program this field
241 * as integer multiple of the maximum packet size for IN transactions.
243 hctsiz
.xfersize
= (dir
== EPDIR_OUT
) ? size
: inpkt_length
;
244 hctsiz
.pktcnt
= packet_cnt
;
247 hcchar
.mps
= packet_size
;
248 hcchar
.epnum
= ep
->endpoint
& 0xf;
250 hcchar
.eptype
= ep
->type
;
252 hcchar
.devaddr
= ep
->dev
->address
;
255 if (ep
->dev
->speed
== LOW_SPEED
)
258 if (size
> DMA_SIZE
) {
259 usb_debug("Transfer too large: %d\n", size
);
264 * Check the buffer address which should be 4-byte aligned and DMA
267 do_copy
= !dma_coherent(data_buf
) || ((uintptr_t)data_buf
& 0x3);
268 aligned_buf
= do_copy
? DWC2_INST(ep
->dev
->controller
)->dma_buffer
:
271 if (do_copy
&& (dir
== EPDIR_OUT
))
272 memcpy(aligned_buf
, data_buf
, size
);
274 if (dwc2_disconnected(ep
->dev
->controller
))
275 return -HCSTAT_DISCONNECTED
;
277 writel(hctsiz
.d32
, ®
->host
.hchn
[ch_num
].hctsizn
);
278 writel((uint32_t)virt_to_bus(aligned_buf
),
279 ®
->host
.hchn
[ch_num
].hcdman
);
280 writel(hcchar
.d32
, ®
->host
.hchn
[ch_num
].hccharn
);
282 ret
= wait_for_complete(ep
, ch_num
);
285 /* Calculate actual transferred length */
286 transferred
= (dir
== EPDIR_IN
) ? inpkt_length
- ret
: size
;
288 if (do_copy
&& (dir
== EPDIR_IN
))
289 memcpy(data_buf
, aligned_buf
, transferred
);
291 if ((short_pkt
!= NULL
) && (dir
== EPDIR_IN
))
292 *short_pkt
= (ret
> 0) ? 1 : 0;
296 /* Save data toggle */
297 hctsiz
.d32
= readl(®
->host
.hchn
[ch_num
].hctsizn
);
298 ep
->toggle
= hctsiz
.pid
;
301 usb_debug("%s Transfer stop code: %x\n", __func__
, ret
);
308 dwc2_split_transfer(endpoint_t
*ep
, int size
, int pid
, ep_dir_t dir
,
309 uint32_t ch_num
, u8
*data_buf
, split_info_t
*split
,
312 dwc2_reg_t
*reg
= DWC2_REG(ep
->dev
->controller
);
314 hcsplit_t hcsplit
= { .d32
= 0 };
315 int ret
, transferred
= 0;
317 hcsplit
.hubaddr
= split
->hubaddr
;
318 hcsplit
.prtaddr
= split
->hubport
;
320 writel(hcsplit
.d32
, ®
->host
.hchn
[ch_num
].hcspltn
);
322 /* Wait for next frame boundary */
324 hfnum
.d32
= readl(®
->host
.hfnum
);
326 if (dwc2_disconnected(ep
->dev
->controller
)) {
327 ret
= -HCSTAT_DISCONNECTED
;
330 } while (hfnum
.frnum
% 8 != 0);
332 /* Handle Start-Split */
333 ret
= dwc2_do_xfer(ep
, dir
== EPDIR_IN
? 0 : size
, pid
, dir
, ch_num
,
339 hcsplit
.compsplt
= 1;
340 writel(hcsplit
.d32
, ®
->host
.hchn
[ch_num
].hcspltn
);
343 if (dir
== EPDIR_OUT
)
346 /* Handle Complete-Split */
348 ret
= dwc2_do_xfer(ep
, dir
== EPDIR_OUT
? 0 : size
, ep
->toggle
,
349 dir
, ch_num
, data_buf
, short_pkt
);
350 } while (ret
== -HCSTAT_NYET
);
356 /* Clear hcsplit reg */
358 hcsplit
.compsplt
= 0;
359 writel(hcsplit
.d32
, ®
->host
.hchn
[ch_num
].hcspltn
);
367 static int dwc2_need_split(usbdev_t
*dev
, split_info_t
*split
)
369 if (dev
->speed
== HIGH_SPEED
)
372 if (closest_usb2_hub(dev
, &split
->hubaddr
, &split
->hubport
))
378 #define USB_FULL_LOW_SPEED_FRAME_US 1000
381 dwc2_transfer(endpoint_t
*ep
, int size
, int pid
, ep_dir_t dir
, uint32_t ch_num
,
382 u8
*src
, uint8_t skip_nak
)
385 int ret
, short_pkt
, transferred
= 0;
386 int timeout
= USB_MAX_PROCESSING_TIME_US
/ USB_FULL_LOW_SPEED_FRAME_US
;
392 if (dwc2_need_split(ep
->dev
, &split
)) {
394 ret
= dwc2_split_transfer(ep
, MIN(ep
->maxpacketsize
,
395 size
), ep
->toggle
, dir
, 0, src
, &split
,
399 * dwc2_split_transfer() waits for the next FullSpeed
400 * frame boundary, so we only need to delay 500 us
401 * here to have one try per millisecond.
403 if (ret
== -HCSTAT_NAK
&& !skip_nak
&& --timeout
) {
404 udelay(USB_FULL_LOW_SPEED_FRAME_US
/ 2);
408 ret
= dwc2_do_xfer(ep
, MIN(DMA_SIZE
, size
), pid
, dir
, 0,
419 } while (size
> 0 && !short_pkt
);
425 dwc2_bulk(endpoint_t
*ep
, int size
, u8
*src
, int finalize
)
429 if (ep
->direction
== IN
)
431 else if (ep
->direction
== OUT
)
432 data_dir
= EPDIR_OUT
;
436 return dwc2_transfer(ep
, size
, ep
->toggle
, data_dir
, 0, src
, 0);
440 dwc2_control(usbdev_t
*dev
, direction_t dir
, int drlen
, void *setup
,
445 endpoint_t
*ep
= &dev
->endpoints
[0];
450 data_dir
= EPDIR_OUT
;
455 if (dwc2_transfer(ep
, drlen
, PID_SETUP
, EPDIR_OUT
, 0, setup
, 0) < 0)
459 ep
->toggle
= PID_DATA1
;
461 ret
= dwc2_transfer(ep
, dalen
, ep
->toggle
, data_dir
, 0, src
, 0);
467 if (dwc2_transfer(ep
, 0, PID_DATA1
, !data_dir
, 0, NULL
, 0) < 0)
474 dwc2_intr(endpoint_t
*ep
, int size
, u8
*src
)
478 if (ep
->direction
== IN
)
480 else if (ep
->direction
== OUT
)
481 data_dir
= EPDIR_OUT
;
485 return dwc2_transfer(ep
, size
, ep
->toggle
, data_dir
, 0, src
, 1);
488 static u32
dwc2_intr_get_timestamp(intr_queue_t
*q
)
492 hci_t
*controller
= q
->endp
->dev
->controller
;
493 dwc_ctrl_t
*dwc2
= DWC2_INST(controller
);
494 dwc2_reg_t
*reg
= DWC2_REG(controller
);
496 hfnum
.d32
= readl(®
->host
.hfnum
);
497 hprt
.d32
= readl(dwc2
->hprt0
);
500 * hfnum.frnum increments when a new SOF is transmitted on
501 * the USB, and is reset to 0 when it reaches 16'h3FFF
503 switch (hprt
.prtspd
) {
505 /* 8 micro-frame per ms for high-speed */
506 return hfnum
.frnum
/ 8;
510 /* 1 micro-frame per ms for high-speed */
511 return hfnum
.frnum
/ 1;
515 /* create and hook-up an intr queue into device schedule */
517 dwc2_create_intr_queue(endpoint_t
*ep
, const int reqsize
,
518 const int reqcount
, const int reqtiming
)
520 intr_queue_t
*q
= (intr_queue_t
*)xzalloc(sizeof(intr_queue_t
));
522 q
->data
= dma_memalign(4, reqsize
);
524 q
->reqsize
= reqsize
;
525 q
->reqtiming
= reqtiming
;
531 dwc2_destroy_intr_queue(endpoint_t
*ep
, void *_q
)
533 intr_queue_t
*q
= (intr_queue_t
*)_q
;
540 * read one intr-packet from queue, if available. extend the queue for
541 * new input. Return NULL if nothing new available.
542 * Recommended use: while (data=poll_intr_queue(q)) process(data);
545 dwc2_poll_intr_queue(void *_q
)
547 intr_queue_t
*q
= (intr_queue_t
*)_q
;
549 u32 timestamp
= dwc2_intr_get_timestamp(q
);
552 * If hfnum.frnum run overflow it will schedule
553 * an interrupt transfer immediately
555 if (timestamp
- q
->timestamp
< q
->reqtiming
)
558 q
->timestamp
= timestamp
;
560 ret
= dwc2_intr(q
->endp
, q
->reqsize
, q
->data
);
568 hci_t
*dwc2_init(void *bar
)
570 hci_t
*controller
= new_controller();
571 controller
->instance
= xzalloc(sizeof(dwc_ctrl_t
));
573 DWC2_INST(controller
)->dma_buffer
= dma_malloc(DMA_SIZE
);
574 if (!DWC2_INST(controller
)->dma_buffer
) {
575 usb_debug("Not enough DMA memory for DWC2 bounce buffer\n");
579 controller
->type
= DWC2
;
580 controller
->start
= dummy
;
581 controller
->stop
= dummy
;
582 controller
->reset
= dummy
;
583 controller
->init
= dwc2_reinit
;
584 controller
->shutdown
= dwc2_shutdown
;
585 controller
->bulk
= dwc2_bulk
;
586 controller
->control
= dwc2_control
;
587 controller
->set_address
= generic_set_address
;
588 controller
->finish_device_config
= NULL
;
589 controller
->destroy_device
= NULL
;
590 controller
->create_intr_queue
= dwc2_create_intr_queue
;
591 controller
->destroy_intr_queue
= dwc2_destroy_intr_queue
;
592 controller
->poll_intr_queue
= dwc2_poll_intr_queue
;
593 controller
->reg_base
= (uintptr_t)bar
;
594 init_device_entry(controller
, 0);
596 /* Init controller */
597 controller
->init(controller
);
599 /* Setup up root hub */
600 controller
->devices
[0]->controller
= controller
;
601 controller
->devices
[0]->init
= dwc2_rh_init
;
602 controller
->devices
[0]->init(controller
->devices
[0]);
606 detach_controller(controller
);
607 free(DWC2_INST(controller
));