183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
25853e133SVivek Gautam /*
35853e133SVivek Gautam * USB HOST XHCI Controller
45853e133SVivek Gautam *
55853e133SVivek Gautam * Based on xHCI host controller driver in linux-kernel
65853e133SVivek Gautam * by Sarah Sharp.
75853e133SVivek Gautam *
85853e133SVivek Gautam * Copyright (C) 2008 Intel Corp.
95853e133SVivek Gautam * Author: Sarah Sharp
105853e133SVivek Gautam *
115853e133SVivek Gautam * Copyright (C) 2013 Samsung Electronics Co.Ltd
125853e133SVivek Gautam * Authors: Vivek Gautam <gautam.vivek@samsung.com>
135853e133SVivek Gautam * Vikas Sajjan <vikas.sajjan@samsung.com>
145853e133SVivek Gautam */
155853e133SVivek Gautam
165853e133SVivek Gautam #ifndef HOST_XHCI_H_
175853e133SVivek Gautam #define HOST_XHCI_H_
185853e133SVivek Gautam
19421a5a0cSSergey Temerkhanov #include <asm/types.h>
205853e133SVivek Gautam #include <asm/cache.h>
215853e133SVivek Gautam #include <asm/io.h>
225853e133SVivek Gautam #include <linux/list.h>
234a755f1dSLijun Pan #include <linux/compat.h>
245853e133SVivek Gautam
255853e133SVivek Gautam #define MAX_EP_CTX_NUM 31
265853e133SVivek Gautam #define XHCI_ALIGNMENT 64
275853e133SVivek Gautam /* Generic timeout for XHCI events */
285853e133SVivek Gautam #define XHCI_TIMEOUT 5000
295853e133SVivek Gautam /* Max number of USB devices for any host controller - limit in section 6.1 */
305853e133SVivek Gautam #define MAX_HC_SLOTS 256
315853e133SVivek Gautam /* Section 5.3.3 - MaxPorts */
321bdcd901SBin Meng #define MAX_HC_PORTS 255
335853e133SVivek Gautam
345853e133SVivek Gautam /* Up to 16 ms to halt an HC */
355853e133SVivek Gautam #define XHCI_MAX_HALT_USEC (16*1000)
365853e133SVivek Gautam
375853e133SVivek Gautam #define XHCI_MAX_RESET_USEC (250*1000)
385853e133SVivek Gautam
395853e133SVivek Gautam /*
405853e133SVivek Gautam * These bits are Read Only (RO) and should be saved and written to the
415853e133SVivek Gautam * registers: 0, 3, 10:13, 30
425853e133SVivek Gautam * connect status, over-current status, port speed, and device removable.
435853e133SVivek Gautam * connect status and port speed are also sticky - meaning they're in
445853e133SVivek Gautam * the AUX well and they aren't changed by a hot, warm, or cold reset.
455853e133SVivek Gautam */
465853e133SVivek Gautam #define XHCI_PORT_RO ((1 << 0) | (1 << 3) | (0xf << 10) | (1 << 30))
475853e133SVivek Gautam /*
485853e133SVivek Gautam * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
495853e133SVivek Gautam * bits 5:8, 9, 14:15, 25:27
505853e133SVivek Gautam * link state, port power, port indicator state, "wake on" enable state
515853e133SVivek Gautam */
525853e133SVivek Gautam #define XHCI_PORT_RWS ((0xf << 5) | (1 << 9) | (0x3 << 14) | (0x7 << 25))
535853e133SVivek Gautam /*
545853e133SVivek Gautam * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
555853e133SVivek Gautam * bit 4 (port reset)
565853e133SVivek Gautam */
575853e133SVivek Gautam #define XHCI_PORT_RW1S ((1 << 4))
585853e133SVivek Gautam /*
595853e133SVivek Gautam * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
605853e133SVivek Gautam * bits 1, 17, 18, 19, 20, 21, 22, 23
615853e133SVivek Gautam * port enable/disable, and
625853e133SVivek Gautam * change bits: connect, PED,
635853e133SVivek Gautam * warm port reset changed (reserved zero for USB 2.0 ports),
645853e133SVivek Gautam * over-current, reset, link state, and L1 change
655853e133SVivek Gautam */
665853e133SVivek Gautam #define XHCI_PORT_RW1CS ((1 << 1) | (0x7f << 17))
675853e133SVivek Gautam /*
685853e133SVivek Gautam * Bit 16 is RW, and writing a '1' to it causes the link state control to be
695853e133SVivek Gautam * latched in
705853e133SVivek Gautam */
715853e133SVivek Gautam #define XHCI_PORT_RW ((1 << 16))
725853e133SVivek Gautam /*
735853e133SVivek Gautam * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
745853e133SVivek Gautam * bits 2, 24, 28:31
755853e133SVivek Gautam */
765853e133SVivek Gautam #define XHCI_PORT_RZ ((1 << 2) | (1 << 24) | (0xf << 28))
775853e133SVivek Gautam
785853e133SVivek Gautam /*
795853e133SVivek Gautam * XHCI Register Space.
805853e133SVivek Gautam */
815853e133SVivek Gautam struct xhci_hccr {
825853e133SVivek Gautam uint32_t cr_capbase;
835853e133SVivek Gautam uint32_t cr_hcsparams1;
845853e133SVivek Gautam uint32_t cr_hcsparams2;
855853e133SVivek Gautam uint32_t cr_hcsparams3;
865853e133SVivek Gautam uint32_t cr_hccparams;
875853e133SVivek Gautam uint32_t cr_dboff;
885853e133SVivek Gautam uint32_t cr_rtsoff;
895853e133SVivek Gautam
905853e133SVivek Gautam /* hc_capbase bitmasks */
915853e133SVivek Gautam /* bits 7:0 - how long is the Capabilities register */
925853e133SVivek Gautam #define HC_LENGTH(p) XHCI_HC_LENGTH(p)
935853e133SVivek Gautam /* bits 31:16 */
945853e133SVivek Gautam #define HC_VERSION(p) (((p) >> 16) & 0xffff)
955853e133SVivek Gautam
965853e133SVivek Gautam /* HCSPARAMS1 - hcs_params1 - bitmasks */
975853e133SVivek Gautam /* bits 0:7, Max Device Slots */
985853e133SVivek Gautam #define HCS_MAX_SLOTS(p) (((p) >> 0) & 0xff)
995853e133SVivek Gautam #define HCS_SLOTS_MASK 0xff
1005853e133SVivek Gautam /* bits 8:18, Max Interrupters */
1015853e133SVivek Gautam #define HCS_MAX_INTRS(p) (((p) >> 8) & 0x7ff)
1025853e133SVivek Gautam /* bits 24:31, Max Ports - max value is 0x7F = 127 ports */
1035853e133SVivek Gautam #define HCS_MAX_PORTS_SHIFT 24
1041bdcd901SBin Meng #define HCS_MAX_PORTS_MASK (0xff << HCS_MAX_PORTS_SHIFT)
1051bdcd901SBin Meng #define HCS_MAX_PORTS(p) (((p) >> 24) & 0xff)
1065853e133SVivek Gautam
1075853e133SVivek Gautam /* HCSPARAMS2 - hcs_params2 - bitmasks */
1085853e133SVivek Gautam /* bits 0:3, frames or uframes that SW needs to queue transactions
1095853e133SVivek Gautam * ahead of the HW to meet periodic deadlines */
1105853e133SVivek Gautam #define HCS_IST(p) (((p) >> 0) & 0xf)
1115853e133SVivek Gautam /* bits 4:7, max number of Event Ring segments */
1125853e133SVivek Gautam #define HCS_ERST_MAX(p) (((p) >> 4) & 0xf)
113209b98deSBin Meng /* bits 21:25 Hi 5 bits of Scratchpad buffers SW must allocate for the HW */
1145853e133SVivek Gautam /* bit 26 Scratchpad restore - for save/restore HW state - not used yet */
115209b98deSBin Meng /* bits 27:31 Lo 5 bits of Scratchpad buffers SW must allocate for the HW */
116209b98deSBin Meng #define HCS_MAX_SCRATCHPAD(p) ((((p) >> 16) & 0x3e0) | (((p) >> 27) & 0x1f))
1175853e133SVivek Gautam
1185853e133SVivek Gautam /* HCSPARAMS3 - hcs_params3 - bitmasks */
1195853e133SVivek Gautam /* bits 0:7, Max U1 to U0 latency for the roothub ports */
1205853e133SVivek Gautam #define HCS_U1_LATENCY(p) (((p) >> 0) & 0xff)
1215853e133SVivek Gautam /* bits 16:31, Max U2 to U0 latency for the roothub ports */
1225853e133SVivek Gautam #define HCS_U2_LATENCY(p) (((p) >> 16) & 0xffff)
1235853e133SVivek Gautam
1245853e133SVivek Gautam /* HCCPARAMS - hcc_params - bitmasks */
1255853e133SVivek Gautam /* true: HC can use 64-bit address pointers */
1265853e133SVivek Gautam #define HCC_64BIT_ADDR(p) ((p) & (1 << 0))
1275853e133SVivek Gautam /* true: HC can do bandwidth negotiation */
1285853e133SVivek Gautam #define HCC_BANDWIDTH_NEG(p) ((p) & (1 << 1))
1295853e133SVivek Gautam /* true: HC uses 64-byte Device Context structures
1305853e133SVivek Gautam * FIXME 64-byte context structures aren't supported yet.
1315853e133SVivek Gautam */
1325853e133SVivek Gautam #define HCC_64BYTE_CONTEXT(p) ((p) & (1 << 2))
1335853e133SVivek Gautam /* true: HC has port power switches */
1345853e133SVivek Gautam #define HCC_PPC(p) ((p) & (1 << 3))
1355853e133SVivek Gautam /* true: HC has port indicators */
1365853e133SVivek Gautam #define HCS_INDICATOR(p) ((p) & (1 << 4))
1375853e133SVivek Gautam /* true: HC has Light HC Reset Capability */
1385853e133SVivek Gautam #define HCC_LIGHT_RESET(p) ((p) & (1 << 5))
1395853e133SVivek Gautam /* true: HC supports latency tolerance messaging */
1405853e133SVivek Gautam #define HCC_LTC(p) ((p) & (1 << 6))
1415853e133SVivek Gautam /* true: no secondary Stream ID Support */
1425853e133SVivek Gautam #define HCC_NSS(p) ((p) & (1 << 7))
1435853e133SVivek Gautam /* Max size for Primary Stream Arrays - 2^(n+1), where n is bits 12:15 */
1445853e133SVivek Gautam #define HCC_MAX_PSA(p) (1 << ((((p) >> 12) & 0xf) + 1))
1455853e133SVivek Gautam /* Extended Capabilities pointer from PCI base - section 5.3.6 */
1465853e133SVivek Gautam #define HCC_EXT_CAPS(p) XHCI_HCC_EXT_CAPS(p)
1475853e133SVivek Gautam
1485853e133SVivek Gautam /* db_off bitmask - bits 0:1 reserved */
1495853e133SVivek Gautam #define DBOFF_MASK (~0x3)
1505853e133SVivek Gautam
1515853e133SVivek Gautam /* run_regs_off bitmask - bits 0:4 reserved */
1525853e133SVivek Gautam #define RTSOFF_MASK (~0x1f)
1535853e133SVivek Gautam
1545853e133SVivek Gautam };
1555853e133SVivek Gautam
1565853e133SVivek Gautam struct xhci_hcor_port_regs {
1575853e133SVivek Gautam volatile uint32_t or_portsc;
1585853e133SVivek Gautam volatile uint32_t or_portpmsc;
1595853e133SVivek Gautam volatile uint32_t or_portli;
1605853e133SVivek Gautam volatile uint32_t reserved_3;
1615853e133SVivek Gautam };
1625853e133SVivek Gautam
1635853e133SVivek Gautam struct xhci_hcor {
1645853e133SVivek Gautam volatile uint32_t or_usbcmd;
1655853e133SVivek Gautam volatile uint32_t or_usbsts;
1665853e133SVivek Gautam volatile uint32_t or_pagesize;
1675853e133SVivek Gautam volatile uint32_t reserved_0[2];
1685853e133SVivek Gautam volatile uint32_t or_dnctrl;
1695853e133SVivek Gautam volatile uint64_t or_crcr;
1705853e133SVivek Gautam volatile uint32_t reserved_1[4];
1715853e133SVivek Gautam volatile uint64_t or_dcbaap;
1725853e133SVivek Gautam volatile uint32_t or_config;
1735853e133SVivek Gautam volatile uint32_t reserved_2[241];
1747274671eSBin Meng struct xhci_hcor_port_regs portregs[MAX_HC_PORTS];
1755853e133SVivek Gautam };
1765853e133SVivek Gautam
1775853e133SVivek Gautam /* USBCMD - USB command - command bitmasks */
1785853e133SVivek Gautam /* start/stop HC execution - do not write unless HC is halted*/
1795853e133SVivek Gautam #define CMD_RUN XHCI_CMD_RUN
1805853e133SVivek Gautam /* Reset HC - resets internal HC state machine and all registers (except
1815853e133SVivek Gautam * PCI config regs). HC does NOT drive a USB reset on the downstream ports.
1825853e133SVivek Gautam * The xHCI driver must reinitialize the xHC after setting this bit.
1835853e133SVivek Gautam */
1845853e133SVivek Gautam #define CMD_RESET (1 << 1)
1855853e133SVivek Gautam /* Event Interrupt Enable - a '1' allows interrupts from the host controller */
1865853e133SVivek Gautam #define CMD_EIE XHCI_CMD_EIE
1875853e133SVivek Gautam /* Host System Error Interrupt Enable - get out-of-band signal for HC errors */
1885853e133SVivek Gautam #define CMD_HSEIE XHCI_CMD_HSEIE
1895853e133SVivek Gautam /* bits 4:6 are reserved (and should be preserved on writes). */
1905853e133SVivek Gautam /* light reset (port status stays unchanged) - reset completed when this is 0 */
1915853e133SVivek Gautam #define CMD_LRESET (1 << 7)
1925853e133SVivek Gautam /* host controller save/restore state. */
1935853e133SVivek Gautam #define CMD_CSS (1 << 8)
1945853e133SVivek Gautam #define CMD_CRS (1 << 9)
1955853e133SVivek Gautam /* Enable Wrap Event - '1' means xHC generates an event when MFINDEX wraps. */
1965853e133SVivek Gautam #define CMD_EWE XHCI_CMD_EWE
1975853e133SVivek Gautam /* MFINDEX power management - '1' means xHC can stop MFINDEX counter if all root
1985853e133SVivek Gautam * hubs are in U3 (selective suspend), disconnect, disabled, or powered-off.
1995853e133SVivek Gautam * '0' means the xHC can power it off if all ports are in the disconnect,
2005853e133SVivek Gautam * disabled, or powered-off state.
2015853e133SVivek Gautam */
2025853e133SVivek Gautam #define CMD_PM_INDEX (1 << 11)
2035853e133SVivek Gautam /* bits 12:31 are reserved (and should be preserved on writes). */
2045853e133SVivek Gautam
2055853e133SVivek Gautam /* USBSTS - USB status - status bitmasks */
2065853e133SVivek Gautam /* HC not running - set to 1 when run/stop bit is cleared. */
2075853e133SVivek Gautam #define STS_HALT XHCI_STS_HALT
2085853e133SVivek Gautam /* serious error, e.g. PCI parity error. The HC will clear the run/stop bit. */
2095853e133SVivek Gautam #define STS_FATAL (1 << 2)
2105853e133SVivek Gautam /* event interrupt - clear this prior to clearing any IP flags in IR set*/
2115853e133SVivek Gautam #define STS_EINT (1 << 3)
2125853e133SVivek Gautam /* port change detect */
2135853e133SVivek Gautam #define STS_PORT (1 << 4)
2145853e133SVivek Gautam /* bits 5:7 reserved and zeroed */
2155853e133SVivek Gautam /* save state status - '1' means xHC is saving state */
2165853e133SVivek Gautam #define STS_SAVE (1 << 8)
2175853e133SVivek Gautam /* restore state status - '1' means xHC is restoring state */
2185853e133SVivek Gautam #define STS_RESTORE (1 << 9)
2195853e133SVivek Gautam /* true: save or restore error */
2205853e133SVivek Gautam #define STS_SRE (1 << 10)
2215853e133SVivek Gautam /* true: Controller Not Ready to accept doorbell or op reg writes after reset */
2225853e133SVivek Gautam #define STS_CNR XHCI_STS_CNR
2235853e133SVivek Gautam /* true: internal Host Controller Error - SW needs to reset and reinitialize */
2245853e133SVivek Gautam #define STS_HCE (1 << 12)
2255853e133SVivek Gautam /* bits 13:31 reserved and should be preserved */
2265853e133SVivek Gautam
2275853e133SVivek Gautam /*
2285853e133SVivek Gautam * DNCTRL - Device Notification Control Register - dev_notification bitmasks
2295853e133SVivek Gautam * Generate a device notification event when the HC sees a transaction with a
2305853e133SVivek Gautam * notification type that matches a bit set in this bit field.
2315853e133SVivek Gautam */
2325853e133SVivek Gautam #define DEV_NOTE_MASK (0xffff)
2335853e133SVivek Gautam #define ENABLE_DEV_NOTE(x) (1 << (x))
2345853e133SVivek Gautam /* Most of the device notification types should only be used for debug.
2355853e133SVivek Gautam * SW does need to pay attention to function wake notifications.
2365853e133SVivek Gautam */
2375853e133SVivek Gautam #define DEV_NOTE_FWAKE ENABLE_DEV_NOTE(1)
2385853e133SVivek Gautam
2395853e133SVivek Gautam /* CRCR - Command Ring Control Register - cmd_ring bitmasks */
2405853e133SVivek Gautam /* bit 0 is the command ring cycle state */
2415853e133SVivek Gautam /* stop ring operation after completion of the currently executing command */
2425853e133SVivek Gautam #define CMD_RING_PAUSE (1 << 1)
2435853e133SVivek Gautam /* stop ring immediately - abort the currently executing command */
2445853e133SVivek Gautam #define CMD_RING_ABORT (1 << 2)
2455853e133SVivek Gautam /* true: command ring is running */
2465853e133SVivek Gautam #define CMD_RING_RUNNING (1 << 3)
2475853e133SVivek Gautam /* bits 4:5 reserved and should be preserved */
2485853e133SVivek Gautam /* Command Ring pointer - bit mask for the lower 32 bits. */
2495853e133SVivek Gautam #define CMD_RING_RSVD_BITS (0x3f)
2505853e133SVivek Gautam
2515853e133SVivek Gautam /* CONFIG - Configure Register - config_reg bitmasks */
2525853e133SVivek Gautam /* bits 0:7 - maximum number of device slots enabled (NumSlotsEn) */
2535853e133SVivek Gautam #define MAX_DEVS(p) ((p) & 0xff)
2545853e133SVivek Gautam /* bits 8:31 - reserved and should be preserved */
2555853e133SVivek Gautam
2565853e133SVivek Gautam /* PORTSC - Port Status and Control Register - port_status_base bitmasks */
2575853e133SVivek Gautam /* true: device connected */
2585853e133SVivek Gautam #define PORT_CONNECT (1 << 0)
2595853e133SVivek Gautam /* true: port enabled */
2605853e133SVivek Gautam #define PORT_PE (1 << 1)
2615853e133SVivek Gautam /* bit 2 reserved and zeroed */
2625853e133SVivek Gautam /* true: port has an over-current condition */
2635853e133SVivek Gautam #define PORT_OC (1 << 3)
2645853e133SVivek Gautam /* true: port reset signaling asserted */
2655853e133SVivek Gautam #define PORT_RESET (1 << 4)
2665853e133SVivek Gautam /* Port Link State - bits 5:8
2675853e133SVivek Gautam * A read gives the current link PM state of the port,
2685853e133SVivek Gautam * a write with Link State Write Strobe set sets the link state.
2695853e133SVivek Gautam */
2705853e133SVivek Gautam #define PORT_PLS_MASK (0xf << 5)
2715853e133SVivek Gautam #define XDEV_U0 (0x0 << 5)
2725853e133SVivek Gautam #define XDEV_U2 (0x2 << 5)
2735853e133SVivek Gautam #define XDEV_U3 (0x3 << 5)
2745853e133SVivek Gautam #define XDEV_RESUME (0xf << 5)
2755853e133SVivek Gautam /* true: port has power (see HCC_PPC) */
2765853e133SVivek Gautam #define PORT_POWER (1 << 9)
2775853e133SVivek Gautam /* bits 10:13 indicate device speed:
2785853e133SVivek Gautam * 0 - undefined speed - port hasn't be initialized by a reset yet
2795853e133SVivek Gautam * 1 - full speed
2805853e133SVivek Gautam * 2 - low speed
2815853e133SVivek Gautam * 3 - high speed
2825853e133SVivek Gautam * 4 - super speed
2835853e133SVivek Gautam * 5-15 reserved
2845853e133SVivek Gautam */
2855853e133SVivek Gautam #define DEV_SPEED_MASK (0xf << 10)
2865853e133SVivek Gautam #define XDEV_FS (0x1 << 10)
2875853e133SVivek Gautam #define XDEV_LS (0x2 << 10)
2885853e133SVivek Gautam #define XDEV_HS (0x3 << 10)
2895853e133SVivek Gautam #define XDEV_SS (0x4 << 10)
2905853e133SVivek Gautam #define DEV_UNDEFSPEED(p) (((p) & DEV_SPEED_MASK) == (0x0<<10))
2915853e133SVivek Gautam #define DEV_FULLSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_FS)
2925853e133SVivek Gautam #define DEV_LOWSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_LS)
2935853e133SVivek Gautam #define DEV_HIGHSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_HS)
2945853e133SVivek Gautam #define DEV_SUPERSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_SS)
2955853e133SVivek Gautam /* Bits 20:23 in the Slot Context are the speed for the device */
2965853e133SVivek Gautam #define SLOT_SPEED_FS (XDEV_FS << 10)
2975853e133SVivek Gautam #define SLOT_SPEED_LS (XDEV_LS << 10)
2985853e133SVivek Gautam #define SLOT_SPEED_HS (XDEV_HS << 10)
2995853e133SVivek Gautam #define SLOT_SPEED_SS (XDEV_SS << 10)
3005853e133SVivek Gautam /* Port Indicator Control */
3015853e133SVivek Gautam #define PORT_LED_OFF (0 << 14)
3025853e133SVivek Gautam #define PORT_LED_AMBER (1 << 14)
3035853e133SVivek Gautam #define PORT_LED_GREEN (2 << 14)
3045853e133SVivek Gautam #define PORT_LED_MASK (3 << 14)
3055853e133SVivek Gautam /* Port Link State Write Strobe - set this when changing link state */
3065853e133SVivek Gautam #define PORT_LINK_STROBE (1 << 16)
3075853e133SVivek Gautam /* true: connect status change */
3085853e133SVivek Gautam #define PORT_CSC (1 << 17)
3095853e133SVivek Gautam /* true: port enable change */
3105853e133SVivek Gautam #define PORT_PEC (1 << 18)
3115853e133SVivek Gautam /* true: warm reset for a USB 3.0 device is done. A "hot" reset puts the port
3125853e133SVivek Gautam * into an enabled state, and the device into the default state. A "warm" reset
3135853e133SVivek Gautam * also resets the link, forcing the device through the link training sequence.
3145853e133SVivek Gautam * SW can also look at the Port Reset register to see when warm reset is done.
3155853e133SVivek Gautam */
3165853e133SVivek Gautam #define PORT_WRC (1 << 19)
3175853e133SVivek Gautam /* true: over-current change */
3185853e133SVivek Gautam #define PORT_OCC (1 << 20)
3195853e133SVivek Gautam /* true: reset change - 1 to 0 transition of PORT_RESET */
3205853e133SVivek Gautam #define PORT_RC (1 << 21)
3215853e133SVivek Gautam /* port link status change - set on some port link state transitions:
3225853e133SVivek Gautam * Transition Reason
3235853e133SVivek Gautam * --------------------------------------------------------------------------
3245853e133SVivek Gautam * - U3 to Resume Wakeup signaling from a device
3255853e133SVivek Gautam * - Resume to Recovery to U0 USB 3.0 device resume
3265853e133SVivek Gautam * - Resume to U0 USB 2.0 device resume
3275853e133SVivek Gautam * - U3 to Recovery to U0 Software resume of USB 3.0 device complete
3285853e133SVivek Gautam * - U3 to U0 Software resume of USB 2.0 device complete
3295853e133SVivek Gautam * - U2 to U0 L1 resume of USB 2.1 device complete
3305853e133SVivek Gautam * - U0 to U0 (???) L1 entry rejection by USB 2.1 device
3315853e133SVivek Gautam * - U0 to disabled L1 entry error with USB 2.1 device
3325853e133SVivek Gautam * - Any state to inactive Error on USB 3.0 port
3335853e133SVivek Gautam */
3345853e133SVivek Gautam #define PORT_PLC (1 << 22)
3355853e133SVivek Gautam /* port configure error change - port failed to configure its link partner */
3365853e133SVivek Gautam #define PORT_CEC (1 << 23)
3375853e133SVivek Gautam /* bit 24 reserved */
3385853e133SVivek Gautam /* wake on connect (enable) */
3395853e133SVivek Gautam #define PORT_WKCONN_E (1 << 25)
3405853e133SVivek Gautam /* wake on disconnect (enable) */
3415853e133SVivek Gautam #define PORT_WKDISC_E (1 << 26)
3425853e133SVivek Gautam /* wake on over-current (enable) */
3435853e133SVivek Gautam #define PORT_WKOC_E (1 << 27)
3445853e133SVivek Gautam /* bits 28:29 reserved */
3455853e133SVivek Gautam /* true: device is removable - for USB 3.0 roothub emulation */
3465853e133SVivek Gautam #define PORT_DEV_REMOVE (1 << 30)
3475853e133SVivek Gautam /* Initiate a warm port reset - complete when PORT_WRC is '1' */
3485853e133SVivek Gautam #define PORT_WR (1 << 31)
3495853e133SVivek Gautam
3505853e133SVivek Gautam /* We mark duplicate entries with -1 */
3515853e133SVivek Gautam #define DUPLICATE_ENTRY ((u8)(-1))
3525853e133SVivek Gautam
3535853e133SVivek Gautam /* Port Power Management Status and Control - port_power_base bitmasks */
3545853e133SVivek Gautam /* Inactivity timer value for transitions into U1, in microseconds.
3555853e133SVivek Gautam * Timeout can be up to 127us. 0xFF means an infinite timeout.
3565853e133SVivek Gautam */
3575853e133SVivek Gautam #define PORT_U1_TIMEOUT(p) ((p) & 0xff)
3585853e133SVivek Gautam /* Inactivity timer value for transitions into U2 */
3595853e133SVivek Gautam #define PORT_U2_TIMEOUT(p) (((p) & 0xff) << 8)
3605853e133SVivek Gautam /* Bits 24:31 for port testing */
3615853e133SVivek Gautam
3625853e133SVivek Gautam /* USB2 Protocol PORTSPMSC */
3635853e133SVivek Gautam #define PORT_L1S_MASK 7
3645853e133SVivek Gautam #define PORT_L1S_SUCCESS 1
3655853e133SVivek Gautam #define PORT_RWE (1 << 3)
3665853e133SVivek Gautam #define PORT_HIRD(p) (((p) & 0xf) << 4)
3675853e133SVivek Gautam #define PORT_HIRD_MASK (0xf << 4)
3685853e133SVivek Gautam #define PORT_L1DS(p) (((p) & 0xff) << 8)
3695853e133SVivek Gautam #define PORT_HLE (1 << 16)
3705853e133SVivek Gautam
3715853e133SVivek Gautam /**
3725853e133SVivek Gautam * struct xhci_intr_reg - Interrupt Register Set
3735853e133SVivek Gautam * @irq_pending: IMAN - Interrupt Management Register. Used to enable
3745853e133SVivek Gautam * interrupts and check for pending interrupts.
3755853e133SVivek Gautam * @irq_control: IMOD - Interrupt Moderation Register.
3765853e133SVivek Gautam * Used to throttle interrupts.
3775853e133SVivek Gautam * @erst_size: Number of segments in the
3785853e133SVivek Gautam Event Ring Segment Table (ERST).
3795853e133SVivek Gautam * @erst_base: ERST base address.
3805853e133SVivek Gautam * @erst_dequeue: Event ring dequeue pointer.
3815853e133SVivek Gautam *
3825853e133SVivek Gautam * Each interrupter (defined by a MSI-X vector) has an event ring and an Event
3835853e133SVivek Gautam * Ring Segment Table (ERST) associated with it.
3845853e133SVivek Gautam * The event ring is comprised of multiple segments of the same size.
3855853e133SVivek Gautam * The HC places events on the ring and "updates the Cycle bit in the TRBs to
3865853e133SVivek Gautam * indicate to software the current position of the Enqueue Pointer."
3875853e133SVivek Gautam * The HCD (Linux) processes those events and updates the dequeue pointer.
3885853e133SVivek Gautam */
3895853e133SVivek Gautam struct xhci_intr_reg {
3905853e133SVivek Gautam volatile __le32 irq_pending;
3915853e133SVivek Gautam volatile __le32 irq_control;
3925853e133SVivek Gautam volatile __le32 erst_size;
3935853e133SVivek Gautam volatile __le32 rsvd;
3945853e133SVivek Gautam volatile __le64 erst_base;
3955853e133SVivek Gautam volatile __le64 erst_dequeue;
3965853e133SVivek Gautam };
3975853e133SVivek Gautam
3985853e133SVivek Gautam /* irq_pending bitmasks */
3995853e133SVivek Gautam #define ER_IRQ_PENDING(p) ((p) & 0x1)
4005853e133SVivek Gautam /* bits 2:31 need to be preserved */
4015853e133SVivek Gautam /* THIS IS BUGGY - FIXME - IP IS WRITE 1 TO CLEAR */
4025853e133SVivek Gautam #define ER_IRQ_CLEAR(p) ((p) & 0xfffffffe)
4035853e133SVivek Gautam #define ER_IRQ_ENABLE(p) ((ER_IRQ_CLEAR(p)) | 0x2)
4045853e133SVivek Gautam #define ER_IRQ_DISABLE(p) ((ER_IRQ_CLEAR(p)) & ~(0x2))
4055853e133SVivek Gautam
4065853e133SVivek Gautam /* irq_control bitmasks */
4075853e133SVivek Gautam /* Minimum interval between interrupts (in 250ns intervals). The interval
4085853e133SVivek Gautam * between interrupts will be longer if there are no events on the event ring.
4095853e133SVivek Gautam * Default is 4000 (1 ms).
4105853e133SVivek Gautam */
4115853e133SVivek Gautam #define ER_IRQ_INTERVAL_MASK (0xffff)
4125853e133SVivek Gautam /* Counter used to count down the time to the next interrupt - HW use only */
4135853e133SVivek Gautam #define ER_IRQ_COUNTER_MASK (0xffff << 16)
4145853e133SVivek Gautam
4155853e133SVivek Gautam /* erst_size bitmasks */
4165853e133SVivek Gautam /* Preserve bits 16:31 of erst_size */
4175853e133SVivek Gautam #define ERST_SIZE_MASK (0xffff << 16)
4185853e133SVivek Gautam
4195853e133SVivek Gautam /* erst_dequeue bitmasks */
4205853e133SVivek Gautam /* Dequeue ERST Segment Index (DESI) - Segment number (or alias)
4215853e133SVivek Gautam * where the current dequeue pointer lies. This is an optional HW hint.
4225853e133SVivek Gautam */
4235853e133SVivek Gautam #define ERST_DESI_MASK (0x7)
4245853e133SVivek Gautam /* Event Handler Busy (EHB) - is the event ring scheduled to be serviced by
4255853e133SVivek Gautam * a work queue (or delayed service routine)?
4265853e133SVivek Gautam */
4275853e133SVivek Gautam #define ERST_EHB (1 << 3)
4285853e133SVivek Gautam #define ERST_PTR_MASK (0xf)
4295853e133SVivek Gautam
4305853e133SVivek Gautam /**
4315853e133SVivek Gautam * struct xhci_run_regs
4325853e133SVivek Gautam * @microframe_index: MFINDEX - current microframe number
4335853e133SVivek Gautam *
4345853e133SVivek Gautam * Section 5.5 Host Controller Runtime Registers:
4355853e133SVivek Gautam * "Software should read and write these registers using only Dword (32 bit)
4365853e133SVivek Gautam * or larger accesses"
4375853e133SVivek Gautam */
4385853e133SVivek Gautam struct xhci_run_regs {
4395853e133SVivek Gautam __le32 microframe_index;
4405853e133SVivek Gautam __le32 rsvd[7];
4415853e133SVivek Gautam struct xhci_intr_reg ir_set[128];
4425853e133SVivek Gautam };
4435853e133SVivek Gautam
4445853e133SVivek Gautam /**
4455853e133SVivek Gautam * struct doorbell_array
4465853e133SVivek Gautam *
4475853e133SVivek Gautam * Bits 0 - 7: Endpoint target
4485853e133SVivek Gautam * Bits 8 - 15: RsvdZ
4495853e133SVivek Gautam * Bits 16 - 31: Stream ID
4505853e133SVivek Gautam *
4515853e133SVivek Gautam * Section 5.6
4525853e133SVivek Gautam */
4535853e133SVivek Gautam struct xhci_doorbell_array {
4545853e133SVivek Gautam volatile __le32 doorbell[256];
4555853e133SVivek Gautam };
4565853e133SVivek Gautam
4575853e133SVivek Gautam #define DB_VALUE(ep, stream) ((((ep) + 1) & 0xff) | ((stream) << 16))
4585853e133SVivek Gautam #define DB_VALUE_HOST 0x00000000
4595853e133SVivek Gautam
4605853e133SVivek Gautam /**
4615853e133SVivek Gautam * struct xhci_protocol_caps
4625853e133SVivek Gautam * @revision: major revision, minor revision, capability ID,
4635853e133SVivek Gautam * and next capability pointer.
4645853e133SVivek Gautam * @name_string: Four ASCII characters to say which spec this xHC
4655853e133SVivek Gautam * follows, typically "USB ".
4665853e133SVivek Gautam * @port_info: Port offset, count, and protocol-defined information.
4675853e133SVivek Gautam */
4685853e133SVivek Gautam struct xhci_protocol_caps {
4695853e133SVivek Gautam u32 revision;
4705853e133SVivek Gautam u32 name_string;
4715853e133SVivek Gautam u32 port_info;
4725853e133SVivek Gautam };
4735853e133SVivek Gautam
4745853e133SVivek Gautam #define XHCI_EXT_PORT_MAJOR(x) (((x) >> 24) & 0xff)
4755853e133SVivek Gautam #define XHCI_EXT_PORT_OFF(x) ((x) & 0xff)
4765853e133SVivek Gautam #define XHCI_EXT_PORT_COUNT(x) (((x) >> 8) & 0xff)
4775853e133SVivek Gautam
4785853e133SVivek Gautam /**
4795853e133SVivek Gautam * struct xhci_container_ctx
4805853e133SVivek Gautam * @type: Type of context. Used to calculated offsets to contained contexts.
4815853e133SVivek Gautam * @size: Size of the context data
4825853e133SVivek Gautam * @bytes: The raw context data given to HW
4835853e133SVivek Gautam *
4845853e133SVivek Gautam * Represents either a Device or Input context. Holds a pointer to the raw
485f2e0315eSBin Meng * memory used for the context (bytes).
4865853e133SVivek Gautam */
4875853e133SVivek Gautam struct xhci_container_ctx {
4885853e133SVivek Gautam unsigned type;
4895853e133SVivek Gautam #define XHCI_CTX_TYPE_DEVICE 0x1
4905853e133SVivek Gautam #define XHCI_CTX_TYPE_INPUT 0x2
4915853e133SVivek Gautam
4925853e133SVivek Gautam int size;
4935853e133SVivek Gautam u8 *bytes;
4945853e133SVivek Gautam };
4955853e133SVivek Gautam
4965853e133SVivek Gautam /**
4975853e133SVivek Gautam * struct xhci_slot_ctx
4985853e133SVivek Gautam * @dev_info: Route string, device speed, hub info, and last valid endpoint
4995853e133SVivek Gautam * @dev_info2: Max exit latency for device number, root hub port number
5005853e133SVivek Gautam * @tt_info: tt_info is used to construct split transaction tokens
5015853e133SVivek Gautam * @dev_state: slot state and device address
5025853e133SVivek Gautam *
5035853e133SVivek Gautam * Slot Context - section 6.2.1.1. This assumes the HC uses 32-byte context
5045853e133SVivek Gautam * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
5055853e133SVivek Gautam * reserved at the end of the slot context for HC internal use.
5065853e133SVivek Gautam */
5075853e133SVivek Gautam struct xhci_slot_ctx {
5085853e133SVivek Gautam __le32 dev_info;
5095853e133SVivek Gautam __le32 dev_info2;
5105853e133SVivek Gautam __le32 tt_info;
5115853e133SVivek Gautam __le32 dev_state;
5125853e133SVivek Gautam /* offset 0x10 to 0x1f reserved for HC internal use */
5135853e133SVivek Gautam __le32 reserved[4];
5145853e133SVivek Gautam };
5155853e133SVivek Gautam
5165853e133SVivek Gautam /* dev_info bitmasks */
5175853e133SVivek Gautam /* Route String - 0:19 */
5185853e133SVivek Gautam #define ROUTE_STRING_MASK (0xfffff)
5195853e133SVivek Gautam /* Device speed - values defined by PORTSC Device Speed field - 20:23 */
5205853e133SVivek Gautam #define DEV_SPEED (0xf << 20)
5215853e133SVivek Gautam /* bit 24 reserved */
5225853e133SVivek Gautam /* Is this LS/FS device connected through a HS hub? - bit 25 */
5235853e133SVivek Gautam #define DEV_MTT (0x1 << 25)
5245853e133SVivek Gautam /* Set if the device is a hub - bit 26 */
5255853e133SVivek Gautam #define DEV_HUB (0x1 << 26)
5265853e133SVivek Gautam /* Index of the last valid endpoint context in this device context - 27:31 */
5275853e133SVivek Gautam #define LAST_CTX_MASK (0x1f << 27)
5285853e133SVivek Gautam #define LAST_CTX(p) ((p) << 27)
5295853e133SVivek Gautam #define LAST_CTX_TO_EP_NUM(p) (((p) >> 27) - 1)
5305853e133SVivek Gautam #define SLOT_FLAG (1 << 0)
5315853e133SVivek Gautam #define EP0_FLAG (1 << 1)
5325853e133SVivek Gautam
5335853e133SVivek Gautam /* dev_info2 bitmasks */
5345853e133SVivek Gautam /* Max Exit Latency (ms) - worst case time to wake up all links in dev path */
5355853e133SVivek Gautam #define MAX_EXIT (0xffff)
5365853e133SVivek Gautam /* Root hub port number that is needed to access the USB device */
5375853e133SVivek Gautam #define ROOT_HUB_PORT(p) (((p) & 0xff) << 16)
5385853e133SVivek Gautam #define ROOT_HUB_PORT_MASK (0xff)
5395853e133SVivek Gautam #define ROOT_HUB_PORT_SHIFT (16)
5405853e133SVivek Gautam #define DEVINFO_TO_ROOT_HUB_PORT(p) (((p) >> 16) & 0xff)
5415853e133SVivek Gautam /* Maximum number of ports under a hub device */
5425853e133SVivek Gautam #define XHCI_MAX_PORTS(p) (((p) & 0xff) << 24)
5435853e133SVivek Gautam
5445853e133SVivek Gautam /* tt_info bitmasks */
5455853e133SVivek Gautam /*
5465853e133SVivek Gautam * TT Hub Slot ID - for low or full speed devices attached to a high-speed hub
5475853e133SVivek Gautam * The Slot ID of the hub that isolates the high speed signaling from
5485853e133SVivek Gautam * this low or full-speed device. '0' if attached to root hub port.
5495853e133SVivek Gautam */
550196ef832SBin Meng #define TT_SLOT(p) (((p) & 0xff) << 0)
5515853e133SVivek Gautam /*
5525853e133SVivek Gautam * The number of the downstream facing port of the high-speed hub
5535853e133SVivek Gautam * '0' if the device is not low or full speed.
5545853e133SVivek Gautam */
555196ef832SBin Meng #define TT_PORT(p) (((p) & 0xff) << 8)
5565853e133SVivek Gautam #define TT_THINK_TIME(p) (((p) & 0x3) << 16)
5575853e133SVivek Gautam
5585853e133SVivek Gautam /* dev_state bitmasks */
5595853e133SVivek Gautam /* USB device address - assigned by the HC */
5605853e133SVivek Gautam #define DEV_ADDR_MASK (0xff)
5615853e133SVivek Gautam /* bits 8:26 reserved */
5625853e133SVivek Gautam /* Slot state */
5635853e133SVivek Gautam #define SLOT_STATE (0x1f << 27)
5645853e133SVivek Gautam #define GET_SLOT_STATE(p) (((p) & (0x1f << 27)) >> 27)
5655853e133SVivek Gautam
5665853e133SVivek Gautam #define SLOT_STATE_DISABLED 0
5675853e133SVivek Gautam #define SLOT_STATE_ENABLED SLOT_STATE_DISABLED
5685853e133SVivek Gautam #define SLOT_STATE_DEFAULT 1
5695853e133SVivek Gautam #define SLOT_STATE_ADDRESSED 2
5705853e133SVivek Gautam #define SLOT_STATE_CONFIGURED 3
5715853e133SVivek Gautam
5725853e133SVivek Gautam /**
5735853e133SVivek Gautam * struct xhci_ep_ctx
5745853e133SVivek Gautam * @ep_info: endpoint state, streams, mult, and interval information.
5755853e133SVivek Gautam * @ep_info2: information on endpoint type, max packet size, max burst size,
5765853e133SVivek Gautam * error count, and whether the HC will force an event for all
5775853e133SVivek Gautam * transactions.
5785853e133SVivek Gautam * @deq: 64-bit ring dequeue pointer address. If the endpoint only
5795853e133SVivek Gautam * defines one stream, this points to the endpoint transfer ring.
5805853e133SVivek Gautam * Otherwise, it points to a stream context array, which has a
5815853e133SVivek Gautam * ring pointer for each flow.
5825853e133SVivek Gautam * @tx_info:
5835853e133SVivek Gautam * Average TRB lengths for the endpoint ring and
5845853e133SVivek Gautam * max payload within an Endpoint Service Interval Time (ESIT).
5855853e133SVivek Gautam *
5865853e133SVivek Gautam * Endpoint Context - section 6.2.1.2.This assumes the HC uses 32-byte context
5875853e133SVivek Gautam * structures.If the HC uses 64-byte contexts, there is an additional 32 bytes
5885853e133SVivek Gautam * reserved at the end of the endpoint context for HC internal use.
5895853e133SVivek Gautam */
5905853e133SVivek Gautam struct xhci_ep_ctx {
5915853e133SVivek Gautam __le32 ep_info;
5925853e133SVivek Gautam __le32 ep_info2;
5935853e133SVivek Gautam __le64 deq;
5945853e133SVivek Gautam __le32 tx_info;
5955853e133SVivek Gautam /* offset 0x14 - 0x1f reserved for HC internal use */
5965853e133SVivek Gautam __le32 reserved[3];
5975853e133SVivek Gautam };
5985853e133SVivek Gautam
5995853e133SVivek Gautam /* ep_info bitmasks */
6005853e133SVivek Gautam /*
6015853e133SVivek Gautam * Endpoint State - bits 0:2
6025853e133SVivek Gautam * 0 - disabled
6035853e133SVivek Gautam * 1 - running
6045853e133SVivek Gautam * 2 - halted due to halt condition - ok to manipulate endpoint ring
6055853e133SVivek Gautam * 3 - stopped
6065853e133SVivek Gautam * 4 - TRB error
6075853e133SVivek Gautam * 5-7 - reserved
6085853e133SVivek Gautam */
6095853e133SVivek Gautam #define EP_STATE_MASK (0xf)
6105853e133SVivek Gautam #define EP_STATE_DISABLED 0
6115853e133SVivek Gautam #define EP_STATE_RUNNING 1
6125853e133SVivek Gautam #define EP_STATE_HALTED 2
6135853e133SVivek Gautam #define EP_STATE_STOPPED 3
6145853e133SVivek Gautam #define EP_STATE_ERROR 4
6155853e133SVivek Gautam /* Mult - Max number of burtst within an interval, in EP companion desc. */
6165853e133SVivek Gautam #define EP_MULT(p) (((p) & 0x3) << 8)
6175853e133SVivek Gautam #define CTX_TO_EP_MULT(p) (((p) >> 8) & 0x3)
6185853e133SVivek Gautam /* bits 10:14 are Max Primary Streams */
6195853e133SVivek Gautam /* bit 15 is Linear Stream Array */
6205853e133SVivek Gautam /* Interval - period between requests to an endpoint - 125u increments. */
6215853e133SVivek Gautam #define EP_INTERVAL(p) (((p) & 0xff) << 16)
6225853e133SVivek Gautam #define EP_INTERVAL_TO_UFRAMES(p) (1 << (((p) >> 16) & 0xff))
6235853e133SVivek Gautam #define CTX_TO_EP_INTERVAL(p) (((p) >> 16) & 0xff)
6245853e133SVivek Gautam #define EP_MAXPSTREAMS_MASK (0x1f << 10)
6255853e133SVivek Gautam #define EP_MAXPSTREAMS(p) (((p) << 10) & EP_MAXPSTREAMS_MASK)
6265853e133SVivek Gautam /* Endpoint is set up with a Linear Stream Array (vs. Secondary Stream Array) */
6275853e133SVivek Gautam #define EP_HAS_LSA (1 << 15)
6285853e133SVivek Gautam
6295853e133SVivek Gautam /* ep_info2 bitmasks */
6305853e133SVivek Gautam /*
6315853e133SVivek Gautam * Force Event - generate transfer events for all TRBs for this endpoint
6325853e133SVivek Gautam * This will tell the HC to ignore the IOC and ISP flags (for debugging only).
6335853e133SVivek Gautam */
6345853e133SVivek Gautam #define FORCE_EVENT (0x1)
6355853e133SVivek Gautam #define ERROR_COUNT(p) (((p) & 0x3) << 1)
6365853e133SVivek Gautam #define ERROR_COUNT_SHIFT (1)
6375853e133SVivek Gautam #define ERROR_COUNT_MASK (0x3)
6385853e133SVivek Gautam #define CTX_TO_EP_TYPE(p) (((p) >> 3) & 0x7)
6395853e133SVivek Gautam #define EP_TYPE(p) ((p) << 3)
6405853e133SVivek Gautam #define EP_TYPE_SHIFT (3)
6415853e133SVivek Gautam #define ISOC_OUT_EP 1
6425853e133SVivek Gautam #define BULK_OUT_EP 2
6435853e133SVivek Gautam #define INT_OUT_EP 3
6445853e133SVivek Gautam #define CTRL_EP 4
6455853e133SVivek Gautam #define ISOC_IN_EP 5
6465853e133SVivek Gautam #define BULK_IN_EP 6
6475853e133SVivek Gautam #define INT_IN_EP 7
6485853e133SVivek Gautam /* bit 6 reserved */
6495853e133SVivek Gautam /* bit 7 is Host Initiate Disable - for disabling stream selection */
6505853e133SVivek Gautam #define MAX_BURST(p) (((p)&0xff) << 8)
6515853e133SVivek Gautam #define MAX_BURST_MASK (0xff)
6525853e133SVivek Gautam #define MAX_BURST_SHIFT (8)
6535853e133SVivek Gautam #define CTX_TO_MAX_BURST(p) (((p) >> 8) & 0xff)
6545853e133SVivek Gautam #define MAX_PACKET(p) (((p)&0xffff) << 16)
6555853e133SVivek Gautam #define MAX_PACKET_MASK (0xffff)
6565853e133SVivek Gautam #define MAX_PACKET_DECODED(p) (((p) >> 16) & 0xffff)
6575853e133SVivek Gautam #define MAX_PACKET_SHIFT (16)
6585853e133SVivek Gautam
6595853e133SVivek Gautam /* Get max packet size from ep desc. Bit 10..0 specify the max packet size.
6605853e133SVivek Gautam * USB2.0 spec 9.6.6.
6615853e133SVivek Gautam */
6625853e133SVivek Gautam #define GET_MAX_PACKET(p) ((p) & 0x7ff)
6635853e133SVivek Gautam
6645853e133SVivek Gautam /* tx_info bitmasks */
665f51966bfSBin Meng #define EP_AVG_TRB_LENGTH(p) ((p) & 0xffff)
666f51966bfSBin Meng #define EP_MAX_ESIT_PAYLOAD_LO(p) (((p) & 0xffff) << 16)
667f51966bfSBin Meng #define EP_MAX_ESIT_PAYLOAD_HI(p) ((((p) >> 16) & 0xff) << 24)
6685853e133SVivek Gautam #define CTX_TO_MAX_ESIT_PAYLOAD(p) (((p) >> 16) & 0xffff)
6695853e133SVivek Gautam
6705853e133SVivek Gautam /* deq bitmasks */
6715853e133SVivek Gautam #define EP_CTX_CYCLE_MASK (1 << 0)
6725853e133SVivek Gautam
6735853e133SVivek Gautam
6745853e133SVivek Gautam /**
6755853e133SVivek Gautam * struct xhci_input_control_context
6765853e133SVivek Gautam * Input control context; see section 6.2.5.
6775853e133SVivek Gautam *
6785853e133SVivek Gautam * @drop_context: set the bit of the endpoint context you want to disable
6795853e133SVivek Gautam * @add_context: set the bit of the endpoint context you want to enable
6805853e133SVivek Gautam */
6815853e133SVivek Gautam struct xhci_input_control_ctx {
6825853e133SVivek Gautam volatile __le32 drop_flags;
6835853e133SVivek Gautam volatile __le32 add_flags;
6845853e133SVivek Gautam __le32 rsvd2[6];
6855853e133SVivek Gautam };
6865853e133SVivek Gautam
6875853e133SVivek Gautam
6885853e133SVivek Gautam /**
6895853e133SVivek Gautam * struct xhci_device_context_array
6905853e133SVivek Gautam * @dev_context_ptr array of 64-bit DMA addresses for device contexts
6915853e133SVivek Gautam */
6925853e133SVivek Gautam struct xhci_device_context_array {
6935853e133SVivek Gautam /* 64-bit device addresses; we only write 32-bit addresses */
6945853e133SVivek Gautam __le64 dev_context_ptrs[MAX_HC_SLOTS];
6955853e133SVivek Gautam };
6965853e133SVivek Gautam /* TODO: write function to set the 64-bit device DMA address */
6975853e133SVivek Gautam /*
6985853e133SVivek Gautam * TODO: change this to be dynamically sized at HC mem init time since the HC
6995853e133SVivek Gautam * might not be able to handle the maximum number of devices possible.
7005853e133SVivek Gautam */
7015853e133SVivek Gautam
7025853e133SVivek Gautam
7035853e133SVivek Gautam struct xhci_transfer_event {
7045853e133SVivek Gautam /* 64-bit buffer address, or immediate data */
7055853e133SVivek Gautam __le64 buffer;
7065853e133SVivek Gautam __le32 transfer_len;
7075853e133SVivek Gautam /* This field is interpreted differently based on the type of TRB */
7085853e133SVivek Gautam volatile __le32 flags;
7095853e133SVivek Gautam };
7105853e133SVivek Gautam
7115853e133SVivek Gautam /* Transfer event TRB length bit mask */
7125853e133SVivek Gautam /* bits 0:23 */
7135853e133SVivek Gautam #define EVENT_TRB_LEN(p) ((p) & 0xffffff)
7145853e133SVivek Gautam
7155853e133SVivek Gautam /** Transfer Event bit fields **/
7165853e133SVivek Gautam #define TRB_TO_EP_ID(p) (((p) >> 16) & 0x1f)
7175853e133SVivek Gautam
7185853e133SVivek Gautam /* Completion Code - only applicable for some types of TRBs */
7195853e133SVivek Gautam #define COMP_CODE_MASK (0xff << 24)
7205853e133SVivek Gautam #define COMP_CODE_SHIFT (24)
7215853e133SVivek Gautam #define GET_COMP_CODE(p) (((p) & COMP_CODE_MASK) >> 24)
7225853e133SVivek Gautam
7235853e133SVivek Gautam typedef enum {
7245853e133SVivek Gautam COMP_SUCCESS = 1,
7255853e133SVivek Gautam /* Data Buffer Error */
7265853e133SVivek Gautam COMP_DB_ERR, /* 2 */
7275853e133SVivek Gautam /* Babble Detected Error */
7285853e133SVivek Gautam COMP_BABBLE, /* 3 */
7295853e133SVivek Gautam /* USB Transaction Error */
7305853e133SVivek Gautam COMP_TX_ERR, /* 4 */
7315853e133SVivek Gautam /* TRB Error - some TRB field is invalid */
7325853e133SVivek Gautam COMP_TRB_ERR, /* 5 */
7335853e133SVivek Gautam /* Stall Error - USB device is stalled */
7345853e133SVivek Gautam COMP_STALL, /* 6 */
7355853e133SVivek Gautam /* Resource Error - HC doesn't have memory for that device configuration */
7365853e133SVivek Gautam COMP_ENOMEM, /* 7 */
7375853e133SVivek Gautam /* Bandwidth Error - not enough room in schedule for this dev config */
7385853e133SVivek Gautam COMP_BW_ERR, /* 8 */
7395853e133SVivek Gautam /* No Slots Available Error - HC ran out of device slots */
7405853e133SVivek Gautam COMP_ENOSLOTS, /* 9 */
7415853e133SVivek Gautam /* Invalid Stream Type Error */
7425853e133SVivek Gautam COMP_STREAM_ERR, /* 10 */
7435853e133SVivek Gautam /* Slot Not Enabled Error - doorbell rung for disabled device slot */
7445853e133SVivek Gautam COMP_EBADSLT, /* 11 */
7455853e133SVivek Gautam /* Endpoint Not Enabled Error */
7465853e133SVivek Gautam COMP_EBADEP,/* 12 */
7475853e133SVivek Gautam /* Short Packet */
7485853e133SVivek Gautam COMP_SHORT_TX, /* 13 */
7495853e133SVivek Gautam /* Ring Underrun - doorbell rung for an empty isoc OUT ep ring */
7505853e133SVivek Gautam COMP_UNDERRUN, /* 14 */
7515853e133SVivek Gautam /* Ring Overrun - isoc IN ep ring is empty when ep is scheduled to RX */
7525853e133SVivek Gautam COMP_OVERRUN, /* 15 */
7535853e133SVivek Gautam /* Virtual Function Event Ring Full Error */
7545853e133SVivek Gautam COMP_VF_FULL, /* 16 */
7555853e133SVivek Gautam /* Parameter Error - Context parameter is invalid */
7565853e133SVivek Gautam COMP_EINVAL, /* 17 */
7575853e133SVivek Gautam /* Bandwidth Overrun Error - isoc ep exceeded its allocated bandwidth */
7585853e133SVivek Gautam COMP_BW_OVER,/* 18 */
7595853e133SVivek Gautam /* Context State Error - illegal context state transition requested */
7605853e133SVivek Gautam COMP_CTX_STATE,/* 19 */
7615853e133SVivek Gautam /* No Ping Response Error - HC didn't get PING_RESPONSE in time to TX */
7625853e133SVivek Gautam COMP_PING_ERR,/* 20 */
7635853e133SVivek Gautam /* Event Ring is full */
7645853e133SVivek Gautam COMP_ER_FULL,/* 21 */
7655853e133SVivek Gautam /* Incompatible Device Error */
7665853e133SVivek Gautam COMP_DEV_ERR,/* 22 */
7675853e133SVivek Gautam /* Missed Service Error - HC couldn't service an isoc ep within interval */
7685853e133SVivek Gautam COMP_MISSED_INT,/* 23 */
7695853e133SVivek Gautam /* Successfully stopped command ring */
7705853e133SVivek Gautam COMP_CMD_STOP, /* 24 */
7715853e133SVivek Gautam /* Successfully aborted current command and stopped command ring */
7725853e133SVivek Gautam COMP_CMD_ABORT, /* 25 */
7735853e133SVivek Gautam /* Stopped - transfer was terminated by a stop endpoint command */
7745853e133SVivek Gautam COMP_STOP,/* 26 */
7755853e133SVivek Gautam /* Same as COMP_EP_STOPPED, but the transferred length in the event
7765853e133SVivek Gautam * is invalid */
7775853e133SVivek Gautam COMP_STOP_INVAL, /* 27*/
7785853e133SVivek Gautam /* Control Abort Error - Debug Capability - control pipe aborted */
7795853e133SVivek Gautam COMP_DBG_ABORT, /* 28 */
7805853e133SVivek Gautam /* Max Exit Latency Too Large Error */
7815853e133SVivek Gautam COMP_MEL_ERR,/* 29 */
7825853e133SVivek Gautam /* TRB type 30 reserved */
7835853e133SVivek Gautam /* Isoc Buffer Overrun - an isoc IN ep sent more data than could fit in TD */
7845853e133SVivek Gautam COMP_BUFF_OVER = 31,
7855853e133SVivek Gautam /* Event Lost Error - xHC has an "internal event overrun condition" */
7865853e133SVivek Gautam COMP_ISSUES, /* 32 */
7875853e133SVivek Gautam /* Undefined Error - reported when other error codes don't apply */
7885853e133SVivek Gautam COMP_UNKNOWN, /* 33 */
7895853e133SVivek Gautam /* Invalid Stream ID Error */
7905853e133SVivek Gautam COMP_STRID_ERR, /* 34 */
7915853e133SVivek Gautam /* Secondary Bandwidth Error - may be returned by a Configure Endpoint cmd */
7925853e133SVivek Gautam COMP_2ND_BW_ERR, /* 35 */
7935853e133SVivek Gautam /* Split Transaction Error */
7945853e133SVivek Gautam COMP_SPLIT_ERR /* 36 */
7955853e133SVivek Gautam
7965853e133SVivek Gautam } xhci_comp_code;
7975853e133SVivek Gautam
7985853e133SVivek Gautam struct xhci_link_trb {
7995853e133SVivek Gautam /* 64-bit segment pointer*/
8005853e133SVivek Gautam volatile __le64 segment_ptr;
8015853e133SVivek Gautam volatile __le32 intr_target;
8025853e133SVivek Gautam volatile __le32 control;
8035853e133SVivek Gautam };
8045853e133SVivek Gautam
8055853e133SVivek Gautam /* control bitfields */
8065853e133SVivek Gautam #define LINK_TOGGLE (0x1 << 1)
8075853e133SVivek Gautam
8085853e133SVivek Gautam /* Command completion event TRB */
8095853e133SVivek Gautam struct xhci_event_cmd {
8105853e133SVivek Gautam /* Pointer to command TRB, or the value passed by the event data trb */
8115853e133SVivek Gautam volatile __le64 cmd_trb;
8125853e133SVivek Gautam volatile __le32 status;
8135853e133SVivek Gautam volatile __le32 flags;
8145853e133SVivek Gautam };
8155853e133SVivek Gautam
8165853e133SVivek Gautam /* flags bitmasks */
8175853e133SVivek Gautam /* bits 16:23 are the virtual function ID */
8185853e133SVivek Gautam /* bits 24:31 are the slot ID */
8195853e133SVivek Gautam #define TRB_TO_SLOT_ID(p) (((p) & (0xff << 24)) >> 24)
8205853e133SVivek Gautam #define TRB_TO_SLOT_ID_SHIFT (24)
8215853e133SVivek Gautam #define TRB_TO_SLOT_ID_MASK (0xff << TRB_TO_SLOT_ID_SHIFT)
8225853e133SVivek Gautam #define SLOT_ID_FOR_TRB(p) (((p) & 0xff) << 24)
8235853e133SVivek Gautam #define SLOT_ID_FOR_TRB_MASK (0xff)
8245853e133SVivek Gautam #define SLOT_ID_FOR_TRB_SHIFT (24)
8255853e133SVivek Gautam
8265853e133SVivek Gautam /* Stop Endpoint TRB - ep_index to endpoint ID for this TRB */
8275853e133SVivek Gautam #define TRB_TO_EP_INDEX(p) ((((p) & (0x1f << 16)) >> 16) - 1)
8285853e133SVivek Gautam #define EP_ID_FOR_TRB(p) ((((p) + 1) & 0x1f) << 16)
8295853e133SVivek Gautam
8305853e133SVivek Gautam #define SUSPEND_PORT_FOR_TRB(p) (((p) & 1) << 23)
8315853e133SVivek Gautam #define TRB_TO_SUSPEND_PORT(p) (((p) & (1 << 23)) >> 23)
8325853e133SVivek Gautam #define LAST_EP_INDEX 30
8335853e133SVivek Gautam
8345853e133SVivek Gautam /* Set TR Dequeue Pointer command TRB fields */
8355853e133SVivek Gautam #define TRB_TO_STREAM_ID(p) ((((p) & (0xffff << 16)) >> 16))
8365853e133SVivek Gautam #define STREAM_ID_FOR_TRB(p) ((((p)) & 0xffff) << 16)
8375853e133SVivek Gautam
8385853e133SVivek Gautam
8395853e133SVivek Gautam /* Port Status Change Event TRB fields */
8405853e133SVivek Gautam /* Port ID - bits 31:24 */
8415853e133SVivek Gautam #define GET_PORT_ID(p) (((p) & (0xff << 24)) >> 24)
8425853e133SVivek Gautam #define PORT_ID_SHIFT (24)
8435853e133SVivek Gautam #define PORT_ID_MASK (0xff << PORT_ID_SHIFT)
8445853e133SVivek Gautam
8455853e133SVivek Gautam /* Normal TRB fields */
8465853e133SVivek Gautam /* transfer_len bitmasks - bits 0:16 */
8475853e133SVivek Gautam #define TRB_LEN(p) ((p) & 0x1ffff)
8485853e133SVivek Gautam #define TRB_LEN_MASK (0x1ffff)
8495853e133SVivek Gautam /* Interrupter Target - which MSI-X vector to target the completion event at */
8505853e133SVivek Gautam #define TRB_INTR_TARGET_SHIFT (22)
8515853e133SVivek Gautam #define TRB_INTR_TARGET_MASK (0x3ff)
8525853e133SVivek Gautam #define TRB_INTR_TARGET(p) (((p) & 0x3ff) << 22)
8535853e133SVivek Gautam #define GET_INTR_TARGET(p) (((p) >> 22) & 0x3ff)
8545853e133SVivek Gautam #define TRB_TBC(p) (((p) & 0x3) << 7)
8555853e133SVivek Gautam #define TRB_TLBPC(p) (((p) & 0xf) << 16)
8565853e133SVivek Gautam
8575853e133SVivek Gautam /* Cycle bit - indicates TRB ownership by HC or HCD */
8585853e133SVivek Gautam #define TRB_CYCLE (1<<0)
8595853e133SVivek Gautam /*
8605853e133SVivek Gautam * Force next event data TRB to be evaluated before task switch.
8615853e133SVivek Gautam * Used to pass OS data back after a TD completes.
8625853e133SVivek Gautam */
8635853e133SVivek Gautam #define TRB_ENT (1<<1)
8645853e133SVivek Gautam /* Interrupt on short packet */
8655853e133SVivek Gautam #define TRB_ISP (1<<2)
8665853e133SVivek Gautam /* Set PCIe no snoop attribute */
8675853e133SVivek Gautam #define TRB_NO_SNOOP (1<<3)
8685853e133SVivek Gautam /* Chain multiple TRBs into a TD */
8695853e133SVivek Gautam #define TRB_CHAIN (1<<4)
8705853e133SVivek Gautam /* Interrupt on completion */
8715853e133SVivek Gautam #define TRB_IOC (1<<5)
8725853e133SVivek Gautam /* The buffer pointer contains immediate data */
8735853e133SVivek Gautam #define TRB_IDT (1<<6)
8745853e133SVivek Gautam
8755853e133SVivek Gautam /* Block Event Interrupt */
8765853e133SVivek Gautam #define TRB_BEI (1<<9)
8775853e133SVivek Gautam
8785853e133SVivek Gautam /* Control transfer TRB specific fields */
8795853e133SVivek Gautam #define TRB_DIR_IN (1<<16)
8805853e133SVivek Gautam #define TRB_TX_TYPE(p) ((p) << 16)
8815853e133SVivek Gautam #define TRB_TX_TYPE_SHIFT (16)
8825853e133SVivek Gautam #define TRB_DATA_OUT 2
8835853e133SVivek Gautam #define TRB_DATA_IN 3
8845853e133SVivek Gautam
8855853e133SVivek Gautam /* Isochronous TRB specific fields */
8865853e133SVivek Gautam #define TRB_SIA (1 << 31)
8875853e133SVivek Gautam
8885853e133SVivek Gautam struct xhci_generic_trb {
8895853e133SVivek Gautam volatile __le32 field[4];
8905853e133SVivek Gautam };
8915853e133SVivek Gautam
8925853e133SVivek Gautam union xhci_trb {
8935853e133SVivek Gautam struct xhci_link_trb link;
8945853e133SVivek Gautam struct xhci_transfer_event trans_event;
8955853e133SVivek Gautam struct xhci_event_cmd event_cmd;
8965853e133SVivek Gautam struct xhci_generic_trb generic;
8975853e133SVivek Gautam };
8985853e133SVivek Gautam
8995853e133SVivek Gautam /* TRB bit mask */
9005853e133SVivek Gautam #define TRB_TYPE_BITMASK (0xfc00)
9015853e133SVivek Gautam #define TRB_TYPE(p) ((p) << 10)
9025853e133SVivek Gautam #define TRB_TYPE_SHIFT (10)
9035853e133SVivek Gautam #define TRB_FIELD_TO_TYPE(p) (((p) & TRB_TYPE_BITMASK) >> 10)
9045853e133SVivek Gautam
9055853e133SVivek Gautam /* TRB type IDs */
9065853e133SVivek Gautam typedef enum {
9075853e133SVivek Gautam /* bulk, interrupt, isoc scatter/gather, and control data stage */
9085853e133SVivek Gautam TRB_NORMAL = 1,
9095853e133SVivek Gautam /* setup stage for control transfers */
9105853e133SVivek Gautam TRB_SETUP, /* 2 */
9115853e133SVivek Gautam /* data stage for control transfers */
9125853e133SVivek Gautam TRB_DATA, /* 3 */
9135853e133SVivek Gautam /* status stage for control transfers */
9145853e133SVivek Gautam TRB_STATUS, /* 4 */
9155853e133SVivek Gautam /* isoc transfers */
9165853e133SVivek Gautam TRB_ISOC, /* 5 */
9175853e133SVivek Gautam /* TRB for linking ring segments */
9185853e133SVivek Gautam TRB_LINK, /* 6 */
9195853e133SVivek Gautam /* TRB for EVENT DATA */
9205853e133SVivek Gautam TRB_EVENT_DATA, /* 7 */
9215853e133SVivek Gautam /* Transfer Ring No-op (not for the command ring) */
9225853e133SVivek Gautam TRB_TR_NOOP, /* 8 */
9235853e133SVivek Gautam /* Command TRBs */
9245853e133SVivek Gautam /* Enable Slot Command */
9255853e133SVivek Gautam TRB_ENABLE_SLOT, /* 9 */
9265853e133SVivek Gautam /* Disable Slot Command */
9275853e133SVivek Gautam TRB_DISABLE_SLOT, /* 10 */
9285853e133SVivek Gautam /* Address Device Command */
9295853e133SVivek Gautam TRB_ADDR_DEV, /* 11 */
9305853e133SVivek Gautam /* Configure Endpoint Command */
9315853e133SVivek Gautam TRB_CONFIG_EP, /* 12 */
9325853e133SVivek Gautam /* Evaluate Context Command */
9335853e133SVivek Gautam TRB_EVAL_CONTEXT, /* 13 */
9345853e133SVivek Gautam /* Reset Endpoint Command */
9355853e133SVivek Gautam TRB_RESET_EP, /* 14 */
9365853e133SVivek Gautam /* Stop Transfer Ring Command */
9375853e133SVivek Gautam TRB_STOP_RING, /* 15 */
9385853e133SVivek Gautam /* Set Transfer Ring Dequeue Pointer Command */
9395853e133SVivek Gautam TRB_SET_DEQ, /* 16 */
9405853e133SVivek Gautam /* Reset Device Command */
9415853e133SVivek Gautam TRB_RESET_DEV, /* 17 */
9425853e133SVivek Gautam /* Force Event Command (opt) */
9435853e133SVivek Gautam TRB_FORCE_EVENT, /* 18 */
9445853e133SVivek Gautam /* Negotiate Bandwidth Command (opt) */
9455853e133SVivek Gautam TRB_NEG_BANDWIDTH, /* 19 */
9465853e133SVivek Gautam /* Set Latency Tolerance Value Command (opt) */
9475853e133SVivek Gautam TRB_SET_LT, /* 20 */
9485853e133SVivek Gautam /* Get port bandwidth Command */
9495853e133SVivek Gautam TRB_GET_BW, /* 21 */
9505853e133SVivek Gautam /* Force Header Command - generate a transaction or link management packet */
9515853e133SVivek Gautam TRB_FORCE_HEADER, /* 22 */
9525853e133SVivek Gautam /* No-op Command - not for transfer rings */
9535853e133SVivek Gautam TRB_CMD_NOOP, /* 23 */
9545853e133SVivek Gautam /* TRB IDs 24-31 reserved */
9555853e133SVivek Gautam /* Event TRBS */
9565853e133SVivek Gautam /* Transfer Event */
9575853e133SVivek Gautam TRB_TRANSFER = 32,
9585853e133SVivek Gautam /* Command Completion Event */
9595853e133SVivek Gautam TRB_COMPLETION, /* 33 */
9605853e133SVivek Gautam /* Port Status Change Event */
9615853e133SVivek Gautam TRB_PORT_STATUS, /* 34 */
9625853e133SVivek Gautam /* Bandwidth Request Event (opt) */
9635853e133SVivek Gautam TRB_BANDWIDTH_EVENT, /* 35 */
9645853e133SVivek Gautam /* Doorbell Event (opt) */
9655853e133SVivek Gautam TRB_DOORBELL, /* 36 */
9665853e133SVivek Gautam /* Host Controller Event */
9675853e133SVivek Gautam TRB_HC_EVENT, /* 37 */
9685853e133SVivek Gautam /* Device Notification Event - device sent function wake notification */
9695853e133SVivek Gautam TRB_DEV_NOTE, /* 38 */
9705853e133SVivek Gautam /* MFINDEX Wrap Event - microframe counter wrapped */
9715853e133SVivek Gautam TRB_MFINDEX_WRAP, /* 39 */
9725853e133SVivek Gautam /* TRB IDs 40-47 reserved, 48-63 is vendor-defined */
9735853e133SVivek Gautam /* Nec vendor-specific command completion event. */
9745853e133SVivek Gautam TRB_NEC_CMD_COMP = 48, /* 48 */
9755853e133SVivek Gautam /* Get NEC firmware revision. */
9765853e133SVivek Gautam TRB_NEC_GET_FW, /* 49 */
9775853e133SVivek Gautam } trb_type;
9785853e133SVivek Gautam
9795853e133SVivek Gautam #define TRB_TYPE_LINK(x) (((x) & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK))
9805853e133SVivek Gautam /* Above, but for __le32 types -- can avoid work by swapping constants: */
9815853e133SVivek Gautam #define TRB_TYPE_LINK_LE32(x) (((x) & cpu_to_le32(TRB_TYPE_BITMASK)) == \
9825853e133SVivek Gautam cpu_to_le32(TRB_TYPE(TRB_LINK)))
9835853e133SVivek Gautam #define TRB_TYPE_NOOP_LE32(x) (((x) & cpu_to_le32(TRB_TYPE_BITMASK)) == \
9845853e133SVivek Gautam cpu_to_le32(TRB_TYPE(TRB_TR_NOOP)))
9855853e133SVivek Gautam
9865853e133SVivek Gautam /*
9875853e133SVivek Gautam * TRBS_PER_SEGMENT must be a multiple of 4,
9885853e133SVivek Gautam * since the command ring is 64-byte aligned.
9895853e133SVivek Gautam * It must also be greater than 16.
9905853e133SVivek Gautam */
9915853e133SVivek Gautam #define TRBS_PER_SEGMENT 64
9925853e133SVivek Gautam /* Allow two commands + a link TRB, along with any reserved command TRBs */
9935853e133SVivek Gautam #define MAX_RSVD_CMD_TRBS (TRBS_PER_SEGMENT - 3)
9945853e133SVivek Gautam #define SEGMENT_SIZE (TRBS_PER_SEGMENT*16)
9955853e133SVivek Gautam /* SEGMENT_SHIFT should be log2(SEGMENT_SIZE).
9965853e133SVivek Gautam * Change this if you change TRBS_PER_SEGMENT!
9975853e133SVivek Gautam */
9985853e133SVivek Gautam #define SEGMENT_SHIFT 10
9995853e133SVivek Gautam /* TRB buffer pointers can't cross 64KB boundaries */
10005853e133SVivek Gautam #define TRB_MAX_BUFF_SHIFT 16
10015853e133SVivek Gautam #define TRB_MAX_BUFF_SIZE (1 << TRB_MAX_BUFF_SHIFT)
10025853e133SVivek Gautam
10035853e133SVivek Gautam struct xhci_segment {
10045853e133SVivek Gautam union xhci_trb *trbs;
10055853e133SVivek Gautam /* private to HCD */
10065853e133SVivek Gautam struct xhci_segment *next;
10075853e133SVivek Gautam };
10085853e133SVivek Gautam
10095853e133SVivek Gautam struct xhci_ring {
10105853e133SVivek Gautam struct xhci_segment *first_seg;
10115853e133SVivek Gautam union xhci_trb *enqueue;
10125853e133SVivek Gautam struct xhci_segment *enq_seg;
10135853e133SVivek Gautam union xhci_trb *dequeue;
10145853e133SVivek Gautam struct xhci_segment *deq_seg;
10155853e133SVivek Gautam /*
10165853e133SVivek Gautam * Write the cycle state into the TRB cycle field to give ownership of
10175853e133SVivek Gautam * the TRB to the host controller (if we are the producer), or to check
10185853e133SVivek Gautam * if we own the TRB (if we are the consumer). See section 4.9.1.
10195853e133SVivek Gautam */
10205853e133SVivek Gautam volatile u32 cycle_state;
10215853e133SVivek Gautam unsigned int num_segs;
10225853e133SVivek Gautam };
10235853e133SVivek Gautam
10245853e133SVivek Gautam struct xhci_erst_entry {
10255853e133SVivek Gautam /* 64-bit event ring segment address */
10265853e133SVivek Gautam __le64 seg_addr;
10275853e133SVivek Gautam __le32 seg_size;
10285853e133SVivek Gautam /* Set to zero */
10295853e133SVivek Gautam __le32 rsvd;
10305853e133SVivek Gautam };
10315853e133SVivek Gautam
10325853e133SVivek Gautam struct xhci_erst {
10335853e133SVivek Gautam struct xhci_erst_entry *entries;
10345853e133SVivek Gautam unsigned int num_entries;
10355853e133SVivek Gautam /* Num entries the ERST can contain */
10365853e133SVivek Gautam unsigned int erst_size;
10375853e133SVivek Gautam };
10385853e133SVivek Gautam
1039209b98deSBin Meng struct xhci_scratchpad {
1040209b98deSBin Meng u64 *sp_array;
1041209b98deSBin Meng };
1042209b98deSBin Meng
10435853e133SVivek Gautam /*
10445853e133SVivek Gautam * Each segment table entry is 4*32bits long. 1K seems like an ok size:
10455853e133SVivek Gautam * (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table,
10465853e133SVivek Gautam * meaning 64 ring segments.
10475853e133SVivek Gautam * Initial allocated size of the ERST, in number of entries */
10487489d22aSMarek Vasut #define ERST_NUM_SEGS 1
10495853e133SVivek Gautam /* Initial number of event segment rings allocated */
10507489d22aSMarek Vasut #define ERST_ENTRIES 1
10515853e133SVivek Gautam /* Initial allocated size of the ERST, in number of entries */
10525853e133SVivek Gautam #define ERST_SIZE 64
10535853e133SVivek Gautam /* Poll every 60 seconds */
10545853e133SVivek Gautam #define POLL_TIMEOUT 60
10555853e133SVivek Gautam /* Stop endpoint command timeout (secs) for URB cancellation watchdog timer */
10565853e133SVivek Gautam #define XHCI_STOP_EP_CMD_TIMEOUT 5
10575853e133SVivek Gautam /* XXX: Make these module parameters */
10585853e133SVivek Gautam
10595853e133SVivek Gautam struct xhci_virt_ep {
10605853e133SVivek Gautam struct xhci_ring *ring;
10615853e133SVivek Gautam unsigned int ep_state;
10625853e133SVivek Gautam #define SET_DEQ_PENDING (1 << 0)
10635853e133SVivek Gautam #define EP_HALTED (1 << 1) /* For stall handling */
10645853e133SVivek Gautam #define EP_HALT_PENDING (1 << 2) /* For URB cancellation */
10655853e133SVivek Gautam /* Transitioning the endpoint to using streams, don't enqueue URBs */
10665853e133SVivek Gautam #define EP_GETTING_STREAMS (1 << 3)
10675853e133SVivek Gautam #define EP_HAS_STREAMS (1 << 4)
10685853e133SVivek Gautam /* Transitioning the endpoint to not using streams, don't enqueue URBs */
10695853e133SVivek Gautam #define EP_GETTING_NO_STREAMS (1 << 5)
10705853e133SVivek Gautam };
10715853e133SVivek Gautam
10725853e133SVivek Gautam #define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32)
10735853e133SVivek Gautam
10745853e133SVivek Gautam struct xhci_virt_device {
10755853e133SVivek Gautam struct usb_device *udev;
10765853e133SVivek Gautam /*
10775853e133SVivek Gautam * Commands to the hardware are passed an "input context" that
10785853e133SVivek Gautam * tells the hardware what to change in its data structures.
10795853e133SVivek Gautam * The hardware will return changes in an "output context" that
10805853e133SVivek Gautam * software must allocate for the hardware. We need to keep
10815853e133SVivek Gautam * track of input and output contexts separately because
10825853e133SVivek Gautam * these commands might fail and we don't trust the hardware.
10835853e133SVivek Gautam */
10845853e133SVivek Gautam struct xhci_container_ctx *out_ctx;
10855853e133SVivek Gautam /* Used for addressing devices and configuration changes */
10865853e133SVivek Gautam struct xhci_container_ctx *in_ctx;
10875853e133SVivek Gautam /* Rings saved to ensure old alt settings can be re-instated */
10885853e133SVivek Gautam #define XHCI_MAX_RINGS_CACHED 31
10895853e133SVivek Gautam struct xhci_virt_ep eps[31];
10905853e133SVivek Gautam };
10915853e133SVivek Gautam
10925853e133SVivek Gautam /* TODO: copied from ehci.h - can be refactored? */
10935853e133SVivek Gautam /* xHCI spec says all registers are little endian */
xhci_readl(uint32_t volatile * regs)10945853e133SVivek Gautam static inline unsigned int xhci_readl(uint32_t volatile *regs)
10955853e133SVivek Gautam {
10965853e133SVivek Gautam return readl(regs);
10975853e133SVivek Gautam }
10985853e133SVivek Gautam
xhci_writel(uint32_t volatile * regs,const unsigned int val)10995853e133SVivek Gautam static inline void xhci_writel(uint32_t volatile *regs, const unsigned int val)
11005853e133SVivek Gautam {
11015853e133SVivek Gautam writel(val, regs);
11025853e133SVivek Gautam }
11035853e133SVivek Gautam
11045853e133SVivek Gautam /*
11055853e133SVivek Gautam * Registers should always be accessed with double word or quad word accesses.
11065853e133SVivek Gautam * Some xHCI implementations may support 64-bit address pointers. Registers
11075853e133SVivek Gautam * with 64-bit address pointers should be written to with dword accesses by
11085853e133SVivek Gautam * writing the low dword first (ptr[0]), then the high dword (ptr[1]) second.
11095853e133SVivek Gautam * xHCI implementations that do not support 64-bit address pointers will ignore
11105853e133SVivek Gautam * the high dword, and write order is irrelevant.
11115853e133SVivek Gautam */
xhci_readq(__le64 volatile * regs)11125853e133SVivek Gautam static inline u64 xhci_readq(__le64 volatile *regs)
11135853e133SVivek Gautam {
1114421a5a0cSSergey Temerkhanov #if BITS_PER_LONG == 64
1115421a5a0cSSergey Temerkhanov return readq(regs);
1116421a5a0cSSergey Temerkhanov #else
11175853e133SVivek Gautam __u32 *ptr = (__u32 *)regs;
11185853e133SVivek Gautam u64 val_lo = readl(ptr);
11195853e133SVivek Gautam u64 val_hi = readl(ptr + 1);
11205853e133SVivek Gautam return val_lo + (val_hi << 32);
1121421a5a0cSSergey Temerkhanov #endif
11225853e133SVivek Gautam }
11235853e133SVivek Gautam
xhci_writeq(__le64 volatile * regs,const u64 val)11245853e133SVivek Gautam static inline void xhci_writeq(__le64 volatile *regs, const u64 val)
11255853e133SVivek Gautam {
1126421a5a0cSSergey Temerkhanov #if BITS_PER_LONG == 64
1127421a5a0cSSergey Temerkhanov writeq(val, regs);
1128421a5a0cSSergey Temerkhanov #else
11295853e133SVivek Gautam __u32 *ptr = (__u32 *)regs;
11305853e133SVivek Gautam u32 val_lo = lower_32_bits(val);
11315853e133SVivek Gautam /* FIXME */
11324a755f1dSLijun Pan u32 val_hi = upper_32_bits(val);
11335853e133SVivek Gautam writel(val_lo, ptr);
11345853e133SVivek Gautam writel(val_hi, ptr + 1);
1135421a5a0cSSergey Temerkhanov #endif
11365853e133SVivek Gautam }
11375853e133SVivek Gautam
11385853e133SVivek Gautam int xhci_hcd_init(int index, struct xhci_hccr **ret_hccr,
11395853e133SVivek Gautam struct xhci_hcor **ret_hcor);
11405853e133SVivek Gautam void xhci_hcd_stop(int index);
11415853e133SVivek Gautam
11425853e133SVivek Gautam
11435853e133SVivek Gautam /*************************************************************
11445853e133SVivek Gautam EXTENDED CAPABILITY DEFINITIONS
11455853e133SVivek Gautam *************************************************************/
11465853e133SVivek Gautam /* Up to 16 ms to halt an HC */
11475853e133SVivek Gautam #define XHCI_MAX_HALT_USEC (16*1000)
11485853e133SVivek Gautam /* HC not running - set to 1 when run/stop bit is cleared. */
11495853e133SVivek Gautam #define XHCI_STS_HALT (1 << 0)
11505853e133SVivek Gautam
11515853e133SVivek Gautam /* HCCPARAMS offset from PCI base address */
11525853e133SVivek Gautam #define XHCI_HCC_PARAMS_OFFSET 0x10
11535853e133SVivek Gautam /* HCCPARAMS contains the first extended capability pointer */
11545853e133SVivek Gautam #define XHCI_HCC_EXT_CAPS(p) (((p)>>16)&0xffff)
11555853e133SVivek Gautam
11565853e133SVivek Gautam /* Command and Status registers offset from the Operational Registers address */
11575853e133SVivek Gautam #define XHCI_CMD_OFFSET 0x00
11585853e133SVivek Gautam #define XHCI_STS_OFFSET 0x04
11595853e133SVivek Gautam
11605853e133SVivek Gautam #define XHCI_MAX_EXT_CAPS 50
11615853e133SVivek Gautam
11625853e133SVivek Gautam /* Capability Register */
11635853e133SVivek Gautam /* bits 7:0 - how long is the Capabilities register */
11645853e133SVivek Gautam #define XHCI_HC_LENGTH(p) (((p) >> 00) & 0x00ff)
11655853e133SVivek Gautam
11665853e133SVivek Gautam /* Extended capability register fields */
11675853e133SVivek Gautam #define XHCI_EXT_CAPS_ID(p) (((p) >> 0) & 0xff)
11685853e133SVivek Gautam #define XHCI_EXT_CAPS_NEXT(p) (((p) >> 8) & 0xff)
11695853e133SVivek Gautam #define XHCI_EXT_CAPS_VAL(p) ((p) >> 16)
11705853e133SVivek Gautam /* Extended capability IDs - ID 0 reserved */
11715853e133SVivek Gautam #define XHCI_EXT_CAPS_LEGACY 1
11725853e133SVivek Gautam #define XHCI_EXT_CAPS_PROTOCOL 2
11735853e133SVivek Gautam #define XHCI_EXT_CAPS_PM 3
11745853e133SVivek Gautam #define XHCI_EXT_CAPS_VIRT 4
11755853e133SVivek Gautam #define XHCI_EXT_CAPS_ROUTE 5
11765853e133SVivek Gautam /* IDs 6-9 reserved */
11775853e133SVivek Gautam #define XHCI_EXT_CAPS_DEBUG 10
11785853e133SVivek Gautam /* USB Legacy Support Capability - section 7.1.1 */
11795853e133SVivek Gautam #define XHCI_HC_BIOS_OWNED (1 << 16)
11805853e133SVivek Gautam #define XHCI_HC_OS_OWNED (1 << 24)
11815853e133SVivek Gautam
11825853e133SVivek Gautam /* USB Legacy Support Capability - section 7.1.1 */
11835853e133SVivek Gautam /* Add this offset, plus the value of xECP in HCCPARAMS to the base address */
11845853e133SVivek Gautam #define XHCI_LEGACY_SUPPORT_OFFSET (0x00)
11855853e133SVivek Gautam
11865853e133SVivek Gautam /* USB Legacy Support Control and Status Register - section 7.1.2 */
11875853e133SVivek Gautam /* Add this offset, plus the value of xECP in HCCPARAMS to the base address */
11885853e133SVivek Gautam #define XHCI_LEGACY_CONTROL_OFFSET (0x04)
11895853e133SVivek Gautam /* bits 1:2, 5:12, and 17:19 need to be preserved; bits 21:28 should be zero */
11905853e133SVivek Gautam #define XHCI_LEGACY_DISABLE_SMI ((0x3 << 1) + (0xff << 5) + (0x7 << 17))
11915853e133SVivek Gautam
11925853e133SVivek Gautam /* USB 2.0 xHCI 0.96 L1C capability - section 7.2.2.1.3.2 */
11935853e133SVivek Gautam #define XHCI_L1C (1 << 16)
11945853e133SVivek Gautam
11955853e133SVivek Gautam /* USB 2.0 xHCI 1.0 hardware LMP capability - section 7.2.2.1.3.2 */
11965853e133SVivek Gautam #define XHCI_HLC (1 << 19)
11975853e133SVivek Gautam
11985853e133SVivek Gautam /* command register values to disable interrupts and halt the HC */
11995853e133SVivek Gautam /* start/stop HC execution - do not write unless HC is halted*/
12005853e133SVivek Gautam #define XHCI_CMD_RUN (1 << 0)
12015853e133SVivek Gautam /* Event Interrupt Enable - get irq when EINT bit is set in USBSTS register */
12025853e133SVivek Gautam #define XHCI_CMD_EIE (1 << 2)
12035853e133SVivek Gautam /* Host System Error Interrupt Enable - get irq when HSEIE bit set in USBSTS */
12045853e133SVivek Gautam #define XHCI_CMD_HSEIE (1 << 3)
12055853e133SVivek Gautam /* Enable Wrap Event - '1' means xHC generates an event when MFINDEX wraps. */
12065853e133SVivek Gautam #define XHCI_CMD_EWE (1 << 10)
12075853e133SVivek Gautam
12085853e133SVivek Gautam #define XHCI_IRQS (XHCI_CMD_EIE | XHCI_CMD_HSEIE | XHCI_CMD_EWE)
12095853e133SVivek Gautam
12105853e133SVivek Gautam /* true: Controller Not Ready to accept doorbell or op reg writes after reset */
12115853e133SVivek Gautam #define XHCI_STS_CNR (1 << 11)
12125853e133SVivek Gautam
12135853e133SVivek Gautam struct xhci_ctrl {
1214*fd09c205SSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
1215a5762fe0SSimon Glass struct udevice *dev;
1216a5762fe0SSimon Glass #endif
12175853e133SVivek Gautam struct xhci_hccr *hccr; /* R/O registers, not need for volatile */
12185853e133SVivek Gautam struct xhci_hcor *hcor;
12195853e133SVivek Gautam struct xhci_doorbell_array *dba;
12205853e133SVivek Gautam struct xhci_run_regs *run_regs;
12215853e133SVivek Gautam struct xhci_device_context_array *dcbaa \
12225853e133SVivek Gautam __attribute__ ((aligned(ARCH_DMA_MINALIGN)));
12235853e133SVivek Gautam struct xhci_ring *event_ring;
12245853e133SVivek Gautam struct xhci_ring *cmd_ring;
12255853e133SVivek Gautam struct xhci_ring *transfer_ring;
12265853e133SVivek Gautam struct xhci_segment *seg;
12275853e133SVivek Gautam struct xhci_intr_reg *ir_set;
12285853e133SVivek Gautam struct xhci_erst erst;
12295853e133SVivek Gautam struct xhci_erst_entry entry[ERST_NUM_SEGS];
1230209b98deSBin Meng struct xhci_scratchpad *scratchpad;
12315853e133SVivek Gautam struct xhci_virt_device *devs[MAX_HC_SLOTS];
12325853e133SVivek Gautam int rootdev;
12335853e133SVivek Gautam };
12345853e133SVivek Gautam
12355853e133SVivek Gautam unsigned long trb_addr(struct xhci_segment *seg, union xhci_trb *trb);
12365853e133SVivek Gautam struct xhci_input_control_ctx
12375853e133SVivek Gautam *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx);
12385853e133SVivek Gautam struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl,
12395853e133SVivek Gautam struct xhci_container_ctx *ctx);
12405853e133SVivek Gautam struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl,
12415853e133SVivek Gautam struct xhci_container_ctx *ctx,
12425853e133SVivek Gautam unsigned int ep_index);
12435853e133SVivek Gautam void xhci_endpoint_copy(struct xhci_ctrl *ctrl,
12445853e133SVivek Gautam struct xhci_container_ctx *in_ctx,
12455853e133SVivek Gautam struct xhci_container_ctx *out_ctx,
12465853e133SVivek Gautam unsigned int ep_index);
12475853e133SVivek Gautam void xhci_slot_copy(struct xhci_ctrl *ctrl,
12485853e133SVivek Gautam struct xhci_container_ctx *in_ctx,
12495853e133SVivek Gautam struct xhci_container_ctx *out_ctx);
1250daec4691SBin Meng void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
1251daec4691SBin Meng struct usb_device *udev, int hop_portnr);
12525853e133SVivek Gautam void xhci_queue_command(struct xhci_ctrl *ctrl, u8 *ptr,
12535853e133SVivek Gautam u32 slot_id, u32 ep_index, trb_type cmd);
12545853e133SVivek Gautam void xhci_acknowledge_event(struct xhci_ctrl *ctrl);
12555853e133SVivek Gautam union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected);
12565853e133SVivek Gautam int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe,
12575853e133SVivek Gautam int length, void *buffer);
12585853e133SVivek Gautam int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe,
12595853e133SVivek Gautam struct devrequest *req, int length, void *buffer);
12605853e133SVivek Gautam int xhci_check_maxpacket(struct usb_device *udev);
1261421a5a0cSSergey Temerkhanov void xhci_flush_cache(uintptr_t addr, u32 type_len);
1262421a5a0cSSergey Temerkhanov void xhci_inval_cache(uintptr_t addr, u32 type_len);
12635853e133SVivek Gautam void xhci_cleanup(struct xhci_ctrl *ctrl);
12645853e133SVivek Gautam struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs);
12657e0c5ee8SSimon Glass int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id);
12665853e133SVivek Gautam int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
12675853e133SVivek Gautam struct xhci_hcor *hcor);
12685853e133SVivek Gautam
1269a5762fe0SSimon Glass /**
1270a5762fe0SSimon Glass * xhci_deregister() - Unregister an XHCI controller
1271a5762fe0SSimon Glass *
1272a5762fe0SSimon Glass * @dev: Controller device
1273a5762fe0SSimon Glass * @return 0 if registered, -ve on error
1274a5762fe0SSimon Glass */
1275a5762fe0SSimon Glass int xhci_deregister(struct udevice *dev);
1276a5762fe0SSimon Glass
1277a5762fe0SSimon Glass /**
1278a5762fe0SSimon Glass * xhci_register() - Register a new XHCI controller
1279a5762fe0SSimon Glass *
1280a5762fe0SSimon Glass * @dev: Controller device
1281a5762fe0SSimon Glass * @hccr: Host controller control registers
1282a5762fe0SSimon Glass * @hcor: Not sure what this means
1283a5762fe0SSimon Glass * @return 0 if registered, -ve on error
1284a5762fe0SSimon Glass */
1285a5762fe0SSimon Glass int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
1286a5762fe0SSimon Glass struct xhci_hcor *hcor);
1287a5762fe0SSimon Glass
1288a5762fe0SSimon Glass extern struct dm_usb_ops xhci_usb_ops;
1289a5762fe0SSimon Glass
12907c1deec0SSimon Glass struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev);
12917c1deec0SSimon Glass
12925853e133SVivek Gautam #endif /* HOST_XHCI_H_ */
1293