xhci.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #pragma once
  2. #include <driver/usb/usb.h>
  3. #include <driver/pci/pci.h>
  4. #define XHCI_MAX_HOST_CONTROLLERS 4 // 本驱动程序最大支持4个xhci root hub controller
  5. #define XHCI_MAX_ROOT_HUB_PORTS 128 // 本驱动程序最大支持127个root hub 端口(第0个保留)
  6. // ========== irq BEGIN ===========
  7. /**
  8. * @brief 每个xhci控制器的中断向量号
  9. *
  10. */
  11. const uint8_t xhci_controller_irq_num[XHCI_MAX_HOST_CONTROLLERS] = {157, 158, 159, 160};
  12. /**
  13. * @brief 通过irq号寻找对应的主机控制器id
  14. *
  15. */
  16. #define xhci_find_hcid_by_irq_num(irq_num) ({ \
  17. int retval = -1; \
  18. for (int i = 0; i < XHCI_MAX_HOST_CONTROLLERS; ++i) \
  19. if (xhci_controller_irq_num[i] == irq_num) \
  20. retval = i; \
  21. retval; \
  22. })
  23. struct xhci_hc_irq_install_info_t
  24. {
  25. int processor; // 中断目标处理器
  26. int8_t edge_trigger; // 是否边缘触发
  27. int8_t assert; // 是否高电平触发
  28. };
  29. // ========== irq END ===========
  30. // ======== Capability Register Set BEGIN ============
  31. // xhci Capability Registers offset
  32. #define XHCI_CAPS_CAPLENGTH 0x00 // Cap 寄存器组的长度
  33. #define XHCI_CAPS_RESERVED 0x01
  34. #define XHCI_CAPS_HCIVERSION 0x02 // 接口版本号
  35. #define XHCI_CAPS_HCSPARAMS1 0x04
  36. #define XHCI_CAPS_HCSPARAMS2 0x08
  37. #define XHCI_CAPS_HCSPARAMS3 0x0c
  38. #define XHCI_CAPS_HCCPARAMS1 0x10 // capability params 1
  39. #define XHCI_CAPS_DBOFF 0x14 // Doorbell offset
  40. #define XHCI_CAPS_RTSOFF 0x18 // Runtime register space offset
  41. #define XHCI_CAPS_HCCPARAMS2 0x1c // capability params 2
  42. struct xhci_caps_HCSPARAMS1_reg_t
  43. {
  44. unsigned max_slots : 8; // 最大插槽数
  45. unsigned max_intrs : 11; // 最大中断数
  46. unsigned reserved : 5;
  47. unsigned max_ports : 8; // 最大端口数
  48. } __attribute__((packed));
  49. struct xhci_caps_HCSPARAMS2_reg_t
  50. {
  51. unsigned ist : 4; // 同步调度阈值
  52. unsigned ERST_Max : 4; // Event Ring Segment Table: Max segs
  53. unsigned Reserved : 13;
  54. unsigned max_scratchpad_buf_HI5 : 5; // 草稿行buffer地址(高5bit)
  55. unsigned spr : 1; // scratchpad restore
  56. unsigned max_scratchpad_buf_LO5 : 5; // 草稿行buffer地址(低5bit)
  57. } __attribute__((packed));
  58. struct xhci_caps_HCSPARAMS3_reg_t
  59. {
  60. uint8_t u1_device_exit_latency; // 0~10ms
  61. uint8_t Reserved;
  62. uint16_t u2_device_exit_latency; // 0~2047ms
  63. } __attribute__((packed));
  64. struct xhci_caps_HCCPARAMS1_reg_t
  65. {
  66. unsigned int ac64 : 1; // 64-bit addressing capability
  67. unsigned int bnc : 1; // bw negotiation capability
  68. unsigned int csz : 1; // context size
  69. unsigned int ppc : 1; // 端口电源控制
  70. unsigned int pind : 1; // port indicators
  71. unsigned int lhrc : 1; // Light HC reset capability
  72. unsigned int ltc : 1; // latency tolerance messaging capability
  73. unsigned int nss : 1; // no secondary SID support
  74. unsigned int pae : 1; // parse all event data
  75. unsigned int spc : 1; // Stopped - Short packet capability
  76. unsigned int sec : 1; // Stopped EDTLA capability
  77. unsigned int cfc : 1; // Continuous Frame ID capability
  78. unsigned int MaxPSASize : 4; // Max Primary Stream Array Size
  79. uint16_t xECP; // xhci extended capabilities pointer
  80. } __attribute__((packed));
  81. struct xhci_caps_HCCPARAMS2_reg_t
  82. {
  83. unsigned u3c : 1; // U3 Entry Capability
  84. unsigned cmc : 1; // ConfigEP command Max exit latency too large
  85. unsigned fsc : 1; // Force Save Context Capability
  86. unsigned ctc : 1; // Compliance Transition Capability
  87. unsigned lec : 1; // large ESIT payload capability
  88. unsigned cic : 1; // configuration information capability
  89. unsigned Reserved : 26;
  90. } __attribute__((packed));
  91. // ======== Capability Register Set END ============
  92. // ======== Operational Register Set BEGIN =========
  93. // xhci operational registers offset
  94. #define XHCI_OPS_USBCMD 0x00 // USB Command
  95. #define XHCI_OPS_USBSTS 0x04 // USB status
  96. #define XHCI_OPS_PAGESIZE 0x08 // Page size
  97. #define XHCI_OPS_DNCTRL 0x14 // Device notification control
  98. #define XHCI_OPS_CRCR 0x18 // Command ring control
  99. #define XHCI_OPS_DCBAAP 0x30 // Device context base address array pointer
  100. #define XHCI_OPS_CONFIG 0x38 // configuire
  101. #define XHCI_OPS_PRS 0x400 // Port register sets
  102. struct xhci_ops_usbcmd_reg_t
  103. {
  104. unsigned rs : 1; // Run/Stop
  105. unsigned hcrst : 1; // host controller reset
  106. unsigned inte : 1; // Interrupt enable
  107. unsigned hsee : 1; // Host system error enable
  108. unsigned rsvd_psvd1 : 3; // Reserved and preserved
  109. unsigned lhcrst : 1; // light host controller reset
  110. unsigned css : 1; // controller save state
  111. unsigned crs : 1; // controller restore state
  112. unsigned ewe : 1; // enable wrap event
  113. unsigned ue3s : 1; // enable U3 MFINDEX Stop
  114. unsigned spe : 1; // stopped short packet enable
  115. unsigned cme : 1; // CEM Enable
  116. unsigned rsvd_psvd2 : 18; // Reserved and preserved
  117. } __attribute__((packed));
  118. struct xhci_ops_usbsts_reg_t
  119. {
  120. unsigned HCHalted : 1;
  121. unsigned rsvd_psvd1 : 1; // Reserved and preserved
  122. unsigned hse : 1; // Host system error
  123. unsigned eint : 1; // event interrupt
  124. unsigned pcd : 1; // Port change detected
  125. unsigned rsvd_zerod : 3; // Reserved and Zero'd
  126. unsigned sss : 1; // Save State Status
  127. unsigned rss : 1; // restore state status
  128. unsigned sre : 1; // save/restore error
  129. unsigned cnr : 1; // controller not ready
  130. unsigned hce : 1; // host controller error
  131. unsigned rsvd_psvd2 : 19; // Reserved and Preserved
  132. } __attribute__((packed));
  133. struct xhci_ops_pagesize_reg_t
  134. {
  135. uint16_t page_size; // The actual pagesize is ((this field)<<12)
  136. uint16_t reserved;
  137. } __attribute__((packed));
  138. struct xhci_ops_dnctrl_reg_t
  139. {
  140. uint16_t value;
  141. uint16_t reserved;
  142. } __attribute__((packed));
  143. struct xhci_ops_config_reg_t
  144. {
  145. uint8_t MaxSlotsEn; // Max slots enabled
  146. unsigned u3e : 1; // U3 Entry Enable
  147. unsigned cie : 1; // Configuration information enable
  148. unsigned rsvd_psvd : 22; // Reserved and Preserved
  149. } __attribute__((packed));
  150. // ======== Operational Register Set END =========
  151. // ========= TRB begin ===========
  152. // TRB的Transfer Type可用值定义
  153. #define XHCI_TRB_TRT_NO_DATA 0
  154. #define XHCI_TRB_TRT_RESERVED 1
  155. #define XHCI_TRB_TRT_OUT_DATA 2
  156. #define XHCI_TRB_TRT_IN_DATA 3
  157. #define XHCI_CMND_RING_TRBS 128 // TRB num of command ring, not more than 4096
  158. #define XHCI_TRBS_PER_RING 256
  159. #define XHCI_TRB_CYCLE_OFF 0
  160. #define XHCI_TRB_CYCLE_ON 1
  161. /**
  162. * @brief xhci通用TRB结构
  163. *
  164. */
  165. struct xhci_TRB_t
  166. {
  167. uint64_t param; // 参数
  168. uint32_t status;
  169. uint32_t command;
  170. } __attribute__((packed));
  171. struct xhci_TRB_normal_t
  172. {
  173. uint64_t buf_paddr; // 数据缓冲区物理地址
  174. unsigned transfer_length : 17; // 传输数据长度
  175. unsigned TD_size : 5; // 传输描述符中剩余的数据包的数量
  176. unsigned intr_target : 10; // 中断目标 [0:MaxIntrs-1]
  177. unsigned cycle : 1; // used to mark the enqueue pointer of transfer ring
  178. unsigned ent : 1; // evaluate next TRB before updating the endpoint's state
  179. unsigned isp : 1; // Interrupt on short packet bit
  180. unsigned ns : 1; // No snoop
  181. unsigned chain : 1; // The chain bit is used to tell the controller that this
  182. // TRB is associated with the next TRB in the TD
  183. unsigned ioc : 1; // 完成时发起中断
  184. unsigned idt : 1; // Immediate Data
  185. unsigned resv : 2; // Reserved and zero'd
  186. unsigned bei : 1; // Block event interrupt
  187. unsigned TRB_type : 6; // TRB类型
  188. uint16_t Reserved; // 保留且置为0
  189. } __attribute__((packed));
  190. struct xhci_TRB_setup_state_t
  191. {
  192. uint8_t bmRequestType;
  193. uint8_t bRequest;
  194. uint16_t wValue;
  195. uint16_t wIndex;
  196. uint16_t wLength;
  197. unsigned transfer_legth : 17;
  198. unsigned resv1 : 5; // Reserved and zero'd
  199. unsigned intr_target : 10;
  200. unsigned cycle : 1;
  201. unsigned resv2 : 4; // Reserved and zero'd
  202. unsigned ioc : 1;
  203. unsigned idt : 1;
  204. unsigned resv3 : 3; // Reserved and zero'd
  205. unsigned TRB_type : 6;
  206. unsigned trt : 2; // Transfer type
  207. unsigned resv4 : 14; // Reserved and zero'd
  208. } __attribute__((packed));
  209. struct xhci_TRB_data_stage_t
  210. {
  211. uint64_t buf_paddr; // 数据缓冲区物理地址
  212. unsigned transfer_length : 17; // 传输数据长度
  213. unsigned TD_size : 5; // 传输描述符中剩余的数据包的数量
  214. unsigned intr_target : 10; // 中断目标 [0:MaxIntrs-1]
  215. unsigned cycle : 1; // used to mark the enqueue pointer of transfer ring
  216. unsigned ent : 1; // evaluate next TRB before updating the endpoint's state
  217. unsigned isp : 1; // Interrupt on short packet bit
  218. unsigned ns : 1; // No snoop
  219. unsigned chain : 1; // The chain bit is used to tell the controller that this
  220. // TRB is associated with the next TRB in the TD
  221. unsigned ioc : 1; // 完成时发起中断
  222. unsigned idt : 1; // Immediate Data
  223. unsigned resv : 3; // Reserved and zero'd
  224. unsigned TRB_type : 6; // TRB类型
  225. unsigned dir : 1; // 0 -> out packet
  226. // 1 -> in packet
  227. unsigned Reserved : 15; // 保留且置为0
  228. } __attribute__((packed));
  229. struct xhci_TRB_status_stage_t
  230. {
  231. uint64_t resv1; // Reserved and zero'd
  232. unsigned resv2 : 22; // Reserved and zero'd
  233. unsigned intr_target : 10; // 中断目标 [0:MaxIntrs-1]
  234. unsigned cycle : 1; // used to mark the enqueue pointer of transfer ring
  235. unsigned ent : 1; // evaluate next TRB before updating the endpoint's state
  236. unsigned resv3 : 2; // Reserved and zero'd
  237. unsigned chain : 1; // The chain bit is used to tell the controller that this
  238. // TRB is associated with the next TRB in the TD
  239. unsigned ioc : 1; // 完成时发起中断
  240. unsigned resv4 : 4; // Reserved and zero'd
  241. unsigned TRB_type : 6; // TRB类型
  242. unsigned dir : 1; // 0 -> out packet
  243. // 1 -> in packet
  244. unsigned Reserved : 15; // 保留且置为0
  245. } __attribute__((packed));
  246. struct xhci_TRB_cmd_complete_t
  247. {
  248. uint64_t cmd_trb_pointer_paddr; // 指向生成当前Event TRB的TRB的物理地址(16bytes对齐)
  249. unsigned resv1 : 24; // Reserved and zero'd
  250. uint8_t code; // Completion code
  251. unsigned cycle : 1; // cycle bit
  252. unsigned resv2 : 9; // Reserved and zero'd
  253. unsigned TRB_type : 6; // TRB类型
  254. uint8_t VF_ID;
  255. uint8_t slot_id; // the id of the slot associated with the
  256. // command that generated the event
  257. } __attribute__((packed));
  258. // ========= TRB end ===========
  259. // ======== Runtime Register Set Begin =========
  260. #define XHCI_RT_IR0 0x20 // 中断寄存器组0距离runtime Register set起始位置的偏移量
  261. #define XHCI_IR_SIZE 32 // 中断寄存器组大小
  262. // 中断寄存器组内的偏移量
  263. #define XHCI_IR_MAN 0x00 // Interrupter Management Register
  264. #define XHCI_IR_MOD 0x04 // Interrupter Moderation
  265. #define XHCI_IR_TABLE_SIZE 0x08 // Event Ring Segment Table size (count of segments)
  266. #define XHCI_IR_TABLE_ADDR 0x10 // Event Ring Segment Table Base Address
  267. #define XHCI_IR_DEQUEUE 0x18 // Event Ring Dequeue Pointer
  268. // MAN寄存器内的bit的含义
  269. #define XHCI_IR_IMR_PENDING (1 << 0) // Interrupt pending bit in Management Register
  270. #define XHCI_IR_IMR_ENABLE (1 << 1) // Interrupt enable bit in Management Register
  271. struct xhci_intr_moderation_t
  272. {
  273. uint16_t interval; // 产生一个中断的时间,是interval*250ns (wait before next interrupt)
  274. uint16_t counter;
  275. } __attribute__((packed));
  276. // ======== Runtime Register Set END =========
  277. // ======= xhci Extended Capabilities List ========
  278. // ID 部分的含义定义
  279. #define XHCI_XECP_ID_RESERVED 0
  280. #define XHCI_XECP_ID_LEGACY 1 // USB Legacy Support
  281. #define XHCI_XECP_ID_PROTOCOL 2 // Supported protocol
  282. #define XHCI_XECP_ID_POWER 3 // Extended power management
  283. #define XHCI_XECP_ID_IOVIRT 4 // I/0 virtualization
  284. #define XHCI_XECP_ID_MSG 5 // Message interrupt
  285. #define XHCI_XECP_ID_LOCAL_MEM 6 // local memory
  286. #define XHCI_XECP_ID_DEBUG 10 // USB Debug capability
  287. #define XHCI_XECP_ID_EXTMSG 17 // Extended message interrupt
  288. #define XHCI_XECP_LEGACY_TIMEOUT 10 // 设置legacy状态的等待时间
  289. #define XHCI_XECP_LEGACY_BIOS_OWNED (1 << 16) // 当bios控制着该hc时,该位被置位
  290. #define XHCI_XECP_LEGACY_OS_OWNED (1 << 24) // 当系统控制着该hc时,该位被置位
  291. #define XHCI_XECP_LEGACY_OWNING_MASK (XHCI_XECP_LEGACY_BIOS_OWNED | XHCI_XECP_LEGACY_OS_OWNED)
  292. // 端口信息标志位
  293. #define XHCI_PROTOCOL_USB2 0
  294. #define XHCI_PROTOCOL_USB3 1
  295. #define XHCI_PROTOCOL_INFO (1 << 0) // 1->usb3, 0->usb2
  296. #define XHCI_PROTOCOL_HSO (1 << 1) // 1-> usb2 high speed only
  297. #define XHCI_PROTOCOL_HAS_PAIR (1 << 2) // 当前位被置位,意味着当前端口具有一个与之配对的端口
  298. #define XHCI_PROTOCOL_ACTIVE (1 << 3) // 当前端口是这个配对中,被激活的端口
  299. /**
  300. * @brief xhci端口信息
  301. *
  302. */
  303. struct xhci_port_info_t
  304. {
  305. uint8_t flags; // port flags
  306. uint8_t paired_port_num; // 与当前端口所配对的另一个端口(相同物理接口的不同速度的port)
  307. uint8_t offset; // offset of this port within this protocal
  308. uint8_t reserved;
  309. } __attribute__((packed));
  310. struct xhci_host_controller_t
  311. {
  312. struct pci_device_structure_general_device_t *pci_dev_hdr; // 指向pci header结构体的指针
  313. int controller_id; // 操作系统给controller的编号
  314. uint64_t vbase; // 虚拟地址base(bar0映射到的虚拟地址)
  315. uint64_t vbase_op; // Operational registers 起始虚拟地址
  316. uint32_t rts_offset; // Runtime Register Space offset
  317. uint32_t db_offset; // Doorbell offset
  318. uint32_t ext_caps_off; // 扩展能力寄存器偏移量
  319. uint8_t context_size; // 上下文大小
  320. uint16_t port_num; // 总的端口数量
  321. uint8_t port_num_u2; // usb 2.0端口数量
  322. uint8_t port_num_u3; // usb 3端口数量
  323. uint8_t page_size; // page size
  324. uint64_t dcbaap_vaddr; // Device Context Base Address Array Pointer的虚拟地址
  325. uint64_t cmd_ring_vaddr; // command ring的虚拟地址
  326. uint64_t event_ring_vaddr; // event ring的虚拟地址
  327. uint64_t event_ring_table_vaddr; // event ring table的虚拟地址
  328. uint8_t cmd_trb_cycle; // 当前command ring cycle
  329. uint8_t current_event_ring_cycle; // 当前event ring cycle
  330. struct xhci_port_info_t ports[XHCI_MAX_ROOT_HUB_PORTS]; // 指向端口信息数组的指针(由于端口offset是从1开始的,因此该数组第0项为空)
  331. };
  332. /**
  333. * @brief 初始化xhci控制器
  334. *
  335. * @param header 指定控制器的pci device头部
  336. */
  337. void xhci_init(struct pci_device_structure_general_device_t *header);