xhci.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "xhci.h"
  2. #include <common/kprint.h>
  3. #include <debug/bug.h>
  4. #include <process/spinlock.h>
  5. #include <mm/mm.h>
  6. #include <debug/traceback/traceback.h>
  7. spinlock_t xhci_controller_init_lock; // xhci控制器初始化锁(在usb_init中被初始化)
  8. static int xhci_ctrl_count = 0; // xhci控制器计数
  9. static struct xhci_host_controller_t xhci_hc[MAX_XHCI_HOST_CONTROLLERS] = {0};
  10. /**
  11. * @brief 初始化xhci控制器
  12. *
  13. * @param header 指定控制器的pci device头部
  14. */
  15. void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
  16. {
  17. spin_lock(&xhci_controller_init_lock);
  18. kinfo("Initializing xhci host controller: bus=%#02x, device=%#02x, func=%#02x, VendorID=%#04x, irq_line=%d, irq_pin=%d", dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, dev_hdr->header.Vendor_ID, dev_hdr->Interrupt_Line, dev_hdr->Interrupt_PIN);
  19. xhci_hc[xhci_ctrl_count].controller_id = xhci_ctrl_count;
  20. xhci_hc[xhci_ctrl_count].pci_dev_hdr = dev_hdr;
  21. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0x4, 0x0006); // mem I/O access enable and bus master enable
  22. // 为当前控制器映射寄存器地址空间
  23. xhci_hc[xhci_ctrl_count].vbase = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + XHCI_MAPPING_OFFSET + PAGE_2M_SIZE * xhci_hc[xhci_ctrl_count].controller_id;
  24. kdebug("dev_hdr->BAR0 & (~0xf)=%#018lx", dev_hdr->BAR0 & (~0xf));
  25. mm_map_phys_addr(xhci_hc[xhci_ctrl_count].vbase, dev_hdr->BAR0 & (~0xf), 65536, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, true);
  26. // 读取xhci控制寄存器
  27. uint16_t iversion = *(uint16_t *)(xhci_hc[xhci_ctrl_count].vbase + XHCI_CAPS_HCIVERSION);
  28. uint32_t dboff = *(uint16_t *)(xhci_hc[xhci_ctrl_count].vbase + XHCI_CAPS_DBOFF);
  29. // kdebug("dboff=%ld", dboff);
  30. // struct xhci_caps_HCSPARAMS1_reg_t hcs1 = *(struct xhci_caps_HCSPARAMS1_reg_t *)(xhci_hc[xhci_ctrl_count].vbase + XHCI_CAPS_HCSPARAMS1);
  31. // kdebug("hcs1.max_ports=%d, hcs1.max_slots=%d, hcs1.max_intrs=%d", hcs1.max_ports, hcs1.max_slots, hcs1.max_intrs);
  32. // kdebug("caps size=%d", *(uint8_t *)xhci_hc[xhci_ctrl_count].vbase);
  33. // kdebug("iversion=%#06x", iversion);
  34. if (iversion < 0x95)
  35. {
  36. kwarn("Unsupported/Unknowned xHCI controller version: %#06x. This may cause unexpected behavior.", iversion);
  37. }
  38. ++xhci_ctrl_count;
  39. spin_unlock(&xhci_controller_init_lock);
  40. return;
  41. failed:;
  42. // 取消地址映射
  43. mm_unmap(xhci_hc[xhci_ctrl_count].vbase, 65536);
  44. // 清空数组
  45. memset((void *)&xhci_hc[xhci_ctrl_count], 0, sizeof(struct xhci_host_controller_t));
  46. spin_unlock(&xhci_controller_init_lock);
  47. }