xhci.h 13 KB

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