Add USB Host (XHCI) driver and MSC support for Tegra X1 - #2
Open
nafields wants to merge 2 commits into
Open
Conversation
Implements USB SSD as a third emuMMC storage backend alongside the
existing SD-partition and file-based paths. When usb_enabled=1 and
usb_sector=<lba> are set in the emummc INI, Atmosphere boots from
the USB drive rather than an SD card partition.
New files:
bdk/usb/usbh.c — XHCI host controller driver (pure MMIO,
no firmware blob); host clocks, PHY init
(mirrored from xusbd.c), port reset, ENABLE_SLOT,
ADDRESS_DEVICE, CONFIGURE_EP, control/bulk xfers.
Ring wrap follows XHCI §4.9.3: link TRB written
with current PCS before PCS is toggled.
bdk/usb/usbh.h — XHCI TRB constants, ring/context structs, public API.
bdk/usb/usbh_msc.c — BOT host driver: CBW→data→CSW, SCSI subset
(TEST_UNIT_READY, INQUIRY, READ_CAPACITY(10),
READ(10)), sector-chunked reads.
bdk/usb/usbh_msc.h — usbh_msc_t struct and declarations.
bdk/usb/usbh_hub.c — Hub class driver (powers ports, resets downstream
port); full multi-slot re-enumeration is TODO —
returns USB_ERROR_INIT with fallback message.
bootloader/storage/usb_blkdev.c — thin block-device wrapper over usbh_msc.
bootloader/storage/usb_blkdev.h — usb_blkdev_t and declarations.
Modified files:
bdk/memory_map.h — USBH_BULK_IN/OUT_BUF_ADDR + USBH_BULK_BUF_SZ.
bdk/usb/usb_t210.h — XUSB_HOST_CONFIGURATION / HOST_CONFIGURATION_EN_FPCI.
bootloader/storage/emummc.h — usb_enabled + usb_sector fields in emummc_cfg_t.
bootloader/storage/emummc.c — INI parsing + routing branches for USB path.
bootloader/main.c — early emummc_load_cfg() + conditional usbh_init().
https://claude.ai/code/session_01BAVie7bqN6o7NEcR5GSrr6
Verification pass against the XHCI spec, T210 TRM and Linux xhci-tegra found one missing hard requirement and several fatal bugs: Missing requirement: - The XUSB host complex is driven by an integrated Falcon microcontroller; the xHCI interface does not function until its firmware is loaded and booted (unlike the XUSB device block, which is pure hardware). Add the CSB/L2IMEM firmware loader (mirrors Linux xhci-tegra.c), reading the standard tegra21x XUSB firmware from sd:/bootloader/sys/xusbfw.bin into a resident DRAM buffer. Also program the XUSB_FALCON clock (204MHz). Fatal fixes: - CLK_SOURCE_XUSB_CORE_HOST is 0x600, not 0x614 (TRM CAR layout around the existing 0x608/0x60C/0x610 defines). - Runtime base had interrupter 0 offset applied twice (rt_base += 0x20 on top of 0x20-based register macros), so ERSTBA/ERDP landed on interrupter 1 and the event ring was never armed. - Input context EP entries were indexed by DCI instead of DCI-1, so ADDRESS_DEVICE read a zeroed EP0 context. - VBUS_ID write OR-ed in ID_OVR GND (value 0) without clearing the OVR field, leaving ID floating and the controller out of host role. - IOC was set on Setup/Data/Status TRBs but only 1-2 events were consumed, desynchronizing the event ring after the first control transfer. IOC is now only on the final TRB (one event per transfer). - Ring wrap mid-control-transfer overwrote the Link TRB (no per-TRB wrap check); replaced with a _trb_alloc helper that handles wrap per enqueue. - Unsolicited Port Status Change Events were consumed as command/transfer completions; they are now filtered out. - 1MB bulk chunks overflowed the 17-bit TRB transfer length field; chunk at 64KB. - emummc_storage_read() USB branch returned 0-on-success into a 1-on-success convention (every successful read reported failure). - emummc_storage_write() had no USB branch and could fall through to the SD-offset path; added a write path (WRITE10 through usbh_msc_write). Robustness/compat: - Parse the configuration descriptor for bConfigurationValue and the real bulk endpoint addresses/max-packet (devices are not all EP1/EP1); use dynamic DCIs for doorbells and endpoint contexts. - Allocate XHCI scratchpad buffers per HCSPARAMS2 (firmware requires them). - Wait for USBSTS.CNR before and after HC reset. - Preserve-mask PED and W1C bits on PORTSC writes (writing PED=1 disables the port); acknowledge CSC/PRC after port reset and check PED. - Source 5V onto VBUS via regulator_5v_usb_src_enable() for the handheld/OTG path (Icosa); dock supplies downstream power itself. - EP0 max packet 64 for FS devices (8 only for LS). - usbh_msc.c was missing the usbd.h include for USB_RES_*/USB_ERROR_*. https://claude.ai/code/session_01BAVie7bqN6o7NEcR5GSrr6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a complete USB Host (XHCI) driver implementation for Tegra X1, enabling the bootloader to read from USB Mass Storage Class (MSC) devices connected via the dock's USB port.
Summary
Implements a minimal but functional XHCI host controller driver that supports device enumeration, control transfers, and bulk I/O operations. Includes a USB MSC (Mass Storage Class) driver using the BOT (Bulk-Only Transport) protocol and a hub class driver stub for future multi-device support. Integrates USB storage as an alternative emuMMC backend alongside existing SD card and file-based options.
Key Changes
Core XHCI Host Driver (
bdk/usb/usbh.c)USB MSC Driver (
bdk/usb/usbh_msc.c)Hub Class Driver (
bdk/usb/usbh_hub.c)Block Device Abstraction (
bootloader/storage/usb_blkdev.c/h)Integration
emummc.cto support USB as an emuMMC backend with configurable LBA offsetmain.cto initialize USB host on boot when configuredmemory_map.husb_t210.hwith XUSB host register definitionsemummc.hto include USB configuration fieldsImplementation Details
https://claude.ai/code/session_01BAVie7bqN6o7NEcR5GSrr6