xhci.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #include <driver/usb/usb.h>
  3. #include <driver/pci/pci.h>
  4. // xhci Capability Registers offset
  5. #define XHCI_CAPS_CAPLENGTH 0x00 // Cap 寄存器组的长度
  6. #define XHCI_CAPS_RESERVED 0x01
  7. #define XHCI_CAPS_HCIVERSION 0x02 // 接口版本号
  8. #define XHCI_CAPS_HCSPARAMS1 0x04
  9. #define XHCI_CAPS_HCSPARAMS2 0x08
  10. #define XHCI_CAPS_HCSPARAMS3 0x0c
  11. #define XHCI_CAPS_HCCPARAMS1 0x10 // capability params 1
  12. #define XHCI_CAPS_DBOFF 0x14 // Doorbell offset
  13. #define XHCI_CAPS_RTSOFF 0x18 // Runtime register space offset
  14. #define XHCI_CAPS_HCCPARAMS2 0x1c // capability params 2
  15. struct xhci_caps_HCSPARAMS1_reg_t
  16. {
  17. unsigned max_slots : 8; // 最大插槽数
  18. unsigned max_intrs : 11; // 最大中断数
  19. unsigned reserved : 5;
  20. unsigned max_ports : 8; // 最大端口数
  21. } __attribute__((packed));
  22. struct xhci_caps_HCSPARAMS2_reg_t
  23. {
  24. unsigned ist : 4; // 同步调度阈值
  25. unsigned ERST_Max : 4; // Event Ring Segment Table Max
  26. unsigned Reserved : 13;
  27. unsigned max_scratchpad_buf_HI5 : 5; // 草稿行buffer地址(高5bit)
  28. unsigned spr : 1; // scratchpad restore
  29. unsigned max_scratchpad_buf_LO5 : 5; // 草稿行buffer地址(低5bit)
  30. } __attribute__((packed));
  31. struct xhci_caps_HCSPARAMS3_reg_t
  32. {
  33. uint8_t u1_device_exit_latency; // 0~10ms
  34. uint8_t Reserved;
  35. uint16_t u2_device_exit_latency; // 0~2047ms
  36. } __attribute__((packed));
  37. struct xhci_caps_HCCPARAMS1_reg_t
  38. {
  39. unsigned ac64 : 1; // 64-bit addressing capability
  40. unsigned bnc : 1; // bw negotiation capability
  41. unsigned csz : 1; // context size
  42. unsigned ppc : 1; // 端口电源控制
  43. unsigned pind : 1; // port indicators
  44. unsigned lhrc : 1; // Light HC reset capability
  45. unsigned ltc : 1; // latency tolerance messaging capability
  46. unsigned nss : 1; // no secondary SID support
  47. unsigned pae : 1; // parse all event data
  48. unsigned spc : 1; // Stopped - Short packet capability
  49. unsigned sec : 1; // Stopped EDTLA capability
  50. unsigned cfc : 1; // Continuous Frame ID capability
  51. unsigned MaxPSASize : 4; // Max Primary Stream Array Size
  52. uint16_t xECP; // xhci extended capabilities pointer
  53. } __attribute__((packed));
  54. struct xhci_caps_HCCPARAMS2_reg_t
  55. {
  56. unsigned u3c : 1; // U3 Entry Capability
  57. unsigned cmc : 1; // ConfigEP command Max exit latency too large
  58. unsigned fsc : 1; // Force Save Context Capability
  59. unsigned ctc : 1; // Compliance Transition Capability
  60. unsigned lec : 1; // large ESIT payload capability
  61. unsigned cic : 1; // configuration information capability
  62. unsigned Reserved : 26;
  63. } __attribute__((packed));
  64. /**
  65. * @brief xhci端口信息
  66. *
  67. */
  68. struct xhci_port_info_t
  69. {
  70. uint8_t flags; // port flags
  71. uint8_t paired_port_num; // 与当前端口所配对的另一个端口(相同物理接口的不同速度的port)
  72. uint8_t offset; // offset of this port within this protocal
  73. uint8_t reserved;
  74. } __attribute__((packed));
  75. struct xhci_controller_t
  76. {
  77. struct pci_device_structure_general_device_t *pci_dev_hdr; // 指向pci header结构体的指针
  78. int controller_id; // 操作系统给controller的编号
  79. int vbase; // 虚拟地址base(bar0映射到的虚拟地址)
  80. struct xhci_port_info_t *ports; // 指向端口信息数组的指针
  81. };
  82. /**
  83. * @brief 初始化xhci控制器
  84. *
  85. * @param header 指定控制器的pci device头部
  86. */
  87. void xhci_init(struct pci_device_structure_general_device_t *header);