xref: /freebsd/sys/dev/usb/controller/xhci.h (revision c1338c65d63b78f4f3a9c95dc11b3edd85a58135)
113540260SHans Petter Selasky /* $FreeBSD$ */
213540260SHans Petter Selasky 
313540260SHans Petter Selasky /*-
413540260SHans Petter Selasky  * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
513540260SHans Petter Selasky  *
613540260SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
713540260SHans Petter Selasky  * modification, are permitted provided that the following conditions
813540260SHans Petter Selasky  * are met:
913540260SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
1013540260SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer.
1113540260SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
1213540260SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
1313540260SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
1413540260SHans Petter Selasky  *
1513540260SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1613540260SHans Petter Selasky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1713540260SHans Petter Selasky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1813540260SHans Petter Selasky  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1913540260SHans Petter Selasky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2013540260SHans Petter Selasky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2113540260SHans Petter Selasky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2213540260SHans Petter Selasky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2313540260SHans Petter Selasky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2413540260SHans Petter Selasky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2513540260SHans Petter Selasky  * SUCH DAMAGE.
2613540260SHans Petter Selasky  */
2713540260SHans Petter Selasky 
2813540260SHans Petter Selasky #ifndef _XHCI_H_
2913540260SHans Petter Selasky #define	_XHCI_H_
3013540260SHans Petter Selasky 
3113540260SHans Petter Selasky #define	XHCI_MAX_DEVICES	MIN(USB_MAX_DEVICES, 128)
3213540260SHans Petter Selasky #define	XHCI_MAX_ENDPOINTS	32	/* hardcoded - do not change */
3313540260SHans Petter Selasky #define	XHCI_MAX_SCRATCHPADS	32
3413540260SHans Petter Selasky #define	XHCI_MAX_EVENTS		(16 * 13)
3513540260SHans Petter Selasky #define	XHCI_MAX_COMMANDS	(16 * 1)
3613540260SHans Petter Selasky #define	XHCI_MAX_RSEG		1
3713540260SHans Petter Selasky #define	XHCI_MAX_TRANSFERS	4
3813540260SHans Petter Selasky 
3913540260SHans Petter Selasky #define	XHCI_DEV_CTX_ADDR_ALIGN		64	/* bytes */
4013540260SHans Petter Selasky #define	XHCI_DEV_CTX_ALIGN		64	/* bytes */
4113540260SHans Petter Selasky #define	XHCI_INPUT_CTX_ALIGN		64	/* bytes */
4213540260SHans Petter Selasky #define	XHCI_SLOT_CTX_ALIGN		32	/* bytes */
4313540260SHans Petter Selasky #define	XHCI_ENDP_CTX_ALIGN		32	/* bytes */
4413540260SHans Petter Selasky #define	XHCI_STREAM_CTX_ALIGN		16	/* bytes */
4513540260SHans Petter Selasky #define	XHCI_TRANS_RING_SEG_ALIGN	16	/* bytes */
4613540260SHans Petter Selasky #define	XHCI_CMD_RING_SEG_ALIGN		64	/* bytes */
4713540260SHans Petter Selasky #define	XHCI_EVENT_RING_SEG_ALIGN	64	/* bytes */
4813540260SHans Petter Selasky #define	XHCI_SCRATCH_BUF_ARRAY_ALIGN	64	/* bytes */
4913540260SHans Petter Selasky #define	XHCI_SCRATCH_BUFFER_ALIGN	USB_PAGE_SIZE
5013540260SHans Petter Selasky #define	XHCI_TRB_ALIGN			16	/* bytes */
5113540260SHans Petter Selasky #define	XHCI_TD_ALIGN			64	/* bytes */
5213540260SHans Petter Selasky #define	XHCI_PAGE_SIZE			4096	/* bytes */
5313540260SHans Petter Selasky 
5413540260SHans Petter Selasky struct xhci_dev_ctx_addr {
5513540260SHans Petter Selasky 	volatile uint64_t	qwBaaDevCtxAddr[USB_MAX_DEVICES + 1];
5613540260SHans Petter Selasky 	struct {
5713540260SHans Petter Selasky 		volatile uint64_t dummy;
5813540260SHans Petter Selasky 	} __aligned(64) padding;
5913540260SHans Petter Selasky 	volatile uint64_t	qwSpBufPtr[XHCI_MAX_SCRATCHPADS];
6013540260SHans Petter Selasky };
6113540260SHans Petter Selasky 
6213540260SHans Petter Selasky #define	XHCI_EPNO2EPID(x) \
6313540260SHans Petter Selasky     ((((x) & UE_DIR_IN) ? 1 : 0) | (2 * ((x) & UE_ADDR)))
6413540260SHans Petter Selasky 
6513540260SHans Petter Selasky struct xhci_slot_ctx {
6613540260SHans Petter Selasky 	volatile uint32_t	dwSctx0;
6713540260SHans Petter Selasky #define	XHCI_SCTX_0_ROUTE_SET(x)		((x) & 0xFFFFF)
6813540260SHans Petter Selasky #define	XHCI_SCTX_0_ROUTE_GET(x)		((x) & 0xFFFFF)
6913540260SHans Petter Selasky #define	XHCI_SCTX_0_SPEED_SET(x)		(((x) & 0xF) << 20)
7013540260SHans Petter Selasky #define	XHCI_SCTX_0_SPEED_GET(x)		(((x) >> 20) & 0xF)
7113540260SHans Petter Selasky #define	XHCI_SCTX_0_MTT_SET(x)			(((x) & 0x1) << 25)
7213540260SHans Petter Selasky #define	XHCI_SCTX_0_MTT_GET(x)			(((x) >> 25) & 0x1)
7313540260SHans Petter Selasky #define	XHCI_SCTX_0_HUB_SET(x)			(((x) & 0x1) << 26)
7413540260SHans Petter Selasky #define	XHCI_SCTX_0_HUB_GET(x)			(((x) >> 26) & 0x1)
7513540260SHans Petter Selasky #define	XHCI_SCTX_0_CTX_NUM_SET(x)		(((x) & 0x1F) << 27)
7613540260SHans Petter Selasky #define	XHCI_SCTX_0_CTX_NUM_GET(x)		(((x) >> 27) & 0x1F)
7713540260SHans Petter Selasky 	volatile uint32_t	dwSctx1;
7813540260SHans Petter Selasky #define	XHCI_SCTX_1_MAX_EL_SET(x)		((x) & 0xFFFF)
7913540260SHans Petter Selasky #define	XHCI_SCTX_1_MAX_EL_GET(x)		((x) & 0xFFFF)
8013540260SHans Petter Selasky #define	XHCI_SCTX_1_RH_PORT_SET(x)		(((x) & 0xFF) << 16)
8113540260SHans Petter Selasky #define	XHCI_SCTX_1_RH_PORT_GET(x)		(((x) >> 16) & 0xFF)
8213540260SHans Petter Selasky #define	XHCI_SCTX_1_NUM_PORTS_SET(x)		(((x) & 0xFF) << 24)
8313540260SHans Petter Selasky #define	XHCI_SCTX_1_NUM_PORTS_GET(x)		(((x) >> 24) & 0xFF)
8413540260SHans Petter Selasky 	volatile uint32_t	dwSctx2;
8513540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_HUB_SID_SET(x)		((x) & 0xFF)
8613540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_HUB_SID_GET(x)		((x) & 0xFF)
8713540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_PORT_NUM_SET(x)		(((x) & 0xFF) << 8)
8813540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_PORT_NUM_GET(x)		(((x) >> 8) & 0xFF)
8913540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_THINK_TIME_SET(x)	(((x) & 0x3) << 16)
9013540260SHans Petter Selasky #define	XHCI_SCTX_2_TT_THINK_TIME_GET(x)	(((x) >> 16) & 0x3)
9113540260SHans Petter Selasky #define	XHCI_SCTX_2_IRQ_TARGET_SET(x)		(((x) & 0x3FF) << 22)
9213540260SHans Petter Selasky #define	XHCI_SCTX_2_IRQ_TARGET_GET(x)		(((x) >> 22) & 0x3FF)
9313540260SHans Petter Selasky 	volatile uint32_t	dwSctx3;
9413540260SHans Petter Selasky #define	XHCI_SCTX_3_DEV_ADDR_SET(x)		((x) & 0xFF)
9513540260SHans Petter Selasky #define	XHCI_SCTX_3_DEV_ADDR_GET(x)		((x) & 0xFF)
9613540260SHans Petter Selasky #define	XHCI_SCTX_3_SLOT_STATE_SET(x)		(((x) & 0x1F) << 27)
9713540260SHans Petter Selasky #define	XHCI_SCTX_3_SLOT_STATE_GET(x)		(((x) >> 27) & 0x1F)
9813540260SHans Petter Selasky 	volatile uint32_t	dwSctx4;
9913540260SHans Petter Selasky 	volatile uint32_t	dwSctx5;
10013540260SHans Petter Selasky 	volatile uint32_t	dwSctx6;
10113540260SHans Petter Selasky 	volatile uint32_t	dwSctx7;
10213540260SHans Petter Selasky };
10313540260SHans Petter Selasky 
10413540260SHans Petter Selasky struct xhci_endp_ctx {
10513540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx0;
10613540260SHans Petter Selasky #define	XHCI_EPCTX_0_EPSTATE_SET(x)		((x) & 0x7)
10713540260SHans Petter Selasky #define	XHCI_EPCTX_0_EPSTATE_GET(x)		((x) & 0x7)
10813540260SHans Petter Selasky #define	XHCI_EPCTX_0_MULT_SET(x)		(((x) & 0x3) << 8)
10913540260SHans Petter Selasky #define	XHCI_EPCTX_0_MULT_GET(x)		(((x) >> 8) & 0x3)
11013540260SHans Petter Selasky #define	XHCI_EPCTX_0_MAXP_STREAMS_SET(x)	(((x) & 0x1F) << 10)
11113540260SHans Petter Selasky #define	XHCI_EPCTX_0_MAXP_STREAMS_GET(x)	(((x) >> 10) & 0x1F)
11213540260SHans Petter Selasky #define	XHCI_EPCTX_0_LSA_SET(x)			(((x) & 0x1) << 15)
11313540260SHans Petter Selasky #define	XHCI_EPCTX_0_LSA_GET(x)			(((x) >> 15) & 0x1)
11413540260SHans Petter Selasky #define	XHCI_EPCTX_0_IVAL_SET(x)		(((x) & 0xFF) << 16)
11513540260SHans Petter Selasky #define	XHCI_EPCTX_0_IVAL_GET(x)		(((x) >> 16) & 0xFF)
11613540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx1;
11713540260SHans Petter Selasky #define	XHCI_EPCTX_1_CERR_SET(x)		(((x) & 0x3) << 1)
11813540260SHans Petter Selasky #define	XHCI_EPCTX_1_CERR_GET(x)		(((x) >> 1) & 0x3)
11913540260SHans Petter Selasky #define	XHCI_EPCTX_1_EPTYPE_SET(x)		(((x) & 0x7) << 3)
12013540260SHans Petter Selasky #define	XHCI_EPCTX_1_EPTYPE_GET(x)		(((x) >> 3) & 0x7)
12113540260SHans Petter Selasky #define	XHCI_EPCTX_1_HID_SET(x)			(((x) & 0x1) << 7)
12213540260SHans Petter Selasky #define	XHCI_EPCTX_1_HID_GET(x)			(((x) >> 7) & 0x1)
12313540260SHans Petter Selasky #define	XHCI_EPCTX_1_MAXB_SET(x)		(((x) & 0xFF) << 8)
12413540260SHans Petter Selasky #define	XHCI_EPCTX_1_MAXB_GET(x)		(((x) >> 8) & 0xFF)
12513540260SHans Petter Selasky #define	XHCI_EPCTX_1_MAXP_SIZE_SET(x)		(((x) & 0xFFFF) << 16)
12613540260SHans Petter Selasky #define	XHCI_EPCTX_1_MAXP_SIZE_GET(x)		(((x) >> 16) & 0xFFFF)
12713540260SHans Petter Selasky 	volatile uint64_t	qwEpCtx2;
12813540260SHans Petter Selasky #define	XHCI_EPCTX_2_DCS_SET(x)			((x) & 0x1)
12913540260SHans Petter Selasky #define	XHCI_EPCTX_2_DCS_GET(x)			((x) & 0x1)
13013540260SHans Petter Selasky #define	XHCI_EPCTX_2_TR_DQ_PTR_MASK		0xFFFFFFFFFFFFFFF0U
13113540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx4;
13213540260SHans Petter Selasky #define	XHCI_EPCTX_4_AVG_TRB_LEN_SET(x)		((x) & 0xFFFF)
13313540260SHans Petter Selasky #define	XHCI_EPCTX_4_AVG_TRB_LEN_GET(x)		((x) & 0xFFFF)
13413540260SHans Petter Selasky #define	XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(x)	(((x) & 0xFFFF) << 16)
13513540260SHans Petter Selasky #define	XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_GET(x)	(((x) >> 16) & 0xFFFF)
13613540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx5;
13713540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx6;
13813540260SHans Petter Selasky 	volatile uint32_t	dwEpCtx7;
13913540260SHans Petter Selasky };
14013540260SHans Petter Selasky 
14113540260SHans Petter Selasky struct xhci_input_ctx {
14213540260SHans Petter Selasky #define	XHCI_INCTX_NON_CTRL_MASK	0xFFFFFFFCU
14313540260SHans Petter Selasky 	volatile uint32_t	dwInCtx0;
14413540260SHans Petter Selasky #define	XHCI_INCTX_0_DROP_MASK(n)	(1U << (n))
14513540260SHans Petter Selasky 	volatile uint32_t	dwInCtx1;
14613540260SHans Petter Selasky #define	XHCI_INCTX_1_ADD_MASK(n)	(1U << (n))
14713540260SHans Petter Selasky 	volatile uint32_t	dwInCtx2;
14813540260SHans Petter Selasky 	volatile uint32_t	dwInCtx3;
14913540260SHans Petter Selasky 	volatile uint32_t	dwInCtx4;
15013540260SHans Petter Selasky 	volatile uint32_t	dwInCtx5;
15113540260SHans Petter Selasky 	volatile uint32_t	dwInCtx6;
15213540260SHans Petter Selasky 	volatile uint32_t	dwInCtx7;
15313540260SHans Petter Selasky };
15413540260SHans Petter Selasky 
15513540260SHans Petter Selasky struct xhci_input_dev_ctx {
15613540260SHans Petter Selasky 	struct xhci_input_ctx	ctx_input;
15713540260SHans Petter Selasky 	struct xhci_slot_ctx	ctx_slot;
15813540260SHans Petter Selasky 	struct xhci_endp_ctx	ctx_ep[XHCI_MAX_ENDPOINTS - 1];
15913540260SHans Petter Selasky };
16013540260SHans Petter Selasky 
16113540260SHans Petter Selasky struct xhci_dev_ctx {
16213540260SHans Petter Selasky 	struct xhci_slot_ctx	ctx_slot;
16313540260SHans Petter Selasky 	struct xhci_endp_ctx	ctx_ep[XHCI_MAX_ENDPOINTS - 1];
16413540260SHans Petter Selasky } __aligned(XHCI_DEV_CTX_ALIGN);
16513540260SHans Petter Selasky 
16613540260SHans Petter Selasky struct xhci_stream_ctx {
16713540260SHans Petter Selasky 	volatile uint64_t	qwSctx0;
16813540260SHans Petter Selasky #define	XHCI_SCTX_0_DCS_GET(x)		((x) & 0x1)
16913540260SHans Petter Selasky #define	XHCI_SCTX_0_DCS_SET(x)		((x) & 0x1)
17013540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_SET(x)		(((x) & 0x7) << 1)
17113540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_GET(x)		(((x) >> 1) & 0x7)
17213540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_SEC_TR_RING	0x0
17313540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_TR_RING	0x1
17413540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_8	0x2
17513540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_16	0x3
17613540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_32	0x4
17713540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_64	0x5
17813540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_128	0x6
17913540260SHans Petter Selasky #define	XHCI_SCTX_0_SCT_PRIM_SSA_256	0x7
18013540260SHans Petter Selasky #define	XHCI_SCTX_0_TR_DQ_PTR_MASK	0xFFFFFFFFFFFFFFF0U
18113540260SHans Petter Selasky 	volatile uint32_t	dwSctx2;
18213540260SHans Petter Selasky 	volatile uint32_t	dwSctx3;
18313540260SHans Petter Selasky };
18413540260SHans Petter Selasky 
18513540260SHans Petter Selasky struct xhci_trb {
18613540260SHans Petter Selasky 	volatile uint64_t	qwTrb0;
18713540260SHans Petter Selasky #define	XHCI_TRB_0_WLENGTH_MASK		(0xFFFFULL << 48)
18813540260SHans Petter Selasky 	volatile uint32_t	dwTrb2;
18913540260SHans Petter Selasky #define	XHCI_TRB_2_ERROR_GET(x)		(((x) >> 24) & 0xFF)
19013540260SHans Petter Selasky #define	XHCI_TRB_2_ERROR_SET(x)		(((x) & 0xFF) << 24)
19113540260SHans Petter Selasky #define	XHCI_TRB_2_TDSZ_GET(x)		(((x) >> 17) & 0x1F)
19213540260SHans Petter Selasky #define	XHCI_TRB_2_TDSZ_SET(x)		(((x) & 0x1F) << 17)
19313540260SHans Petter Selasky #define	XHCI_TRB_2_REM_GET(x)		((x) & 0xFFFFFF)
19413540260SHans Petter Selasky #define	XHCI_TRB_2_REM_SET(x)		((x) & 0xFFFFFF)
19513540260SHans Petter Selasky #define	XHCI_TRB_2_BYTES_GET(x)		((x) & 0x1FFFF)
19613540260SHans Petter Selasky #define	XHCI_TRB_2_BYTES_SET(x)		((x) & 0x1FFFF)
19713540260SHans Petter Selasky #define	XHCI_TRB_2_IRQ_GET(x)		(((x) >> 22) & 0x3FF)
19813540260SHans Petter Selasky #define	XHCI_TRB_2_IRQ_SET(x)		(((x) & 0x3FF) << 22)
19913540260SHans Petter Selasky #define	XHCI_TRB_2_STREAM_GET(x)	(((x) >> 16) & 0xFFFF)
20013540260SHans Petter Selasky #define	XHCI_TRB_2_STREAM_SET(x)	(((x) & 0xFFFF) << 16)
20113540260SHans Petter Selasky 
20213540260SHans Petter Selasky 	volatile uint32_t	dwTrb3;
20313540260SHans Petter Selasky #define	XHCI_TRB_3_TYPE_GET(x)		(((x) >> 10) & 0x3F)
20413540260SHans Petter Selasky #define	XHCI_TRB_3_TYPE_SET(x)		(((x) & 0x3F) << 10)
20513540260SHans Petter Selasky #define	XHCI_TRB_3_CYCLE_BIT		(1U << 0)
20613540260SHans Petter Selasky #define	XHCI_TRB_3_TC_BIT		(1U << 1)	/* command ring only */
20713540260SHans Petter Selasky #define	XHCI_TRB_3_ENT_BIT		(1U << 1)	/* transfer ring only */
20813540260SHans Petter Selasky #define	XHCI_TRB_3_ISP_BIT		(1U << 2)
20913540260SHans Petter Selasky #define	XHCI_TRB_3_NSNOOP_BIT		(1U << 3)
21013540260SHans Petter Selasky #define	XHCI_TRB_3_CHAIN_BIT		(1U << 4)
21113540260SHans Petter Selasky #define	XHCI_TRB_3_IOC_BIT		(1U << 5)
21213540260SHans Petter Selasky #define	XHCI_TRB_3_IDT_BIT		(1U << 6)
21313540260SHans Petter Selasky #define	XHCI_TRB_3_TBC_GET(x)		(((x) >> 7) & 3)
21413540260SHans Petter Selasky #define	XHCI_TRB_3_TBC_SET(x)		(((x) & 3) << 7)
21513540260SHans Petter Selasky #define	XHCI_TRB_3_BEI_BIT		(1U << 9)
21613540260SHans Petter Selasky #define	XHCI_TRB_3_DCEP_BIT		(1U << 9)
21713540260SHans Petter Selasky #define	XHCI_TRB_3_PRSV_BIT		(1U << 9)
21813540260SHans Petter Selasky #define	XHCI_TRB_3_BSR_BIT		(1U << 9)
21913540260SHans Petter Selasky #define	XHCI_TRB_3_TRT_MASK		(3U << 16)
22013540260SHans Petter Selasky #define	XHCI_TRB_3_TRT_NONE		(0U << 16)
22113540260SHans Petter Selasky #define	XHCI_TRB_3_TRT_OUT		(2U << 16)
22213540260SHans Petter Selasky #define	XHCI_TRB_3_TRT_IN		(3U << 16)
22313540260SHans Petter Selasky #define	XHCI_TRB_3_DIR_IN		(1U << 16)
22413540260SHans Petter Selasky #define	XHCI_TRB_3_TLBPC_GET(x)		(((x) >> 16) & 0xF)
22513540260SHans Petter Selasky #define	XHCI_TRB_3_TLBPC_SET(x)		(((x) & 0xF) << 16)
22613540260SHans Petter Selasky #define	XHCI_TRB_3_EP_GET(x)		(((x) >> 16) & 0x1F)
22713540260SHans Petter Selasky #define	XHCI_TRB_3_EP_SET(x)		(((x) & 0x1F) << 16)
22813540260SHans Petter Selasky #define	XHCI_TRB_3_FRID_GET(x)		(((x) >> 20) & 0x7FF)
22913540260SHans Petter Selasky #define	XHCI_TRB_3_FRID_SET(x)		(((x) & 0x7FF) << 20)
23013540260SHans Petter Selasky #define	XHCI_TRB_3_ISO_SIA_BIT		(1U << 31)
23113540260SHans Petter Selasky #define	XHCI_TRB_3_SUSP_EP_BIT		(1U << 23)
23213540260SHans Petter Selasky #define	XHCI_TRB_3_SLOT_GET(x)		(((x) >> 24) & 0xFF)
23313540260SHans Petter Selasky #define	XHCI_TRB_3_SLOT_SET(x)		(((x) & 0xFF) << 24)
23413540260SHans Petter Selasky 
23513540260SHans Petter Selasky /* Commands */
23613540260SHans Petter Selasky #define	XHCI_TRB_TYPE_RESERVED		0x00
23713540260SHans Petter Selasky #define	XHCI_TRB_TYPE_NORMAL		0x01
23813540260SHans Petter Selasky #define	XHCI_TRB_TYPE_SETUP_STAGE	0x02
23913540260SHans Petter Selasky #define	XHCI_TRB_TYPE_DATA_STAGE	0x03
24013540260SHans Petter Selasky #define	XHCI_TRB_TYPE_STATUS_STAGE	0x04
24113540260SHans Petter Selasky #define	XHCI_TRB_TYPE_ISOCH		0x05
24213540260SHans Petter Selasky #define	XHCI_TRB_TYPE_LINK		0x06
24313540260SHans Petter Selasky #define	XHCI_TRB_TYPE_EVENT_DATA	0x07
24413540260SHans Petter Selasky #define	XHCI_TRB_TYPE_NOOP		0x08
24513540260SHans Petter Selasky #define	XHCI_TRB_TYPE_ENABLE_SLOT	0x09
24613540260SHans Petter Selasky #define	XHCI_TRB_TYPE_DISABLE_SLOT	0x0A
24713540260SHans Petter Selasky #define	XHCI_TRB_TYPE_ADDRESS_DEVICE	0x0B
24813540260SHans Petter Selasky #define	XHCI_TRB_TYPE_CONFIGURE_EP	0x0C
24913540260SHans Petter Selasky #define	XHCI_TRB_TYPE_EVALUATE_CTX	0x0D
25013540260SHans Petter Selasky #define	XHCI_TRB_TYPE_RESET_EP		0x0E
25113540260SHans Petter Selasky #define	XHCI_TRB_TYPE_STOP_EP		0x0F
25213540260SHans Petter Selasky #define	XHCI_TRB_TYPE_SET_TR_DEQUEUE	0x10
25313540260SHans Petter Selasky #define	XHCI_TRB_TYPE_RESET_DEVICE	0x11
25413540260SHans Petter Selasky #define	XHCI_TRB_TYPE_FORCE_EVENT	0x12
25513540260SHans Petter Selasky #define	XHCI_TRB_TYPE_NEGOTIATE_BW	0x13
25613540260SHans Petter Selasky #define	XHCI_TRB_TYPE_SET_LATENCY_TOL  	0x14
25713540260SHans Petter Selasky #define	XHCI_TRB_TYPE_GET_PORT_BW	0x15
25813540260SHans Petter Selasky #define	XHCI_TRB_TYPE_FORCE_HEADER	0x16
25913540260SHans Petter Selasky #define	XHCI_TRB_TYPE_NOOP_CMD		0x17
26013540260SHans Petter Selasky 
26113540260SHans Petter Selasky /* Events */
26213540260SHans Petter Selasky #define	XHCI_TRB_EVENT_TRANSFER		0x20
26313540260SHans Petter Selasky #define	XHCI_TRB_EVENT_CMD_COMPLETE	0x21
26413540260SHans Petter Selasky #define	XHCI_TRB_EVENT_PORT_STS_CHANGE  0x22
26513540260SHans Petter Selasky #define	XHCI_TRB_EVENT_BW_REQUEST      	0x23
26613540260SHans Petter Selasky #define	XHCI_TRB_EVENT_DOORBELL		0x24
26713540260SHans Petter Selasky #define	XHCI_TRB_EVENT_HOST_CTRL	0x25
26813540260SHans Petter Selasky #define	XHCI_TRB_EVENT_DEVICE_NOTIFY	0x26
26913540260SHans Petter Selasky #define	XHCI_TRB_EVENT_MFINDEX_WRAP	0x27
27013540260SHans Petter Selasky 
27113540260SHans Petter Selasky /* Error codes */
27213540260SHans Petter Selasky #define	XHCI_TRB_ERROR_INVALID		0x00
27313540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SUCCESS		0x01
27413540260SHans Petter Selasky #define	XHCI_TRB_ERROR_DATA_BUF		0x02
27513540260SHans Petter Selasky #define	XHCI_TRB_ERROR_BABBLE		0x03
27613540260SHans Petter Selasky #define	XHCI_TRB_ERROR_XACT		0x04
27713540260SHans Petter Selasky #define	XHCI_TRB_ERROR_TRB		0x05
27813540260SHans Petter Selasky #define	XHCI_TRB_ERROR_STALL		0x06
27913540260SHans Petter Selasky #define	XHCI_TRB_ERROR_RESOURCE		0x07
28013540260SHans Petter Selasky #define	XHCI_TRB_ERROR_BANDWIDTH	0x08
28113540260SHans Petter Selasky #define	XHCI_TRB_ERROR_NO_SLOTS		0x09
28213540260SHans Petter Selasky #define	XHCI_TRB_ERROR_STREAM_TYPE	0x0A
28313540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SLOT_NOT_ON	0x0B
28413540260SHans Petter Selasky #define	XHCI_TRB_ERROR_ENDP_NOT_ON	0x0C
28513540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SHORT_PKT	0x0D
28613540260SHans Petter Selasky #define	XHCI_TRB_ERROR_RING_UNDERRUN	0x0E
28713540260SHans Petter Selasky #define	XHCI_TRB_ERROR_RING_OVERRUN	0x0F
28813540260SHans Petter Selasky #define	XHCI_TRB_ERROR_VF_RING_FULL	0x10
28913540260SHans Petter Selasky #define	XHCI_TRB_ERROR_PARAMETER	0x11
29013540260SHans Petter Selasky #define	XHCI_TRB_ERROR_BW_OVERRUN	0x12
29113540260SHans Petter Selasky #define	XHCI_TRB_ERROR_CONTEXT_STATE	0x13
29213540260SHans Petter Selasky #define	XHCI_TRB_ERROR_NO_PING_RESP	0x14
29313540260SHans Petter Selasky #define	XHCI_TRB_ERROR_EV_RING_FULL	0x15
29413540260SHans Petter Selasky #define	XHCI_TRB_ERROR_INCOMPAT_DEV	0x16
29513540260SHans Petter Selasky #define	XHCI_TRB_ERROR_MISSED_SERVICE	0x17
29613540260SHans Petter Selasky #define	XHCI_TRB_ERROR_CMD_RING_STOP	0x18
29713540260SHans Petter Selasky #define	XHCI_TRB_ERROR_CMD_ABORTED	0x19
29813540260SHans Petter Selasky #define	XHCI_TRB_ERROR_STOPPED		0x1A
29913540260SHans Petter Selasky #define	XHCI_TRB_ERROR_LENGTH		0x1B
30013540260SHans Petter Selasky #define	XHCI_TRB_ERROR_BAD_MELAT	0x1D
30113540260SHans Petter Selasky #define	XHCI_TRB_ERROR_ISOC_OVERRUN	0x1F
30213540260SHans Petter Selasky #define	XHCI_TRB_ERROR_EVENT_LOST	0x20
30313540260SHans Petter Selasky #define	XHCI_TRB_ERROR_UNDEFINED	0x21
30413540260SHans Petter Selasky #define	XHCI_TRB_ERROR_INVALID_SID	0x22
30513540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SEC_BW		0x23
30613540260SHans Petter Selasky #define	XHCI_TRB_ERROR_SPLIT_XACT	0x24
30713540260SHans Petter Selasky } __aligned(4);
30813540260SHans Petter Selasky 
30913540260SHans Petter Selasky struct xhci_dev_endpoint_trbs {
31013540260SHans Petter Selasky 	struct xhci_trb		trb[XHCI_MAX_ENDPOINTS][XHCI_MAX_TRANSFERS];
31113540260SHans Petter Selasky };
31213540260SHans Petter Selasky 
31313540260SHans Petter Selasky #define	XHCI_TD_PAGE_NBUF	17	/* units, room enough for 64Kbytes */
31413540260SHans Petter Selasky #define	XHCI_TD_PAGE_SIZE	4096	/* bytes */
31513540260SHans Petter Selasky #define	XHCI_TD_PAYLOAD_MAX	(XHCI_TD_PAGE_SIZE * (XHCI_TD_PAGE_NBUF - 1))
31613540260SHans Petter Selasky 
31713540260SHans Petter Selasky struct xhci_td {
31813540260SHans Petter Selasky 	struct xhci_trb		td_trb[XHCI_TD_PAGE_NBUF + 1];
31913540260SHans Petter Selasky 
32013540260SHans Petter Selasky /*
32113540260SHans Petter Selasky  * Extra information needed:
32213540260SHans Petter Selasky  */
32313540260SHans Petter Selasky 	uint64_t		td_self;
32413540260SHans Petter Selasky 	struct xhci_td		*next;
32513540260SHans Petter Selasky 	struct xhci_td		*alt_next;
32613540260SHans Petter Selasky 	struct xhci_td		*obj_next;
32713540260SHans Petter Selasky 	struct usb_page_cache	*page_cache;
32813540260SHans Petter Selasky 	uint32_t		len;
32913540260SHans Petter Selasky 	uint32_t		remainder;
33013540260SHans Petter Selasky 	uint8_t			ntrb;
33113540260SHans Petter Selasky 	uint8_t			status;
33213540260SHans Petter Selasky } __aligned(XHCI_TRB_ALIGN);
33313540260SHans Petter Selasky 
33413540260SHans Petter Selasky struct xhci_command {
33513540260SHans Petter Selasky 	struct xhci_trb		trb;
33613540260SHans Petter Selasky 	TAILQ_ENTRY(xhci_command) entry;
33713540260SHans Petter Selasky };
33813540260SHans Petter Selasky 
33913540260SHans Petter Selasky struct xhci_event_ring_seg {
34013540260SHans Petter Selasky 	volatile uint64_t	qwEvrsTablePtr;
34113540260SHans Petter Selasky 	volatile uint32_t	dwEvrsTableSize;
34213540260SHans Petter Selasky 	volatile uint32_t	dwEvrsReserved;
34313540260SHans Petter Selasky };
34413540260SHans Petter Selasky 
34513540260SHans Petter Selasky struct xhci_hw_root {
34613540260SHans Petter Selasky 	struct xhci_event_ring_seg	hwr_ring_seg[XHCI_MAX_RSEG];
34713540260SHans Petter Selasky 	struct {
34813540260SHans Petter Selasky 		volatile uint64_t dummy;
34913540260SHans Petter Selasky 	} __aligned(64)			padding;
35013540260SHans Petter Selasky 	struct xhci_trb			hwr_events[XHCI_MAX_EVENTS];
35113540260SHans Petter Selasky 	struct xhci_trb			hwr_commands[XHCI_MAX_COMMANDS];
35213540260SHans Petter Selasky };
35313540260SHans Petter Selasky 
35413540260SHans Petter Selasky struct xhci_endpoint_ext {
35513540260SHans Petter Selasky 	struct xhci_trb		*trb;
35613540260SHans Petter Selasky 	struct usb_xfer		*xfer[XHCI_MAX_TRANSFERS - 1];
35713540260SHans Petter Selasky 	struct usb_page_cache	*page_cache;
35813540260SHans Petter Selasky 	uint64_t		physaddr;
35913540260SHans Petter Selasky 	uint8_t			trb_used;
36013540260SHans Petter Selasky 	uint8_t			trb_index;
36113540260SHans Petter Selasky 	uint8_t			trb_halted;
36213540260SHans Petter Selasky 	uint8_t			trb_running;
36313540260SHans Petter Selasky };
36413540260SHans Petter Selasky 
36513540260SHans Petter Selasky enum {
36613540260SHans Petter Selasky 	XHCI_ST_DISABLED,
36713540260SHans Petter Selasky 	XHCI_ST_ENABLED,
36813540260SHans Petter Selasky 	XHCI_ST_DEFAULT,
36913540260SHans Petter Selasky 	XHCI_ST_ADDRESSED,
37013540260SHans Petter Selasky 	XHCI_ST_CONFIGURED,
37113540260SHans Petter Selasky 	XHCI_ST_MAX
37213540260SHans Petter Selasky };
37313540260SHans Petter Selasky 
37413540260SHans Petter Selasky struct xhci_hw_dev {
37513540260SHans Petter Selasky 	struct usb_page_cache	device_pc;
37613540260SHans Petter Selasky 	struct usb_page_cache	input_pc;
37713540260SHans Petter Selasky 	struct usb_page_cache	endpoint_pc;
37813540260SHans Petter Selasky 
37913540260SHans Petter Selasky 	struct usb_page		device_pg;
38013540260SHans Petter Selasky 	struct usb_page		input_pg;
38113540260SHans Petter Selasky 	struct usb_page		endpoint_pg;
38213540260SHans Petter Selasky 
38313540260SHans Petter Selasky 	struct xhci_endpoint_ext endp[XHCI_MAX_ENDPOINTS];
38413540260SHans Petter Selasky 
38513540260SHans Petter Selasky 	uint8_t			state;
38613540260SHans Petter Selasky 	uint8_t			nports;
38713540260SHans Petter Selasky 	uint8_t			tt;
38813540260SHans Petter Selasky 	uint8_t			reserved;
38913540260SHans Petter Selasky };
39013540260SHans Petter Selasky 
39113540260SHans Petter Selasky struct xhci_hw_softc {
39213540260SHans Petter Selasky 	struct usb_page_cache	root_pc;
39313540260SHans Petter Selasky 	struct usb_page_cache	ctx_pc;
39413540260SHans Petter Selasky 	struct usb_page_cache	scratch_pc[XHCI_MAX_SCRATCHPADS];
39513540260SHans Petter Selasky 
39613540260SHans Petter Selasky 	struct usb_page		root_pg;
39713540260SHans Petter Selasky 	struct usb_page		ctx_pg;
39813540260SHans Petter Selasky 	struct usb_page		scratch_pg[XHCI_MAX_SCRATCHPADS];
39913540260SHans Petter Selasky 
40013540260SHans Petter Selasky 	struct xhci_hw_dev	devs[XHCI_MAX_DEVICES + 1];
40113540260SHans Petter Selasky };
40213540260SHans Petter Selasky 
40313540260SHans Petter Selasky struct xhci_config_desc {
40413540260SHans Petter Selasky 	struct usb_config_descriptor		confd;
40513540260SHans Petter Selasky 	struct usb_interface_descriptor		ifcd;
40613540260SHans Petter Selasky 	struct usb_endpoint_descriptor		endpd;
40713540260SHans Petter Selasky 	struct usb_endpoint_ss_comp_descriptor	endpcd;
40813540260SHans Petter Selasky } __packed;
40913540260SHans Petter Selasky 
41013540260SHans Petter Selasky struct xhci_bos_desc {
41113540260SHans Petter Selasky 	struct usb_bos_descriptor		bosd;
41213540260SHans Petter Selasky 	struct usb_devcap_usb2ext_descriptor	usb2extd;
41313540260SHans Petter Selasky 	struct usb_devcap_ss_descriptor		usbdcd;
41413540260SHans Petter Selasky 	struct usb_devcap_container_id_descriptor cidd;
41513540260SHans Petter Selasky } __packed;
41613540260SHans Petter Selasky 
41713540260SHans Petter Selasky union xhci_hub_desc {
41813540260SHans Petter Selasky 	struct usb_status		stat;
41913540260SHans Petter Selasky 	struct usb_port_status		ps;
42013540260SHans Petter Selasky 	struct usb_hub_ss_descriptor	hubd;
42113540260SHans Petter Selasky 	uint8_t				temp[128];
42213540260SHans Petter Selasky };
42313540260SHans Petter Selasky 
42413540260SHans Petter Selasky struct xhci_softc {
42513540260SHans Petter Selasky 	struct xhci_hw_softc	sc_hw;
42613540260SHans Petter Selasky 	/* base device */
42713540260SHans Petter Selasky 	struct usb_bus		sc_bus;
42813540260SHans Petter Selasky 	/* configure process */
42913540260SHans Petter Selasky 	struct usb_process	sc_config_proc;
43013540260SHans Petter Selasky 	struct usb_bus_msg	sc_config_msg[2];
43113540260SHans Petter Selasky 
43213540260SHans Petter Selasky 	union xhci_hub_desc	sc_hub_desc;
43313540260SHans Petter Selasky 
43413540260SHans Petter Selasky 	struct cv		sc_cmd_cv;
43513540260SHans Petter Selasky 	struct sx		sc_cmd_sx;
43613540260SHans Petter Selasky 
43713540260SHans Petter Selasky 	struct usb_device	*sc_devices[XHCI_MAX_DEVICES];
43813540260SHans Petter Selasky 	struct resource		*sc_io_res;
43913540260SHans Petter Selasky 	struct resource		*sc_irq_res;
44013540260SHans Petter Selasky 
44113540260SHans Petter Selasky 	void			*sc_intr_hdl;
44213540260SHans Petter Selasky 	bus_size_t		sc_io_size;
44313540260SHans Petter Selasky 	bus_space_tag_t		sc_io_tag;
44413540260SHans Petter Selasky 	bus_space_handle_t	sc_io_hdl;
44513540260SHans Petter Selasky 	/* last pending command address */
44613540260SHans Petter Selasky 	uint64_t		sc_cmd_addr;
44713540260SHans Petter Selasky 	/* result of command */
44813540260SHans Petter Selasky 	uint32_t		sc_cmd_result[2];
44913540260SHans Petter Selasky  	/* copy of cmd register */
45013540260SHans Petter Selasky 	uint32_t		sc_cmd;
45113540260SHans Petter Selasky 	/* worst case exit latency */
45213540260SHans Petter Selasky 	uint32_t		sc_exit_lat_max;
45313540260SHans Petter Selasky 
45413540260SHans Petter Selasky 	/* offset to operational registers */
45513540260SHans Petter Selasky 	uint32_t		sc_oper_off;
45613540260SHans Petter Selasky 	/* offset to capability registers */
45713540260SHans Petter Selasky 	uint32_t		sc_capa_off;
45813540260SHans Petter Selasky 	/* offset to runtime registers */
45913540260SHans Petter Selasky 	uint32_t		sc_runt_off;
46013540260SHans Petter Selasky 	/* offset to doorbell registers */
46113540260SHans Petter Selasky 	uint32_t		sc_door_off;
46213540260SHans Petter Selasky 
46313540260SHans Petter Selasky 	/* chip specific */
46413540260SHans Petter Selasky 	uint16_t		sc_erst_max;
46513540260SHans Petter Selasky 	uint16_t		sc_event_idx;
46613540260SHans Petter Selasky 	uint16_t		sc_command_idx;
46713540260SHans Petter Selasky 
46813540260SHans Petter Selasky 	uint8_t			sc_event_ccs;
46913540260SHans Petter Selasky 	uint8_t			sc_command_ccs;
47013540260SHans Petter Selasky 	/* number of XHCI device slots */
47113540260SHans Petter Selasky 	uint8_t			sc_noslot;
47213540260SHans Petter Selasky 	/* number of ports on root HUB */
47313540260SHans Petter Selasky 	uint8_t			sc_noport;
47413540260SHans Petter Selasky 	/* number of scratch pages */
47513540260SHans Petter Selasky 	uint8_t			sc_noscratch;
47613540260SHans Petter Selasky 	/* root HUB device configuration */
47713540260SHans Petter Selasky 	uint8_t			sc_conf;
47813540260SHans Petter Selasky 	uint8_t			sc_hub_idata[2];
47913540260SHans Petter Selasky 
480*c1338c65SHans Petter Selasky 	/* size of context */
481*c1338c65SHans Petter Selasky 	uint8_t			sc_ctx_is_64_byte;
482*c1338c65SHans Petter Selasky 
48313540260SHans Petter Selasky 	/* vendor string for root HUB */
48413540260SHans Petter Selasky 	char			sc_vendor[16];
48513540260SHans Petter Selasky };
48613540260SHans Petter Selasky 
48713540260SHans Petter Selasky #define	XHCI_CMD_LOCK(sc)	sx_xlock(&(sc)->sc_cmd_sx)
48813540260SHans Petter Selasky #define	XHCI_CMD_UNLOCK(sc)	sx_xunlock(&(sc)->sc_cmd_sx)
48913540260SHans Petter Selasky #define	XHCI_CMD_ASSERT_LOCKED(sc) sx_assert(&(sc)->sc_cmd_sx, SA_LOCKED)
49013540260SHans Petter Selasky 
49113540260SHans Petter Selasky /* prototypes */
49213540260SHans Petter Selasky 
49313540260SHans Petter Selasky usb_error_t xhci_halt_controller(struct xhci_softc *);
49413540260SHans Petter Selasky usb_error_t xhci_init(struct xhci_softc *, device_t);
49513540260SHans Petter Selasky usb_error_t xhci_start_controller(struct xhci_softc *);
49613540260SHans Petter Selasky void	xhci_interrupt(struct xhci_softc *);
49713540260SHans Petter Selasky void	xhci_resume(struct xhci_softc *);
49813540260SHans Petter Selasky void	xhci_shutdown(struct xhci_softc *);
49913540260SHans Petter Selasky void	xhci_suspend(struct xhci_softc *);
50013540260SHans Petter Selasky void	xhci_uninit(struct xhci_softc *);
50113540260SHans Petter Selasky 
50213540260SHans Petter Selasky #endif					/* _XHCI_H_ */
503