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