xhci.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  66. * @brief xhci端口信息
  67. *
  68. */
  69. struct xhci_port_info_t
  70. {
  71. uint8_t flags; // port flags
  72. uint8_t paired_port_num; // 与当前端口所配对的另一个端口(相同物理接口的不同速度的port)
  73. uint8_t offset; // offset of this port within this protocal
  74. uint8_t reserved;
  75. } __attribute__((packed));
  76. struct xhci_host_controller_t
  77. {
  78. struct pci_device_structure_general_device_t *pci_dev_hdr; // 指向pci header结构体的指针
  79. int controller_id; // 操作系统给controller的编号
  80. uint64_t vbase; // 虚拟地址base(bar0映射到的虚拟地址)
  81. struct xhci_port_info_t *ports; // 指向端口信息数组的指针
  82. };
  83. /**
  84. * @brief 初始化xhci控制器
  85. *
  86. * @param header 指定控制器的pci device头部
  87. */
  88. void xhci_init(struct pci_device_structure_general_device_t *header);