From 5cdeb8739333d8b02589447fa0e1772e64ffa05d Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sun, 22 Mar 2026 18:39:53 +0100 Subject: [PATCH] XHCI perform init sequence --- kernel/device/xhci.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/kernel/device/xhci.c b/kernel/device/xhci.c index 101fc27..197656d 100644 --- a/kernel/device/xhci.c +++ b/kernel/device/xhci.c @@ -9,6 +9,11 @@ #include #include #include +#include + +/* REF: + * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf + */ /* clang-format off */ @@ -97,6 +102,37 @@ DEFINE_DEVICE_INIT (xhci_init) { uint32_t dboff = xhci_read32 (xhci->xhci_mmio_base, XHCI_DBOFF); xhci->xhci_doorbell_base = xhci->xhci_mmio_base + dboff; + DEBUG ("starting init sequence\n"); + + /* assert CNR is 0 */ + while (xhci_read32 (xhci->xhci_oper_base, XHCI_USBSTS) & (1 << 11)) + spin_lock_relax (); + + /* STOP */ + uint32_t usbcmd = xhci_read32 (xhci->xhci_oper_base, XHCI_USBCMD); + xhci_write32 (xhci->xhci_oper_base, XHCI_USBCMD, usbcmd & ~0x01); + + /* wait for HCH bit */ + int timeout = 100000; + while (!(xhci_read32 (xhci->xhci_oper_base, XHCI_USBSTS) & (1 << 12))) { + if (--timeout == 0) + break; + + spin_lock_relax (); + } + + /* RESET */ + xhci_write32 (xhci->xhci_oper_base, XHCI_USBCMD, (1 << 1)); + + while (xhci_read32 (xhci->xhci_oper_base, XHCI_USBCMD) & (1 << 1)) + spin_lock_relax (); + + /* Stall while controller not ready */ + while (xhci_read32 (xhci->xhci_oper_base, XHCI_USBSTS) & (1 << 11)) + spin_lock_relax (); + + DEBUG ("XHCI init done\n"); + return true; }