xref: /freebsd/sys/dev/usb/controller/xhci.h (revision 1354026034bcac3abf55b01e0a427be249283d95)
1*13540260SHans Petter Selasky /* $FreeBSD$ */
2*13540260SHans Petter Selasky 
3*13540260SHans Petter Selasky /*-
4*13540260SHans Petter Selasky  * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
5*13540260SHans Petter Selasky  *
6*13540260SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
7*13540260SHans Petter Selasky  * modification, are permitted provided that the following conditions
8*13540260SHans Petter Selasky  * are met:
9*13540260SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
10*13540260SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer.
11*13540260SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
12*13540260SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
13*13540260SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
14*13540260SHans Petter Selasky  *
15*13540260SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*13540260SHans Petter Selasky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*13540260SHans Petter Selasky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*13540260SHans Petter Selasky  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*13540260SHans Petter Selasky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*13540260SHans Petter Selasky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*13540260SHans Petter Selasky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*13540260SHans Petter Selasky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*13540260SHans Petter Selasky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*13540260SHans Petter Selasky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*13540260SHans Petter Selasky  * SUCH DAMAGE.
26*13540260SHans Petter Selasky  */
27*13540260SHans Petter Selasky 
28*13540260SHans Petter Selasky #ifndef _XHCI_H_
29*13540260SHans Petter Selasky #define	_XHCI_H_
30*13540260SHans Petter Selasky 
31*13540260SHans Petter Selasky #define	XHCI_MAX_DEVICES	MIN(USB_MAX_DEVICES, 128)
32*13540260SHans Petter Selasky #define	XHCI_MAX_ENDPOINTS	32	/* hardcoded - do not change */
33*13540260SHans Petter Selasky #define	XHCI_MAX_SCRATCHPADS	32
34*13540260SHans Petter Selasky #define	XHCI_MAX_EVENTS		(16 * 13)
35*13540260SHans Petter Selasky #define	XHCI_MAX_COMMANDS	(16 * 1)
36*13540260SHans Petter Selasky #define	XHCI_MAX_RSEG		1
37*13540260SHans Petter Selasky #define	XHCI_MAX_TRANSFERS	4
38*13540260SHans Petter Selasky 
39*13540260SHans Petter Selasky #define	XHCI_DEV_CTX_ADDR_ALIGN		64	/* bytes */
40*13540260SHans Petter Selasky #define	XHCI_DEV_CTX_ALIGN		64	/* bytes */
41*13540260SHans Petter Selasky #define	XHCI_INPUT_CTX_ALIGN		64	/* bytes */
42*13540260SHans Petter Selasky #define	XHCI_SLOT_CTX_ALIGN		32	/* bytes */
43*13540260SHans Petter Selasky #define	XHCI_ENDP_CTX_ALIGN		32	/* bytes */
44*13540260SHans Petter Selasky #define	XHCI_STREAM_CTX_ALIGN		16	/* bytes */
45*13540260SHans Petter Selasky #define	XHCI_TRANS_RING_SEG_ALIGN	16	/* bytes */
46*13540260SHans Petter Selasky #define	XHCI_CMD_RING_SEG_ALIGN		64	/* bytes */
47*13540260SHans Petter Selasky #define	XHCI_EVENT_RING_SEG_ALIGN	64	/* bytes */
48*13540260SHans Petter Selasky #define	XHCI_SCRATCH_BUF_ARRAY_ALIGN	64	/* bytes */
49*13540260SHans Petter Selasky #define	XHCI_SCRATCH_BUFFER_ALIGN	USB_PAGE_SIZE
50*13540260SHans Petter Selasky #define	XHCI_TRB_ALIGN			16	/* bytes */
51*13540260SHans Petter Selasky #define	XHCI_TD_ALIGN			64	/* bytes */
52*13540260SHans Petter Selasky #define	XHCI_PAGE_SIZE			4096	/* bytes */
53*13540260SHans Petter Selasky 
54*13540260SHans Petter Selasky struct xhci_dev_ctx_addr {
55*13540260SHans Petter Selasky 	volatile uint64_t	qwBaaDevCtxAddr[USB_MAX_DEVICES + 1];
56*13540260SHans Petter Selasky 	struct {
57*13540260SHans Petter Selasky 		volatile uint64_t dummy;
58*13540260SHans Petter Selasky 	} __aligned(64) padding;
59*13540260SHans Petter Selasky 	volatile uint64_t	qwSpBufPtr[XHCI_MAX_SCRATCHPADS];
60*13540260SHans Petter Selasky };
61*13540260SHans Petter Selasky 
62*13540260SHans Petter Selasky #define	XHCI_EPNO2EPID(x) \
63*13540260SHans Petter Selasky     ((((x) & UE_DIR_IN) ? 1 : 0) | (2 * ((x) & UE_ADDR)))
64*13540260SHans Petter Selasky 
65*13540260SHans Petter Selasky struct xhci_slot_ctx {
66*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx0;
67*13540260SHans Petter Selasky #define	XHCI_SCTX_0_ROUTE_SET(x)		((x) & 0xFFFFF)
68*13540260SHans Petter Selasky #define	XHCI_SCTX_0_ROUTE_GET(x)		((x) & 0xFFFFF)
69*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SPEED_SET(x)		(((x) & 0xF) << 20)
70*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SPEED_GET(x)		(((x) >> 20) & 0xF)
71*13540260SHans Petter Selasky #define	XHCI_SCTX_0_MTT_SET(x)			(((x) & 0x1) << 25)
72*13540260SHans Petter Selasky #define	XHCI_SCTX_0_MTT_GET(x)			(((x) >> 25) & 0x1)
73*13540260SHans Petter Selasky #define	XHCI_SCTX_0_HUB_SET(x)			(((x) & 0x1) << 26)
74*13540260SHans Petter Selasky #define	XHCI_SCTX_0_HUB_GET(x)			(((x) >> 26) & 0x1)
75*13540260SHans Petter Selasky #define	XHCI_SCTX_0_CTX_NUM_SET(x)		(((x) & 0x1F) << 27)
76*13540260SHans Petter Selasky #define	XHCI_SCTX_0_CTX_NUM_GET(x)		(((x) >> 27) & 0x1F)
77*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx1;
78*13540260SHans Petter Selasky #define	XHCI_SCTX_1_MAX_EL_SET(x)		((x) & 0xFFFF)
79*13540260SHans Petter Selasky #define	XHCI_SCTX_1_MAX_EL_GET(x)		((x) & 0xFFFF)
80*13540260SHans Petter Selasky #define	XHCI_SCTX_1_RH_PORT_SET(x)		(((x) & 0xFF) << 16)
81*13540260SHans Petter Selasky #define	XHCI_SCTX_1_RH_PORT_GET(x)		(((x) >> 16) & 0xFF)
82*13540260SHans Petter Selasky #define	XHCI_SCTX_1_NUM_PORTS_SET(x)		(((x) & 0xFF) << 24)
83*13540260SHans Petter Selasky #define	XHCI_SCTX_1_NUM_PORTS_GET(x)		(((x) >> 24) & 0xFF)
84*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx2;
85*13540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_HUB_SID_SET(x)		((x) & 0xFF)
86*13540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_HUB_SID_GET(x)		((x) & 0xFF)
87*13540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_PORT_NUM_SET(x)		(((x) & 0xFF) << 8)
88*13540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_PORT_NUM_GET(x)		(((x) >> 8) & 0xFF)
89*13540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_THINK_TIME_SET(x)	(((x) & 0x3) << 16)
90*13540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_THINK_TIME_GET(x)	(((x) >> 16) & 0x3)
91*13540260SHans Petter Selasky #define	XHCI_SCTX_2_IRQ_TARGET_SET(x)		(((x) & 0x3FF) << 22)
92*13540260SHans Petter Selasky #define	XHCI_SCTX_2_IRQ_TARGET_GET(x)		(((x) >> 22) & 0x3FF)
93*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx3;
94*13540260SHans Petter Selasky #define	XHCI_SCTX_3_DEV_ADDR_SET(x)		((x) & 0xFF)
95*13540260SHans Petter Selasky #define	XHCI_SCTX_3_DEV_ADDR_GET(x)		((x) & 0xFF)
96*13540260SHans Petter Selasky #define	XHCI_SCTX_3_SLOT_STATE_SET(x)		(((x) & 0x1F) << 27)
97*13540260SHans Petter Selasky #define	XHCI_SCTX_3_SLOT_STATE_GET(x)		(((x) >> 27) & 0x1F)
98*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx4;
99*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx5;
100*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx6;
101*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx7;
102*13540260SHans Petter Selasky };
103*13540260SHans Petter Selasky 
104*13540260SHans Petter Selasky struct xhci_endp_ctx {
105*13540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx0;
106*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_EPSTATE_SET(x)		((x) & 0x7)
107*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_EPSTATE_GET(x)		((x) & 0x7)
108*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_MULT_SET(x)		(((x) & 0x3) << 8)
109*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_MULT_GET(x)		(((x) >> 8) & 0x3)
110*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_MAXP_STREAMS_SET(x)	(((x) & 0x1F) << 10)
111*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_MAXP_STREAMS_GET(x)	(((x) >> 10) & 0x1F)
112*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_LSA_SET(x)			(((x) & 0x1) << 15)
113*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_LSA_GET(x)			(((x) >> 15) & 0x1)
114*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_IVAL_SET(x)		(((x) & 0xFF) << 16)
115*13540260SHans Petter Selasky #define	XHCI_EPCTX_0_IVAL_GET(x)		(((x) >> 16) & 0xFF)
116*13540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx1;
117*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_CERR_SET(x)		(((x) & 0x3) << 1)
118*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_CERR_GET(x)		(((x) >> 1) & 0x3)
119*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_EPTYPE_SET(x)		(((x) & 0x7) << 3)
120*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_EPTYPE_GET(x)		(((x) >> 3) & 0x7)
121*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_HID_SET(x)			(((x) & 0x1) << 7)
122*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_HID_GET(x)			(((x) >> 7) & 0x1)
123*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_MAXB_SET(x)		(((x) & 0xFF) << 8)
124*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_MAXB_GET(x)		(((x) >> 8) & 0xFF)
125*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_MAXP_SIZE_SET(x)		(((x) & 0xFFFF) << 16)
126*13540260SHans Petter Selasky #define	XHCI_EPCTX_1_MAXP_SIZE_GET(x)		(((x) >> 16) & 0xFFFF)
127*13540260SHans Petter Selasky 	volatile uint64_t	qwEpCtx2;
128*13540260SHans Petter Selasky #define	XHCI_EPCTX_2_DCS_SET(x)			((x) & 0x1)
129*13540260SHans Petter Selasky #define	XHCI_EPCTX_2_DCS_GET(x)			((x) & 0x1)
130*13540260SHans Petter Selasky #define	XHCI_EPCTX_2_TR_DQ_PTR_MASK		0xFFFFFFFFFFFFFFF0U
131*13540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx4;
132*13540260SHans Petter Selasky #define	XHCI_EPCTX_4_AVG_TRB_LEN_SET(x)		((x) & 0xFFFF)
133*13540260SHans Petter Selasky #define	XHCI_EPCTX_4_AVG_TRB_LEN_GET(x)		((x) & 0xFFFF)
134*13540260SHans Petter Selasky #define	XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(x)	(((x) & 0xFFFF) << 16)
135*13540260SHans Petter Selasky #define	XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_GET(x)	(((x) >> 16) & 0xFFFF)
136*13540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx5;
137*13540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx6;
138*13540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx7;
139*13540260SHans Petter Selasky };
140*13540260SHans Petter Selasky 
141*13540260SHans Petter Selasky struct xhci_input_ctx {
142*13540260SHans Petter Selasky #define	XHCI_INCTX_NON_CTRL_MASK	0xFFFFFFFCU
143*13540260SHans Petter Selasky 	volatile uint32_t	dwInCtx0;
144*13540260SHans Petter Selasky #define	XHCI_INCTX_0_DROP_MASK(n)	(1U << (n))
145*13540260SHans Petter Selasky 	volatile uint32_t	dwInCtx1;
146*13540260SHans Petter Selasky #define	XHCI_INCTX_1_ADD_MASK(n)	(1U << (n))
147*13540260SHans Petter Selasky 	volatile uint32_t	dwInCtx2;
148*13540260SHans Petter Selasky 	volatile uint32_t	dwInCtx3;
149*13540260SHans Petter Selasky 	volatile uint32_t	dwInCtx4;
150*13540260SHans Petter Selasky 	volatile uint32_t	dwInCtx5;
151*13540260SHans Petter Selasky 	volatile uint32_t	dwInCtx6;
152*13540260SHans Petter Selasky 	volatile uint32_t	dwInCtx7;
153*13540260SHans Petter Selasky };
154*13540260SHans Petter Selasky 
155*13540260SHans Petter Selasky struct xhci_input_dev_ctx {
156*13540260SHans Petter Selasky 	struct xhci_input_ctx	ctx_input;
157*13540260SHans Petter Selasky 	struct xhci_slot_ctx	ctx_slot;
158*13540260SHans Petter Selasky 	struct xhci_endp_ctx	ctx_ep[XHCI_MAX_ENDPOINTS - 1];
159*13540260SHans Petter Selasky };
160*13540260SHans Petter Selasky 
161*13540260SHans Petter Selasky struct xhci_dev_ctx {
162*13540260SHans Petter Selasky 	struct xhci_slot_ctx	ctx_slot;
163*13540260SHans Petter Selasky 	struct xhci_endp_ctx	ctx_ep[XHCI_MAX_ENDPOINTS - 1];
164*13540260SHans Petter Selasky } __aligned(XHCI_DEV_CTX_ALIGN);
165*13540260SHans Petter Selasky 
166*13540260SHans Petter Selasky struct xhci_stream_ctx {
167*13540260SHans Petter Selasky 	volatile uint64_t	qwSctx0;
168*13540260SHans Petter Selasky #define	XHCI_SCTX_0_DCS_GET(x)		((x) & 0x1)
169*13540260SHans Petter Selasky #define	XHCI_SCTX_0_DCS_SET(x)		((x) & 0x1)
170*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_SET(x)		(((x) & 0x7) << 1)
171*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_GET(x)		(((x) >> 1) & 0x7)
172*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_SEC_TR_RING	0x0
173*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_TR_RING	0x1
174*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_8	0x2
175*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_16	0x3
176*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_32	0x4
177*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_64	0x5
178*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_128	0x6
179*13540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_256	0x7
180*13540260SHans Petter Selasky #define	XHCI_SCTX_0_TR_DQ_PTR_MASK	0xFFFFFFFFFFFFFFF0U
181*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx2;
182*13540260SHans Petter Selasky 	volatile uint32_t	dwSctx3;
183*13540260SHans Petter Selasky };
184*13540260SHans Petter Selasky 
185*13540260SHans Petter Selasky struct xhci_trb {
186*13540260SHans Petter Selasky 	volatile uint64_t	qwTrb0;
187*13540260SHans Petter Selasky #define	XHCI_TRB_0_WLENGTH_MASK		(0xFFFFULL << 48)
188*13540260SHans Petter Selasky 	volatile uint32_t	dwTrb2;
189*13540260SHans Petter Selasky #define	XHCI_TRB_2_ERROR_GET(x)		(((x) >> 24) & 0xFF)
190*13540260SHans Petter Selasky #define	XHCI_TRB_2_ERROR_SET(x)		(((x) & 0xFF) << 24)
191*13540260SHans Petter Selasky #define	XHCI_TRB_2_TDSZ_GET(x)		(((x) >> 17) & 0x1F)
192*13540260SHans Petter Selasky #define	XHCI_TRB_2_TDSZ_SET(x)		(((x) & 0x1F) << 17)
193*13540260SHans Petter Selasky #define	XHCI_TRB_2_REM_GET(x)		((x) & 0xFFFFFF)
194*13540260SHans Petter Selasky #define	XHCI_TRB_2_REM_SET(x)		((x) & 0xFFFFFF)
195*13540260SHans Petter Selasky #define	XHCI_TRB_2_BYTES_GET(x)		((x) & 0x1FFFF)
196*13540260SHans Petter Selasky #define	XHCI_TRB_2_BYTES_SET(x)		((x) & 0x1FFFF)
197*13540260SHans Petter Selasky #define	XHCI_TRB_2_IRQ_GET(x)		(((x) >> 22) & 0x3FF)
198*13540260SHans Petter Selasky #define	XHCI_TRB_2_IRQ_SET(x)		(((x) & 0x3FF) << 22)
199*13540260SHans Petter Selasky #define	XHCI_TRB_2_STREAM_GET(x)	(((x) >> 16) & 0xFFFF)
200*13540260SHans Petter Selasky #define	XHCI_TRB_2_STREAM_SET(x)	(((x) & 0xFFFF) << 16)
201*13540260SHans Petter Selasky 
202*13540260SHans Petter Selasky 	volatile uint32_t	dwTrb3;
203*13540260SHans Petter Selasky #define	XHCI_TRB_3_TYPE_GET(x)		(((x) >> 10) & 0x3F)
204*13540260SHans Petter Selasky #define	XHCI_TRB_3_TYPE_SET(x)		(((x) & 0x3F) << 10)
205*13540260SHans Petter Selasky #define	XHCI_TRB_3_CYCLE_BIT		(1U << 0)
206*13540260SHans Petter Selasky #define	XHCI_TRB_3_TC_BIT		(1U << 1)	/* command ring only */
207*13540260SHans Petter Selasky #define	XHCI_TRB_3_ENT_BIT		(1U << 1)	/* transfer ring only */
208*13540260SHans Petter Selasky #define	XHCI_TRB_3_ISP_BIT		(1U << 2)
209*13540260SHans Petter Selasky #define	XHCI_TRB_3_NSNOOP_BIT		(1U << 3)
210*13540260SHans Petter Selasky #define	XHCI_TRB_3_CHAIN_BIT		(1U << 4)
211*13540260SHans Petter Selasky #define	XHCI_TRB_3_IOC_BIT		(1U << 5)
212*13540260SHans Petter Selasky #define	XHCI_TRB_3_IDT_BIT		(1U << 6)
213*13540260SHans Petter Selasky #define	XHCI_TRB_3_TBC_GET(x)		(((x) >> 7) & 3)
214*13540260SHans Petter Selasky #define	XHCI_TRB_3_TBC_SET(x)		(((x) & 3) << 7)
215*13540260SHans Petter Selasky #define	XHCI_TRB_3_BEI_BIT		(1U << 9)
216*13540260SHans Petter Selasky #define	XHCI_TRB_3_DCEP_BIT		(1U << 9)
217*13540260SHans Petter Selasky #define	XHCI_TRB_3_PRSV_BIT		(1U << 9)
218*13540260SHans Petter Selasky #define	XHCI_TRB_3_BSR_BIT		(1U << 9)
219*13540260SHans Petter Selasky #define	XHCI_TRB_3_TRT_MASK		(3U << 16)
220*13540260SHans Petter Selasky #define	XHCI_TRB_3_TRT_NONE		(0U << 16)
221*13540260SHans Petter Selasky #define	XHCI_TRB_3_TRT_OUT		(2U << 16)
222*13540260SHans Petter Selasky #define	XHCI_TRB_3_TRT_IN		(3U << 16)
223*13540260SHans Petter Selasky #define	XHCI_TRB_3_DIR_IN		(1U << 16)
224*13540260SHans Petter Selasky #define	XHCI_TRB_3_TLBPC_GET(x)		(((x) >> 16) & 0xF)
225*13540260SHans Petter Selasky #define	XHCI_TRB_3_TLBPC_SET(x)		(((x) & 0xF) << 16)
226*13540260SHans Petter Selasky #define	XHCI_TRB_3_EP_GET(x)		(((x) >> 16) & 0x1F)
227*13540260SHans Petter Selasky #define	XHCI_TRB_3_EP_SET(x)		(((x) & 0x1F) << 16)
228*13540260SHans Petter Selasky #define	XHCI_TRB_3_FRID_GET(x)		(((x) >> 20) & 0x7FF)
229*13540260SHans Petter Selasky #define	XHCI_TRB_3_FRID_SET(x)		(((x) & 0x7FF) << 20)
230*13540260SHans Petter Selasky #define	XHCI_TRB_3_ISO_SIA_BIT		(1U << 31)
231*13540260SHans Petter Selasky #define	XHCI_TRB_3_SUSP_EP_BIT		(1U << 23)
232*13540260SHans Petter Selasky #define	XHCI_TRB_3_SLOT_GET(x)		(((x) >> 24) & 0xFF)
233*13540260SHans Petter Selasky #define	XHCI_TRB_3_SLOT_SET(x)		(((x) & 0xFF) << 24)
234*13540260SHans Petter Selasky 
235*13540260SHans Petter Selasky /* Commands */
236*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_RESERVED		0x00
237*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_NORMAL		0x01
238*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_SETUP_STAGE	0x02
239*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_DATA_STAGE	0x03
240*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_STATUS_STAGE	0x04
241*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_ISOCH		0x05
242*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_LINK		0x06
243*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_EVENT_DATA	0x07
244*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_NOOP		0x08
245*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_ENABLE_SLOT	0x09
246*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_DISABLE_SLOT	0x0A
247*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_ADDRESS_DEVICE	0x0B
248*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_CONFIGURE_EP	0x0C
249*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_EVALUATE_CTX	0x0D
250*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_RESET_EP		0x0E
251*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_STOP_EP		0x0F
252*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_SET_TR_DEQUEUE	0x10
253*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_RESET_DEVICE	0x11
254*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_FORCE_EVENT	0x12
255*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_NEGOTIATE_BW	0x13
256*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_SET_LATENCY_TOL  	0x14
257*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_GET_PORT_BW	0x15
258*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_FORCE_HEADER	0x16
259*13540260SHans Petter Selasky #define	XHCI_TRB_TYPE_NOOP_CMD		0x17
260*13540260SHans Petter Selasky 
261*13540260SHans Petter Selasky /* Events */
262*13540260SHans Petter Selasky #define	XHCI_TRB_EVENT_TRANSFER		0x20
263*13540260SHans Petter Selasky #define	XHCI_TRB_EVENT_CMD_COMPLETE	0x21
264*13540260SHans Petter Selasky #define	XHCI_TRB_EVENT_PORT_STS_CHANGE  0x22
265*13540260SHans Petter Selasky #define	XHCI_TRB_EVENT_BW_REQUEST      	0x23
266*13540260SHans Petter Selasky #define	XHCI_TRB_EVENT_DOORBELL		0x24
267*13540260SHans Petter Selasky #define	XHCI_TRB_EVENT_HOST_CTRL	0x25
268*13540260SHans Petter Selasky #define	XHCI_TRB_EVENT_DEVICE_NOTIFY	0x26
269*13540260SHans Petter Selasky #define	XHCI_TRB_EVENT_MFINDEX_WRAP	0x27
270*13540260SHans Petter Selasky 
271*13540260SHans Petter Selasky /* Error codes */
272*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_INVALID		0x00
273*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SUCCESS		0x01
274*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_DATA_BUF		0x02
275*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_BABBLE		0x03
276*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_XACT		0x04
277*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_TRB		0x05
278*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_STALL		0x06
279*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_RESOURCE		0x07
280*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_BANDWIDTH	0x08
281*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_NO_SLOTS		0x09
282*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_STREAM_TYPE	0x0A
283*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SLOT_NOT_ON	0x0B
284*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_ENDP_NOT_ON	0x0C
285*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SHORT_PKT	0x0D
286*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_RING_UNDERRUN	0x0E
287*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_RING_OVERRUN	0x0F
288*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_VF_RING_FULL	0x10
289*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_PARAMETER	0x11
290*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_BW_OVERRUN	0x12
291*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_CONTEXT_STATE	0x13
292*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_NO_PING_RESP	0x14
293*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_EV_RING_FULL	0x15
294*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_INCOMPAT_DEV	0x16
295*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_MISSED_SERVICE	0x17
296*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_CMD_RING_STOP	0x18
297*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_CMD_ABORTED	0x19
298*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_STOPPED		0x1A
299*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_LENGTH		0x1B
300*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_BAD_MELAT	0x1D
301*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_ISOC_OVERRUN	0x1F
302*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_EVENT_LOST	0x20
303*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_UNDEFINED	0x21
304*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_INVALID_SID	0x22
305*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SEC_BW		0x23
306*13540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SPLIT_XACT	0x24
307*13540260SHans Petter Selasky } __aligned(4);
308*13540260SHans Petter Selasky 
309*13540260SHans Petter Selasky struct xhci_dev_endpoint_trbs {
310*13540260SHans Petter Selasky 	struct xhci_trb		trb[XHCI_MAX_ENDPOINTS][XHCI_MAX_TRANSFERS];
311*13540260SHans Petter Selasky };
312*13540260SHans Petter Selasky 
313*13540260SHans Petter Selasky #define	XHCI_TD_PAGE_NBUF	17	/* units, room enough for 64Kbytes */
314*13540260SHans Petter Selasky #define	XHCI_TD_PAGE_SIZE	4096	/* bytes */
315*13540260SHans Petter Selasky #define	XHCI_TD_PAYLOAD_MAX	(XHCI_TD_PAGE_SIZE * (XHCI_TD_PAGE_NBUF - 1))
316*13540260SHans Petter Selasky 
317*13540260SHans Petter Selasky struct xhci_td {
318*13540260SHans Petter Selasky 	struct xhci_trb		td_trb[XHCI_TD_PAGE_NBUF + 1];
319*13540260SHans Petter Selasky 
320*13540260SHans Petter Selasky /*
321*13540260SHans Petter Selasky  * Extra information needed:
322*13540260SHans Petter Selasky  */
323*13540260SHans Petter Selasky 	uint64_t		td_self;
324*13540260SHans Petter Selasky 	struct xhci_td		*next;
325*13540260SHans Petter Selasky 	struct xhci_td		*alt_next;
326*13540260SHans Petter Selasky 	struct xhci_td		*obj_next;
327*13540260SHans Petter Selasky 	struct usb_page_cache	*page_cache;
328*13540260SHans Petter Selasky 	uint32_t		len;
329*13540260SHans Petter Selasky 	uint32_t		remainder;
330*13540260SHans Petter Selasky 	uint8_t			ntrb;
331*13540260SHans Petter Selasky 	uint8_t			status;
332*13540260SHans Petter Selasky } __aligned(XHCI_TRB_ALIGN);
333*13540260SHans Petter Selasky 
334*13540260SHans Petter Selasky struct xhci_command {
335*13540260SHans Petter Selasky 	struct xhci_trb		trb;
336*13540260SHans Petter Selasky 	TAILQ_ENTRY(xhci_command) entry;
337*13540260SHans Petter Selasky };
338*13540260SHans Petter Selasky 
339*13540260SHans Petter Selasky struct xhci_event_ring_seg {
340*13540260SHans Petter Selasky 	volatile uint64_t	qwEvrsTablePtr;
341*13540260SHans Petter Selasky 	volatile uint32_t	dwEvrsTableSize;
342*13540260SHans Petter Selasky 	volatile uint32_t	dwEvrsReserved;
343*13540260SHans Petter Selasky };
344*13540260SHans Petter Selasky 
345*13540260SHans Petter Selasky struct xhci_hw_root {
346*13540260SHans Petter Selasky 	struct xhci_event_ring_seg	hwr_ring_seg[XHCI_MAX_RSEG];
347*13540260SHans Petter Selasky 	struct {
348*13540260SHans Petter Selasky 		volatile uint64_t dummy;
349*13540260SHans Petter Selasky 	} __aligned(64)			padding;
350*13540260SHans Petter Selasky 	struct xhci_trb			hwr_events[XHCI_MAX_EVENTS];
351*13540260SHans Petter Selasky 	struct xhci_trb			hwr_commands[XHCI_MAX_COMMANDS];
352*13540260SHans Petter Selasky };
353*13540260SHans Petter Selasky 
354*13540260SHans Petter Selasky struct xhci_endpoint_ext {
355*13540260SHans Petter Selasky 	struct xhci_trb		*trb;
356*13540260SHans Petter Selasky 	struct usb_xfer		*xfer[XHCI_MAX_TRANSFERS - 1];
357*13540260SHans Petter Selasky 	struct usb_page_cache	*page_cache;
358*13540260SHans Petter Selasky 	uint64_t		physaddr;
359*13540260SHans Petter Selasky 	uint8_t			trb_used;
360*13540260SHans Petter Selasky 	uint8_t			trb_index;
361*13540260SHans Petter Selasky 	uint8_t			trb_halted;
362*13540260SHans Petter Selasky 	uint8_t			trb_running;
363*13540260SHans Petter Selasky };
364*13540260SHans Petter Selasky 
365*13540260SHans Petter Selasky enum {
366*13540260SHans Petter Selasky 	XHCI_ST_DISABLED,
367*13540260SHans Petter Selasky 	XHCI_ST_ENABLED,
368*13540260SHans Petter Selasky 	XHCI_ST_DEFAULT,
369*13540260SHans Petter Selasky 	XHCI_ST_ADDRESSED,
370*13540260SHans Petter Selasky 	XHCI_ST_CONFIGURED,
371*13540260SHans Petter Selasky 	XHCI_ST_MAX
372*13540260SHans Petter Selasky };
373*13540260SHans Petter Selasky 
374*13540260SHans Petter Selasky struct xhci_hw_dev {
375*13540260SHans Petter Selasky 	struct usb_page_cache	device_pc;
376*13540260SHans Petter Selasky 	struct usb_page_cache	input_pc;
377*13540260SHans Petter Selasky 	struct usb_page_cache	endpoint_pc;
378*13540260SHans Petter Selasky 
379*13540260SHans Petter Selasky 	struct usb_page		device_pg;
380*13540260SHans Petter Selasky 	struct usb_page		input_pg;
381*13540260SHans Petter Selasky 	struct usb_page		endpoint_pg;
382*13540260SHans Petter Selasky 
383*13540260SHans Petter Selasky 	struct xhci_endpoint_ext endp[XHCI_MAX_ENDPOINTS];
384*13540260SHans Petter Selasky 
385*13540260SHans Petter Selasky 	uint8_t			state;
386*13540260SHans Petter Selasky 	uint8_t			nports;
387*13540260SHans Petter Selasky 	uint8_t			tt;
388*13540260SHans Petter Selasky 	uint8_t			reserved;
389*13540260SHans Petter Selasky };
390*13540260SHans Petter Selasky 
391*13540260SHans Petter Selasky struct xhci_hw_softc {
392*13540260SHans Petter Selasky 	struct usb_page_cache	root_pc;
393*13540260SHans Petter Selasky 	struct usb_page_cache	ctx_pc;
394*13540260SHans Petter Selasky 	struct usb_page_cache	scratch_pc[XHCI_MAX_SCRATCHPADS];
395*13540260SHans Petter Selasky 
396*13540260SHans Petter Selasky 	struct usb_page		root_pg;
397*13540260SHans Petter Selasky 	struct usb_page		ctx_pg;
398*13540260SHans Petter Selasky 	struct usb_page		scratch_pg[XHCI_MAX_SCRATCHPADS];
399*13540260SHans Petter Selasky 
400*13540260SHans Petter Selasky 	struct xhci_hw_dev	devs[XHCI_MAX_DEVICES + 1];
401*13540260SHans Petter Selasky };
402*13540260SHans Petter Selasky 
403*13540260SHans Petter Selasky struct xhci_config_desc {
404*13540260SHans Petter Selasky 	struct usb_config_descriptor		confd;
405*13540260SHans Petter Selasky 	struct usb_interface_descriptor		ifcd;
406*13540260SHans Petter Selasky 	struct usb_endpoint_descriptor		endpd;
407*13540260SHans Petter Selasky 	struct usb_endpoint_ss_comp_descriptor	endpcd;
408*13540260SHans Petter Selasky } __packed;
409*13540260SHans Petter Selasky 
410*13540260SHans Petter Selasky struct xhci_bos_desc {
411*13540260SHans Petter Selasky 	struct usb_bos_descriptor		bosd;
412*13540260SHans Petter Selasky 	struct usb_devcap_usb2ext_descriptor	usb2extd;
413*13540260SHans Petter Selasky 	struct usb_devcap_ss_descriptor		usbdcd;
414*13540260SHans Petter Selasky 	struct usb_devcap_container_id_descriptor cidd;
415*13540260SHans Petter Selasky } __packed;
416*13540260SHans Petter Selasky 
417*13540260SHans Petter Selasky union xhci_hub_desc {
418*13540260SHans Petter Selasky 	struct usb_status		stat;
419*13540260SHans Petter Selasky 	struct usb_port_status		ps;
420*13540260SHans Petter Selasky 	struct usb_hub_ss_descriptor	hubd;
421*13540260SHans Petter Selasky 	uint8_t				temp[128];
422*13540260SHans Petter Selasky };
423*13540260SHans Petter Selasky 
424*13540260SHans Petter Selasky struct xhci_softc {
425*13540260SHans Petter Selasky 	struct xhci_hw_softc	sc_hw;
426*13540260SHans Petter Selasky 	/* base device */
427*13540260SHans Petter Selasky 	struct usb_bus		sc_bus;
428*13540260SHans Petter Selasky 	/* configure process */
429*13540260SHans Petter Selasky 	struct usb_process	sc_config_proc;
430*13540260SHans Petter Selasky 	struct usb_bus_msg	sc_config_msg[2];
431*13540260SHans Petter Selasky 
432*13540260SHans Petter Selasky 	union xhci_hub_desc	sc_hub_desc;
433*13540260SHans Petter Selasky 
434*13540260SHans Petter Selasky 	struct cv		sc_cmd_cv;
435*13540260SHans Petter Selasky 	struct sx		sc_cmd_sx;
436*13540260SHans Petter Selasky 
437*13540260SHans Petter Selasky 	struct usb_device	*sc_devices[XHCI_MAX_DEVICES];
438*13540260SHans Petter Selasky 	struct resource		*sc_io_res;
439*13540260SHans Petter Selasky 	struct resource		*sc_irq_res;
440*13540260SHans Petter Selasky 
441*13540260SHans Petter Selasky 	void			*sc_intr_hdl;
442*13540260SHans Petter Selasky 	bus_size_t		sc_io_size;
443*13540260SHans Petter Selasky 	bus_space_tag_t		sc_io_tag;
444*13540260SHans Petter Selasky 	bus_space_handle_t	sc_io_hdl;
445*13540260SHans Petter Selasky 	/* last pending command address */
446*13540260SHans Petter Selasky 	uint64_t		sc_cmd_addr;
447*13540260SHans Petter Selasky 	/* result of command */
448*13540260SHans Petter Selasky 	uint32_t		sc_cmd_result[2];
449*13540260SHans Petter Selasky  	/* copy of cmd register */
450*13540260SHans Petter Selasky 	uint32_t		sc_cmd;
451*13540260SHans Petter Selasky 	/* worst case exit latency */
452*13540260SHans Petter Selasky 	uint32_t		sc_exit_lat_max;
453*13540260SHans Petter Selasky 
454*13540260SHans Petter Selasky 	/* offset to operational registers */
455*13540260SHans Petter Selasky 	uint32_t		sc_oper_off;
456*13540260SHans Petter Selasky 	/* offset to capability registers */
457*13540260SHans Petter Selasky 	uint32_t		sc_capa_off;
458*13540260SHans Petter Selasky 	/* offset to runtime registers */
459*13540260SHans Petter Selasky 	uint32_t		sc_runt_off;
460*13540260SHans Petter Selasky 	/* offset to doorbell registers */
461*13540260SHans Petter Selasky 	uint32_t		sc_door_off;
462*13540260SHans Petter Selasky 
463*13540260SHans Petter Selasky 	/* chip specific */
464*13540260SHans Petter Selasky 	uint16_t		sc_erst_max;
465*13540260SHans Petter Selasky 	uint16_t		sc_event_idx;
466*13540260SHans Petter Selasky 	uint16_t		sc_command_idx;
467*13540260SHans Petter Selasky 
468*13540260SHans Petter Selasky 	uint8_t			sc_event_ccs;
469*13540260SHans Petter Selasky 	uint8_t			sc_command_ccs;
470*13540260SHans Petter Selasky 	/* number of XHCI device slots */
471*13540260SHans Petter Selasky 	uint8_t			sc_noslot;
472*13540260SHans Petter Selasky 	/* number of ports on root HUB */
473*13540260SHans Petter Selasky 	uint8_t			sc_noport;
474*13540260SHans Petter Selasky 	/* number of scratch pages */
475*13540260SHans Petter Selasky 	uint8_t			sc_noscratch;
476*13540260SHans Petter Selasky 	/* root HUB device configuration */
477*13540260SHans Petter Selasky 	uint8_t			sc_conf;
478*13540260SHans Petter Selasky 	uint8_t			sc_hub_idata[2];
479*13540260SHans Petter Selasky 
480*13540260SHans Petter Selasky 	/* vendor string for root HUB */
481*13540260SHans Petter Selasky 	char			sc_vendor[16];
482*13540260SHans Petter Selasky };
483*13540260SHans Petter Selasky 
484*13540260SHans Petter Selasky #define	XHCI_CMD_LOCK(sc)	sx_xlock(&(sc)->sc_cmd_sx)
485*13540260SHans Petter Selasky #define	XHCI_CMD_UNLOCK(sc)	sx_xunlock(&(sc)->sc_cmd_sx)
486*13540260SHans Petter Selasky #define	XHCI_CMD_ASSERT_LOCKED(sc) sx_assert(&(sc)->sc_cmd_sx, SA_LOCKED)
487*13540260SHans Petter Selasky 
488*13540260SHans Petter Selasky /* prototypes */
489*13540260SHans Petter Selasky 
490*13540260SHans Petter Selasky usb_error_t xhci_halt_controller(struct xhci_softc *);
491*13540260SHans Petter Selasky usb_error_t xhci_init(struct xhci_softc *, device_t);
492*13540260SHans Petter Selasky usb_error_t xhci_start_controller(struct xhci_softc *);
493*13540260SHans Petter Selasky void	xhci_interrupt(struct xhci_softc *);
494*13540260SHans Petter Selasky void	xhci_resume(struct xhci_softc *);
495*13540260SHans Petter Selasky void	xhci_shutdown(struct xhci_softc *);
496*13540260SHans Petter Selasky void	xhci_suspend(struct xhci_softc *);
497*13540260SHans Petter Selasky void	xhci_uninit(struct xhci_softc *);
498*13540260SHans Petter Selasky 
499*13540260SHans Petter Selasky #endif					/* _XHCI_H_ */
500