blob: 26f766653971beeb849e1276209b2856d9bf4650 [file] [log] [blame]
Patrick Georgi6615ef32010-08-13 09:18:58 +00001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2010 Patrick Georgi
Nico Huber90292652013-06-13 14:37:15 +02005 * Copyright (C) 2013 secunet Security Networks AG
Patrick Georgi6615ef32010-08-13 09:18:58 +00006 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#ifndef __XHCI_PRIVATE_H
32#define __XHCI_PRIVATE_H
33
Nico Huber90292652013-06-13 14:37:15 +020034//#define USB_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +000035#include <usb/usb.h>
Yidi Lind42ee152015-05-07 15:36:04 +080036#include <arch/barrier.h>
37#include <kconfig.h>
Patrick Georgi6615ef32010-08-13 09:18:58 +000038
Nico Huber90292652013-06-13 14:37:15 +020039//#define XHCI_DUMPS
40#define xhci_debug(fmt, args...) usb_debug("%s: " fmt, __func__, ## args)
41#ifdef XHCI_SPEW_DEBUG
42# define xhci_spew(fmt, args...) xhci_debug(fmt, ##args)
43#else
44# define xhci_spew(fmt, args...) do {} while(0)
45#endif
46
Patrick Georgi6615ef32010-08-13 09:18:58 +000047#define MASK(startbit, lenbit) (((1<<(lenbit))-1)<<(startbit))
48
Nico Huber90292652013-06-13 14:37:15 +020049#define TIMEOUT -1
50#define CONTROLLER_ERROR -2
51#define COMMUNICATION_ERROR -3
52#define OUT_OF_MEMORY -4
53#define DRIVER_ERROR -5
Patrick Georgi6615ef32010-08-13 09:18:58 +000054
Nico Huber90292652013-06-13 14:37:15 +020055#define CC_SUCCESS 1
56#define CC_TRB_ERROR 5
57#define CC_STALL_ERROR 6
Julius Wernere44a4e82015-07-08 22:36:00 -070058#define CC_RESOURCE_ERROR 7
59#define CC_BANDWIDTH_ERROR 8
60#define CC_NO_SLOTS_AVAILABLE 9
Nico Huber90292652013-06-13 14:37:15 +020061#define CC_SHORT_PACKET 13
62#define CC_EVENT_RING_FULL_ERROR 21
63#define CC_COMMAND_RING_STOPPED 24
64#define CC_COMMAND_ABORTED 25
65#define CC_STOPPED 26
66#define CC_STOPPED_LENGTH_INVALID 27
Patrick Georgi6615ef32010-08-13 09:18:58 +000067
Nico Huber90292652013-06-13 14:37:15 +020068enum {
69 TRB_NORMAL = 1,
70 TRB_SETUP_STAGE = 2, TRB_DATA_STAGE = 3, TRB_STATUS_STAGE = 4,
Julius Werner83da5012013-09-27 12:45:11 -070071 TRB_LINK = 6, TRB_EVENT_DATA = 7,
Nico Huber90292652013-06-13 14:37:15 +020072 TRB_CMD_ENABLE_SLOT = 9, TRB_CMD_DISABLE_SLOT = 10, TRB_CMD_ADDRESS_DEV = 11,
73 TRB_CMD_CONFIGURE_EP = 12, TRB_CMD_EVAL_CTX = 13, TRB_CMD_RESET_EP = 14,
74 TRB_CMD_STOP_EP = 15, TRB_CMD_SET_TR_DQ = 16, TRB_CMD_NOOP = 23,
75 TRB_EV_TRANSFER = 32, TRB_EV_CMD_CMPL = 33, TRB_EV_PORTSC = 34, TRB_EV_HOST = 37,
76};
77enum { TRB_TRT_NO_DATA = 0, TRB_TRT_OUT_DATA = 2, TRB_TRT_IN_DATA = 3 };
78enum { TRB_DIR_OUT = 0, TRB_DIR_IN = 1 };
Patrick Georgi6615ef32010-08-13 09:18:58 +000079
Nico Huber90292652013-06-13 14:37:15 +020080#define TRB_PORT_FIELD ptr_low
81#define TRB_PORT_START 24
82#define TRB_PORT_LEN 8
83#define TRB_TL_FIELD status /* TL - Transfer Length */
84#define TRB_TL_START 0
85#define TRB_TL_LEN 17
86#define TRB_EVTL_FIELD status /* EVTL - (Event TRB) Transfer Length */
87#define TRB_EVTL_START 0
88#define TRB_EVTL_LEN 24
89#define TRB_TDS_FIELD status /* TDS - TD Size */
90#define TRB_TDS_START 17
91#define TRB_TDS_LEN 5
92#define TRB_CC_FIELD status /* CC - Completion Code */
93#define TRB_CC_START 24
94#define TRB_CC_LEN 8
95#define TRB_C_FIELD control /* C - Cycle Bit */
96#define TRB_C_START 0
97#define TRB_C_LEN 1
98#define TRB_TC_FIELD control /* TC - Toggle Cycle */
99#define TRB_TC_START 1
100#define TRB_TC_LEN 1
Sourabh Banerjeee73335c2014-09-24 16:14:45 +0530101#define TRB_ENT_FIELD control /* ENT - Evaluate Next TRB */
102#define TRB_ENT_START 1
103#define TRB_ENT_LEN 1
Nico Huber90292652013-06-13 14:37:15 +0200104#define TRB_ISP_FIELD control /* ISP - Interrupt-on Short Packet */
105#define TRB_ISP_START 2
106#define TRB_ISP_LEN 1
107#define TRB_CH_FIELD control /* CH - Chain Bit */
108#define TRB_CH_START 4
109#define TRB_CH_LEN 1
110#define TRB_IOC_FIELD control /* IOC - Interrupt On Completion */
111#define TRB_IOC_START 5
112#define TRB_IOC_LEN 1
113#define TRB_IDT_FIELD control /* IDT - Immediate Data */
114#define TRB_IDT_START 6
115#define TRB_IDT_LEN 1
116#define TRB_DC_FIELD control /* DC - Deconfigure */
117#define TRB_DC_START 9
118#define TRB_DC_LEN 1
119#define TRB_TT_FIELD control /* TT - TRB Type */
120#define TRB_TT_START 10
121#define TRB_TT_LEN 6
122#define TRB_TRT_FIELD control /* TRT - Transfer Type */
123#define TRB_TRT_START 16
124#define TRB_TRT_LEN 2
125#define TRB_DIR_FIELD control /* DIR - Direction */
126#define TRB_DIR_START 16
127#define TRB_DIR_LEN 1
128#define TRB_EP_FIELD control /* EP - Endpoint ID */
129#define TRB_EP_START 16
130#define TRB_EP_LEN 5
131#define TRB_ID_FIELD control /* ID - Slot ID */
132#define TRB_ID_START 24
133#define TRB_ID_LEN 8
134#define TRB_MASK(tok) MASK(TRB_##tok##_START, TRB_##tok##_LEN)
135#define TRB_GET(tok, trb) (((trb)->TRB_##tok##_FIELD & TRB_MASK(tok)) \
136 >> TRB_##tok##_START)
137#define TRB_SET(tok, trb, to) (trb)->TRB_##tok##_FIELD = \
138 (((trb)->TRB_##tok##_FIELD & ~TRB_MASK(tok)) | \
139 (((to) << TRB_##tok##_START) & TRB_MASK(tok)))
140#define TRB_DUMP(tok, trb) usb_debug(" "#tok"\t0x%04"PRIx32"\n", TRB_GET(tok, trb))
141
142#define TRB_CYCLE (1 << 0)
143typedef volatile struct trb {
144 u32 ptr_low;
145 u32 ptr_high;
146 u32 status;
147 u32 control;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000148} trb_t;
149
Rajmohan Manid6fb32b2014-05-30 13:06:01 -0700150#define TRB_MAX_TD_SIZE 0x1F /* bits 21:17 of TD Size in TRB */
151
Nico Huber90292652013-06-13 14:37:15 +0200152#define EVENT_RING_SIZE 64
153typedef struct {
154 trb_t *ring;
155 trb_t *cur;
156 trb_t *last;
157 u8 ccs;
158 u8 adv;
159} event_ring_t;
160
Julius Werner83da5012013-09-27 12:45:11 -0700161/* Never raise this above 256 to prevent transfer event length overflow! */
Nico Huber90292652013-06-13 14:37:15 +0200162#define TRANSFER_RING_SIZE 32
163typedef struct {
164 trb_t *ring;
165 trb_t *cur;
166 u8 pcs;
167} __attribute__ ((packed)) transfer_ring_t;
168
169#define COMMAND_RING_SIZE 4
170typedef transfer_ring_t command_ring_t;
171
172#define SC_ROUTE_FIELD f1 /* ROUTE - Route String */
173#define SC_ROUTE_START 0
174#define SC_ROUTE_LEN 20
Julius Wernere00ba212013-09-24 20:03:54 -0700175#define SC_SPEED1_FIELD f1 /* SPEED - Port speed plus one (compared to usb_speed enum) */
176#define SC_SPEED1_START 20
177#define SC_SPEED1_LEN 4
Nico Huber90292652013-06-13 14:37:15 +0200178#define SC_MTT_FIELD f1 /* MTT - Multi Transaction Translator */
179#define SC_MTT_START 25
180#define SC_MTT_LEN 1
181#define SC_HUB_FIELD f1 /* HUB - Is this a hub? */
182#define SC_HUB_START 26
183#define SC_HUB_LEN 1
184#define SC_CTXENT_FIELD f1 /* CTXENT - Context Entries (number of following ep contexts) */
185#define SC_CTXENT_START 27
186#define SC_CTXENT_LEN 5
187#define SC_RHPORT_FIELD f2 /* RHPORT - Root Hub Port Number */
188#define SC_RHPORT_START 16
189#define SC_RHPORT_LEN 8
190#define SC_NPORTS_FIELD f2 /* NPORTS - Number of Ports */
191#define SC_NPORTS_START 24
192#define SC_NPORTS_LEN 8
193#define SC_TTID_FIELD f3 /* TTID - TT Hub Slot ID */
194#define SC_TTID_START 0
195#define SC_TTID_LEN 8
196#define SC_TTPORT_FIELD f3 /* TTPORT - TT Port Number */
197#define SC_TTPORT_START 8
198#define SC_TTPORT_LEN 8
199#define SC_TTT_FIELD f3 /* TTT - TT Think Time */
200#define SC_TTT_START 16
201#define SC_TTT_LEN 2
202#define SC_UADDR_FIELD f4 /* UADDR - USB Device Address */
203#define SC_UADDR_START 0
204#define SC_UADDR_LEN 8
205#define SC_STATE_FIELD f4 /* STATE - Slot State */
206#define SC_STATE_START 27
207#define SC_STATE_LEN 8
208#define SC_MASK(tok) MASK(SC_##tok##_START, SC_##tok##_LEN)
Julius Werner1f864342013-09-03 17:15:31 -0700209#define SC_GET(tok, sc) (((sc)->SC_##tok##_FIELD & SC_MASK(tok)) \
Nico Huber90292652013-06-13 14:37:15 +0200210 >> SC_##tok##_START)
Julius Werner1f864342013-09-03 17:15:31 -0700211#define SC_SET(tok, sc, to) (sc)->SC_##tok##_FIELD = \
212 (((sc)->SC_##tok##_FIELD & ~SC_MASK(tok)) | \
Nico Huber90292652013-06-13 14:37:15 +0200213 (((to) << SC_##tok##_START) & SC_MASK(tok)))
214#define SC_DUMP(tok, sc) usb_debug(" "#tok"\t0x%04"PRIx32"\n", SC_GET(tok, sc))
Julius Werner1f864342013-09-03 17:15:31 -0700215typedef volatile struct slotctx {
Nico Huber90292652013-06-13 14:37:15 +0200216 u32 f1;
217 u32 f2;
218 u32 f3;
219 u32 f4;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000220 u32 rsvd[4];
221} slotctx_t;
222
Nico Huber90292652013-06-13 14:37:15 +0200223#define EC_STATE_FIELD f1 /* STATE - Endpoint State */
224#define EC_STATE_START 0
225#define EC_STATE_LEN 3
226#define EC_INTVAL_FIELD f1 /* INTVAL - Interval */
227#define EC_INTVAL_START 16
228#define EC_INTVAL_LEN 8
229#define EC_CERR_FIELD f2 /* CERR - Error Count */
230#define EC_CERR_START 1
231#define EC_CERR_LEN 2
232#define EC_TYPE_FIELD f2 /* TYPE - EP Type */
233#define EC_TYPE_START 3
234#define EC_TYPE_LEN 3
235#define EC_MBS_FIELD f2 /* MBS - Max Burst Size */
236#define EC_MBS_START 8
237#define EC_MBS_LEN 8
238#define EC_MPS_FIELD f2 /* MPS - Max Packet Size */
239#define EC_MPS_START 16
240#define EC_MPS_LEN 16
241#define EC_DCS_FIELD tr_dq_low /* DCS - Dequeue Cycle State */
242#define EC_DCS_START 0
243#define EC_DCS_LEN 1
244#define EC_AVRTRB_FIELD f5 /* AVRTRB - Average TRB Length */
245#define EC_AVRTRB_START 0
246#define EC_AVRTRB_LEN 16
247#define EC_MXESIT_FIELD f5 /* MXESIT - Max ESIT Payload */
248#define EC_MXESIT_START 16
249#define EC_MXESIT_LEN 16
Yidi Lind42ee152015-05-07 15:36:04 +0800250#define EC_BPKTS_FIELD rsvd[0] /* BPKTS - packets tx in scheduled uframe */
251#define EC_BPKTS_START 0
252#define EC_BPKTS_LEN 6
253#define EC_BBM_FIELD rsvd[0] /* BBM - burst mode for scheduling */
254#define EC_BBM_START 11
255#define EC_BBM_LEN 1
256
Nico Huber90292652013-06-13 14:37:15 +0200257#define EC_MASK(tok) MASK(EC_##tok##_START, EC_##tok##_LEN)
Julius Werner1f864342013-09-03 17:15:31 -0700258#define EC_GET(tok, ec) (((ec)->EC_##tok##_FIELD & EC_MASK(tok)) \
Nico Huber90292652013-06-13 14:37:15 +0200259 >> EC_##tok##_START)
Julius Werner1f864342013-09-03 17:15:31 -0700260#define EC_SET(tok, ec, to) (ec)->EC_##tok##_FIELD = \
261 (((ec)->EC_##tok##_FIELD & ~EC_MASK(tok)) | \
Nico Huber90292652013-06-13 14:37:15 +0200262 (((to) << EC_##tok##_START) & EC_MASK(tok)))
263#define EC_DUMP(tok, ec) usb_debug(" "#tok"\t0x%04"PRIx32"\n", EC_GET(tok, ec))
264enum { EP_ISOC_OUT = 1, EP_BULK_OUT = 2, EP_INTR_OUT = 3,
265 EP_CONTROL = 4, EP_ISOC_IN = 5, EP_BULK_IN = 6, EP_INTR_IN = 7 };
Julius Werner1f864342013-09-03 17:15:31 -0700266typedef volatile struct epctx {
Nico Huber90292652013-06-13 14:37:15 +0200267 u32 f1;
268 u32 f2;
269 u32 tr_dq_low;
270 u32 tr_dq_high;
271 u32 f5;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000272 u32 rsvd[3];
273} epctx_t;
274
Julius Werner1f864342013-09-03 17:15:31 -0700275#define NUM_EPS 32
276#define CTXSIZE(xhci) ((xhci)->capreg->csz ? 64 : 32)
277
Nico Huber90292652013-06-13 14:37:15 +0200278typedef union devctx {
Julius Werner1f864342013-09-03 17:15:31 -0700279 /* set of pointers, so we can dynamically adjust Slot/EP context size */
Patrick Georgi6615ef32010-08-13 09:18:58 +0000280 struct {
Julius Werner1f864342013-09-03 17:15:31 -0700281 union {
282 slotctx_t *slot;
283 void *raw; /* Pointer to the whole dev context. */
284 };
285 epctx_t *ep0;
286 epctx_t *eps1_30[NUM_EPS - 2];
Nico Huber90292652013-06-13 14:37:15 +0200287 };
Julius Werner1f864342013-09-03 17:15:31 -0700288 epctx_t *ep[NUM_EPS]; /* At index 0 it's actually the slotctx,
289 we have it like that so we can use
290 the ep_id directly as index. */
Patrick Georgi6615ef32010-08-13 09:18:58 +0000291} devctx_t;
292
Nico Huber90292652013-06-13 14:37:15 +0200293typedef struct inputctx {
Julius Werner1f864342013-09-03 17:15:31 -0700294 union { /* The drop flags are located at the start of the */
295 u32 *drop; /* structure, so a pointer to them is equivalent */
296 void *raw; /* to a pointer to the whole (raw) input context. */
297 };
298 u32 *add;
Nico Huber90292652013-06-13 14:37:15 +0200299 devctx_t dev;
300} inputctx_t;
301
302typedef struct intrq {
303 size_t size; /* Size of each transfer */
304 size_t count; /* The number of TRBs to fill at once */
305 trb_t *next; /* The next TRB expected to be processed by the controller */
306 trb_t *ready; /* The last TRB in the transfer ring processed by the controller */
307 endpoint_t *ep;
308} intrq_t;
309
310typedef struct devinfo {
Julius Werner1f864342013-09-03 17:15:31 -0700311 devctx_t ctx;
312 transfer_ring_t *transfer_rings[NUM_EPS];
Julius Wernere44a4e82015-07-08 22:36:00 -0700313 intrq_t *interrupt_queues[NUM_EPS];
Nico Huber90292652013-06-13 14:37:15 +0200314} devinfo_t;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000315
316typedef struct erst_entry {
317 u32 seg_base_lo;
318 u32 seg_base_hi;
319 u32 seg_size;
320 u32 rsvd;
321} erst_entry_t;
322
323typedef struct xhci {
324 /* capreg is read-only, so no need for volatile,
325 and thus 32bit accesses can be assumed. */
326 struct capreg {
327 u8 caplength;
328 u8 res1;
329 union {
330 u16 hciversion;
331 struct {
332 u8 hciver_lo;
333 u8 hciver_hi;
334 } __attribute__ ((packed));
335 } __attribute__ ((packed));
336 union {
337 u32 hcsparams1;
338 struct {
339 unsigned long MaxSlots:7;
340 unsigned long MaxIntrs:11;
341 unsigned long:6;
342 unsigned long MaxPorts:8;
343 } __attribute__ ((packed));
344 } __attribute__ ((packed));
345 union {
346 u32 hcsparams2;
347 struct {
348 unsigned long IST:4;
349 unsigned long ERST_Max:4;
350 unsigned long:18;
351 unsigned long SPR:1;
352 unsigned long Max_Scratchpad_Bufs:5;
353 } __attribute__ ((packed));
354 } __attribute__ ((packed));
355 union {
356 u32 hcsparams3;
357 struct {
358 unsigned long u1latency:8;
359 unsigned long:8;
360 unsigned long u2latency:16;
361 } __attribute__ ((packed));
362 } __attribute__ ((packed));
363 union {
364 u32 hccparams;
365 struct {
366 unsigned long ac64:1;
367 unsigned long bnc:1;
368 unsigned long csz:1;
369 unsigned long ppc:1;
370 unsigned long pind:1;
371 unsigned long lhrc:1;
372 unsigned long ltc:1;
373 unsigned long nss:1;
374 unsigned long:4;
375 unsigned long MaxPSASize:4;
376 unsigned long xECP:16;
377 } __attribute__ ((packed));
378 } __attribute__ ((packed));
379 u32 dboff;
380 u32 rtsoff;
381 } __attribute__ ((packed)) *capreg;
382
383 /* opreg is R/W is most places, so volatile access is necessary.
384 volatile means that the compiler seeks byte writes if possible,
385 making bitfields unusable for MMIO register blocks. Yay C :-( */
386 volatile struct opreg {
387 u32 usbcmd;
388#define USBCMD_RS 1<<0
389#define USBCMD_HCRST 1<<1
Nico Huber90292652013-06-13 14:37:15 +0200390#define USBCMD_INTE 1<<2
Patrick Georgi6615ef32010-08-13 09:18:58 +0000391 u32 usbsts;
392#define USBSTS_HCH 1<<0
393#define USBSTS_HSE 1<<2
394#define USBSTS_EINT 1<<3
395#define USBSTS_PCD 1<<4
396#define USBSTS_CNR 1<<11
Nico Huber90292652013-06-13 14:37:15 +0200397#define USBSTS_PRSRV_MASK ((1 << 1) | 0xffffe000)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000398 u32 pagesize;
399 u8 res1[0x13-0x0c+1];
400 u32 dnctrl;
401 u32 crcr_lo;
402 u32 crcr_hi;
403#define CRCR_RCS 1<<0
404#define CRCR_CS 1<<1
405#define CRCR_CA 1<<2
406#define CRCR_CRR 1<<3
407 u8 res2[0x2f-0x20+1];
408 u32 dcbaap_lo;
409 u32 dcbaap_hi;
410 u32 config;
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700411#define CONFIG_LP_MASK_MaxSlotsEn 0xff
Patrick Georgi6615ef32010-08-13 09:18:58 +0000412 u8 res3[0x3ff-0x3c+1];
413 struct {
414 u32 portsc;
Nico Huber90292652013-06-13 14:37:15 +0200415#define PORTSC_CCS (1<<0)
416#define PORTSC_PED (1<<1)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000417 // BIT 2 rsvdZ
Nico Huber90292652013-06-13 14:37:15 +0200418#define PORTSC_OCA (1<<3)
419#define PORTSC_PR (1<<4)
420#define PORTSC_PLS (1<<5)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000421#define PORTSC_PLS_MASK MASK(5, 4)
Nico Huber90292652013-06-13 14:37:15 +0200422#define PORTSC_PP (1<<9)
423#define PORTSC_PORT_SPEED_START 10
424#define PORTSC_PORT_SPEED (1<<PORTSC_PORT_SPEED_START)
425#define PORTSC_PORT_SPEED_MASK MASK(PORTSC_PORT_SPEED_START, 4)
426#define PORTSC_PIC (1<<14)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000427#define PORTSC_PIC_MASK MASK(14, 2)
Nico Huber90292652013-06-13 14:37:15 +0200428#define PORTSC_LWS (1<<16)
429#define PORTSC_CSC (1<<17)
430#define PORTSC_PEC (1<<18)
431#define PORTSC_WRC (1<<19)
432#define PORTSC_OCC (1<<20)
433#define PORTSC_PRC (1<<21)
434#define PORTSC_PLC (1<<22)
435#define PORTSC_CEC (1<<23)
436#define PORTSC_CAS (1<<24)
437#define PORTSC_WCE (1<<25)
438#define PORTSC_WDE (1<<26)
439#define PORTSC_WOE (1<<27)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000440 // BIT 29:28 rsvdZ
Nico Huber90292652013-06-13 14:37:15 +0200441#define PORTSC_DR (1<<30)
442#define PORTSC_WPR (1<<31)
443#define PORTSC_RW_MASK (PORTSC_PR | PORTSC_PLS_MASK | PORTSC_PP | PORTSC_PIC_MASK | PORTSC_LWS | PORTSC_WCE | PORTSC_WDE | PORTSC_WOE)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000444 u32 portpmsc;
445 u32 portli;
446 u32 res;
447 } __attribute__ ((packed)) prs[];
448 } __attribute__ ((packed)) *opreg;
449
450 /* R/W, volatile, MMIO -> no bitfields */
451 volatile struct hcrreg {
452 u32 mfindex;
453 u8 res1[0x20-0x4];
454 struct {
455 u32 iman;
456 u32 imod;
457 u32 erstsz;
458 u32 res;
459 u32 erstba_lo;
460 u32 erstba_hi;
461 u32 erdp_lo;
462 u32 erdp_hi;
463 } __attribute__ ((packed)) intrrs[]; // up to 1024, but maximum host specific, given in capreg->MaxIntrs
464 } __attribute__ ((packed)) *hcrreg;
465
466 /* R/W, volatile, MMIO -> no bitfields */
467 volatile u32 *dbreg;
468
469 /* R/W, volatile, Memory -> bitfields allowed */
Nico Huber90292652013-06-13 14:37:15 +0200470 u64 *dcbaa; /* pointers to sp_ptrs and output (device) contexts */
471 u64 *sp_ptrs; /* pointers to scratchpad buffers */
Patrick Georgi6615ef32010-08-13 09:18:58 +0000472
Nico Huber90292652013-06-13 14:37:15 +0200473 command_ring_t cr;
474 event_ring_t er;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000475 volatile erst_entry_t *ev_ring_table;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000476
477 usbdev_t *roothub;
Nico Huber90292652013-06-13 14:37:15 +0200478
479 u8 max_slots_en;
Julius Werner1f864342013-09-03 17:15:31 -0700480 devinfo_t *dev; /* array of devinfos by slot_id */
481
482#define DMA_SIZE (64 * 1024)
483 void *dma_buffer;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000484} xhci_t;
485
486#define XHCI_INST(controller) ((xhci_t*)((controller)->instance))
487
Nico Huber90292652013-06-13 14:37:15 +0200488void *xhci_align(const size_t min_align, const size_t size);
489void xhci_init_cycle_ring(transfer_ring_t *, const size_t ring_size);
Julius Wernerd13e2c42013-09-17 22:16:04 -0700490usbdev_t *xhci_set_address (hci_t *, usb_speed speed, int hubport, int hubaddr);
Nico Huber90292652013-06-13 14:37:15 +0200491int xhci_finish_device_config(usbdev_t *);
492void xhci_destroy_dev(hci_t *, int slot_id);
493
494void xhci_reset_event_ring(event_ring_t *);
495void xhci_advance_event_ring(xhci_t *);
496void xhci_update_event_dq(xhci_t *);
497void xhci_handle_events(xhci_t *);
498int xhci_wait_for_command_aborted(xhci_t *, const trb_t *);
499int xhci_wait_for_command_done(xhci_t *, const trb_t *, int clear_event);
500int xhci_wait_for_transfer(xhci_t *, const int slot_id, const int ep_id);
501
502void xhci_clear_trb(trb_t *, int pcs);
503
504trb_t *xhci_next_command_trb(xhci_t *);
505void xhci_post_command(xhci_t *);
506int xhci_cmd_enable_slot(xhci_t *, int *slot_id);
507int xhci_cmd_disable_slot(xhci_t *, int slot_id);
508int xhci_cmd_address_device(xhci_t *, int slot_id, inputctx_t *);
509int xhci_cmd_configure_endpoint(xhci_t *, int slot_id, int config_id, inputctx_t *);
510int xhci_cmd_evaluate_context(xhci_t *, int slot_id, inputctx_t *);
511int xhci_cmd_reset_endpoint(xhci_t *, int slot_id, int ep);
512int xhci_cmd_stop_endpoint(xhci_t *, int slot_id, int ep);
513int xhci_cmd_set_tr_dq(xhci_t *, int slot_id, int ep, trb_t *, int dcs);
514
515static inline int xhci_ep_id(const endpoint_t *const ep) {
516 return ((ep->endpoint & 0x7f) << 1) + (ep->direction == IN);
517}
518
519
520#ifdef XHCI_DUMPS
521void xhci_dump_slotctx(const slotctx_t *);
522void xhci_dump_epctx(const epctx_t *);
523void xhci_dump_devctx(const devctx_t *, const u32 ctx_mask);
524void xhci_dump_inputctx(const inputctx_t *);
525void xhci_dump_transfer_trb(const trb_t *);
526void xhci_dump_transfer_trbs(const trb_t *first, const trb_t *last);
527#else
528#define xhci_dump_slotctx(args...) do {} while(0)
529#define xhci_dump_epctx(args...) do {} while(0)
530#define xhci_dump_devctx(args...) do {} while(0)
531#define xhci_dump_inputctx(args...) do {} while(0)
532#define xhci_dump_transfer_trb(args...) do {} while(0)
533#define xhci_dump_transfer_trbs(args...) do {} while(0)
534#endif
535
Patrick Georgi6615ef32010-08-13 09:18:58 +0000536#endif