xhci.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #ifndef __LINUX_XHCI_HCD_H
  23. #define __LINUX_XHCI_HCD_H
  24. #include <linux/usb.h>
  25. #include "../core/hcd.h"
  26. /* Code sharing between pci-quirks and xhci hcd */
  27. #include "xhci-ext-caps.h"
  28. /* xHCI PCI Configuration Registers */
  29. #define XHCI_SBRN_OFFSET (0x60)
  30. /* Max number of USB devices for any host controller - limit in section 6.1 */
  31. #define MAX_HC_SLOTS 256
  32. /*
  33. * xHCI register interface.
  34. * This corresponds to the eXtensible Host Controller Interface (xHCI)
  35. * Revision 0.95 specification
  36. *
  37. * Registers should always be accessed with double word or quad word accesses.
  38. *
  39. * Some xHCI implementations may support 64-bit address pointers. Registers
  40. * with 64-bit address pointers should be written to with dword accesses by
  41. * writing the low dword first (ptr[0]), then the high dword (ptr[1]) second.
  42. * xHCI implementations that do not support 64-bit address pointers will ignore
  43. * the high dword, and write order is irrelevant.
  44. */
  45. /**
  46. * struct xhci_cap_regs - xHCI Host Controller Capability Registers.
  47. * @hc_capbase: length of the capabilities register and HC version number
  48. * @hcs_params1: HCSPARAMS1 - Structural Parameters 1
  49. * @hcs_params2: HCSPARAMS2 - Structural Parameters 2
  50. * @hcs_params3: HCSPARAMS3 - Structural Parameters 3
  51. * @hcc_params: HCCPARAMS - Capability Parameters
  52. * @db_off: DBOFF - Doorbell array offset
  53. * @run_regs_off: RTSOFF - Runtime register space offset
  54. */
  55. struct xhci_cap_regs {
  56. u32 hc_capbase;
  57. u32 hcs_params1;
  58. u32 hcs_params2;
  59. u32 hcs_params3;
  60. u32 hcc_params;
  61. u32 db_off;
  62. u32 run_regs_off;
  63. /* Reserved up to (CAPLENGTH - 0x1C) */
  64. } __attribute__ ((packed));
  65. /* hc_capbase bitmasks */
  66. /* bits 7:0 - how long is the Capabilities register */
  67. #define HC_LENGTH(p) XHCI_HC_LENGTH(p)
  68. /* bits 31:16 */
  69. #define HC_VERSION(p) (((p) >> 16) & 0xffff)
  70. /* HCSPARAMS1 - hcs_params1 - bitmasks */
  71. /* bits 0:7, Max Device Slots */
  72. #define HCS_MAX_SLOTS(p) (((p) >> 0) & 0xff)
  73. #define HCS_SLOTS_MASK 0xff
  74. /* bits 8:18, Max Interrupters */
  75. #define HCS_MAX_INTRS(p) (((p) >> 8) & 0x7ff)
  76. /* bits 24:31, Max Ports - max value is 0x7F = 127 ports */
  77. #define HCS_MAX_PORTS(p) (((p) >> 24) & 0x7f)
  78. /* HCSPARAMS2 - hcs_params2 - bitmasks */
  79. /* bits 0:3, frames or uframes that SW needs to queue transactions
  80. * ahead of the HW to meet periodic deadlines */
  81. #define HCS_IST(p) (((p) >> 0) & 0xf)
  82. /* bits 4:7, max number of Event Ring segments */
  83. #define HCS_ERST_MAX(p) (((p) >> 4) & 0xf)
  84. /* bit 26 Scratchpad restore - for save/restore HW state - not used yet */
  85. /* bits 27:31 number of Scratchpad buffers SW must allocate for the HW */
  86. /* HCSPARAMS3 - hcs_params3 - bitmasks */
  87. /* bits 0:7, Max U1 to U0 latency for the roothub ports */
  88. #define HCS_U1_LATENCY(p) (((p) >> 0) & 0xff)
  89. /* bits 16:31, Max U2 to U0 latency for the roothub ports */
  90. #define HCS_U2_LATENCY(p) (((p) >> 16) & 0xffff)
  91. /* HCCPARAMS - hcc_params - bitmasks */
  92. /* true: HC can use 64-bit address pointers */
  93. #define HCC_64BIT_ADDR(p) ((p) & (1 << 0))
  94. /* true: HC can do bandwidth negotiation */
  95. #define HCC_BANDWIDTH_NEG(p) ((p) & (1 << 1))
  96. /* true: HC uses 64-byte Device Context structures
  97. * FIXME 64-byte context structures aren't supported yet.
  98. */
  99. #define HCC_64BYTE_CONTEXT(p) ((p) & (1 << 2))
  100. /* true: HC has port power switches */
  101. #define HCC_PPC(p) ((p) & (1 << 3))
  102. /* true: HC has port indicators */
  103. #define HCS_INDICATOR(p) ((p) & (1 << 4))
  104. /* true: HC has Light HC Reset Capability */
  105. #define HCC_LIGHT_RESET(p) ((p) & (1 << 5))
  106. /* true: HC supports latency tolerance messaging */
  107. #define HCC_LTC(p) ((p) & (1 << 6))
  108. /* true: no secondary Stream ID Support */
  109. #define HCC_NSS(p) ((p) & (1 << 7))
  110. /* Max size for Primary Stream Arrays - 2^(n+1), where n is bits 12:15 */
  111. #define HCC_MAX_PSA (1 << ((((p) >> 12) & 0xf) + 1))
  112. /* Extended Capabilities pointer from PCI base - section 5.3.6 */
  113. #define HCC_EXT_CAPS(p) XHCI_HCC_EXT_CAPS(p)
  114. /* db_off bitmask - bits 0:1 reserved */
  115. #define DBOFF_MASK (~0x3)
  116. /* run_regs_off bitmask - bits 0:4 reserved */
  117. #define RTSOFF_MASK (~0x1f)
  118. /* Number of registers per port */
  119. #define NUM_PORT_REGS 4
  120. /**
  121. * struct xhci_op_regs - xHCI Host Controller Operational Registers.
  122. * @command: USBCMD - xHC command register
  123. * @status: USBSTS - xHC status register
  124. * @page_size: This indicates the page size that the host controller
  125. * supports. If bit n is set, the HC supports a page size
  126. * of 2^(n+12), up to a 128MB page size.
  127. * 4K is the minimum page size.
  128. * @cmd_ring: CRP - 64-bit Command Ring Pointer
  129. * @dcbaa_ptr: DCBAAP - 64-bit Device Context Base Address Array Pointer
  130. * @config_reg: CONFIG - Configure Register
  131. * @port_status_base: PORTSCn - base address for Port Status and Control
  132. * Each port has a Port Status and Control register,
  133. * followed by a Port Power Management Status and Control
  134. * register, a Port Link Info register, and a reserved
  135. * register.
  136. * @port_power_base: PORTPMSCn - base address for
  137. * Port Power Management Status and Control
  138. * @port_link_base: PORTLIn - base address for Port Link Info (current
  139. * Link PM state and control) for USB 2.1 and USB 3.0
  140. * devices.
  141. */
  142. struct xhci_op_regs {
  143. u32 command;
  144. u32 status;
  145. u32 page_size;
  146. u32 reserved1;
  147. u32 reserved2;
  148. u32 dev_notification;
  149. u32 cmd_ring[2];
  150. /* rsvd: offset 0x20-2F */
  151. u32 reserved3[4];
  152. u32 dcbaa_ptr[2];
  153. u32 config_reg;
  154. /* rsvd: offset 0x3C-3FF */
  155. u32 reserved4[241];
  156. /* port 1 registers, which serve as a base address for other ports */
  157. u32 port_status_base;
  158. u32 port_power_base;
  159. u32 port_link_base;
  160. u32 reserved5;
  161. /* registers for ports 2-255 */
  162. u32 reserved6[NUM_PORT_REGS*254];
  163. } __attribute__ ((packed));
  164. /* USBCMD - USB command - command bitmasks */
  165. /* start/stop HC execution - do not write unless HC is halted*/
  166. #define CMD_RUN XHCI_CMD_RUN
  167. /* Reset HC - resets internal HC state machine and all registers (except
  168. * PCI config regs). HC does NOT drive a USB reset on the downstream ports.
  169. * The xHCI driver must reinitialize the xHC after setting this bit.
  170. */
  171. #define CMD_RESET (1 << 1)
  172. /* Event Interrupt Enable - a '1' allows interrupts from the host controller */
  173. #define CMD_EIE XHCI_CMD_EIE
  174. /* Host System Error Interrupt Enable - get out-of-band signal for HC errors */
  175. #define CMD_HSEIE XHCI_CMD_HSEIE
  176. /* bits 4:6 are reserved (and should be preserved on writes). */
  177. /* light reset (port status stays unchanged) - reset completed when this is 0 */
  178. #define CMD_LRESET (1 << 7)
  179. /* FIXME: ignoring host controller save/restore state for now. */
  180. #define CMD_CSS (1 << 8)
  181. #define CMD_CRS (1 << 9)
  182. /* Enable Wrap Event - '1' means xHC generates an event when MFINDEX wraps. */
  183. #define CMD_EWE XHCI_CMD_EWE
  184. /* MFINDEX power management - '1' means xHC can stop MFINDEX counter if all root
  185. * hubs are in U3 (selective suspend), disconnect, disabled, or powered-off.
  186. * '0' means the xHC can power it off if all ports are in the disconnect,
  187. * disabled, or powered-off state.
  188. */
  189. #define CMD_PM_INDEX (1 << 11)
  190. /* bits 12:31 are reserved (and should be preserved on writes). */
  191. /* USBSTS - USB status - status bitmasks */
  192. /* HC not running - set to 1 when run/stop bit is cleared. */
  193. #define STS_HALT XHCI_STS_HALT
  194. /* serious error, e.g. PCI parity error. The HC will clear the run/stop bit. */
  195. #define STS_FATAL (1 << 2)
  196. /* event interrupt - clear this prior to clearing any IP flags in IR set*/
  197. #define STS_EINT (1 << 3)
  198. /* port change detect */
  199. #define STS_PORT (1 << 4)
  200. /* bits 5:7 reserved and zeroed */
  201. /* save state status - '1' means xHC is saving state */
  202. #define STS_SAVE (1 << 8)
  203. /* restore state status - '1' means xHC is restoring state */
  204. #define STS_RESTORE (1 << 9)
  205. /* true: save or restore error */
  206. #define STS_SRE (1 << 10)
  207. /* true: Controller Not Ready to accept doorbell or op reg writes after reset */
  208. #define STS_CNR XHCI_STS_CNR
  209. /* true: internal Host Controller Error - SW needs to reset and reinitialize */
  210. #define STS_HCE (1 << 12)
  211. /* bits 13:31 reserved and should be preserved */
  212. /*
  213. * DNCTRL - Device Notification Control Register - dev_notification bitmasks
  214. * Generate a device notification event when the HC sees a transaction with a
  215. * notification type that matches a bit set in this bit field.
  216. */
  217. #define DEV_NOTE_MASK (0xffff)
  218. #define ENABLE_DEV_NOTE(x) (1 << x)
  219. /* Most of the device notification types should only be used for debug.
  220. * SW does need to pay attention to function wake notifications.
  221. */
  222. #define DEV_NOTE_FWAKE ENABLE_DEV_NOTE(1)
  223. /* CRCR - Command Ring Control Register - cmd_ring bitmasks */
  224. /* bit 0 is the command ring cycle state */
  225. /* stop ring operation after completion of the currently executing command */
  226. #define CMD_RING_PAUSE (1 << 1)
  227. /* stop ring immediately - abort the currently executing command */
  228. #define CMD_RING_ABORT (1 << 2)
  229. /* true: command ring is running */
  230. #define CMD_RING_RUNNING (1 << 3)
  231. /* bits 4:5 reserved and should be preserved */
  232. /* Command Ring pointer - bit mask for the lower 32 bits. */
  233. #define CMD_RING_ADDR_MASK (0xffffffc0)
  234. /* CONFIG - Configure Register - config_reg bitmasks */
  235. /* bits 0:7 - maximum number of device slots enabled (NumSlotsEn) */
  236. #define MAX_DEVS(p) ((p) & 0xff)
  237. /* bits 8:31 - reserved and should be preserved */
  238. /* PORTSC - Port Status and Control Register - port_status_base bitmasks */
  239. /* true: device connected */
  240. #define PORT_CONNECT (1 << 0)
  241. /* true: port enabled */
  242. #define PORT_PE (1 << 1)
  243. /* bit 2 reserved and zeroed */
  244. /* true: port has an over-current condition */
  245. #define PORT_OC (1 << 3)
  246. /* true: port reset signaling asserted */
  247. #define PORT_RESET (1 << 4)
  248. /* Port Link State - bits 5:8
  249. * A read gives the current link PM state of the port,
  250. * a write with Link State Write Strobe set sets the link state.
  251. */
  252. /* true: port has power (see HCC_PPC) */
  253. #define PORT_POWER (1 << 9)
  254. /* bits 10:13 indicate device speed:
  255. * 0 - undefined speed - port hasn't be initialized by a reset yet
  256. * 1 - full speed
  257. * 2 - low speed
  258. * 3 - high speed
  259. * 4 - super speed
  260. * 5-15 reserved
  261. */
  262. #define DEV_SPEED_MASK (0xf<<10)
  263. #define DEV_UNDEFSPEED(p) (((p) & DEV_SPEED_MASK) == (0x0<<10))
  264. #define DEV_FULLSPEED(p) (((p) & DEV_SPEED_MASK) == (0x1<<10))
  265. #define DEV_LOWSPEED(p) (((p) & DEV_SPEED_MASK) == (0x2<<10))
  266. #define DEV_HIGHSPEED(p) (((p) & DEV_SPEED_MASK) == (0x3<<10))
  267. #define DEV_SUPERSPEED(p) (((p) & DEV_SPEED_MASK) == (0x4<<10))
  268. /* Port Indicator Control */
  269. #define PORT_LED_OFF (0 << 14)
  270. #define PORT_LED_AMBER (1 << 14)
  271. #define PORT_LED_GREEN (2 << 14)
  272. #define PORT_LED_MASK (3 << 14)
  273. /* Port Link State Write Strobe - set this when changing link state */
  274. #define PORT_LINK_STROBE (1 << 16)
  275. /* true: connect status change */
  276. #define PORT_CSC (1 << 17)
  277. /* true: port enable change */
  278. #define PORT_PEC (1 << 18)
  279. /* true: warm reset for a USB 3.0 device is done. A "hot" reset puts the port
  280. * into an enabled state, and the device into the default state. A "warm" reset
  281. * also resets the link, forcing the device through the link training sequence.
  282. * SW can also look at the Port Reset register to see when warm reset is done.
  283. */
  284. #define PORT_WRC (1 << 19)
  285. /* true: over-current change */
  286. #define PORT_OCC (1 << 20)
  287. /* true: reset change - 1 to 0 transition of PORT_RESET */
  288. #define PORT_RC (1 << 21)
  289. /* port link status change - set on some port link state transitions:
  290. * Transition Reason
  291. * ------------------------------------------------------------------------------
  292. * - U3 to Resume Wakeup signaling from a device
  293. * - Resume to Recovery to U0 USB 3.0 device resume
  294. * - Resume to U0 USB 2.0 device resume
  295. * - U3 to Recovery to U0 Software resume of USB 3.0 device complete
  296. * - U3 to U0 Software resume of USB 2.0 device complete
  297. * - U2 to U0 L1 resume of USB 2.1 device complete
  298. * - U0 to U0 (???) L1 entry rejection by USB 2.1 device
  299. * - U0 to disabled L1 entry error with USB 2.1 device
  300. * - Any state to inactive Error on USB 3.0 port
  301. */
  302. #define PORT_PLC (1 << 22)
  303. /* port configure error change - port failed to configure its link partner */
  304. #define PORT_CEC (1 << 23)
  305. /* bit 24 reserved */
  306. /* wake on connect (enable) */
  307. #define PORT_WKCONN_E (1 << 25)
  308. /* wake on disconnect (enable) */
  309. #define PORT_WKDISC_E (1 << 26)
  310. /* wake on over-current (enable) */
  311. #define PORT_WKOC_E (1 << 27)
  312. /* bits 28:29 reserved */
  313. /* true: device is removable - for USB 3.0 roothub emulation */
  314. #define PORT_DEV_REMOVE (1 << 30)
  315. /* Initiate a warm port reset - complete when PORT_WRC is '1' */
  316. #define PORT_WR (1 << 31)
  317. /* Port Power Management Status and Control - port_power_base bitmasks */
  318. /* Inactivity timer value for transitions into U1, in microseconds.
  319. * Timeout can be up to 127us. 0xFF means an infinite timeout.
  320. */
  321. #define PORT_U1_TIMEOUT(p) ((p) & 0xff)
  322. /* Inactivity timer value for transitions into U2 */
  323. #define PORT_U2_TIMEOUT(p) (((p) & 0xff) << 8)
  324. /* Bits 24:31 for port testing */
  325. /**
  326. * struct intr_reg - Interrupt Register Set
  327. * @irq_pending: IMAN - Interrupt Management Register. Used to enable
  328. * interrupts and check for pending interrupts.
  329. * @irq_control: IMOD - Interrupt Moderation Register.
  330. * Used to throttle interrupts.
  331. * @erst_size: Number of segments in the Event Ring Segment Table (ERST).
  332. * @erst_base: ERST base address.
  333. * @erst_dequeue: Event ring dequeue pointer.
  334. *
  335. * Each interrupter (defined by a MSI-X vector) has an event ring and an Event
  336. * Ring Segment Table (ERST) associated with it. The event ring is comprised of
  337. * multiple segments of the same size. The HC places events on the ring and
  338. * "updates the Cycle bit in the TRBs to indicate to software the current
  339. * position of the Enqueue Pointer." The HCD (Linux) processes those events and
  340. * updates the dequeue pointer.
  341. */
  342. struct intr_reg {
  343. u32 irq_pending;
  344. u32 irq_control;
  345. u32 erst_size;
  346. u32 rsvd;
  347. u32 erst_base[2];
  348. u32 erst_dequeue[2];
  349. } __attribute__ ((packed));
  350. /* irq_pending bitmasks */
  351. #define ER_IRQ_PENDING(p) ((p) & 0x1)
  352. /* bits 2:31 need to be preserved */
  353. #define ER_IRQ_CLEAR(p) ((p) & 0xfffffffe)
  354. #define ER_IRQ_ENABLE(p) ((ER_IRQ_CLEAR(p)) | 0x2)
  355. #define ER_IRQ_DISABLE(p) ((ER_IRQ_CLEAR(p)) & ~(0x2))
  356. /* irq_control bitmasks */
  357. /* Minimum interval between interrupts (in 250ns intervals). The interval
  358. * between interrupts will be longer if there are no events on the event ring.
  359. * Default is 4000 (1 ms).
  360. */
  361. #define ER_IRQ_INTERVAL_MASK (0xffff)
  362. /* Counter used to count down the time to the next interrupt - HW use only */
  363. #define ER_IRQ_COUNTER_MASK (0xffff << 16)
  364. /* erst_size bitmasks */
  365. /* Preserve bits 16:31 of erst_size */
  366. #define ERST_SIZE_MASK (0xffff << 16)
  367. /* erst_dequeue bitmasks */
  368. /* Dequeue ERST Segment Index (DESI) - Segment number (or alias)
  369. * where the current dequeue pointer lies. This is an optional HW hint.
  370. */
  371. #define ERST_DESI_MASK (0x7)
  372. /* Event Handler Busy (EHB) - is the event ring scheduled to be serviced by
  373. * a work queue (or delayed service routine)?
  374. */
  375. #define ERST_EHB (1 << 3)
  376. #define ERST_PTR_MASK (0xf)
  377. /**
  378. * struct xhci_run_regs
  379. * @microframe_index:
  380. * MFINDEX - current microframe number
  381. *
  382. * Section 5.5 Host Controller Runtime Registers:
  383. * "Software should read and write these registers using only Dword (32 bit)
  384. * or larger accesses"
  385. */
  386. struct xhci_run_regs {
  387. u32 microframe_index;
  388. u32 rsvd[7];
  389. struct intr_reg ir_set[128];
  390. } __attribute__ ((packed));
  391. /**
  392. * struct doorbell_array
  393. *
  394. * Section 5.6
  395. */
  396. struct xhci_doorbell_array {
  397. u32 doorbell[256];
  398. } __attribute__ ((packed));
  399. #define DB_TARGET_MASK 0xFFFFFF00
  400. #define DB_STREAM_ID_MASK 0x0000FFFF
  401. #define DB_TARGET_HOST 0x0
  402. #define DB_STREAM_ID_HOST 0x0
  403. #define DB_MASK (0xff << 8)
  404. /**
  405. * struct xhci_slot_ctx
  406. * @dev_info: Route string, device speed, hub info, and last valid endpoint
  407. * @dev_info2: Max exit latency for device number, root hub port number
  408. * @tt_info: tt_info is used to construct split transaction tokens
  409. * @dev_state: slot state and device address
  410. *
  411. * Slot Context - section 6.2.1.1. This assumes the HC uses 32-byte context
  412. * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
  413. * reserved at the end of the slot context for HC internal use.
  414. */
  415. struct xhci_slot_ctx {
  416. u32 dev_info;
  417. u32 dev_info2;
  418. u32 tt_info;
  419. u32 dev_state;
  420. /* offset 0x10 to 0x1f reserved for HC internal use */
  421. u32 reserved[4];
  422. } __attribute__ ((packed));
  423. /* dev_info bitmasks */
  424. /* Route String - 0:19 */
  425. #define ROUTE_STRING_MASK (0xfffff)
  426. /* Device speed - values defined by PORTSC Device Speed field - 20:23 */
  427. #define DEV_SPEED (0xf << 20)
  428. /* bit 24 reserved */
  429. /* Is this LS/FS device connected through a HS hub? - bit 25 */
  430. #define DEV_MTT (0x1 << 25)
  431. /* Set if the device is a hub - bit 26 */
  432. #define DEV_HUB (0x1 << 26)
  433. /* Index of the last valid endpoint context in this device context - 27:31 */
  434. #define LAST_EP_MASK (0x1f << 27)
  435. #define LAST_EP(p) ((p) << 27)
  436. /* dev_info2 bitmasks */
  437. /* Max Exit Latency (ms) - worst case time to wake up all links in dev path */
  438. #define MAX_EXIT (0xffff)
  439. /* Root hub port number that is needed to access the USB device */
  440. #define ROOT_HUB_PORT (0xff << 16)
  441. /* tt_info bitmasks */
  442. /*
  443. * TT Hub Slot ID - for low or full speed devices attached to a high-speed hub
  444. * The Slot ID of the hub that isolates the high speed signaling from
  445. * this low or full-speed device. '0' if attached to root hub port.
  446. */
  447. #define TT_SLOT (0xff)
  448. /*
  449. * The number of the downstream facing port of the high-speed hub
  450. * '0' if the device is not low or full speed.
  451. */
  452. #define TT_PORT (0xff << 8)
  453. /* dev_state bitmasks */
  454. /* USB device address - assigned by the HC */
  455. #define DEV_ADDR (0xff)
  456. /* bits 8:26 reserved */
  457. /* Slot state */
  458. #define SLOT_STATE (0x1f << 27)
  459. /**
  460. * struct xhci_ep_ctx
  461. * @ep_info: endpoint state, streams, mult, and interval information.
  462. * @ep_info2: information on endpoint type, max packet size, max burst size,
  463. * error count, and whether the HC will force an event for all
  464. * transactions.
  465. * @ep_ring: 64-bit ring address. If the endpoint only defines one flow,
  466. * this points to the endpoint transfer ring. Otherwise, it points
  467. * to a flow context array, which has a ring pointer for each flow.
  468. * @intr_target:
  469. * 64-bit address of the Interrupter Target that will receive
  470. * events from this endpoint.
  471. *
  472. * Endpoint Context - section 6.2.1.2. This assumes the HC uses 32-byte context
  473. * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
  474. * reserved at the end of the endpoint context for HC internal use.
  475. */
  476. struct xhci_ep_ctx {
  477. u32 ep_info;
  478. u32 ep_info2;
  479. /* 64-bit endpoint ring address */
  480. u32 ep_ring[2];
  481. /* 64-bit address of the interrupter target */
  482. u32 intr_target[2];
  483. /* offset 0x14 - 0x1f reserved for HC internal use */
  484. u32 reserved[2];
  485. } __attribute__ ((packed));
  486. /* ep_info bitmasks */
  487. /*
  488. * Endpoint State - bits 0:2
  489. * 0 - disabled
  490. * 1 - running
  491. * 2 - halted due to halt condition - ok to manipulate endpoint ring
  492. * 3 - stopped
  493. * 4 - TRB error
  494. * 5-7 - reserved
  495. */
  496. #define EP_STATE (0xf)
  497. /* Mult - Max number of burtst within an interval, in EP companion desc. */
  498. #define EP_MULT(p) ((p & 0x3) << 8)
  499. /* bits 10:14 are Max Primary Streams */
  500. /* bit 15 is Linear Stream Array */
  501. /* Interval - period between requests to an endpoint - 125u increments. */
  502. #define EP_INTERVAL (0xff << 16)
  503. /* ep_info2 bitmasks */
  504. /*
  505. * Force Event - generate transfer events for all TRBs for this endpoint
  506. * This will tell the HC to ignore the IOC and ISP flags (for debugging only).
  507. */
  508. #define FORCE_EVENT (0x1)
  509. #define ERROR_COUNT(p) (((p) & 0x3) << 1)
  510. #define EP_TYPE(p) ((p) << 3)
  511. #define ISOC_OUT_EP 1
  512. #define BULK_OUT_EP 2
  513. #define INT_OUT_EP 3
  514. #define CTRL_EP 4
  515. #define ISOC_IN_EP 5
  516. #define BULK_IN_EP 6
  517. #define INT_IN_EP 7
  518. /* bit 6 reserved */
  519. /* bit 7 is Host Initiate Disable - for disabling stream selection */
  520. #define MAX_BURST(p) (((p)&0xff) << 8)
  521. #define MAX_PACKET(p) (((p)&0xffff) << 16)
  522. /**
  523. * struct xhci_device_control
  524. * Input/Output context; see section 6.2.5.
  525. *
  526. * @drop_context: set the bit of the endpoint context you want to disable
  527. * @add_context: set the bit of the endpoint context you want to enable
  528. */
  529. struct xhci_device_control {
  530. u32 drop_flags;
  531. u32 add_flags;
  532. u32 rsvd[6];
  533. struct xhci_slot_ctx slot;
  534. struct xhci_ep_ctx ep[31];
  535. } __attribute__ ((packed));
  536. /* drop context bitmasks */
  537. #define DROP_EP(x) (0x1 << x)
  538. /* add context bitmasks */
  539. #define ADD_EP(x) (0x1 << x)
  540. /**
  541. * struct xhci_device_context_array
  542. * @dev_context_ptr array of 64-bit DMA addresses for device contexts
  543. */
  544. struct xhci_device_context_array {
  545. /* 64-bit device addresses; we only write 32-bit addresses */
  546. u32 dev_context_ptrs[2*MAX_HC_SLOTS];
  547. /* private xHCD pointers */
  548. dma_addr_t dma;
  549. } __attribute__ ((packed));
  550. /* TODO: write function to set the 64-bit device DMA address */
  551. /*
  552. * TODO: change this to be dynamically sized at HC mem init time since the HC
  553. * might not be able to handle the maximum number of devices possible.
  554. */
  555. struct xhci_stream_ctx {
  556. /* 64-bit stream ring address, cycle state, and stream type */
  557. u32 stream_ring[2];
  558. /* offset 0x14 - 0x1f reserved for HC internal use */
  559. u32 reserved[2];
  560. } __attribute__ ((packed));
  561. struct xhci_transfer_event {
  562. /* 64-bit buffer address, or immediate data */
  563. u32 buffer[2];
  564. u32 transfer_len;
  565. /* This field is interpreted differently based on the type of TRB */
  566. u32 flags;
  567. } __attribute__ ((packed));
  568. /* Completion Code - only applicable for some types of TRBs */
  569. #define COMP_CODE_MASK (0xff << 24)
  570. #define GET_COMP_CODE(p) (((p) & COMP_CODE_MASK) >> 24)
  571. #define COMP_SUCCESS 1
  572. /* Data Buffer Error */
  573. #define COMP_DB_ERR 2
  574. /* Babble Detected Error */
  575. #define COMP_BABBLE 3
  576. /* USB Transaction Error */
  577. #define COMP_TX_ERR 4
  578. /* TRB Error - some TRB field is invalid */
  579. #define COMP_TRB_ERR 5
  580. /* Stall Error - USB device is stalled */
  581. #define COMP_STALL 6
  582. /* Resource Error - HC doesn't have memory for that device configuration */
  583. #define COMP_ENOMEM 7
  584. /* Bandwidth Error - not enough room in schedule for this dev config */
  585. #define COMP_BW_ERR 8
  586. /* No Slots Available Error - HC ran out of device slots */
  587. #define COMP_ENOSLOTS 9
  588. /* Invalid Stream Type Error */
  589. #define COMP_STREAM_ERR 10
  590. /* Slot Not Enabled Error - doorbell rung for disabled device slot */
  591. #define COMP_EBADSLT 11
  592. /* Endpoint Not Enabled Error */
  593. #define COMP_EBADEP 12
  594. /* Short Packet */
  595. #define COMP_SHORT_TX 13
  596. /* Ring Underrun - doorbell rung for an empty isoc OUT ep ring */
  597. #define COMP_UNDERRUN 14
  598. /* Ring Overrun - isoc IN ep ring is empty when ep is scheduled to RX */
  599. #define COMP_OVERRUN 15
  600. /* Virtual Function Event Ring Full Error */
  601. #define COMP_VF_FULL 16
  602. /* Parameter Error - Context parameter is invalid */
  603. #define COMP_EINVAL 17
  604. /* Bandwidth Overrun Error - isoc ep exceeded its allocated bandwidth */
  605. #define COMP_BW_OVER 18
  606. /* Context State Error - illegal context state transition requested */
  607. #define COMP_CTX_STATE 19
  608. /* No Ping Response Error - HC didn't get PING_RESPONSE in time to TX */
  609. #define COMP_PING_ERR 20
  610. /* Event Ring is full */
  611. #define COMP_ER_FULL 21
  612. /* Missed Service Error - HC couldn't service an isoc ep within interval */
  613. #define COMP_MISSED_INT 23
  614. /* Successfully stopped command ring */
  615. #define COMP_CMD_STOP 24
  616. /* Successfully aborted current command and stopped command ring */
  617. #define COMP_CMD_ABORT 25
  618. /* Stopped - transfer was terminated by a stop endpoint command */
  619. #define COMP_STOP 26
  620. /* Same as COMP_EP_STOPPED, but the transfered length in the event is invalid */
  621. #define COMP_STOP_INVAL 27
  622. /* Control Abort Error - Debug Capability - control pipe aborted */
  623. #define COMP_DBG_ABORT 28
  624. /* TRB type 29 and 30 reserved */
  625. /* Isoc Buffer Overrun - an isoc IN ep sent more data than could fit in TD */
  626. #define COMP_BUFF_OVER 31
  627. /* Event Lost Error - xHC has an "internal event overrun condition" */
  628. #define COMP_ISSUES 32
  629. /* Undefined Error - reported when other error codes don't apply */
  630. #define COMP_UNKNOWN 33
  631. /* Invalid Stream ID Error */
  632. #define COMP_STRID_ERR 34
  633. /* Secondary Bandwidth Error - may be returned by a Configure Endpoint cmd */
  634. /* FIXME - check for this */
  635. #define COMP_2ND_BW_ERR 35
  636. /* Split Transaction Error */
  637. #define COMP_SPLIT_ERR 36
  638. struct xhci_link_trb {
  639. /* 64-bit segment pointer*/
  640. u32 segment_ptr[2];
  641. u32 intr_target;
  642. u32 control;
  643. } __attribute__ ((packed));
  644. /* control bitfields */
  645. #define LINK_TOGGLE (0x1<<1)
  646. union xhci_trb {
  647. struct xhci_link_trb link;
  648. struct xhci_transfer_event trans_event;
  649. };
  650. /* Normal TRB fields */
  651. /* transfer_len bitmasks - bits 0:16 */
  652. #define TRB_LEN(p) ((p) & 0x1ffff)
  653. /* TD size - number of bytes remaining in the TD (including this TRB):
  654. * bits 17 - 21. Shift the number of bytes by 10. */
  655. #define TD_REMAINDER(p) ((((p) >> 10) & 0x1f) << 17)
  656. /* Interrupter Target - which MSI-X vector to target the completion event at */
  657. #define TRB_INTR_TARGET(p) (((p) & 0x3ff) << 22)
  658. #define GET_INTR_TARGET(p) (((p) >> 22) & 0x3ff)
  659. /* Cycle bit - indicates TRB ownership by HC or HCD */
  660. #define TRB_CYCLE (1<<0)
  661. /*
  662. * Force next event data TRB to be evaluated before task switch.
  663. * Used to pass OS data back after a TD completes.
  664. */
  665. #define TRB_ENT (1<<1)
  666. /* Interrupt on short packet */
  667. #define TRB_ISP (1<<2)
  668. /* Set PCIe no snoop attribute */
  669. #define TRB_NO_SNOOP (1<<3)
  670. /* Chain multiple TRBs into a TD */
  671. #define TRB_CHAIN (1<<4)
  672. /* Interrupt on completion */
  673. #define TRB_IOC (1<<5)
  674. /* The buffer pointer contains immediate data */
  675. #define TRB_IDT (1<<6)
  676. /* Control transfer TRB specific fields */
  677. #define TRB_DIR_IN (1<<16)
  678. /* TRB bit mask */
  679. #define TRB_TYPE_BITMASK (0xfc00)
  680. #define TRB_TYPE(p) ((p) << 10)
  681. /* TRB type IDs */
  682. /* bulk, interrupt, isoc scatter/gather, and control data stage */
  683. #define TRB_NORMAL 1
  684. /* setup stage for control transfers */
  685. #define TRB_SETUP 2
  686. /* data stage for control transfers */
  687. #define TRB_DATA 3
  688. /* status stage for control transfers */
  689. #define TRB_STATUS 4
  690. /* isoc transfers */
  691. #define TRB_ISOC 5
  692. /* TRB for linking ring segments */
  693. #define TRB_LINK 6
  694. #define TRB_EVENT_DATA 7
  695. /* Transfer Ring No-op (not for the command ring) */
  696. #define TRB_TR_NOOP 8
  697. /* Command TRBs */
  698. /* Enable Slot Command */
  699. #define TRB_ENABLE_SLOT 9
  700. /* Disable Slot Command */
  701. #define TRB_DISABLE_SLOT 10
  702. /* Address Device Command */
  703. #define TRB_ADDR_DEV 11
  704. /* Configure Endpoint Command */
  705. #define TRB_CONFIG_EP 12
  706. /* Evaluate Context Command */
  707. #define TRB_EVAL_CONTEXT 13
  708. /* Reset Transfer Ring Command */
  709. #define TRB_RESET_RING 14
  710. /* Stop Transfer Ring Command */
  711. #define TRB_STOP_RING 15
  712. /* Set Transfer Ring Dequeue Pointer Command */
  713. #define TRB_SET_DEQ 16
  714. /* Reset Device Command */
  715. #define TRB_RESET_DEV 17
  716. /* Force Event Command (opt) */
  717. #define TRB_FORCE_EVENT 18
  718. /* Negotiate Bandwidth Command (opt) */
  719. #define TRB_NEG_BANDWIDTH 19
  720. /* Set Latency Tolerance Value Command (opt) */
  721. #define TRB_SET_LT 20
  722. /* Get port bandwidth Command */
  723. #define TRB_GET_BW 21
  724. /* Force Header Command - generate a transaction or link management packet */
  725. #define TRB_FORCE_HEADER 22
  726. /* No-op Command - not for transfer rings */
  727. #define TRB_CMD_NOOP 23
  728. /* TRB IDs 24-31 reserved */
  729. /* Event TRBS */
  730. /* Transfer Event */
  731. #define TRB_TRANSFER 32
  732. /* Command Completion Event */
  733. #define TRB_COMPLETION 33
  734. /* Port Status Change Event */
  735. #define TRB_PORT_STATUS 34
  736. /* Bandwidth Request Event (opt) */
  737. #define TRB_BANDWIDTH_EVENT 35
  738. /* Doorbell Event (opt) */
  739. #define TRB_DOORBELL 36
  740. /* Host Controller Event */
  741. #define TRB_HC_EVENT 37
  742. /* Device Notification Event - device sent function wake notification */
  743. #define TRB_DEV_NOTE 38
  744. /* MFINDEX Wrap Event - microframe counter wrapped */
  745. #define TRB_MFINDEX_WRAP 39
  746. /* TRB IDs 40-47 reserved, 48-63 is vendor-defined */
  747. /*
  748. * TRBS_PER_SEGMENT must be a multiple of 4,
  749. * since the command ring is 64-byte aligned.
  750. * It must also be greater than 16.
  751. */
  752. #define TRBS_PER_SEGMENT 64
  753. #define SEGMENT_SIZE (TRBS_PER_SEGMENT*16)
  754. struct xhci_segment {
  755. union xhci_trb *trbs;
  756. /* private to HCD */
  757. struct xhci_segment *next;
  758. dma_addr_t dma;
  759. } __attribute__ ((packed));
  760. struct xhci_ring {
  761. struct xhci_segment *first_seg;
  762. union xhci_trb *enqueue;
  763. union xhci_trb *dequeue;
  764. /*
  765. * Write the cycle state into the TRB cycle field to give ownership of
  766. * the TRB to the host controller (if we are the producer), or to check
  767. * if we own the TRB (if we are the consumer). See section 4.9.1.
  768. */
  769. u32 cycle_state;
  770. };
  771. struct xhci_erst_entry {
  772. /* 64-bit event ring segment address */
  773. u32 seg_addr[2];
  774. u32 seg_size;
  775. /* Set to zero */
  776. u32 rsvd;
  777. } __attribute__ ((packed));
  778. struct xhci_erst {
  779. struct xhci_erst_entry *entries;
  780. unsigned int num_entries;
  781. /* xhci->event_ring keeps track of segment dma addresses */
  782. dma_addr_t erst_dma_addr;
  783. /* Num entries the ERST can contain */
  784. unsigned int erst_size;
  785. };
  786. /*
  787. * Each segment table entry is 4*32bits long. 1K seems like an ok size:
  788. * (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table,
  789. * meaning 64 ring segments.
  790. * Initial allocated size of the ERST, in number of entries */
  791. #define ERST_NUM_SEGS 1
  792. /* Initial allocated size of the ERST, in number of entries */
  793. #define ERST_SIZE 64
  794. /* Initial number of event segment rings allocated */
  795. #define ERST_ENTRIES 1
  796. /* XXX: Make these module parameters */
  797. /* There is one ehci_hci structure per controller */
  798. struct xhci_hcd {
  799. /* glue to PCI and HCD framework */
  800. struct xhci_cap_regs __iomem *cap_regs;
  801. struct xhci_op_regs __iomem *op_regs;
  802. struct xhci_run_regs __iomem *run_regs;
  803. struct xhci_doorbell_array __iomem *dba;
  804. /* Our HCD's current interrupter register set */
  805. struct intr_reg __iomem *ir_set;
  806. /* Cached register copies of read-only HC data */
  807. __u32 hcs_params1;
  808. __u32 hcs_params2;
  809. __u32 hcs_params3;
  810. __u32 hcc_params;
  811. spinlock_t lock;
  812. /* packed release number */
  813. u8 sbrn;
  814. u16 hci_version;
  815. u8 max_slots;
  816. u8 max_interrupters;
  817. u8 max_ports;
  818. u8 isoc_threshold;
  819. int event_ring_max;
  820. int addr_64;
  821. /* 4KB min, 128MB max */
  822. int page_size;
  823. /* Valid values are 12 to 20, inclusive */
  824. int page_shift;
  825. /* only one MSI vector for now, but might need more later */
  826. int msix_count;
  827. struct msix_entry *msix_entries;
  828. /* data structures */
  829. struct xhci_device_context_array *dcbaa;
  830. struct xhci_ring *cmd_ring;
  831. struct xhci_ring *event_ring;
  832. struct xhci_erst erst;
  833. /* DMA pools */
  834. struct dma_pool *device_pool;
  835. struct dma_pool *segment_pool;
  836. };
  837. /* convert between an HCD pointer and the corresponding EHCI_HCD */
  838. static inline struct xhci_hcd *hcd_to_xhci(struct usb_hcd *hcd)
  839. {
  840. return (struct xhci_hcd *) (hcd->hcd_priv);
  841. }
  842. static inline struct usb_hcd *xhci_to_hcd(struct xhci_hcd *xhci)
  843. {
  844. return container_of((void *) xhci, struct usb_hcd, hcd_priv);
  845. }
  846. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  847. #define XHCI_DEBUG 1
  848. #else
  849. #define XHCI_DEBUG 0
  850. #endif
  851. #define xhci_dbg(xhci, fmt, args...) \
  852. do { if (XHCI_DEBUG) dev_dbg(xhci_to_hcd(xhci)->self.controller , fmt , ## args); } while (0)
  853. #define xhci_info(xhci, fmt, args...) \
  854. do { if (XHCI_DEBUG) dev_info(xhci_to_hcd(xhci)->self.controller , fmt , ## args); } while (0)
  855. #define xhci_err(xhci, fmt, args...) \
  856. dev_err(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  857. #define xhci_warn(xhci, fmt, args...) \
  858. dev_warn(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  859. /* TODO: copied from ehci.h - can be refactored? */
  860. /* xHCI spec says all registers are little endian */
  861. static inline unsigned int xhci_readl(const struct xhci_hcd *xhci,
  862. __u32 __iomem *regs)
  863. {
  864. return readl(regs);
  865. }
  866. static inline void xhci_writel(const struct xhci_hcd *xhci,
  867. const unsigned int val, __u32 __iomem *regs)
  868. {
  869. if (!in_interrupt())
  870. xhci_dbg(xhci, "`MEM_WRITE_DWORD(3'b000, 32'h%0x, 32'h%0x, 4'hf);\n",
  871. (unsigned int) regs, val);
  872. writel(val, regs);
  873. }
  874. /* xHCI debugging */
  875. void xhci_print_ir_set(struct xhci_hcd *xhci, struct intr_reg *ir_set, int set_num);
  876. void xhci_print_registers(struct xhci_hcd *xhci);
  877. void xhci_dbg_regs(struct xhci_hcd *xhci);
  878. void xhci_print_run_regs(struct xhci_hcd *xhci);
  879. void xhci_debug_ring(struct xhci_hcd *xhci, struct xhci_ring *ring);
  880. void xhci_dbg_erst(struct xhci_hcd *xhci, struct xhci_erst *erst);
  881. void xhci_dbg_cmd_ptrs(struct xhci_hcd *xhci);
  882. /* xHCI memory managment */
  883. void xhci_mem_cleanup(struct xhci_hcd *xhci);
  884. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags);
  885. #ifdef CONFIG_PCI
  886. /* xHCI PCI glue */
  887. int xhci_register_pci(void);
  888. void xhci_unregister_pci(void);
  889. #endif
  890. /* xHCI host controller glue */
  891. int xhci_halt(struct xhci_hcd *xhci);
  892. int xhci_reset(struct xhci_hcd *xhci);
  893. int xhci_init(struct usb_hcd *hcd);
  894. int xhci_run(struct usb_hcd *hcd);
  895. void xhci_stop(struct usb_hcd *hcd);
  896. void xhci_shutdown(struct usb_hcd *hcd);
  897. int xhci_get_frame(struct usb_hcd *hcd);
  898. #endif /* __LINUX_XHCI_HCD_H */