xhci.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #include <driver/usb/usb.h>
  3. #include <driver/pci/pci.h>
  4. #define MAX_XHCI_HOST_CONTROLLERS 8
  5. // xhci Capability Registers offset
  6. #define XHCI_CAPS_CAPLENGTH 0x00 // Cap 寄存器组的长度
  7. #define XHCI_CAPS_RESERVED 0x01
  8. #define XHCI_CAPS_HCIVERSION 0x02 // 接口版本号
  9. #define XHCI_CAPS_HCSPARAMS1 0x04
  10. #define XHCI_CAPS_HCSPARAMS2 0x08
  11. #define XHCI_CAPS_HCSPARAMS3 0x0c
  12. #define XHCI_CAPS_HCCPARAMS1 0x10 // capability params 1
  13. #define XHCI_CAPS_DBOFF 0x14 // Doorbell offset
  14. #define XHCI_CAPS_RTSOFF 0x18 // Runtime register space offset
  15. #define XHCI_CAPS_HCCPARAMS2 0x1c // capability params 2
  16. struct xhci_caps_HCSPARAMS1_reg_t
  17. {
  18. unsigned max_slots : 8; // 最大插槽数
  19. unsigned max_intrs : 11; // 最大中断数
  20. unsigned reserved : 5;
  21. unsigned max_ports : 8; // 最大端口数
  22. } __attribute__((packed));
  23. struct xhci_caps_HCSPARAMS2_reg_t
  24. {
  25. unsigned ist : 4; // 同步调度阈值
  26. unsigned ERST_Max : 4; // Event Ring Segment Table Max
  27. unsigned Reserved : 13;
  28. unsigned max_scratchpad_buf_HI5 : 5; // 草稿行buffer地址(高5bit)
  29. unsigned spr : 1; // scratchpad restore
  30. unsigned max_scratchpad_buf_LO5 : 5; // 草稿行buffer地址(低5bit)
  31. } __attribute__((packed));
  32. struct xhci_caps_HCSPARAMS3_reg_t
  33. {
  34. uint8_t u1_device_exit_latency; // 0~10ms
  35. uint8_t Reserved;
  36. uint16_t u2_device_exit_latency; // 0~2047ms
  37. } __attribute__((packed));
  38. struct xhci_caps_HCCPARAMS1_reg_t
  39. {
  40. unsigned ac64 : 1; // 64-bit addressing capability
  41. unsigned bnc : 1; // bw negotiation capability
  42. unsigned csz : 1; // context size
  43. unsigned ppc : 1; // 端口电源控制
  44. unsigned pind : 1; // port indicators
  45. unsigned lhrc : 1; // Light HC reset capability
  46. unsigned ltc : 1; // latency tolerance messaging capability
  47. unsigned nss : 1; // no secondary SID support
  48. unsigned pae : 1; // parse all event data
  49. unsigned spc : 1; // Stopped - Short packet capability
  50. unsigned sec : 1; // Stopped EDTLA capability
  51. unsigned cfc : 1; // Continuous Frame ID capability
  52. unsigned MaxPSASize : 4; // Max Primary Stream Array Size
  53. uint16_t xECP; // xhci extended capabilities pointer
  54. } __attribute__((packed));
  55. struct xhci_caps_HCCPARAMS2_reg_t
  56. {
  57. unsigned u3c : 1; // U3 Entry Capability
  58. unsigned cmc : 1; // ConfigEP command Max exit latency too large
  59. unsigned fsc : 1; // Force Save Context Capability
  60. unsigned ctc : 1; // Compliance Transition Capability
  61. unsigned lec : 1; // large ESIT payload capability
  62. unsigned cic : 1; // configuration information capability
  63. unsigned Reserved : 26;
  64. } __attribute__((packed));
  65. // xhci operational registers offset
  66. #define XHCI_OPS_USBCMD 0x00 // USB Command
  67. #define XHCI_OPS_USBSTS 0x04 // USB status
  68. #define XHCI_OPS_PAGESIZE 0x08 // Page size
  69. #define XHCI_OPS_DNCTRL 0x14 // Device notification control
  70. #define XHCI_OPS_CRCR 0x18 // Command ring control
  71. #define XHCI_OPS_DCBAAP 0x30 // Device context base address array pointer
  72. #define XHCI_OPS_CONFIG 0x38 // configuire
  73. #define XHCI_OPS_PRS 0x400 // Port register sets
  74. struct xhci_ops_usbcmd_reg_t
  75. {
  76. unsigned rs : 1; // Run/Stop
  77. unsigned hcrst : 1; // host controller reset
  78. unsigned inte : 1; // Interrupt enable
  79. unsigned hsee : 1; // Host system error enable
  80. unsigned rsvd_psvd1 : 3; // Reserved and preserved
  81. unsigned lhcrst : 1; // light host controller reset
  82. unsigned css : 1; // controller save state
  83. unsigned crs : 1; // controller restore state
  84. unsigned ewe : 1; // enable wrap event
  85. unsigned ue3s : 1; // enable U3 MFINDEX Stop
  86. unsigned spe : 1; // stopped short packet enable
  87. unsigned cme : 1; // CEM Enable
  88. unsigned rsvd_psvd2 : 18; // Reserved and preserved
  89. } __attribute__((packed));
  90. struct xhci_ops_usbsts_reg_t
  91. {
  92. unsigned HCHalted : 1;
  93. unsigned rsvd_psvd1 : 1; // Reserved and preserved
  94. unsigned hse : 1; // Host system error
  95. unsigned eint : 1; // event interrupt
  96. unsigned pcd : 1; // Port change detected
  97. unsigned rsvd_zerod : 3; // Reserved and Zero'd
  98. unsigned sss : 1; // Save State Status
  99. unsigned rss : 1; // restore state status
  100. unsigned sre : 1; // save/restore error
  101. unsigned cnr : 1; // controller not ready
  102. unsigned hce : 1; // host controller error
  103. unsigned rsvd_psvd2 : 19; // Reserved and Preserved
  104. } __attribute__((packed));
  105. struct xhci_ops_pagesize_reg_t
  106. {
  107. uint16_t page_size; // The actual pagesize is ((this field)<<12)
  108. uint16_t reserved;
  109. } __attribute__((packed));
  110. struct xhci_ops_dnctrl_reg_t
  111. {
  112. uint16_t value;
  113. uint16_t reserved;
  114. } __attribute__((packed));
  115. struct xhci_ops_config_reg_t
  116. {
  117. uint8_t MaxSlotsEn; // Max slots enabled
  118. unsigned u3e : 1; // U3 Entry Enable
  119. unsigned cie : 1; // Configuration information enable
  120. unsigned rsvd_psvd : 22; // Reserved and Preserved
  121. } __attribute__((packed));
  122. /**
  123. * @brief xhci端口信息
  124. *
  125. */
  126. struct xhci_port_info_t
  127. {
  128. uint8_t flags; // port flags
  129. uint8_t paired_port_num; // 与当前端口所配对的另一个端口(相同物理接口的不同速度的port)
  130. uint8_t offset; // offset of this port within this protocal
  131. uint8_t reserved;
  132. } __attribute__((packed));
  133. struct xhci_host_controller_t
  134. {
  135. struct pci_device_structure_general_device_t *pci_dev_hdr; // 指向pci header结构体的指针
  136. int controller_id; // 操作系统给controller的编号
  137. uint64_t vbase; // 虚拟地址base(bar0映射到的虚拟地址)
  138. uint64_t vbase_op; // Operational registers 起始虚拟地址
  139. struct xhci_port_info_t *ports; // 指向端口信息数组的指针
  140. };
  141. /**
  142. * @brief 初始化xhci控制器
  143. *
  144. * @param header 指定控制器的pci device头部
  145. */
  146. void xhci_init(struct pci_device_structure_general_device_t *header);