From 7988310a579872859ab7b87a01c2deaeb4e503e5 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:45:52 +1100 Subject: [PATCH 01/29] rtkit: wait for a stopped coprocessor's CPU_STATUS.STOPPED, not a fixed 1 ms rtkit_stop() clears RUN on an IOP that never acknowledged a power transition, waited a fixed millisecond for messages still in flight, and drained the mailbox. The ASC reports when its CPU has halted in CPU_STATUS.STOPPED (bit 1, the bit aura already reads for an idle DCP), and nothing the CPU sends can follow that. Poll that bit instead, with the same 1 ms bound named RTKIT_STOP_TIMEOUT_US, and say so with the status word when the CPU does not halt in time. The caller's reset follows either way, as before. Signed-off-by: Ryan Murray --- src/asc.c | 6 ++++++ src/asc.h | 1 + src/rtkit.c | 18 +++++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/asc.c b/src/asc.c index 707b81826..357c77132 100644 --- a/src/asc.c +++ b/src/asc.c @@ -158,6 +158,12 @@ bool asc_cpu_running(asc_dev_t *asc) return read32(asc->cpu_base + ASC_CPU_CONTROL) & ASC_CPU_CONTROL_START; } +/* The coprocessor's CPU has halted (in its idle loop, or after asc_cpu_stop()). */ +bool asc_cpu_stopped(asc_dev_t *asc) +{ + return read32(asc->cpu_base + ASC_CPU_STATUS) & ASC_CPU_STATUS_STOPPED; +} + /* The mailbox's receive side (I2A) on or off, as macOS sets it around an IOP's power loss. */ void asc_i2a_enable(asc_dev_t *asc, bool on) { diff --git a/src/asc.h b/src/asc.h index 1075053ed..4187055bc 100644 --- a/src/asc.h +++ b/src/asc.h @@ -21,6 +21,7 @@ bool asc_is_v8(asc_dev_t *asc); void asc_cpu_start(asc_dev_t *asc); void asc_cpu_stop(asc_dev_t *asc); bool asc_cpu_running(asc_dev_t *asc); +bool asc_cpu_stopped(asc_dev_t *asc); bool asc_cpu_idle(asc_dev_t *asc); void asc_i2a_enable(asc_dev_t *asc, bool on); u32 asc_cpu_status(asc_dev_t *asc); diff --git a/src/rtkit.c b/src/rtkit.c index cf46aecb3..52bc0c640 100644 --- a/src/rtkit.c +++ b/src/rtkit.c @@ -1075,17 +1075,29 @@ static void rtkit_drain(rtkit_dev_t *rtk) rtkit_printf("dropping late message 0x%02x: %lx\n", msg.msg1, msg.msg0); } +/* How long a coprocessor CPU may take to halt once RUN is cleared. */ +#define RTKIT_STOP_TIMEOUT_US 1000 + /* * Stop an IOP that did not complete a power transition. Its CPU is left with RUN set and its * acknowledgement may still arrive, so a later owner would find a running coprocessor and stale - * mailbox traffic instead of a fresh boot. Stop the CPU and drop what it sent, so that a reset - * after this leaves the same state as a completed sleep. + * mailbox traffic instead of a fresh boot. Stop the CPU, wait until CPU_STATUS reports it halted + * (nothing it sends can follow that), and drop what it sent, so that a reset after this leaves + * the same state as a completed sleep. A CPU that never reports halted is reported; the + * caller's reset follows either way. */ void rtkit_stop(rtkit_dev_t *rtk) { rtkit_drain(rtk); asc_cpu_stop(rtk->asc); - udelay(1000); + u64 timeout = timeout_calculate(RTKIT_STOP_TIMEOUT_US); + while (!asc_cpu_stopped(rtk->asc)) { + if (timeout_expired(timeout)) { + printf("rtkit(%s): CPU not halted %u us after RUN was cleared (status 0x%x)\n", + rtk->name, RTKIT_STOP_TIMEOUT_US, asc_cpu_status(rtk->asc)); + break; + } + } rtkit_drain(rtk); rtk->crashed = true; } From b70791ecd701cf6c0819365c599f076740a47e80 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:46:53 +1100 Subject: [PATCH 02/29] kboot: refuse a trial payload whose watchdog U-Boot would take over On an aura-try trial every aura stage arms the trial watchdog, and kboot marks the watchdog node u-boot,noautostart so that U-Boot leaves it alone; a U-Boot that waits forever then still resets into the configured Stage 2. When that property could not be set, kboot logged it and handed off anyway: U-Boot would take the watchdog over and feed it, and a trial that hangs in U-Boot would never be reset, with nobody at the Mac. Fail closed instead: kboot_prepare_dt() refuses the payload, and the trial path reboots into the configured Stage 2 as for any trial that cannot start its payload. Non-trial boots are unchanged. Signed-off-by: Ryan Murray --- src/kboot.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/kboot.c b/src/kboot.c index e5696139f..76be0d9a8 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -837,10 +837,10 @@ static int dt_reserve_secondary_ro(void) * watchdog over and services it from its own loops, so a U-Boot that waits forever (on a disk it * cannot reach, say) would never be reset. Tell it to leave the watchdog alone on a trial boot. */ -static void dt_set_trial_watchdog(void) +static int dt_set_trial_watchdog(void) { if (!aura_trial_enabled()) - return; + return 0; /* The compatibles U-Boot's apple_wdt driver binds to */ static const char *const compat[] = {"apple,t8103-wdt", "apple,wdt"}; for (size_t i = 0; i < ARRAY_SIZE(compat); i++) { @@ -848,12 +848,16 @@ static void dt_set_trial_watchdog(void) while ((node = fdt_node_offset_by_compatible(dt, node, compat[i])) >= 0) { if (fdt_getprop(dt, node, "u-boot,noautostart", NULL)) continue; + /* + * Unmarked, U-Boot would feed the watchdog forever and a trial that hangs in it would + * never reset. Refuse the payload: the trial path reboots into the configured Stage 2. + */ if (fdt_setprop(dt, node, "u-boot,noautostart", NULL, 0)) - printf("FDT: couldn't mark the watchdog for the trial\n"); - else - printf("FDT: trial boot: U-Boot leaves the watchdog to the trial budget\n"); + bail("FDT: couldn't mark the watchdog for the trial\n"); + printf("FDT: trial boot: U-Boot leaves the watchdog to the trial budget\n"); } } + return 0; } /* @@ -11909,7 +11913,8 @@ int kboot_prepare_dt(void *fdt) return -1; if (dt_set_chosen()) return -1; - dt_set_trial_watchdog(); + if (dt_set_trial_watchdog()) + return -1; if (dt_check_trial_esp()) return -1; secboot_export_fdt(dt); From 713cf9253561df1257c7867e1087a8947d35eadf Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:47:53 +1100 Subject: [PATCH 03/29] kboot: disable the T6040/T6041 AOP when its firmware segments are not reserved On T6040 and T6041 the OS starts the AOP from the DRAM segments iBoot left (__ETEXT and __OS_LOG), mapped through the AOP's DART from the asc-mem reservations kboot adds. When that reservation failed, kboot logged it and left the AOP enabled: those pages, or the ones after a partial failure, were free RAM to the OS, and the AOP would be started on code in pages the kernel reuses. Disable the AOP node instead, as the T8132 AOP handoff already does for the same failure, and refuse the handoff only if the node cannot be disabled. A successful reservation is unchanged. Signed-off-by: Ryan Murray --- src/kboot.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/kboot.c b/src/kboot.c index 76be0d9a8..e20188293 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -9272,10 +9272,19 @@ static int dt_set_aop_firmware(void) return 0; } - if (dt_reserve_asc_segments("/arm-io/aop", NULL, path, true, 0, true)) - printf("FDT: %s: reserving the AOP firmware segments failed\n", path); - else - printf("FDT: %s: AOP firmware segments reserved\n", path); + /* + * The OS starts the AOP from these segments through its DART. Unreserved (or only some of + * them), they are free RAM to the OS, and the AOP would run code from pages it reuses: + * disable the AOP instead, as on T8132. + */ + if (dt_reserve_asc_segments("/arm-io/aop", NULL, path, true, 0, true)) { + printf("FDT: %s: reserving the AOP firmware segments failed, disabling the AOP\n", path); + int node = fdt_path_offset(dt, path); + if (node < 0 || fdt_setprop_string(dt, node, "status", "disabled")) + bail("FDT: %s: cannot disable the AOP\n", path); + return 0; + } + printf("FDT: %s: AOP firmware segments reserved\n", path); return 0; } From 360177e5666f59ce684e88a6ee94eea444cd3cd2 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:44:19 +1100 Subject: [PATCH 04/29] sptm_virt: call AURA_TRAP_DELAY_NS a diagnostic and record what it found The trap delay was described as a workaround knob. It is a default-off bisection aid: sptm_virt_trap_delay_ns is 0 and only run_guest_sptm.py writes it. Say so, and record what the bisection found since: only the sysreg traps matter (t106-t110), SPTM's rare EL2 control writes alone do not (t115), and the last guest writes before every stall power up the camera ISP; with the ISP and its DART hidden the guest runs on without a delay (t121, eb665623). The race itself stays open and is tracked in its own issue. No code change. Signed-off-by: Ryan Murray --- proxyclient/tools/run_guest_sptm.py | 2 +- src/sptm_virt_gl.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/proxyclient/tools/run_guest_sptm.py b/proxyclient/tools/run_guest_sptm.py index da7d407f2..d9ad52b7d 100755 --- a/proxyclient/tools/run_guest_sptm.py +++ b/proxyclient/tools/run_guest_sptm.py @@ -71,7 +71,7 @@ AURA_KEEP_ANE keep /arm-io/ane and dart-ane (the exclaves' manifests name its mappers) AURA_KEEP_WIFI keep the kernelcache workaround's PCIe/Wi-Fi/Bluetooth nodes (not the debug port's): WindowServer waits on TimeSync, which needs a network port - AURA_TRAP_DELAY_NS= spin n ns in every GL trap (workaround: holds off the ~20 s stall) + AURA_TRAP_DELAY_NS= spin n ns in every GL trap (diagnostic: delays the ~20 s stall) AURA_TRAP_DELAY_MASK= which GL traps spin: 1 GEXIT, 2 g-ERET, 4 IMPDEF, 8 SPTM's entry sysreg read, 16 HVC, 32 rest, 64 other sysregs AURA_SYSREG_CENSUS print which sysregs the guarded world traps on, every 20 s diff --git a/src/sptm_virt_gl.c b/src/sptm_virt_gl.c index 8e0189795..efa1b4da5 100644 --- a/src/sptm_virt_gl.c +++ b/src/sptm_virt_gl.c @@ -1030,12 +1030,17 @@ void sptm_virt_canary_tick(void) } /* - * AURA_TRAP_DELAY_NS: a workaround knob, not a fix. The guest stops (no trap, no WFI, no panic) - * ~20-25 s into XNU when aura's GL traps are fast; slowing each one moves that out -- 2.5 us to - * ~95 s (t102), 10 us to ~6 min (t105) -- so something races with aura's speed. + * AURA_TRAP_DELAY_NS: a diagnostic for the macOS guest's ~20 s stall, not a fix, and off unless + * run_guest_sptm.py writes it (0 here). No boot path sets it. The guest stops (no trap, no WFI, + * no panic) ~20-25 s into XNU when aura's GL traps are fast; slowing each one moves that out -- + * 2.5 us to ~95 s (t102), 10 us to ~6 min (t105) -- so something races with aura's speed. * AURA_TRAP_DELAY_MASK picks the traps that wait (default all): bit 0 GEXIT, 1 guarded ERET, * 2 other IMPDEF ops, 3 s3_6_c15_c11_1 (SPTM's entry read), 4 HVC, 5 the rest, 6 other sysregs. - * Only the sysreg traps matter (t110). + * Only the sysreg traps matter (t106-t110); SPTM's rare EL2 control writes alone do not (t115). + * The last guest writes before every stall power up the camera ISP; with /arm-io/isp and + * /arm-io/dart-isp hidden (AURA_HIDE_NODES) the guest runs on without a delay (t121, eb665623). + * The race itself is open: tracked in aura issue "sptm_virt: guest stalls ~20 s into XNU when + * the ISP powers up (AURA_TRAP_DELAY_NS hides it)". Keep the knob for that bisection. */ u32 sptm_virt_trap_delay_mask = ~0u; From d2c3686eb2c664e693289283792e826cb0602634 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:42:38 +1100 Subject: [PATCH 05/29] usb_cdc: wait for the HPM firmware after the cold reset The J700 CDC transition reset HPM0 with Gaid and then slept a fixed second before talking to it again. Gaid never completes like other commands: the controller resets, which clears CMD1, and then boots its application firmware, which reports itself in the MODE register. Wait on that instead. tps6598x_wait_ready() first sees CMD1 lose Gaid (or the transport drop while the controller is in reset), so the old firmware's MODE cannot pass as ready, then polls MODE until it reads APP or APP1. Transport errors during the reset are expected; on SPMI the controller is woken again before the next poll. The wait is bounded by TPS6598X_RESET_READY_MS (2000 ms: TI's 1000 ms reset to ready, Linux tipd TPS_SETUP_MS, inside the 2000 ms Linux allows the reset command) and fails the transition, loudly, when the firmware does not come back, instead of carrying on into a controller that is not there. Name the MODE values and the !CMD reply as four-character tags. Signed-off-by: Ryan Murray --- src/tps6598x.c | 55 ++++++++++++++---- src/tps6598x.h | 2 + src/tps6598x_command_core.c | 54 ++++++++++++++++- src/tps6598x_command_core.h | 17 ++++++ src/usb_cdc.c | 5 +- tests/python/test_tps6598x_command.py | 84 ++++++++++++++++++++++++++- 6 files changed, 199 insertions(+), 18 deletions(-) diff --git a/src/tps6598x.c b/src/tps6598x.c index 3de6393b6..e20885d7c 100644 --- a/src/tps6598x.c +++ b/src/tps6598x.c @@ -13,13 +13,12 @@ #define TPS_REG_MODE 0x03 #define TPS_REG_CMD1 0x08 -#define TPS_CMD_INVALID 0x444d4321 // !CMD as LE u32 +#define TPS_CMD_INVALID TPS6598X_TAG('!', 'C', 'M', 'D') #define TPS_REG_INT_EVENT1 0x14 #define TPS_REG_INT_MASK1 0x16 #define TPS_REG_INT_CLEAR1 0x18 #define TPS_REG_STATUS 0x1a #define TPS_REG_POWER_STATE 0x20 -#define TPS_MODE_DBMA ((u32)'D' | ((u32)'B' << 8) | ((u32)'M' << 16) | ((u32)'a' << 24)) #define TPS_SPMI_REG_SELECT 0x00 #define TPS_SPMI_REG_DATA 0x20 @@ -35,12 +34,13 @@ struct tps6598x_dev { bool resetting; bool command_active; u64 command_start; + u32 command_budget_ms; }; static bool tps6598x_command_expired(tps6598x_dev_t *dev) { return dev->command_active && - ticks_to_msecs(get_ticks() - dev->command_start) >= TPS6598X_COMMAND_DEADLINE_MS; + ticks_to_msecs(get_ticks() - dev->command_start) >= dev->command_budget_ms; } static tps6598x_dev_t *tps6598x_init(const char *adt_node, const char *addr_prop) @@ -177,19 +177,29 @@ static void tps6598x_command_delay_us(void *ctx, unsigned usec) udelay(usec); } +static int tps6598x_command_wake(void *ctx) +{ + tps6598x_dev_t *dev = ctx; + return dev->spmi ? spmi_send_wakeup(dev->spmi, dev->addr) : 0; +} + +static const struct tps6598x_command_ops tps6598x_command_ops = { + .write = tps6598x_command_write, + .read = tps6598x_command_read, + .now_ms = tps6598x_command_now_ms, + .delay_us = tps6598x_command_delay_us, + .wake = tps6598x_command_wake, +}; + int tps6598x_command(tps6598x_dev_t *dev, const char *cmd, const u8 *data_in, size_t len_in, u8 *data_out, size_t len_out) { - static const struct tps6598x_command_ops ops = { - .write = tps6598x_command_write, - .read = tps6598x_command_read, - .now_ms = tps6598x_command_now_ms, - .delay_us = tps6598x_command_delay_us, - }; + const struct tps6598x_command_ops *ops = &tps6598x_command_ops; dev->command_start = get_ticks(); + dev->command_budget_ms = TPS6598X_COMMAND_DEADLINE_MS; dev->command_active = true; - int ret = tps6598x_command_execute(&ops, dev, cmd, data_in, len_in, data_out, len_out); + int ret = tps6598x_command_execute(ops, dev, cmd, data_in, len_in, data_out, len_out); if (tps6598x_command_expired(dev)) ret = TPS6598X_COMMAND_TIMEOUT; dev->command_active = false; @@ -198,13 +208,34 @@ int tps6598x_command(tps6598x_dev_t *dev, const char *cmd, const u8 *data_in, si int tps6598x_cold_reset(tps6598x_dev_t *dev) { - /* Gaid resets the HPM itself, so completion cannot be polled on this session. */ + /* Gaid resets the HPM itself, so it never completes like other commands; + * tps6598x_wait_ready() observes the reset and the firmware coming back. */ if (tps6598x_write_reg(dev, TPS_REG_CMD1, (const u8 *)"Gaid", 4) != 4) return -1; dev->resetting = true; return 0; } +int tps6598x_wait_ready(tps6598x_dev_t *dev) +{ + u32 mode; + + /* Bound SPMI register selection by the same deadline as the whole wait. */ + dev->command_start = get_ticks(); + dev->command_budget_ms = TPS6598X_RESET_READY_MS; + dev->command_active = true; + int ret = tps6598x_reset_wait_execute(&tps6598x_command_ops, dev, &mode); + u32 elapsed = ticks_to_msecs(get_ticks() - dev->command_start); + dev->command_active = false; + if (ret) { + printf("tps6598x: not ready %u ms after reset (MODE 0x%08x)\n", elapsed, mode); + return -1; + } + dev->resetting = false; + printf("tps6598x: application firmware ready %u ms after reset\n", elapsed); + return 0; +} + int tps6598x_cmd_status(tps6598x_dev_t *dev, const char *cmd) { u32 cmd_status; @@ -377,7 +408,7 @@ static int tps6598x_dbma_route(tps6598x_dev_t *dev, const u8 vdm[4], const char } ret = tps6598x_read_reg(dev, TPS_REG_MODE, (u8 *)&mode, 4); - if (ret != 4 || mode != TPS_MODE_DBMA) { + if (ret != 4 || mode != TPS6598X_MODE_DBMA) { printf("%s: Failed to enter DBMa mode, mode=0x%08x\n", who, mode); return -1; } diff --git a/src/tps6598x.h b/src/tps6598x.h index 3760788d4..5df41e3c5 100644 --- a/src/tps6598x.h +++ b/src/tps6598x.h @@ -16,6 +16,8 @@ void tps6598x_shutdown(tps6598x_dev_t *dev); int tps6598x_command(tps6598x_dev_t *dev, const char *cmd, const u8 *data_in, size_t len_in, u8 *data_out, size_t len_out); int tps6598x_cold_reset(tps6598x_dev_t *dev); +/* After tps6598x_cold_reset(): wait for the application firmware, or fail. */ +int tps6598x_wait_ready(tps6598x_dev_t *dev); int tps6598x_powerup(tps6598x_dev_t *dev); /* STATUS (plug) and DATA_STATUS (connection, data role) as little-endian words */ int tps6598x_read_status(tps6598x_dev_t *dev, u32 *status, u32 *data_status); diff --git a/src/tps6598x_command_core.c b/src/tps6598x_command_core.c index ac868688f..cb12c0ee9 100644 --- a/src/tps6598x_command_core.c +++ b/src/tps6598x_command_core.c @@ -2,9 +2,11 @@ #include "tps6598x_command_core.h" +#define TPS_REG_MODE 0x03 #define TPS_REG_CMD1 0x08 #define TPS_REG_DATA1 0x09 -#define TPS_CMD_INVALID 0x444d4321 /* !CMD, little endian */ +#define TPS_CMD_INVALID TPS6598X_TAG('!', 'C', 'M', 'D') +#define TPS_CMD_GAID TPS6598X_TAG('G', 'a', 'i', 'd') static bool deadline_expired(const struct tps6598x_command_ops *ops, void *ctx, u64 start) { @@ -31,8 +33,7 @@ int tps6598x_command_execute(const struct tps6598x_command_ops *ops, void *ctx, u8 bytes[4]; if (ops->read(ctx, TPS_REG_CMD1, bytes, sizeof(bytes)) != sizeof(bytes)) goto transport_error; - u32 status = - (u32)bytes[0] | ((u32)bytes[1] << 8) | ((u32)bytes[2] << 16) | ((u32)bytes[3] << 24); + u32 status = TPS6598X_TAG(bytes[0], bytes[1], bytes[2], bytes[3]); if (status == TPS_CMD_INVALID) return -1; if (!status) @@ -47,3 +48,50 @@ int tps6598x_command_execute(const struct tps6598x_command_ops *ops, void *ctx, transport_error: return deadline_expired(ops, ctx, start) ? TPS6598X_COMMAND_TIMEOUT : -1; } + +/* + * Wait for the controller to come back after Gaid was written to CMD1. Gaid + * never completes like other commands: the controller resets, which clears + * CMD1, and then boots its application firmware. So first see CMD1 lose Gaid + * (or the transport drop while the controller is in reset), then wait for + * MODE to read APP/APP1. A MODE read before CMD1 lost Gaid could still be + * the old firmware and does not count. Transport errors during the reset are + * expected: wake the transport again (SPMI) and keep polling until the + * deadline. Returns 0, or TPS6598X_COMMAND_TIMEOUT with the last MODE in + * *mode (0 if it never read). + */ +int tps6598x_reset_wait_execute(const struct tps6598x_command_ops *ops, void *ctx, u32 *mode) +{ + if (!ops || !ops->read || !ops->now_ms || !ops->delay_us || !mode) + return -1; + + u64 start = ops->now_ms(ctx); + bool reset_seen = false; + *mode = 0; + for (;;) { + u8 bytes[4]; + bool transport_ok = true; + + if (!reset_seen) { + if (ops->read(ctx, TPS_REG_CMD1, bytes, sizeof(bytes)) != sizeof(bytes)) + transport_ok = false; + if (!transport_ok || + TPS6598X_TAG(bytes[0], bytes[1], bytes[2], bytes[3]) != TPS_CMD_GAID) + reset_seen = true; + } + if (reset_seen && transport_ok) { + if (ops->read(ctx, TPS_REG_MODE, bytes, sizeof(bytes)) == sizeof(bytes)) { + *mode = TPS6598X_TAG(bytes[0], bytes[1], bytes[2], bytes[3]); + if (*mode == TPS6598X_MODE_APP || *mode == TPS6598X_MODE_APP1) + return 0; + } else { + transport_ok = false; + } + } + if (ops->now_ms(ctx) - start >= TPS6598X_RESET_READY_MS) + return TPS6598X_COMMAND_TIMEOUT; + if (!transport_ok && ops->wake) + ops->wake(ctx); + ops->delay_us(ctx, TPS6598X_RESET_POLL_US); + } +} diff --git a/src/tps6598x_command_core.h b/src/tps6598x_command_core.h index 50f90f066..d0245ebd3 100644 --- a/src/tps6598x_command_core.h +++ b/src/tps6598x_command_core.h @@ -16,14 +16,31 @@ typedef uint64_t u64; #define TPS6598X_COMMAND_DEADLINE_MS 1500 #define TPS6598X_COMMAND_TIMEOUT -2 +/* + * Reset (Gaid) to application firmware running again. TI gives up to 1000 ms + * from reset de-assertion to ready for operation (Linux tipd TPS_SETUP_MS); + * Linux allows its reset command 2000 ms. Poll MODE within the latter. + */ +#define TPS6598X_RESET_READY_MS 2000 +#define TPS6598X_RESET_POLL_US 10000 + +/* MODE register values, four ASCII characters read as a little-endian word. */ +#define TPS6598X_TAG(a, b, c, d) ((u32)(a) | ((u32)(b) << 8) | ((u32)(c) << 16) | ((u32)(d) << 24)) +#define TPS6598X_MODE_APP TPS6598X_TAG('A', 'P', 'P', ' ') +#define TPS6598X_MODE_APP1 TPS6598X_TAG('A', 'P', 'P', '1') +#define TPS6598X_MODE_DBMA TPS6598X_TAG('D', 'B', 'M', 'a') + struct tps6598x_command_ops { int (*write)(void *ctx, u8 reg, const u8 *data, size_t len); int (*read)(void *ctx, u8 reg, u8 *data, size_t len); u64 (*now_ms)(void *ctx); void (*delay_us)(void *ctx, unsigned usec); + /* Optional: bring the transport back after the controller reset (SPMI wakeup). */ + int (*wake)(void *ctx); }; int tps6598x_command_execute(const struct tps6598x_command_ops *ops, void *ctx, const char *cmd, const u8 *data_in, size_t len_in, u8 *data_out, size_t len_out); +int tps6598x_reset_wait_execute(const struct tps6598x_command_ops *ops, void *ctx, u32 *mode); #endif diff --git a/src/usb_cdc.c b/src/usb_cdc.c index 849475ab5..83a2adf1e 100644 --- a/src/usb_cdc.c +++ b/src/usb_cdc.c @@ -46,7 +46,9 @@ static int cdc_reset_hpm(char *path, tps6598x_dev_t *hpm, void *unused) { (void)path; (void)unused; - return tps6598x_cold_reset(hpm) ? HPM_ACTION_ERROR : HPM_ACTION_STOP; + if (tps6598x_cold_reset(hpm) || tps6598x_wait_ready(hpm)) + return HPM_ACTION_ERROR; + return HPM_ACTION_STOP; } static int cdc_power_hpm(char *path, tps6598x_dev_t *hpm, void *unused) @@ -120,7 +122,6 @@ void usb_cdc_poll(void) if (cdc.flags & BIT(2)) { if (tps6598x_foreach_hpm(cdc_dfu_hpm, cdc_reset_hpm, NULL) != HPM_ACTION_STOP) goto failed; - mdelay(1000); } cdc.step = 2; diff --git a/tests/python/test_tps6598x_command.py b/tests/python/test_tps6598x_command.py index 01d2c6db5..df4fcda1f 100644 --- a/tests/python/test_tps6598x_command.py +++ b/tests/python/test_tps6598x_command.py @@ -11,11 +11,12 @@ ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t) NOW = ctypes.CFUNCTYPE(ctypes.c_uint64, ctypes.c_void_p) DELAY = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_uint) +WAKE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p) class Ops(ctypes.Structure): _fields_ = [("write", WRITE), ("read", READ), - ("now_ms", NOW), ("delay_us", DELAY)] + ("now_ms", NOW), ("delay_us", DELAY), ("wake", WAKE)] def command_lib(tmp_path): @@ -29,9 +30,90 @@ def command_lib(tmp_path): ctypes.c_size_t, ctypes.c_void_p, ctypes.c_size_t] lib.tps6598x_command_execute.restype = ctypes.c_int + lib.tps6598x_reset_wait_execute.argtypes = [ctypes.POINTER(Ops), ctypes.c_void_p, + ctypes.POINTER(ctypes.c_uint32)] + lib.tps6598x_reset_wait_execute.restype = ctypes.c_int return lib +class ResettingHpm: + """CMD1/MODE as a controller rebooting after Gaid: answers `stale` polls + from the old firmware, fails the transport for `down` polls, then reads + MODE `modes` in turn (the last one sticks).""" + + def __init__(self, stale, down, modes): + self.clock = 0 + self.stale = stale + self.down = down + self.modes = list(modes) + self.wakes = 0 + self.ops = Ops(WRITE(lambda ctx, reg, data, length: length), READ(self.read), + NOW(lambda ctx: self.clock), DELAY(self.delay), WAKE(self.wake)) + + def delay(self, ctx, usec): + self.clock += usec // 1000 + if self.stale: + self.stale -= 1 + elif self.down: + self.down -= 1 + elif len(self.modes) > 1: + self.modes.pop(0) + + def wake(self, ctx): + self.wakes += 1 + return 0 + + def read(self, ctx, reg, data, length): + if self.stale: + value = b"Gaid" if reg == 0x08 else b"APP " + elif self.down: + return -1 + else: + value = b"\0\0\0\0" if reg == 0x08 else self.modes[0] + ctypes.memmove(data, value, 4) + return length + + +def test_reset_wait_ignores_old_firmware_and_rides_out_the_reset(tmp_path): + lib = command_lib(tmp_path) + hpm = ResettingHpm(stale=3, down=20, modes=[b"BOOT", b"BOOT", b"APP "]) + mode = ctypes.c_uint32() + assert lib.tps6598x_reset_wait_execute(ctypes.byref(hpm.ops), None, + ctypes.byref(mode)) == 0 + assert mode.value == int.from_bytes(b"APP ", "little") + # Ready only after the reset was seen and the boot modes passed. + assert hpm.clock == (3 + 20 + 2) * 10 + assert hpm.wakes == 20 + + +def test_reset_wait_accepts_app1(tmp_path): + lib = command_lib(tmp_path) + hpm = ResettingHpm(stale=0, down=0, modes=[b"APP1"]) + mode = ctypes.c_uint32() + assert lib.tps6598x_reset_wait_execute(ctypes.byref(hpm.ops), None, + ctypes.byref(mode)) == 0 + assert hpm.clock == 0 + + +def test_reset_wait_fails_when_firmware_never_returns(tmp_path): + lib = command_lib(tmp_path) + hpm = ResettingHpm(stale=0, down=0, modes=[b"BOOT"]) + mode = ctypes.c_uint32() + assert lib.tps6598x_reset_wait_execute(ctypes.byref(hpm.ops), None, + ctypes.byref(mode)) == -2 + assert mode.value == int.from_bytes(b"BOOT", "little") + assert hpm.clock == 2000 + + +def test_reset_wait_fails_when_reset_never_happens(tmp_path): + lib = command_lib(tmp_path) + hpm = ResettingHpm(stale=10**6, down=0, modes=[b"APP "]) + mode = ctypes.c_uint32() + assert lib.tps6598x_reset_wait_execute(ctypes.byref(hpm.ops), None, + ctypes.byref(mode)) == -2 + assert mode.value == 0 + + def test_stuck_register_selection_uses_whole_command_deadline(tmp_path): lib = command_lib(tmp_path) clock = [0] From 717b862f807056d06ce7f1801770731024ceefe5 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:44:01 +1100 Subject: [PATCH 06/29] tps6598x: wait for the firmware after the debugusb reset When the port controller reports a failed command before the DebugUSB LOCK, it is reset with Gaid and given 20 ms before LOCK. Gaid reboots the controller's firmware, which TI allows up to 1000 ms; polling CMD1 for the reset to complete also fails as soon as the transport drops during the reset. Write Gaid without waiting for a reply and wait for the application firmware with tps6598x_wait_ready(), as the CDC transition does, before LOCK. The DebugUSB setup now fails, with the controller's MODE in the log, when the firmware does not come back within TPS6598X_RESET_READY_MS. Signed-off-by: Ryan Murray --- src/tps6598x.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/tps6598x.c b/src/tps6598x.c index e20885d7c..b0c8dc2ca 100644 --- a/src/tps6598x.c +++ b/src/tps6598x.c @@ -385,13 +385,12 @@ static int tps6598x_dbma_route(tps6598x_dev_t *dev, const u8 vdm[4], const char for (int i = 0; i < 4; i++) key[i] = target[3 - i]; - // check status and soft reset if it fails + // check status and reset the controller if it fails if (tps6598x_cmd_status(dev, "LOCK")) { if (!reset_ok) return -1; - if (tps6598x_command(dev, "Gaid", NULL, 0, NULL, 0)) + if (tps6598x_cold_reset(dev) || tps6598x_wait_ready(dev)) return -1; - mdelay(20); } ret = tps6598x_command(dev, "LOCK", key, 4, (u8 *)&out, 1); From 72978d6b1a5417b037d1318bbaeb35fde3e0cb76 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:45:29 +1100 Subject: [PATCH 07/29] usb_cdc: name the schedule flags and the recovery budget The CDC schedule flags were bare bits (BIT(0) lane swap, BIT(1) Gen1, BIT(2) HPM cold reset) checked as 7u/2u masks, and the schedule window and re-advertise budget were literals repeated in three places. Give them names in usb_cdc_state.h and use them in usb_cdc.c, the state machine and the Stage 1 proxy window. No change in behaviour. Signed-off-by: Ryan Murray --- src/uartproxy.c | 4 +++- src/usb_cdc.c | 7 ++++--- src/usb_cdc_state.c | 13 +++++++------ src/usb_cdc_state.h | 15 +++++++++++++++ 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/uartproxy.c b/src/uartproxy.c index 06d0e9b98..edf469ff6 100644 --- a/src/uartproxy.c +++ b/src/uartproxy.c @@ -9,6 +9,7 @@ #include "types.h" #include "usb.h" #include "usb_cdc.h" +#include "usb_cdc_state.h" #include "utils.h" #include "wdt.h" @@ -113,7 +114,8 @@ enum stage1_proxy_result uartproxy_wait_stage1(unsigned int timeout_ms) stage1_proxy_window_init(&window, timeout_ms); bool schedule_failed = false; if (chip_id == T8140) { - schedule_failed = usb_cdc_schedule(1000, 0, BIT(1) | BIT(2)); + schedule_failed = usb_cdc_schedule(USB_CDC_SCHEDULE_MIN_MS, 0, + USB_CDC_FLAG_GEN1 | USB_CDC_FLAG_HPM_RESET); } else { usb_init(); usb_iodev_init(); diff --git a/src/usb_cdc.c b/src/usb_cdc.c index 83a2adf1e..24b69e54d 100644 --- a/src/usb_cdc.c +++ b/src/usb_cdc.c @@ -69,7 +69,8 @@ int usb_cdc_schedule(u32 delay_ms, u32 reserved, u32 flags) { if (usb2_target()) { /* Explicit USB2 transition only: no HPM reset, lane swap or watchdog. */ - if (flags || reserved || cdc.state != USB_CDC_IDLE || delay_ms < 1000 || delay_ms > 15000) + if (flags || reserved || cdc.state != USB_CDC_IDLE || delay_ms < USB_CDC_SCHEDULE_MIN_MS || + delay_ms > USB_CDC_SCHEDULE_MAX_MS) return -1; cdc.deadline_ms = ticks_to_msecs(get_ticks()) + delay_ms; cdc.state = USB_CDC_SCHEDULED; @@ -119,7 +120,7 @@ void usb_cdc_poll(void) cdc.step = 1; /* HPM0 cold reset irrevocably retires the inherited KIS carrier. */ carrier_owned = true; - if (cdc.flags & BIT(2)) { + if (cdc.flags & USB_CDC_FLAG_HPM_RESET) { if (tps6598x_foreach_hpm(cdc_dfu_hpm, cdc_reset_hpm, NULL) != HPM_ACTION_STOP) goto failed; } @@ -128,7 +129,7 @@ void usb_cdc_poll(void) if (tps6598x_foreach_hpm(cdc_dfu_hpm, cdc_power_hpm, NULL) != HPM_ACTION_STOP) goto failed; - if (usb_cdc_link_start(cdc_set_step, !!(cdc.flags & BIT(0)))) + if (usb_cdc_link_start(cdc_set_step, !!(cdc.flags & USB_CDC_FLAG_FORCE_SWAPPED))) goto failed; cdc.state = USB_CDC_DWC_READY; diff --git a/src/usb_cdc_state.c b/src/usb_cdc_state.c index d8803b947..2764dd226 100644 --- a/src/usb_cdc_state.c +++ b/src/usb_cdc_state.c @@ -6,8 +6,9 @@ int usb_cdc_state_schedule(struct usb_cdc_state *cdc, u32 delay_ms, u32 reserved u64 now_ms) { /* Only Gen1 descriptors and controller setup are implemented. */ - if (!cdc || cdc->state != USB_CDC_IDLE || delay_ms < 1000 || delay_ms > 15000 || reserved || - (flags & ~7u) || !(flags & 2u)) + if (!cdc || cdc->state != USB_CDC_IDLE || delay_ms < USB_CDC_SCHEDULE_MIN_MS || + delay_ms > USB_CDC_SCHEDULE_MAX_MS || reserved || (flags & ~USB_CDC_FLAGS_ALL) || + !(flags & USB_CDC_FLAG_GEN1)) return -1; cdc->flags = flags; cdc->step = 0; @@ -28,10 +29,10 @@ u32 usb_cdc_state_status(const struct usb_cdc_state *cdc) void usb_cdc_recovery_arm(struct usb_cdc_recovery *recovery, u64 now_ms) { - if (!recovery || recovery->pending || recovery->attempts >= 3) + if (!recovery || recovery->pending || recovery->attempts >= USB_CDC_RECOVERY_ATTEMPTS) return; recovery->pending = true; - recovery->deadline_ms = now_ms + 25; + recovery->deadline_ms = now_ms + USB_CDC_RECOVERY_FIRST_MS; } void usb_cdc_recovery_connected(struct usb_cdc_recovery *recovery) @@ -53,10 +54,10 @@ void usb_cdc_recovery_attempted(struct usb_cdc_recovery *recovery, u64 now_ms) if (!recovery || !recovery->pending) return; recovery->attempts++; - if (recovery->attempts >= 3) + if (recovery->attempts >= USB_CDC_RECOVERY_ATTEMPTS) recovery->pending = false; else - recovery->deadline_ms = now_ms + 2000; + recovery->deadline_ms = now_ms + USB_CDC_RECOVERY_RETRY_MS; } bool usb_cdc_dma_may_release(bool active, int end_status, bool reset_complete) diff --git a/src/usb_cdc_state.h b/src/usb_cdc_state.h index 8dd682f4f..b696144a7 100644 --- a/src/usb_cdc_state.h +++ b/src/usb_cdc_state.h @@ -11,6 +11,21 @@ typedef uint64_t u64; #include "types.h" #endif +/* usb_cdc_schedule() flags (proxy P_CDC_SCHEDULE argument 2). */ +#define USB_CDC_FLAG_FORCE_SWAPPED (1u << 0) /* drive the ATC lanes flipped */ +#define USB_CDC_FLAG_GEN1 (1u << 1) /* Gen1 descriptors and link: required */ +#define USB_CDC_FLAG_HPM_RESET (1u << 2) /* cold-reset HPM0 first, retiring KIS */ +#define USB_CDC_FLAGS_ALL (USB_CDC_FLAG_FORCE_SWAPPED | USB_CDC_FLAG_GEN1 | USB_CDC_FLAG_HPM_RESET) + +/* Accepted delay before a scheduled transition starts (policy, not a hardware wait). */ +#define USB_CDC_SCHEDULE_MIN_MS 1000 +#define USB_CDC_SCHEDULE_MAX_MS 15000 + +/* Re-advertise after a host disconnect: first try, spacing and budget (policy). */ +#define USB_CDC_RECOVERY_FIRST_MS 25 +#define USB_CDC_RECOVERY_RETRY_MS 2000 +#define USB_CDC_RECOVERY_ATTEMPTS 3 + enum usb_cdc_state_id { USB_CDC_IDLE, USB_CDC_SCHEDULED, From 5a4e6d52dc1b2fa7a5468fec9dc5e251d3e5e016 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 01:00:08 +1100 Subject: [PATCH 08/29] usb: name the ATC USB2 PHY and PIPE register fields in the CDC paths The T8140 CDC ATC path kept its own copy of the USB2 PHY and PIPE handler offsets and wrote their bits as bare numbers, and the T8152 USB0 path used raw offsets for the USB2 PHY, the PIPE handler and the DWC3 core (0xc200, 0xc2c0, 0xc120, 0xc704). The T6050 path compared the PIPE mux against 0x73/0x42/0x22. Move the USB2 PHY, PIPE handler and USB2 event counter registers out of usb.c into usb_atc_regs.h, named and laid out as in Linux drivers/phy/apple/atc.c, add the per-generation MUX_CTRL field helpers, and use them, and the existing DWC3 register names, in all three paths. The PSD/AON power registers of the T8152 path have no public names and stay as they are. No functional change: usb.o, usb_t8152.o and usb_cdc_atc.o disassemble identically before and after. Signed-off-by: Ryan Murray --- src/usb.c | 89 ++++++++++++++++----------------------------- src/usb_atc_regs.h | 90 ++++++++++++++++++++++++++++++++++++++++++++++ src/usb_cdc_atc.c | 64 ++++++++++++++++----------------- src/usb_t8152.c | 54 +++++++++++++++++----------- 4 files changed, 183 insertions(+), 114 deletions(-) create mode 100644 src/usb_atc_regs.h diff --git a/src/usb.c b/src/usb.c index 27f2a0b58..7f468d56d 100644 --- a/src/usb.c +++ b/src/usb.c @@ -15,6 +15,7 @@ #include "tps6598x.h" #include "tunables.h" #include "types.h" +#include "usb_atc_regs.h" #include "usb_cdc.h" #include "usb_cdc_atc.h" #include "usb_dwc3.h" @@ -51,47 +52,6 @@ struct usb_drd_regs { static tps6598x_irq_state_t tps6598x_irq_state[USB_IODEV_COUNT]; static bool usb_is_initialized = false; -/* USB2 PHY regs */ -#define USB2PHY_USBCTL 0x00 -#define USB2PHY_USBCTL_USB_MODE 0x7 -#define USB2PHY_USBCTL_RUN 2 -#define USB2PHY_USBCTL_ISOLATION 4 - -#define USB2PHY_CTL 0x04 -#define USB2PHY_CTL_RESET BIT(0) -#define USB2PHY_CTL_PORT_RESET BIT(1) -#define USB2PHY_CTL_APB_RESET_N BIT(2) -#define USB2PHY_CTL_SIDDQ BIT(3) - -#define USB2PHY_SIG 0x08 -#define USB2PHY_SIG_VBUSDET_FORCE_VAL BIT(0) -#define USB2PHY_SIG_VBUSDET_FORCE_EN BIT(1) -#define USB2PHY_SIG_VBUSVLDEXT_FORCE_VAL BIT(2) -#define USB2PHY_SIG_VBUSVLDEXT_FORCE_EN BIT(3) -#define USB2PHY_SIG_HOST (7 << 12) - -#define USB2PHY_MISCTUNE 0x1c -#define USB2PHY_MISCTUNE_APBCLK_GATE_OFF BIT(29) -#define USB2PHY_MISCTUNE_REFCLK_GATE_OFF BIT(30) - -#define PIPEHANDLER_MUX_CTRL 0x0c -#define PIPEHANDLER_MUX_CTRL_USB3 0x08 -#define PIPEHANDLER_MUX_CTRL_USB4_TUNNEL 0x11 -#define PIPEHANDLER_MUX_CTRL_DUMMY 0x22 - -#define PIPEHANDLER_LOCK_REQ 0x10 -#define PIPEHANDLER_LOCK_ACK 0x14 -#define PIPEHANDLER_LOCK_EN BIT(0) - -#define PIPEHANDLER_AON_GEN 0x1C -#define PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN BIT(4) -#define PIPEHANDLER_AON_GEN_DWC3_RESET_N BIT(0) - -#define PIPEHANDLER_NONSELECTED_OVERRIDE 0x20 -#define PIPEHANDLER_NATIVE_RESET BIT(12) -#define PIPEHANDLER_DUMMY_PHY_EN BIT(15) -#define PIPEHANDLER_NATIVE_POWER_DOWN GENMASK(3, 0) - /* * Resolve the ADT path of a USB port node. Most ADTs number every port's nodes * (usb-drd0, dart-usb0/mapper-usb0), but an ADT with a single port may name them @@ -209,6 +169,11 @@ static int usb_t6050_get_regs(struct usb_t6050_regs *regs) * apple_t6050_usb3_pipe.h (cold dummy), atc-t6050-combo.h (reset). * Only USB2 is needed: no ACIO firmware, high-speed lanes or internal PHY3. * Device-role values come from this machine's ADT, not the host-role table. */ + +/* Older loaders leave the PIPE on the dummy data producer with the clock field + * as the domain came up (0x22, see apple_t6050_usb3_pipe_cold_dummy()). */ +#define T6050_MUX_OLD_LOADER_RELEASED PIPEHANDLER_MUX_T8142(USB4, DUMMY) + static dwc3_dev_t *usb_t6050_init(void (*set_step)(unsigned step)) { struct usb_t6050_regs regs; @@ -225,7 +190,7 @@ static dwc3_dev_t *usb_t6050_init(void (*set_step)(unsigned step)) u64 pipe = regs.pipe; u32 aon = read32(pipe + PIPEHANDLER_AON_GEN); - u32 mux = read32(pipe + PIPEHANDLER_MUX_CTRL) & 0x73; + u32 mux = read32(pipe + PIPEHANDLER_MUX_CTRL) & PIPEHANDLER_MUX_CTRL_T8142_MASK; if (dart_adt_locked("/arm-io/dart-usb0", 1)) { printf("CDC USB2: T6050 USB DART is locked; leaving it alone\n"); return NULL; @@ -233,14 +198,15 @@ static dwc3_dev_t *usb_t6050_init(void (*set_step)(unsigned step)) /* An in-place raw chainload retires Stage 1's gadget before this stage * starts. Older loaders leave its dummy PIPE released (0x22). Accept * only that fully stopped device, never a live or native PIPE producer. */ - if ((aon & PIPEHANDLER_AON_GEN_DWC3_RESET_N) || mux == 0x22) { + if ((aon & PIPEHANDLER_AON_GEN_DWC3_RESET_N) || mux == T6050_MUX_OLD_LOADER_RELEASED) { u32 gctl = read32(regs.dwc + DWC3_GCTL); - bool retired = (mux == 0x22 || mux == 0x42) && - (gctl & DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)) == - DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE) && - !(read32(regs.dwc + DWC3_DCTL) & DWC3_DCTL_RUN_STOP) && - (read32(regs.dwc + DWC3_DSTS) & DWC3_DSTS_DEVCTRLHLT) && - !read32(regs.dwc + DWC3_DEVTEN) && !read32(regs.dwc + DWC3_DALEPENA); + bool retired = + (mux == T6050_MUX_OLD_LOADER_RELEASED || mux == PIPEHANDLER_MUX_T8142(DUMMY, DUMMY)) && + (gctl & DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)) == + DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE) && + !(read32(regs.dwc + DWC3_DCTL) & DWC3_DCTL_RUN_STOP) && + (read32(regs.dwc + DWC3_DSTS) & DWC3_DSTS_DEVCTRLHLT) && + !read32(regs.dwc + DWC3_DEVTEN) && !read32(regs.dwc + DWC3_DALEPENA); if (!retired) { printf("CDC USB2: T6050 PIPE consumer is not retired (aon=%x mux=%x)\n", aon, mux); return NULL; @@ -251,18 +217,24 @@ static dwc3_dev_t *usb_t6050_init(void (*set_step)(unsigned step)) return NULL; udelay(10); printf("CDC USB2: retired Stage 1 gadget held in reset\n"); - } else if (mux != 0 && mux != 2 && mux != 0x42) { + } else if (mux != PIPEHANDLER_MUX_T8142(OFF, USB3) && + mux != PIPEHANDLER_MUX_T8142(OFF, DUMMY) && + mux != PIPEHANDLER_MUX_T8142(DUMMY, DUMMY)) { printf("CDC USB2: unknown T6050 PIPE producer (aon=%x mux=%x)\n", aon, mux); return NULL; } set32(pipe + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN); - mask32(pipe + PIPEHANDLER_MUX_CTRL, 0x70, 0); + mask32(pipe + PIPEHANDLER_MUX_CTRL, PIPEHANDLER_MUX_CTRL_CLK_T8142, + FIELD_PREP(PIPEHANDLER_MUX_CTRL_CLK_T8142, PIPEHANDLER_MUX_CTRL_CLK_OFF)); udelay(10); - mask32(pipe + PIPEHANDLER_MUX_CTRL, 3, 2); + mask32(pipe + PIPEHANDLER_MUX_CTRL, PIPEHANDLER_MUX_CTRL_DATA_T8142, + FIELD_PREP(PIPEHANDLER_MUX_CTRL_DATA_T8142, PIPEHANDLER_MUX_CTRL_DATA_DUMMY)); udelay(10); - mask32(pipe + PIPEHANDLER_MUX_CTRL, 0x70, 0x40); + mask32(pipe + PIPEHANDLER_MUX_CTRL, PIPEHANDLER_MUX_CTRL_CLK_T8142, + FIELD_PREP(PIPEHANDLER_MUX_CTRL_CLK_T8142, PIPEHANDLER_MUX_CTRL_CLK_DUMMY)); udelay(10); - if ((read32(pipe + PIPEHANDLER_MUX_CTRL) & 0x73) != 0x42) + if ((read32(pipe + PIPEHANDLER_MUX_CTRL) & PIPEHANDLER_MUX_CTRL_T8142_MASK) != + PIPEHANDLER_MUX_T8142(DUMMY, DUMMY)) return NULL; set_step(3); @@ -271,14 +243,13 @@ static dwc3_dev_t *usb_t6050_init(void (*set_step)(unsigned step)) tunables_apply_compact_addr("/arm-io/atc-phy0", "tunable_USB2PHY_DEV", regs.phy, regs.phy_size)) return NULL; - set32(regs.phy + USB2PHY_SIG, USB2PHY_SIG_VBUSDET_FORCE_VAL | USB2PHY_SIG_VBUSDET_FORCE_EN | - USB2PHY_SIG_VBUSVLDEXT_FORCE_VAL | - USB2PHY_SIG_VBUSVLDEXT_FORCE_EN); + set32(regs.phy + USB2PHY_SIG, USB2PHY_SIG_VBUS_FORCE); udelay(10000); clear32(regs.phy + USB2PHY_CTL, USB2PHY_CTL_SIDDQ); udelay(10); clear32(regs.phy + USB2PHY_CTL, USB2PHY_CTL_RESET | USB2PHY_CTL_PORT_RESET); - set32(regs.digital, BIT(0) | BIT(3)); /* USB2 event counter enable/load */ + set32(regs.digital + ATCPHY_EVT_USB2_CTL, + ATCPHY_EVT_USB2_CTL_EVT_EN | ATCPHY_EVT_USB2_CTL_LOAD_CNT); set32(regs.phy + USB2PHY_CTL, USB2PHY_CTL_APB_RESET_N); clear32(regs.phy + USB2PHY_MISCTUNE, USB2PHY_MISCTUNE_APBCLK_GATE_OFF | USB2PHY_MISCTUNE_REFCLK_GATE_OFF); @@ -293,7 +264,7 @@ static dwc3_dev_t *usb_t6050_init(void (*set_step)(unsigned step)) PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN | PIPEHANDLER_AON_GEN_DWC3_RESET_N, PIPEHANDLER_AON_GEN_DWC3_RESET_N, 1000) || (read32(regs.phy + USB2PHY_USBCTL) & USB2PHY_USBCTL_USB_MODE) != USB2PHY_USBCTL_RUN || - (read32(regs.phy + USB2PHY_CTL) & 0xf) != USB2PHY_CTL_APB_RESET_N) { + (read32(regs.phy + USB2PHY_CTL) & USB2PHY_CTL_MASK) != USB2PHY_CTL_APB_RESET_N) { printf("CDC USB2: T6050 reset/clock setup did not latch\n"); return NULL; } diff --git a/src/usb_atc_regs.h b/src/usb_atc_regs.h new file mode 100644 index 000000000..617962f19 --- /dev/null +++ b/src/usb_atc_regs.h @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * ATC USB2 PHY, PIPE handler and event-counter registers used by the USB + * device paths. Names and fields follow Linux drivers/phy/apple/atc.c. + */ +#ifndef USB_ATC_REGS_H +#define USB_ATC_REGS_H + +#include "types.h" + +/* USB2 PHY regs */ +#define USB2PHY_USBCTL 0x00 +#define USB2PHY_USBCTL_USB_MODE 0x7 +#define USB2PHY_USBCTL_RUN 2 +#define USB2PHY_USBCTL_ISOLATION 4 + +#define USB2PHY_CTL 0x04 +#define USB2PHY_CTL_RESET BIT(0) +#define USB2PHY_CTL_PORT_RESET BIT(1) +#define USB2PHY_CTL_APB_RESET_N BIT(2) +#define USB2PHY_CTL_SIDDQ BIT(3) +#define USB2PHY_CTL_MASK \ + (USB2PHY_CTL_RESET | USB2PHY_CTL_PORT_RESET | USB2PHY_CTL_APB_RESET_N | USB2PHY_CTL_SIDDQ) + +#define USB2PHY_SIG 0x08 +#define USB2PHY_SIG_VBUSDET_FORCE_VAL BIT(0) +#define USB2PHY_SIG_VBUSDET_FORCE_EN BIT(1) +#define USB2PHY_SIG_VBUSVLDEXT_FORCE_VAL BIT(2) +#define USB2PHY_SIG_VBUSVLDEXT_FORCE_EN BIT(3) +#define USB2PHY_SIG_VBUS_FORCE \ + (USB2PHY_SIG_VBUSDET_FORCE_VAL | USB2PHY_SIG_VBUSDET_FORCE_EN | \ + USB2PHY_SIG_VBUSVLDEXT_FORCE_VAL | USB2PHY_SIG_VBUSVLDEXT_FORCE_EN) +#define USB2PHY_SIG_HOST (7 << 12) + +#define USB2PHY_MISCTUNE 0x1c +#define USB2PHY_MISCTUNE_APBCLK_GATE_OFF BIT(29) +#define USB2PHY_MISCTUNE_REFCLK_GATE_OFF BIT(30) + +/* USB2 event counter block */ +#define ATCPHY_EVT_USB2_CTL 0x00 +#define ATCPHY_EVT_USB2_CTL_EVT_EN BIT(0) +#define ATCPHY_EVT_USB2_CTL_LOAD_CNT BIT(3) + +#define PIPEHANDLER_OVERRIDE 0x00 +#define PIPEHANDLER_OVERRIDE_RXVALID BIT(0) +#define PIPEHANDLER_OVERRIDE_RXDETECT BIT(2) + +/* + * MUX_CTRL selects the PIPE clock and data source. The fields moved between + * generations: T8103 layout CLK [5:3] DATA [2:0], T8142 layout CLK [6:4] + * DATA [1:0]. + */ +#define PIPEHANDLER_MUX_CTRL 0x0c +#define PIPEHANDLER_MUX_CTRL_USB3 0x08 +#define PIPEHANDLER_MUX_CTRL_USB4_TUNNEL 0x11 +#define PIPEHANDLER_MUX_CTRL_DUMMY 0x22 +#define PIPEHANDLER_MUX_CTRL_CLK_T8103 GENMASK(5, 3) +#define PIPEHANDLER_MUX_CTRL_DATA_T8103 GENMASK(2, 0) +#define PIPEHANDLER_MUX_CTRL_CLK_T8142 GENMASK(6, 4) +#define PIPEHANDLER_MUX_CTRL_DATA_T8142 GENMASK(1, 0) +#define PIPEHANDLER_MUX_CTRL_CLK_OFF 0 +#define PIPEHANDLER_MUX_CTRL_CLK_USB3 1 +#define PIPEHANDLER_MUX_CTRL_CLK_USB4 2 +#define PIPEHANDLER_MUX_CTRL_CLK_DUMMY 4 +#define PIPEHANDLER_MUX_CTRL_DATA_USB3 0 +#define PIPEHANDLER_MUX_CTRL_DATA_USB4 1 +#define PIPEHANDLER_MUX_CTRL_DATA_DUMMY 2 +#define PIPEHANDLER_MUX_CTRL_T8142_MASK \ + (PIPEHANDLER_MUX_CTRL_CLK_T8142 | PIPEHANDLER_MUX_CTRL_DATA_T8142) +#define PIPEHANDLER_MUX_T8103(clk, data) \ + (FIELD_PREP(PIPEHANDLER_MUX_CTRL_CLK_T8103, PIPEHANDLER_MUX_CTRL_CLK_##clk) | \ + FIELD_PREP(PIPEHANDLER_MUX_CTRL_DATA_T8103, PIPEHANDLER_MUX_CTRL_DATA_##data)) +#define PIPEHANDLER_MUX_T8142(clk, data) \ + (FIELD_PREP(PIPEHANDLER_MUX_CTRL_CLK_T8142, PIPEHANDLER_MUX_CTRL_CLK_##clk) | \ + FIELD_PREP(PIPEHANDLER_MUX_CTRL_DATA_T8142, PIPEHANDLER_MUX_CTRL_DATA_##data)) + +#define PIPEHANDLER_LOCK_REQ 0x10 +#define PIPEHANDLER_LOCK_ACK 0x14 +#define PIPEHANDLER_LOCK_EN BIT(0) + +#define PIPEHANDLER_AON_GEN 0x1C +#define PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN BIT(4) +#define PIPEHANDLER_AON_GEN_DWC3_RESET_N BIT(0) + +#define PIPEHANDLER_NONSELECTED_OVERRIDE 0x20 +#define PIPEHANDLER_NATIVE_RESET BIT(12) +#define PIPEHANDLER_DUMMY_PHY_EN BIT(15) +#define PIPEHANDLER_NATIVE_POWER_DOWN GENMASK(3, 0) + +#endif diff --git a/src/usb_cdc_atc.c b/src/usb_cdc_atc.c index 10ded3dab..b2d78784a 100644 --- a/src/usb_cdc_atc.c +++ b/src/usb_cdc_atc.c @@ -4,28 +4,19 @@ #include "adt.h" #include "string.h" #include "tunables.h" +#include "usb_atc_regs.h" #include "usb_cdc.h" #include "utils.h" /* T8130 ATC register facts from public Asahi Linux atc.c and the J700 ADT. */ -#define USB2_CTL 0x04 -#define USB2_SIG 0x08 -#define USB2_MISCTUNE 0x1c -#define USB2_USBCTL 0x00 -#define ATC_EVT_USB2_CTL 0x00 -#define ATC_CFG0 0x08 -#define ATC_LANE_MODE 0x60 -#define ATC_CROSSBAR 0x64 -#define ATC_RCAL 0x04 -#define ATC_BIAS 0x00 -#define ATC_POWER_CTRL 0x00 -#define ATC_POWER_STAT 0x04 -#define ATC_MISC 0x08 - -#define PIPE_OVERRIDE 0x00 -#define PIPE_MUX 0x0c -#define PIPE_AON_GEN 0x1c -#define PIPE_NONSELECTED 0x20 +#define ATC_CFG0 0x08 +#define ATC_LANE_MODE 0x60 +#define ATC_CROSSBAR 0x64 +#define ATC_RCAL 0x04 +#define ATC_BIAS 0x00 +#define ATC_POWER_CTRL 0x00 +#define ATC_POWER_STAT 0x04 +#define ATC_MISC 0x08 struct atc_tuning_group { const char *name; @@ -144,28 +135,29 @@ int usb_cdc_atc_power_on(uintptr_t pipehandler) if (validate_group(node, &lane_groups[lane][i])) return -1; - set32(usb2 + USB2_SIG, 0xf); - clear32(usb2 + USB2_SIG, 7 << 12); /* device role */ + set32(usb2 + USB2PHY_SIG, USB2PHY_SIG_VBUS_FORCE); + clear32(usb2 + USB2PHY_SIG, USB2PHY_SIG_HOST); /* device role */ udelay(10); - clear32(usb2 + USB2_CTL, BIT(3)); /* SIDDQ */ + clear32(usb2 + USB2PHY_CTL, USB2PHY_CTL_SIDDQ); udelay(10); - clear32(usb2 + USB2_CTL, BIT(0)); /* RESET */ + clear32(usb2 + USB2PHY_CTL, USB2PHY_CTL_RESET); udelay(10); - clear32(usb2 + USB2_CTL, BIT(1)); /* PORT_RESET */ + clear32(usb2 + USB2PHY_CTL, USB2PHY_CTL_PORT_RESET); udelay(10); - set32(core + ATC_EVT_USB2_CTL, BIT(0) | BIT(3)); + set32(core + ATCPHY_EVT_USB2_CTL, ATCPHY_EVT_USB2_CTL_EVT_EN | ATCPHY_EVT_USB2_CTL_LOAD_CNT); udelay(10); - set32(usb2 + USB2_CTL, BIT(2)); /* APB_RESET_N */ + set32(usb2 + USB2PHY_CTL, USB2PHY_CTL_APB_RESET_N); udelay(10); - clear32(usb2 + USB2_MISCTUNE, BIT(29) | BIT(30)); + clear32(usb2 + USB2PHY_MISCTUNE, + USB2PHY_MISCTUNE_APBCLK_GATE_OFF | USB2PHY_MISCTUNE_REFCLK_GATE_OFF); /* T8140's eUSB2 repeater needs to settle after reset release (Linux atc.c). */ usb_cdc_wait_us(5000); - write32(usb2 + USB2_USBCTL, 2); /* RUN */ + write32(usb2 + USB2PHY_USBCTL, USB2PHY_USBCTL_RUN); /* Park the upstream DWC3 PIPE on the dummy PHY until DWC3 is ready. */ - write32(pipe + PIPE_MUX, 0x22); - write32(pipe + PIPE_AON_GEN, BIT(0)); - write32(pipe + PIPE_NONSELECTED, 0x9332); + write32(pipe + PIPEHANDLER_MUX_CTRL, PIPEHANDLER_MUX_T8103(DUMMY, DUMMY)); + write32(pipe + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_RESET_N); + write32(pipe + PIPEHANDLER_NONSELECTED_OVERRIDE, 0x9332); set32(power + ATC_MISC, BIT(0)); set32(power + ATC_POWER_CTRL, BIT(0)); @@ -245,12 +237,16 @@ int usb_cdc_atc_switch_pipe(void) printf("CDC ATC: cable orientation changed, lane=%u\n", lane); } - mask32(pipe + PIPE_MUX, 7 << 3, 0); + mask32(pipe + PIPEHANDLER_MUX_CTRL, PIPEHANDLER_MUX_CTRL_CLK_T8103, + FIELD_PREP(PIPEHANDLER_MUX_CTRL_CLK_T8103, PIPEHANDLER_MUX_CTRL_CLK_OFF)); udelay(10); - mask32(pipe + PIPE_MUX, 7, 0); + mask32(pipe + PIPEHANDLER_MUX_CTRL, PIPEHANDLER_MUX_CTRL_DATA_T8103, + FIELD_PREP(PIPEHANDLER_MUX_CTRL_DATA_T8103, PIPEHANDLER_MUX_CTRL_DATA_USB3)); udelay(10); - mask32(pipe + PIPE_MUX, 7 << 3, BIT(3)); + mask32(pipe + PIPEHANDLER_MUX_CTRL, PIPEHANDLER_MUX_CTRL_CLK_T8103, + FIELD_PREP(PIPEHANDLER_MUX_CTRL_CLK_T8103, PIPEHANDLER_MUX_CTRL_CLK_USB3)); udelay(10); - clear32(pipe + PIPE_OVERRIDE, BIT(0) | BIT(2)); + clear32(pipe + PIPEHANDLER_OVERRIDE, + PIPEHANDLER_OVERRIDE_RXVALID | PIPEHANDLER_OVERRIDE_RXDETECT); return 0; } diff --git a/src/usb_t8152.c b/src/usb_t8152.c index dcb8758b5..3ef7be6e6 100644 --- a/src/usb_t8152.c +++ b/src/usb_t8152.c @@ -5,6 +5,8 @@ #include "dart.h" #include "soc.h" #include "string.h" +#include "usb_atc_regs.h" +#include "usb_dwc3_regs.h" #include "utils.h" #define USB0_DWC 0x402430000ULL @@ -16,6 +18,11 @@ #define USB0_GATE 0x308290120ULL #define USB0_WAIT 250000 +/* USB2 event counter: advances only while the PHY's UTMI clock runs. */ +#define USB0_EVT_USB2_COUNT 0x20 +/* GSNPSID of the DWC_usb31 core this path was brought up on. */ +#define USB0_GSNPSID 0x33313130 + static bool reg_is(const char *path, const char *compat, unsigned idx, u64 base, u64 size) { int trace[8]; @@ -170,39 +177,44 @@ dwc3_dev_t *usb_t8152_init(void (*set_step)(unsigned step)) return NULL; set_step(3); - if (read32(USB0_PIPE + 0xc) != 0x42 || read32(USB0_PIPE + 0x1c) != 0 || read32(USB0_PHY) != 4 || - read32(USB0_PHY + 4) != 0xb) + if (read32(USB0_PIPE + PIPEHANDLER_MUX_CTRL) != PIPEHANDLER_MUX_T8142(DUMMY, DUMMY) || + read32(USB0_PIPE + PIPEHANDLER_AON_GEN) != 0 || + read32(USB0_PHY + USB2PHY_USBCTL) != USB2PHY_USBCTL_ISOLATION || + read32(USB0_PHY + USB2PHY_CTL) != + (USB2PHY_CTL_SIDDQ | USB2PHY_CTL_PORT_RESET | USB2PHY_CTL_RESET)) return NULL; - clear32(USB0_DWC + 0xc200, 0x40); - clear32(USB0_DWC + 0xc2c0, 0x20000); - write32(USB0_PIPE + 0x1c, 1); - if (read32(USB0_DWC + 0xc120) != 0x33313130 || (read32(USB0_DWC + 0xc704) & BIT(31))) { - write32(USB0_PIPE + 0x1c, 0); + clear32(USB0_DWC + DWC3_GUSB2PHYCFG(0), DWC3_GUSB2PHYCFG_SUSPHY); + clear32(USB0_DWC + DWC3_GUSB3PIPECTL(0), DWC3_GUSB3PIPECTL_SUSPHY); + write32(USB0_PIPE + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_RESET_N); + if (read32(USB0_DWC + DWC3_GSNPSID) != USB0_GSNPSID || + (read32(USB0_DWC + DWC3_DCTL) & DWC3_DCTL_RUN_STOP)) { + write32(USB0_PIPE + PIPEHANDLER_AON_GEN, 0); return NULL; } - write32(USB0_PIPE + 0x1c, 0); + write32(USB0_PIPE + PIPEHANDLER_AON_GEN, 0); phy_tuning(phy, "tunable_USB2PHY_DFLT", true); phy_tuning(phy, "tunable_USB2PHY_DEV", true); - set32(USB0_PHY + 8, 4); - set32(USB0_PHY + 8, 8); + set32(USB0_PHY + USB2PHY_SIG, USB2PHY_SIG_VBUSVLDEXT_FORCE_VAL); + set32(USB0_PHY + USB2PHY_SIG, USB2PHY_SIG_VBUSVLDEXT_FORCE_EN); mdelay(10); - clear32(USB0_PHY + 4, 8); + clear32(USB0_PHY + USB2PHY_CTL, USB2PHY_CTL_SIDDQ); udelay(10); - clear32(USB0_PHY + 4, 1); - clear32(USB0_PHY + 4, 2); - set32(USB0_EVT, 9); - set32(USB0_PHY + 4, 4); - clear32(USB0_PHY + 0x1c, BIT(29)); - clear32(USB0_PHY + 0x1c, BIT(30)); + clear32(USB0_PHY + USB2PHY_CTL, USB2PHY_CTL_RESET); + clear32(USB0_PHY + USB2PHY_CTL, USB2PHY_CTL_PORT_RESET); + set32(USB0_EVT + ATCPHY_EVT_USB2_CTL, + ATCPHY_EVT_USB2_CTL_EVT_EN | ATCPHY_EVT_USB2_CTL_LOAD_CNT); + set32(USB0_PHY + USB2PHY_CTL, USB2PHY_CTL_APB_RESET_N); + clear32(USB0_PHY + USB2PHY_MISCTUNE, USB2PHY_MISCTUNE_APBCLK_GATE_OFF); + clear32(USB0_PHY + USB2PHY_MISCTUNE, USB2PHY_MISCTUNE_REFCLK_GATE_OFF); udelay(30); - if (!read32(USB0_EVT + 0x20)) { + if (!read32(USB0_EVT + USB0_EVT_USB2_COUNT)) { printf("USB0: no UTMI clock; leaving controller in reset\n"); return NULL; } mdelay(5); - mask32(USB0_PHY, 7, 2); - write32(USB0_PIPE + 0x1c, 1); - set32(USB0_PIPE + 0x20, BIT(15)); /* DUMMY_PHY_READY */ + mask32(USB0_PHY + USB2PHY_USBCTL, USB2PHY_USBCTL_USB_MODE, USB2PHY_USBCTL_RUN); + write32(USB0_PIPE + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_RESET_N); + set32(USB0_PIPE + PIPEHANDLER_NONSELECTED_OVERRIDE, PIPEHANDLER_DUMMY_PHY_EN); set_step(4); dart_dev_t *dart = dart_gen3_init_adt_cold("/arm-io/dart-usb0", 1, 1); From decc58c178b80bb12dc6f566364bd5d698346d2e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:39:54 +1100 Subject: [PATCH 09/29] j813_n1: release the N1 through the ADT function-reset helper j813_n1_reset() was a copy of smc_adt_function_reset() with the SMC key and value of pci-bridge0's function-reset (pKW4 'gP13', 0x800000) hard-coded. Use the helper on the bridge's ADT node instead, as the T6050 and J873 N1 paths already do: same writes (level 0, then 1, 50 ms each), but taken from the ADT, and a missing or non-SMC function-reset now stops the bring-up instead of writing a key the board may not have. Signed-off-by: Ryan Murray --- src/j813_n1.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/j813_n1.c b/src/j813_n1.c index 837554b52..5edec936e 100644 --- a/src/j813_n1.c +++ b/src/j813_n1.c @@ -35,9 +35,8 @@ #define DBI_RO_WR 0x8bc -/* SMC key gP13: ADT pci-bridge0 function-reset = pKW4('gP13', 0x800000). */ -#define SMC_KEY_GP13 0x67503133 -#define N1_RESET_LO 0x800000 +/* SMC function-reset level hold: assert, then release (smc_adt_function_reset()) */ +#define N1_RESET_SETTLE_MS 50 /* AP GPIO 192 (ADT pci-bridge0 function-perst), driven high as output. */ #define GPIO_N1_PERST 0x39a000300 @@ -69,21 +68,6 @@ static int j813_tunables(const char *path, const char *prop, u64 base) return 0; } -static int j813_n1_reset(void) -{ - smc_dev_t *smc = smc_init(); - int ret = 0; - - if (!smc) - return -1; - for (u32 level = 0; level < 2 && !ret; level++) { - ret = smc_write_u32(smc, SMC_KEY_GP13, N1_RESET_LO | level); - mdelay(50); - } - smc_shutdown(smc); - return ret; -} - static int j813_n1_link(void) { int path[8]; @@ -135,7 +119,8 @@ static int j813_n1_link(void) if (j813_wait(rc + 0x58, BIT(0), BIT(0), 250000, "gtb_initialized")) return -1; - if (j813_n1_reset()) { + /* The SMC holds the N1 in reset: pci-bridge0 function-reset (pKW4 gP13). */ + if (smc_adt_function_reset(APCIE_PORT, N1_RESET_SETTLE_MS)) { printf("j813-n1: SMC N1 reset release failed\n"); return -1; } From 1930234a7839c155cc27d22b71ac26bc99f5e0d3 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:41:02 +1100 Subject: [PATCH 10/29] pcie: name the endpoint reset waits and poll config readiness The J813 N1 and T6050 GP bring-ups slept a bare 100 ms after the SMC function-reset release and again after link-up, then read the endpoint's ID once. Both waits are spec times, and the single read is where a readiness poll belongs: - After the function-reset release: PCIe CEM T_PVPERL, at least 100 ms with the endpoint powered before PERST# is deasserted (PCIE_CEM_T_PVPERL_MS). - After link-up: PCIe Base 6.6.1, at least 100 ms after link training before the first Configuration Request on a port faster than 5 GT/s. Every apcie bridge in the ADT states this as perst-to-config (100 on J813, J873, J873s, J700, J773s); pcie_adt_perst_to_config_ms() takes the longer of it and the spec value. - Then the endpoint has until 1.0 s after reset to complete config requests (Base 6.6.1). pcie_wait_config_ready() polls its vendor ID past all-ones and Request Retry Status for up to that long, and fails loudly if it never answers, instead of judging the endpoint by one early read. No change to the time either path spends on a healthy endpoint. Signed-off-by: Ryan Murray --- src/j813_n1.c | 10 +++++++--- src/pcie.c | 29 +++++++++++++++++++++++++++++ src/pcie.h | 24 ++++++++++++++++++++++++ src/pcie_t6050.c | 10 ++++++---- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/j813_n1.c b/src/j813_n1.c index 5edec936e..b0b9a5512 100644 --- a/src/j813_n1.c +++ b/src/j813_n1.c @@ -14,6 +14,7 @@ #include "j813_n1.h" #include "adt.h" +#include "pcie.h" #include "pmgr.h" #include "smc.h" #include "tunables.h" @@ -124,7 +125,8 @@ static int j813_n1_link(void) printf("j813-n1: SMC N1 reset release failed\n"); return -1; } - mdelay(100); + /* The N1 is out of its SMC reset: T_PVPERL before PERST# is released below. */ + mdelay(PCIE_CEM_T_PVPERL_MS); /* APCIECoreRCGen4Port::_resetPortHardware */ static const u32 port_reset[][2] = { @@ -172,8 +174,10 @@ static int j813_n1_link(void) /* Root port bus numbers, then check the endpoint. */ write32(ecam + 0x18, 0x00010100); - mdelay(100); - u32 ident = read32(ecam + 0x100000); + mdelay(pcie_adt_perst_to_config_ms(APCIE_PORT)); + u32 ident; + if (pcie_wait_config_ready(ecam + 0x100000, &ident)) + return -1; printf("j813-n1: link up (0x%x), endpoint %04x:%04x\n", read32(port + PORT_LINKSTS), ident & 0xffff, ident >> 16); return (ident & 0xffff) == 0x106b ? 0 : -1; diff --git a/src/pcie.c b/src/pcie.c index e50691219..4c622f51b 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -1090,6 +1090,35 @@ static int pcie_init_controller(int controller, const char *path, bool ans_off) return 0; } +u32 pcie_adt_perst_to_config_ms(const char *bridge) +{ + int node = adt_path_offset(adt, bridge); + u32 ms = 0; + + if (node < 0 || ADT_GETPROP(adt, node, "perst-to-config", &ms) < 0) + ms = 0; + return max(ms, (u32)PCIE_RESET_TO_CONFIG_MS); +} + +int pcie_wait_config_ready(u64 cfg, u32 *id) +{ + u64 timeout = timeout_calculate(PCIE_RESET_READY_MS * 1000); + + while (true) { + u32 vendor = (*id = read32(cfg)) & 0xffff; + + /* All ones: no completion; 0x0001: Request Retry Status */ + if (vendor != 0xffff && vendor != 0x0001) + return 0; + if (timeout_expired(timeout)) { + printf("pcie: function at 0x%lx not ready after %u ms (0x%08x)\n", cfg, + PCIE_RESET_READY_MS, *id); + return -1; + } + udelay(100); + } +} + int pcie_init(void) { bool success = false; diff --git a/src/pcie.h b/src/pcie.h index 172e1f568..772d6f8d0 100644 --- a/src/pcie.h +++ b/src/pcie.h @@ -19,6 +19,30 @@ int pcie_ans_power_off(void); /* Back on after pcie_ans_power_off(), CPU stopped */ int pcie_ans_power_on(void); +/* + * Endpoint reset timing every port bring-up shares. + * + * PCIE_CEM_T_PVPERL_MS: PCIe CEM r5.0 2.2, T_PVPERL: the endpoint's power stable at least 100 ms + * before PERST# is deasserted. Taken after an SMC function-reset releases the endpoint. + * + * PCIE_RESET_TO_CONFIG_MS: PCIe Base r6.0 6.6.1: with a port faster than 5 GT/s, at least 100 ms + * after link training completes before the first Configuration Request. The ADT bridge states it + * too, as perst-to-config (ms); pcie_adt_perst_to_config_ms() takes the longer of the two. + * + * PCIE_RESET_READY_MS: PCIe Base r6.0 6.6.1: a function completes Configuration Requests + * successfully within 1.0 s of a Conventional Reset. Until then it answers with Request Retry + * Status (vendor ID 0x0001 when software visibility is on) or not at all (all ones). + */ +#define PCIE_CEM_T_PVPERL_MS 100 +#define PCIE_RESET_TO_CONFIG_MS 100 +#define PCIE_RESET_READY_MS 1000 + +/* The ADT bridge's perst-to-config in ms, at least PCIE_RESET_TO_CONFIG_MS */ +u32 pcie_adt_perst_to_config_ms(const char *bridge); +/* Wait up to PCIE_RESET_READY_MS for the function at `cfg` to answer config space; *id gets its + * vendor/device word. -1, and a log line, if it never does. */ +int pcie_wait_config_ready(u64 cfg, u32 *id); + /* Disable optional radio PCIe admission after rail/controller/DART failure. */ int pcie_disable_j713_fdt(void *dt); /* Post-M4: copy the APCIe tables from the ADT to the DT's PCIe host and its port. */ diff --git a/src/pcie_t6050.c b/src/pcie_t6050.c index f0bc22f3d..e4de0b16d 100644 --- a/src/pcie_t6050.c +++ b/src/pcie_t6050.c @@ -562,7 +562,7 @@ static int stage_e(const struct port_regs *pp) TRY(rmw("LTSSM start", p2 + PORT_LTSSMCTL, 0, BIT(0))); TRY(poll_set("link up", p2 + PORT_LINKSTS, BIT(0), 500)); - mdelay(100); + mdelay(pcie_adt_perst_to_config_ms(pp->bridge)); return 0; } @@ -612,7 +612,9 @@ static int stage_f(struct port_regs *pp) TRY(set_bus(rp, bus)); } pp->bus = bus; - u32 id = read32(r.ecam + ((u64)bus << 20)); + u32 id; + if (pcie_wait_config_ready(r.ecam + ((u64)bus << 20), &id)) + FAIL("endpoint %02x:00.0 never answered config space\n", bus); if (id != pp->p->endpoint_id[0] && (!pp->p->endpoint_id[1] || id != pp->p->endpoint_id[1])) FAIL("endpoint %02x:00.0 is 0x%08x\n", bus, id); printf("pcie-t6050: port %d endpoint %04x:%04x\n", pp->p->port, id & 0xffff, id >> 16); @@ -651,8 +653,8 @@ static int port_up(struct port_regs *pp) int ret = smc_adt_function_reset(pp->bridge, 50); if (ret < 0) FAIL("port %d: its SMC function-reset was not released\n", pp->p->port); - if (!ret) - mdelay(100); /* the endpoint out of reset before the port's own reset table */ + if (!ret) /* out of its SMC reset: T_PVPERL before PERST# is released in E */ + mdelay(PCIE_CEM_T_PVPERL_MS); TRY(stage_b(pp)); TRY(stage_c(pp)); TRY(stage_d(pp)); From 1bf7d2f881bf8860db7d5f69e561cd5443e84071 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:42:14 +1100 Subject: [PATCH 11/29] pcie: take the N1's PERST# pad and refclk time from the ADT on J813 j813_n1.c drove PERST# through a hard-coded pad address (0x39a000300, AP GPIO 192) and waited a fixed 1 ms for refclk-to-PERST#. pci-bridge0 gives both: function-perst = GPIO(0xc0) of /arm-io/gpio0 and t-refclk-to-perst = 100 (us). pcie_t6050.c already resolved its pads that way, inline. Move that resolver to pcie.c as pcie_adt_perst_gpio() and use it from both, and resolve J813's pad and refclk time before anything is powered, so a board whose ADT lacks them stops cleanly. The 1 ms floor both paths keep over the ADT's 100 us is named APCIE_REFCLK_TO_PERST_MIN_US: it is what every bring-up has used, and nothing shorter has been tried. The J813 pad resolves to the same address (gpio0 0x18a000000 + arm-io 0x210000000 + 4 * 0xc0); no change in what is written. Signed-off-by: Ryan Murray --- src/j813_n1.c | 20 ++++++++++++++++---- src/pcie.c | 24 ++++++++++++++++++++++++ src/pcie.h | 9 +++++++++ src/pcie_t6050.c | 28 ++++++++-------------------- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/j813_n1.c b/src/j813_n1.c index b0b9a5512..651faad00 100644 --- a/src/j813_n1.c +++ b/src/j813_n1.c @@ -39,8 +39,9 @@ /* SMC function-reset level hold: assert, then release (smc_adt_function_reset()) */ #define N1_RESET_SETTLE_MS 50 -/* AP GPIO 192 (ADT pci-bridge0 function-perst), driven high as output. */ -#define GPIO_N1_PERST 0x39a000300 +/* function-perst pad: output (bits 3:1 = 1), data bit 0 high releases PERST# */ +#define GPIO_PERST_MODE_MASK 0xf +#define GPIO_PERST_RELEASE 0x3 static int j813_reg(int *path, int idx, u64 *addr) { @@ -82,6 +83,16 @@ static int j813_n1_link(void) return -1; u64 phycommon = phy + PHY_COMMON_OFF; + int bridge = adt_path_offset(adt, APCIE_PORT); + u32 refclk_to_perst_us; + u64 perst; + if (bridge < 0 || ADT_GETPROP(adt, bridge, "t-refclk-to-perst", &refclk_to_perst_us) < 0) { + printf("j813-n1: %s has no t-refclk-to-perst\n", APCIE_PORT); + return -1; + } + if (pcie_adt_perst_gpio(APCIE_PORT, &perst)) + return -1; + if (pmgr_adt_power_enable(APCIE)) return -1; @@ -152,10 +163,11 @@ static int j813_n1_link(void) udelay(1000); set32(lane, BIT(9)); set32(lane, BIT(10)); - udelay(1000); /* t-refclk-to-perst */ + /* refclk to PERST#: the bridge's t-refclk-to-perst */ + udelay(max(refclk_to_perst_us, (u32)APCIE_REFCLK_TO_PERST_MIN_US)); set32(port + PORT_PERST, BIT(0)); - mask32(GPIO_N1_PERST, 0xf, 0x3); + mask32(perst, GPIO_PERST_MODE_MASK, GPIO_PERST_RELEASE); if (j813_wait(port + PORT_PIPE_STS, BIT(0), BIT(0), 100000, "lane pipe reset") || j813_wait(port + PORT_STATUS, BIT(0), BIT(0), 250000, "port RUN") || j813_wait(port + PORT_LINKSTS, BIT(2), 0, 500000, "port idle")) diff --git a/src/pcie.c b/src/pcie.c index 4c622f51b..2e13db1ea 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -1119,6 +1119,30 @@ int pcie_wait_config_ready(u64 cfg, u32 *id) } } +int pcie_adt_perst_gpio(const char *bridge, u64 *addr) +{ + struct { + u32 phandle, fourcc, pin, flags; + } perst; + int gpio_path[8]; + u32 gpio_phandle; + u64 base, span; + int node = adt_path_offset(adt, bridge); + int gpio = adt_path_offset_trace(adt, "/arm-io/gpio0", gpio_path); + + /* function-perst = GPIO(pin, flags) of /arm-io/gpio0, its fourcc "OIPG" in ADT byte order */ + if (node < 0 || gpio < 0 || + adt_getprop_copy(adt, node, "function-perst", &perst, sizeof(perst)) < 0 || + perst.fourcc != 0x4750494f || ADT_GETPROP(adt, gpio, "AAPL,phandle", &gpio_phandle) < 0 || + gpio_phandle != perst.phandle || adt_get_reg(adt, gpio_path, "reg", 0, &base, &span) || + span < 4 * ((u64)perst.pin + 1)) { + printf("pcie: cannot resolve %s function-perst\n", bridge); + return -1; + } + *addr = base + 4 * perst.pin; + return 0; +} + int pcie_init(void) { bool success = false; diff --git a/src/pcie.h b/src/pcie.h index 772d6f8d0..ce39318a1 100644 --- a/src/pcie.h +++ b/src/pcie.h @@ -43,6 +43,15 @@ u32 pcie_adt_perst_to_config_ms(const char *bridge); * vendor/device word. -1, and a log line, if it never does. */ int pcie_wait_config_ready(u64 cfg, u32 *id); +/* The MMIO pad register of the ADT bridge's function-perst GPIO on /arm-io/gpio0 */ +int pcie_adt_perst_gpio(const char *bridge, u64 *addr); +/* + * Refclk running to PERST# deasserted: the ADT bridge's t-refclk-to-perst (us; 100 on every apcie + * bridge, the CEM T_PERST-CLK minimum), but never less than 1 ms, the time every Aura bring-up has + * used since its first trace. Nothing shorter has been tried. + */ +#define APCIE_REFCLK_TO_PERST_MIN_US 1000 + /* Disable optional radio PCIe admission after rail/controller/DART failure. */ int pcie_disable_j713_fdt(void *dt); /* Post-M4: copy the APCIe tables from the ADT to the DT's PCIe host and its port. */ diff --git a/src/pcie_t6050.c b/src/pcie_t6050.c index e4de0b16d..6834f27f8 100644 --- a/src/pcie_t6050.c +++ b/src/pcie_t6050.c @@ -91,10 +91,9 @@ static const struct t6050_port { #define PCI_EXP_LNKCTL2 0x30 #define PCI_CFG_SIZE 0x1000 -#define GPIO_PERST_MASK 0x27f -#define GPIO_PERST_LOW 0x202 -#define GPIO_PERST_LOCK BIT(21) -#define GPIO_FOURCC_GPIO 0x4750494f /* "OIPG" in ADT byte order */ +#define GPIO_PERST_MASK 0x27f +#define GPIO_PERST_LOW 0x202 +#define GPIO_PERST_LOCK BIT(21) static const char apcie_path[] = "/arm-io/apcie0"; @@ -235,7 +234,7 @@ static int find_regs(void) /* A port's own windows, PERST# pad, refclk-to-PERST# time and DART, from apcie0 and its bridge */ static int find_port_regs(struct port_regs *pp) { - int path_nodes[8], gpio_path[8], dart_path[8]; + int path_nodes[8], dart_path[8]; u64 unused, span; int n = pp->p->port; @@ -256,25 +255,14 @@ static int find_port_regs(struct port_regs *pp) FAIL("apcie0 reg[%d] missing or short\n", regs[i].idx); pp->rp = r.ecam + ((u64)n << 15); - /* PERST#: pci-bridge function-perst = GPIO(pin) of /arm-io/gpio0 */ snprintf(pp->bridge, sizeof(pp->bridge), "%s/pci-bridge%d", apcie_path, n); - struct { - u32 phandle, fourcc, pin, flags; - } perst; int bridge = adt_path_offset(adt, pp->bridge); - int gpio = adt_path_offset_trace(adt, "/arm-io/gpio0", gpio_path); - u32 gpio_phandle, apcie_port; - u64 gpio_base; + u32 apcie_port; if (bridge < 0 || ADT_GETPROP(adt, bridge, "apcie-port", &apcie_port) < 0 || apcie_port != (u32)n) FAIL("%s is not apcie port %d\n", pp->bridge, n); - if (gpio < 0 || adt_getprop_copy(adt, bridge, "function-perst", &perst, sizeof(perst)) < 0 || - perst.fourcc != GPIO_FOURCC_GPIO || - ADT_GETPROP(adt, gpio, "AAPL,phandle", &gpio_phandle) < 0 || - gpio_phandle != perst.phandle || adt_get_reg(adt, gpio_path, "reg", 0, &gpio_base, &span) || - span < 4 * (perst.pin + 1)) + if (pcie_adt_perst_gpio(pp->bridge, &pp->perst)) FAIL("cannot resolve %s function-perst\n", pp->bridge); - pp->perst = gpio_base + 4 * perst.pin; if (ADT_GETPROP(adt, bridge, "t-refclk-to-perst", &pp->refclk_to_perst_us) < 0) FAIL("%s has no t-refclk-to-perst\n", pp->bridge); @@ -549,8 +537,8 @@ static int stage_e(const struct port_regs *pp) mdelay(1); TRY(rmw("lane bit 9", pp->lane, 0, BIT(9))); TRY(rmw("lane bit 10", pp->lane, 0, BIT(10))); - /* refclk to PERST#: the bridge's t-refclk-to-perst (port 2: 100 us), at least 1 ms */ - udelay(max(pp->refclk_to_perst_us, 1000u)); + /* refclk to PERST#: the bridge's t-refclk-to-perst */ + udelay(max(pp->refclk_to_perst_us, (u32)APCIE_REFCLK_TO_PERST_MIN_US)); u32 old = read32(pp->perst); if (old & GPIO_PERST_LOCK) From 14a152175f7477b67388a254f76a92abcfdf506c Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:43:45 +1100 Subject: [PATCH 12/29] pcie, j813_n1: describe the steps, not macOS driver names, in comments 0bbd6f36 reworded two of j813_n1.c's comments; the file header and five step comments still named macOS driver classes and methods, and the T8142 ANS gate comment in pcie.c named the macOS driver. Say what each step does instead. No code changes. Signed-off-by: Ryan Murray --- src/j813_n1.c | 12 ++++++------ src/pcie.c | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/j813_n1.c b/src/j813_n1.c index 651faad00..38fe78762 100644 --- a/src/j813_n1.c +++ b/src/j813_n1.c @@ -3,7 +3,7 @@ /* * J813 (T8142, MacBook Air M5): train the PCIe link to the N1 Wi-Fi/Bluetooth * module on apcie port 0, so Linux can use the root port as a generic ECAM - * host. The sequence follows macOS 25G76 AppleT8142PCIe / APCIECoreRCGen4: + * host. The sequence follows the traced bring-up on this machine: * AUS5 PHY power-up, root complex, N1 reset, port reset, refclk buffer, * PERST, root complex init and LTSSM. * @@ -96,7 +96,7 @@ static int j813_n1_link(void) if (pmgr_adt_power_enable(APCIE)) return -1; - /* _apcieGPInit: PHY tunables, reference PLL. */ + /* PHY tunables, reference PLL. */ if (j813_tunables(APCIE, "apcie-phy-tunables", phy) || j813_wait(phycommon, BIT(31), BIT(31), 1000000, "refpll_clk_good")) return -1; @@ -125,7 +125,7 @@ static int j813_n1_link(void) if (j813_tunables(APCIE, "apcie-axi2af-tunables", axi)) return -1; - /* _enableRootComplex */ + /* Root complex run, then its ready bit */ write32(rc + 0x54, 0x140); set32(rc + 0x50, BIT(0)); if (j813_wait(rc + 0x58, BIT(0), BIT(0), 250000, "gtb_initialized")) @@ -139,7 +139,7 @@ static int j813_n1_link(void) /* The N1 is out of its SMC reset: T_PVPERL before PERST# is released below. */ mdelay(PCIE_CEM_T_PVPERL_MS); - /* APCIECoreRCGen4Port::_resetPortHardware */ + /* Port register reset table */ static const u32 port_reset[][2] = { {0x88, 0x110}, {0x148, 0xffffffff}, {0x210, 0xffffffff}, {0x84, 0}, {0x104, 0xfffffff0}, {0x124, 0x100}, {0x16c, 0}, {0x13c, 0}, @@ -152,7 +152,7 @@ static int j813_n1_link(void) return -1; set32(port + PORT_APPCLK, BIT(0)); - /* APCIECoreRCGen4Port::_initializeRefclkBuffer (port PHY lane reg 0) */ + /* Port refclk buffer (port PHY lane reg 0) */ set32(lane, BIT(0)); if (j813_wait(lane, BIT(2), BIT(2), 100000, "refclk buffer (1)")) return -1; @@ -173,7 +173,7 @@ static int j813_n1_link(void) j813_wait(port + PORT_LINKSTS, BIT(2), 0, 500000, "port idle")) return -1; - /* _initializeRootComplex: root port DBI tunables, then the LTSSM. */ + /* Root port DBI tunables, then the LTSSM. */ set32(ecam + DBI_RO_WR, BIT(0)); if (j813_tunables(APCIE_PORT, "pcie-rc-tunables", ecam) || j813_tunables(APCIE_PORT, "pcie-rc-gen3-shadow-tunables", ecam) || diff --git a/src/pcie.c b/src/pcie.c index 2e13db1ea..9326d9750 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -1225,7 +1225,7 @@ static const u8 t6050_ans_on[] = {6, 5, 4, 1, 3, 0, 2}; /* * T8142 (J704/J813/J815: same gates, ids and parents in their 27.2.1 ADTs), the storage half of - * AppleT8142PCIe's power-down: ST0_AUS5 then ST0_AUS5_AON (its CLK0REQ-down step), then of its + * the traced macOS power-down: ST0_AUS5 then ST0_AUS5_AON (its CLK0REQ-down step), then of its * gate loop the ones on ANS's side, APCIE_SYS_ST, APCIE_ST and ANS. Its power-up raises the gates * in ADT order, which is the reverse. APCIE_GP, APCIE_SYS_GP and APCIE_PHY_SW stay up for N1 (and * J704's second port): macOS leaves PHY_SW on with APCIE_SYS_ST off too. From 5aa1f7f0f8a30861e5dec9f4600eb9f4421ff43e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:44:38 +1100 Subject: [PATCH 13/29] pcie: stop the ANS power-off if a PHY clock does not drop pcie_ans_power_off()'s whole-block path (post-M4 SoCs without a storage gate table: T8132, T8140, T6040, T6041) drops each PHY's CLK1 and CLK0 requests and polls for the acknowledges to fall, but ignored the poll result and powered the gates off regardless, the same way whether the PHY let go of its clocks or not. Check both polls. On a timeout, log the PHY control value and return -1 before any gate is touched; nvme already treats a failed ANS power-off as fatal for this boot ("stays down until a reboot") rather than continuing on a half-stopped PHY. Signed-off-by: Ryan Murray --- src/pcie.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/pcie.c b/src/pcie.c index 9326d9750..5384e18ad 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -577,9 +577,17 @@ static int pcie_init_controller(int controller, const char *path, bool ans_off) u64 ctrl = state->phy_base[phy] + APCIE_PHY_CTRL; set32(ctrl, state->pcie_regs->phy_ctrl_reset); clear32(ctrl, APCIE_PHY_CTRL_CLK1REQ); - poll32(ctrl, APCIE_PHY_CTRL_CLK1ACK, 0, 50000); + if (poll32(ctrl, APCIE_PHY_CTRL_CLK1ACK, 0, 50000)) { + printf("pcie: Timeout dropping PHY %d CLK1 (0x%x); not powering ANS off\n", phy, + read32(ctrl)); + return -1; + } clear32(ctrl, APCIE_PHY_CTRL_CLK0REQ); - poll32(ctrl, APCIE_PHY_CTRL_CLK0ACK, 0, 50000); + if (poll32(ctrl, APCIE_PHY_CTRL_CLK0ACK, 0, 50000)) { + printf("pcie: Timeout dropping PHY %d CLK0 (0x%x); not powering ANS off\n", phy, + read32(ctrl)); + return -1; + } /* PHY control keeps its value through the power cycle: take the PHY down whole. */ clear32(ctrl, pcie_phy_up(state)); clear32(state->phy_base[phy] + 4, 0x10); From f2a6f909bd61bf782264c893cc8c9fe8e787e878 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:45:16 +1100 Subject: [PATCH 14/29] j873: poll for the eUSB2 PHY's UTMI clock and name its settle times j873_usb_phy_host() slept 1 ms and then read the UTMI clock event counter once to decide whether the PHY started. The counter is the readiness signal: poll it for up to that same 1 ms and go on as soon as it moves, failing as before if it never does. The three remaining waits (10 ms power-up settle, 1 ms after the PHY reset release, 5 ms before the mode switch) have no status bit we know of; name them, with a pointer to the tracking issue, instead of leaving bare numbers. Signed-off-by: Ryan Murray --- src/j873.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/j873.c b/src/j873.c index 4a5a136eb..a5b9e0158 100644 --- a/src/j873.c +++ b/src/j873.c @@ -24,6 +24,17 @@ #define POLL_US 1000000 +/* + * eUSB2 PHY bring-up waits with no ready bit known, the times of the traced sequence (tracking + * issue: find the PHY's status bits): power-up settle, PHY reset release to the UTMI clock request, + * and UTMI clock up to the PHY mode switch. The UTMI clock itself is polled (evt + 0x20 counts its + * events), within the 1 ms the sequence allows it. + */ +#define EUSB2_POWER_SETTLE_MS 10 +#define EUSB2_RESET_SETTLE_US 1000 +#define EUSB2_UTMI_CLK_US 1000 +#define EUSB2_MODE_SETTLE_MS 5 + struct j873_usb { u64 aon, bank, gate, dart0, dart1, pipe, phy, evt, dwc; const char *hpm; @@ -137,21 +148,24 @@ static int j873_usb_phy_host(const struct j873_usb *u) mask32(u->phy + 0x14, 0x7f000000, 0x23000000); mask32(u->phy + 0x18, 0x703, 0x103); mask32(u->phy + 8, 0x7003, 0x7003); - mdelay(10); + mdelay(EUSB2_POWER_SETTLE_MS); mask32(u->phy + 4, 8, 0); - udelay(1000); + udelay(EUSB2_RESET_SETTLE_US); mask32(u->phy + 4, 1, 0); mask32(u->phy + 4, 2, 0); mask32(u->evt, 0, 9); mask32(u->phy + 4, 0, 4); mask32(u->phy + 0x1c, 0x20000000, 0); mask32(u->phy + 0x1c, 0x40000000, 0); - udelay(1000); - if (!read32(u->evt + 0x20)) { - printf("j873: USB: no UTMI clock events, PHY did not start\n"); - return -1; + u64 timeout = timeout_calculate(EUSB2_UTMI_CLK_US); + while (!read32(u->evt + 0x20)) { + if (timeout_expired(timeout)) { + printf("j873: USB: no UTMI clock events, PHY did not start\n"); + return -1; + } + udelay(10); } - mdelay(5); + mdelay(EUSB2_MODE_SETTLE_MS); mask32(u->phy, 7, 2); write32(u->pipe + 0x1c, 1); mask32(u->pipe + 0x20, 0, 0x8000); From 37d6e7689bffff970b4976406b9a082e3d96529a Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:22:54 +1100 Subject: [PATCH 15/29] kboot: remove a speaker amp group's configuration when its DT update fails part way kboot_audio hands each SN012776 amp group's configuration (brownout prevention, DC blocker, per-amp speaker and sense slot rows) to every amp of the group, and documents that the kernel gets either the whole group's configuration or none of it. That held for a group whose amps could not be matched, but not for a DT write that failed part way (no room left in the tree): the amps written before the failure kept the new properties, the rest did not, and the amps of one speaker group were set up differently. On a failed write, remove every one of these properties from all of the group's amps again (removing a property never needs room in the tree) and say so. A group that is written in full is unchanged. The host test gains a sweep over the free space left in the tree: with the old code 0 bytes free leaves a partial group; now each of 129 sizes gives either the whole group (116) or none of it (13). Signed-off-by: Ryan Murray --- src/kboot_audio.c | 30 ++++++++++++++++++++++++++---- tests/kboot_audio_host.c | 7 +++++-- tests/run-kboot-audio.sh | 23 +++++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/kboot_audio.c b/src/kboot_audio.c index 1e802cfbc..da8110dd1 100644 --- a/src/kboot_audio.c +++ b/src/kboot_audio.c @@ -21,8 +21,9 @@ * A property the group does not have (or one without a row for every amp) is * removed from its amps, so the kernel never sees a value that did not come from this Mac. Amps * are matched to the DT's ti,sn012776 nodes by I2C address, which must name exactly one node. A - * group with an amp that cannot be matched is left as it is: the kernel gets either the whole - * group's configuration or none of it. Nothing here stops the boot. + * group with an amp that cannot be matched is left as it is, and a group whose DT update fails + * part way has every one of these properties removed from all of its amps again: the kernel gets + * either the whole group's configuration or none of it. Nothing here stops the boot. */ #include "kboot_audio.h" @@ -128,6 +129,21 @@ static int dt_amp_setprop(void *dt, u32 addr, const char *name, const void *val, return 0; } +/* + * Remove every property this file hands over from the group's amps. Used when a DT update fails + * part way, so that no amp keeps a configuration its group-mates did not get. Removing a + * property never needs space in the tree, so this cannot fail the way the update did. + */ +static void amp_group_clear(void *dt, const u32 *addrs, int n) +{ + for (int i = 0; i < n; i++) { + for (size_t p = 0; p < ARRAY_SIZE(amp_group_props); p++) + dt_amp_setprop(dt, addrs[i], amp_group_props[p].dt, NULL, 0); + for (size_t p = 0; p < ARRAY_SIZE(amp_row_props); p++) + dt_amp_setprop(dt, addrs[i], amp_row_props[p].dt, NULL, 0); + } +} + static int setup_amp_group(void *dt, const void *adt, int arm_io, int group) { const char *name = adt_get_name(adt, group); @@ -181,7 +197,7 @@ static int setup_amp_group(void *dt, const void *adt, int arm_io, int group) for (int i = 0; i < n; i++) { if (dt_amp_setprop(dt, addrs[i], amp_group_props[p].dt, val, len)) - return -1; + goto fail; } } @@ -199,12 +215,18 @@ static int setup_amp_group(void *dt, const void *adt, int arm_io, int group) for (int i = 0; i < n; i++) { if (dt_amp_setprop(dt, addrs[i], amp_row_props[p].dt, rows ? rows + i * row : NULL, row)) - return -1; + goto fail; } } printf("kboot: audio: %s: amp configuration for %d amps\n", name, n); return 0; + +fail: + amp_group_clear(dt, addrs, n); + printf("kboot: audio: %s: DT update failed, amp configuration removed from all %d amps\n", name, + n); + return -1; } int kboot_audio_amp_config(void *dt, const void *adt) diff --git a/tests/kboot_audio_host.c b/tests/kboot_audio_host.c index b1018f1ef..0935f7e60 100644 --- a/tests/kboot_audio_host.c +++ b/tests/kboot_audio_host.c @@ -143,8 +143,11 @@ int main(int argc, char **argv) } adt = slurp(argv[1], 0, &asz); adt_size = asz; - dt = slurp(argv[2], 64 * 1024, &dsz); - if (fdt_open_into(dt, dt, dsz + 64 * 1024)) { + /* Free space in the tree for the code under test; a small value makes its updates fail */ + const char *spare_env = getenv("KBOOT_AUDIO_DT_SPARE"); + size_t spare = spare_env ? strtoul(spare_env, NULL, 0) : 64 * 1024; + dt = slurp(argv[2], spare, &dsz); + if (fdt_open_into(dt, dt, dsz + spare)) { fprintf(stderr, "bad dtb\n"); return 2; } diff --git a/tests/run-kboot-audio.sh b/tests/run-kboot-audio.sh index 379c8a27b..ee5c092d8 100644 --- a/tests/run-kboot-audio.sh +++ b/tests/run-kboot-audio.sh @@ -189,4 +189,27 @@ for s in $(python3 -I tests/kboot_audio_adt.py scenarios); do n=$((n + 1)) done [ "$n" -gt 0 ] || fail "no scenarios ran" + +# A tree too full for the whole group: whatever free space the update runs out at, every amp must +# end up with the whole group's configuration or with none of it, never a mix. +none() { amp 40 - - - -; amp 42 - - - -; amp 41 - - - -; } +expect group > "$tmp/full.want" +none > "$tmp/none.want" +full=0 empty=0 +for spare in $(seq 0 16 2048); do + KBOOT_AUDIO_DT_SPARE=$spare KBOOT_AUDIO_GPIO=$(gpio_env group) "$tmp/kboot-audio-host" \ + "$tmp/group.adt" "$tmp/group.dtb" > "$tmp/spare.out" 2>&1 || true + grep -E '^amp ' "$tmp/spare.out" > "$tmp/spare.got" || true + if cmp -s "$tmp/spare.got" "$tmp/full.want"; then + full=$((full + 1)) + elif cmp -s "$tmp/spare.got" "$tmp/none.want"; then + empty=$((empty + 1)) + else + cat "$tmp/spare.out" + fail "dt-full: $spare bytes free left a partial group configuration" + fi +done +[ "$full" -gt 0 ] && [ "$empty" -gt 0 ] || fail "dt-full: sweep did not cover both outcomes ($full full, $empty none)" +echo "ok dt-full: $full whole, $empty none, no partial group" +n=$((n + 1)) echo "kboot audio: $n scenarios pass" From 364b9d9f9a8ec05fd42127775bd5abcb85294d5a Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 20 Nov 2026 03:39:21 +1100 Subject: [PATCH 16/29] tests: build the T6030 display host test against today's kboot_atc.c kboot_atc.c now reads #address-cells/#size-cells through libfdt's fdt_address_cells()/fdt_size_cells() and matches ADT compatibles with adt_is_compatible(). The host test linked neither, so it no longer built; nothing noticed because no CI job runs it. Link libfdt's fdt_addresses.c and add an adt_is_compatible() stand-in that matches any entry of the compatible list, as aura's ADT reader does. All checks pass again with gcc and clang. Signed-off-by: Ryan Murray --- tests/run-t6030-display.sh | 2 +- tests/t6030_display_host.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/run-t6030-display.sh b/tests/run-t6030-display.sh index ab88811e7..67468ce08 100755 --- a/tests/run-t6030-display.sh +++ b/tests/run-t6030-display.sh @@ -27,7 +27,7 @@ build() { # build -I"$out/src" -I"$root/src" -I"$root" \ "$out/src/t6030_display_host.c" "$out/src/kboot_atc.c" "$out/src/kboot_display_facts.c" \ "$root"/src/libfdt/fdt.c "$root"/src/libfdt/fdt_ro.c "$root"/src/libfdt/fdt_rw.c \ - "$root"/src/libfdt/fdt_wip.c "$root"/src/libfdt/fdt_sw.c \ + "$root"/src/libfdt/fdt_wip.c "$root"/src/libfdt/fdt_sw.c "$root"/src/libfdt/fdt_addresses.c \ "$root"/src/libfdt/fdt_empty_tree.c "$root"/src/libfdt/fdt_strerror.c \ -Wl,--wrap=malloc -o "$out/t6030-display-host" } diff --git a/tests/t6030_display_host.c b/tests/t6030_display_host.c index deb9367de..1eb1c2782 100644 --- a/tests/t6030_display_host.c +++ b/tests/t6030_display_host.c @@ -337,6 +337,24 @@ bool adt_is_compatible_at(const void *a, int node, const char *compat, size_t in return false; } +/* Any entry of the compatible list, as aura's ADT reader (rust/src/adt.rs) matches it */ +bool adt_is_compatible(const void *a, int node, const char *compat) +{ + u32 len = 0; + const char *c = adt_getprop(a, node, "compatible", &len); + + while (c && len) { + size_t n = strnlen(c, len); + if (n == strlen(compat) && !memcmp(c, compat, n)) + return true; + if (n + 1 > len) + break; + c += n + 1; + len -= n + 1; + } + return false; +} + /* Child lookup by name; a miss past the last child is an error, not a panic */ static int child_named(int node, const char *name, size_t n) { From f292cfe05c160fe0e149e69607a9fdfab3362447 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 20 Nov 2026 03:39:21 +1100 Subject: [PATCH 17/29] ci: run the host tests that need no files from a Mac Eight tests/run-*.sh host tests (speaker amp configuration and speaker ID, logo, payload profile, T6030 display facts, T8122 PMP, full start and GPU diagnostics, and the M3 start.S move gate) were never run by CI, and one of them had stopped building unnoticed. Run them on the ubuntu-latest jobs, with dtc and qemu-user installed; the start.S move gate runs on the LLVM job, which has clang and the LLVM binutils. run-smbios.sh stays out: it needs a Mac's ADT and DTB. Signed-off-by: Ryan Murray --- .github/workflows/test.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3f8bf7980..74b6f4a12 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -85,6 +85,29 @@ jobs: CC: ${{ matrix.compiler == 'llvm' && 'clang' || 'gcc' }} run: tests/run-t8122-gpu26-identity.sh + - name: Install host test tools + if: matrix.os == 'ubuntu-latest' + run: | + sudo apt-get update + sudo apt-get install --no-install-recommends -y device-tree-compiler qemu-user-static + + # The host tests of tests/run-*.sh that need no files from a Mac + - name: Run host tests + if: matrix.os == 'ubuntu-latest' + env: + CC: ${{ matrix.compiler == 'llvm' && 'clang' || 'gcc' }} + run: | + for t in kboot-audio logo payload-profile t6030-display t8122-air-pmp t8122-full-start \ + t8122-gpu-diagnostics; do + echo "::group::run-$t" + sh "tests/run-$t.sh" + echo "::endgroup::" + done + + - name: Run the M3 start.S move gate under qemu + if: matrix.os == 'ubuntu-latest' && matrix.compiler == 'llvm' + run: PATH="$(llvm-config --prefix)/bin:$PATH" sh tests/run-m3-move-gate.sh + arm64-native: name: Pytest (ubuntu-24.04-arm, native start.S) runs-on: ubuntu-24.04-arm From 4a3d9dd34216c74f30f9bfda163b21949d648d86 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 03:40:05 +1100 Subject: [PATCH 18/29] dptx-phy: T8152: fail activation when calibration does not complete The T8152 HDMI phy activation logged an impedance calibration timeout and then programmed the termination from 0x1c24 anyway, which holds no calibration result until the completion bit is set. A missing phy node likewise skipped every tunable and carried on. Poll the completion bit with poll32() and a named 1 s bound (the old loop's 100000 x 10 us), and return an error from both cases so DPTX_APCALL_ACTIVATE reports kIOReturnError instead of training a link on an uncalibrated phy. Signed-off-by: Ryan Murray --- src/dcp/dptx_phy.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/dcp/dptx_phy.c b/src/dcp/dptx_phy.c index d2dd33fd6..ce439d8e9 100644 --- a/src/dcp/dptx_phy.c +++ b/src/dcp/dptx_phy.c @@ -59,12 +59,12 @@ static const struct dptx_t8152_tunable dptx_t8152_tunables[] = { /* Every ADT tunable record is {offset, width, mask, pad, value, pad} and is * applied as an unshifted read-modify-write. */ -static void dptx_t8152_apply_tunables(dptx_phy_t *phy) +static int dptx_t8152_apply_tunables(dptx_phy_t *phy) { int node = adt_path_offset(adt, phy->path); if (node < 0) { printf("DPTX-PHY: failed to find %s for tunables\n", phy->path); - return; + return -1; } for (size_t i = 0; i < ARRAY_SIZE(dptx_t8152_tunables); i++) { @@ -81,6 +81,7 @@ static void dptx_t8152_apply_tunables(dptx_phy_t *phy) write32(phy->regs[0] + tunable->offset + rec[0], (reg & ~rec[2]) | rec[4]); } } + return 0; } /* Per-lane configuration on the lane shm blocks. The bits are lane-disable @@ -204,23 +205,27 @@ static int dptx_t8152_set_link_rate(dptx_phy_t *phy, u32 link_rate) } /* Impedance calibration: the adapter reports completion in bit 0 of 0x1c04 - * and the calibration result in 0x1c24. */ -static void dptx_t8152_impedance_calibration(dptx_phy_t *phy) + * and the calibration result in 0x1c24. Without the completion bit 0x1c24 + * holds no calibration result, so a timeout fails the activation instead of + * programming the termination from it. */ +#define DPTX_T8152_IMPCAL_STATUS 0x1c04 +#define DPTX_T8152_IMPCAL_DONE BIT(0) +#define DPTX_T8152_IMPCAL_TIMEOUT_US 1000000 + +static int dptx_t8152_impedance_calibration(dptx_phy_t *phy) { - u32 i; - - for (i = 0; i < 100000; i++) { - if (read32(phy->regs[0] + 0x1c04) & 1) - break; - udelay(10); + if (poll32(phy->regs[0] + DPTX_T8152_IMPCAL_STATUS, DPTX_T8152_IMPCAL_DONE, + DPTX_T8152_IMPCAL_DONE, DPTX_T8152_IMPCAL_TIMEOUT_US)) { + printf("DPTX-PHY: impedance calibration timed out (status %#x)\n", + read32(phy->regs[0] + DPTX_T8152_IMPCAL_STATUS)); + return -1; } - if (i == 100000) - printf("DPTX-PHY: impedance calibration timed out\n"); dptx_t8152_set_bits(phy, 0x1c00, 4, 0x3f0, 0x3f); u32 value = read32(phy->regs[0] + 0x1c24); dptx_t8152_set_bits(phy, 0x1c18, 0xf, 0x3f8000, (value >> 6) & 0x7f); dptx_t8152_set_bits(phy, 0x1c18, 0xe, 0x4000, 1); + return 0; } /* T8152 activation: power up, apply the adapter tunables from the devicetree, @@ -244,7 +249,8 @@ static int dptx_t8152_activate(dptx_phy_t *phy) set32(phy->regs[1] + 0x48, 0x002); clear32(phy->regs[1] + 0x48, 0x004); - dptx_t8152_apply_tunables(phy); + if (dptx_t8152_apply_tunables(phy)) + return -1; /* PLL: single-ended clock output, no pclkdrv-select property */ dptx_t8152_set_bits(phy, 0x08d0, 4, 0x10, 1); @@ -259,9 +265,7 @@ static int dptx_t8152_activate(dptx_phy_t *phy) return ret; /* T8152 needs no separate AUX power-up or termination override */ - dptx_t8152_impedance_calibration(phy); - - return 0; + return dptx_t8152_impedance_calibration(phy); } int dptx_phy_activate(dptx_phy_t *phy) From d4e77e9dd04ef6b8ab51df6e5dfd20a87152685a Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 03:40:05 +1100 Subject: [PATCH 19/29] dcp: name the T8152 ATC and dptx-phy settle delays The T8152 USB-C (ATC) and HDMI (dptx-phy) bring-up waits between control steps with bare udelay()/mdelay() literals. None of these steps has a known status bit; the ones that do (CFG status, crossbar acks, PSD, PLL ack/lock, impedance calibration) are already polled. Name each wait and say what it separates: ATC_STEP_DELAY_US for the clamp/power/isolation/sleep steps, ATC_DPTX_DISABLE_SETTLE_US, ATC_ALT_MODE_POLL_MS for the alternate-mode poll interval, and ATC_ROUTE_SETTLE_MS and DPTX_T8152_POWER_SETTLE_MS, whose cause is not known and which carry tracking references. The lane enable loop waited twice in a row after the last step; that duplicate is dropped (2 us -> 1 us per lane). No other timing changes. Signed-off-by: Ryan Murray --- src/dcp/atc_phy.c | 70 +++++++++++++++++++++++++++++----------------- src/dcp/dptx_phy.c | 17 ++++++++--- 2 files changed, 57 insertions(+), 30 deletions(-) diff --git a/src/dcp/atc_phy.c b/src/dcp/atc_phy.c index 6283753c5..53f9ae0ae 100644 --- a/src/dcp/atc_phy.c +++ b/src/dcp/atc_phy.c @@ -18,9 +18,28 @@ #define ATC_XBAR 0x74000 #define ATC_TIMEOUT 10000 #define ATC_ALT_MODE_TIMEOUT 2000000 +#define ATC_ALT_MODE_POLL_MS 50 #define ATC_SAVED_MAX 192 #define ATC_PSD58 0x303a13a00ULL +/* Gap between consecutive PHY control steps that have no status bit: lane + * clamp, power and isolation fields in AON 0x1e18/0x1e1c, the PMA clock and + * AUX sleep requests in AON 0x24, the CFG clock, reset and sleep controls, + * and the crossbar enable writes ahead of their acknowledgement polls. Steps + * that do report completion are polled instead (CFG status, crossbar acks, + * PSD). */ +#define ATC_STEP_DELAY_US 1 + +/* Settle time after the DPTX enable in 0x2040 is withdrawn, before the + * transmitter control bits in 0x2000 are cleared. No status bit is known. */ +#define ATC_DPTX_DISABLE_SETTLE_US 5 + +/* Wait after the DCP0 -> DFP0 route is configured, before the DCP continues + * link training. The route start already polls every crossbar acknowledgement; + * what this additionally waits for is not known. Kept as qualified on J873g + * (tracking: aura#344). */ +#define ATC_ROUTE_SETTLE_MS 5 + struct atc_saved { u32 offset; u32 value; @@ -307,7 +326,7 @@ atc_phy_t *atc_phy_init(const char *path) (data_status & TPS6598X_DATA_DP_CONNECTION)) || timeout_expired(deadline)) break; - mdelay(50); + mdelay(ATC_ALT_MODE_POLL_MS); } tps6598x_shutdown(tps); spmi_shutdown(spmi); @@ -382,7 +401,7 @@ static int atc_route_stop(atc_phy_t *phy) if (atc_update(phy, ATC_XBAR + 0x3c, 1, 0) || atc_update(phy, ATC_XBAR, 1, 0) || atc_update(phy, ATC_XBAR + 0xc, 1, 0) || atc_update(phy, ATC_XBAR + 0x24, 0x100, 0)) return -1; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (poll32(phy->base + ATC_XBAR + 0x800, 1, 0, ATC_TIMEOUT) || poll32(phy->base + ATC_XBAR + 0x808, 1, 0, ATC_TIMEOUT) || poll32(phy->base + ATC_XBAR + 0x81c, 0x100, 0, ATC_TIMEOUT)) { @@ -426,7 +445,7 @@ static int atc_route_start(atc_phy_t *phy) atc_update(phy, ATC_XBAR + 0x44, 0x00000f00, 0) || atc_update(phy, ATC_XBAR + 4, 1, 0) || atc_update(phy, ATC_XBAR + 0x1c, 1, 0) || atc_update(phy, ATC_XBAR + 0x34, 0x100, 0)) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (poll32(base + 0x804, 1, 0, ATC_TIMEOUT) || poll32(base + 0x818, 1, 0, ATC_TIMEOUT) || poll32(base + 0x82c, 0x100, 0, ATC_TIMEOUT)) { printf("ATC3: crossbar reset acknowledgement timeout\n"); @@ -459,7 +478,7 @@ static int atc_dp_stop(atc_phy_t *phy) } if (atc_update(phy, 0x2040, BIT(16), 0)) return -1; - udelay(5); + udelay(ATC_DPTX_DISABLE_SETTLE_US); if (atc_update(phy, 0x2000, BIT(15), 0) || atc_update(phy, 0x2000, BIT(13), 0) || atc_update(phy, 0x2000, BIT(14), 0) || atc_update(phy, 0x2000, BIT(2), 0) || atc_update(phy, 0x2000, BIT(3), BIT(3)) || atc_update(phy, 0xe10, BIT(2), BIT(2)) || @@ -482,20 +501,20 @@ static int atc_dp_stop(atc_phy_t *phy) for (unsigned int i = 0; i < ARRAY_SIZE(requests); i++) if (atc_update(phy, ATC_AON + 0x24, requests[i], 0)) return -1; - udelay(1); + udelay(ATC_STEP_DELAY_US); for (u32 offset = 0x1e18; offset <= 0x1e1c; offset += 4) { if (atc_update(phy, offset, BIT(5), 0)) return -1; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, offset, BIT(0), BIT(0))) return -1; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, offset, BIT(2), 0)) return -1; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, offset, BIT(1), 0)) return -1; - udelay(1); + udelay(ATC_STEP_DELAY_US); } phy->dp_started = false; phy->link_rate = 0; @@ -513,7 +532,7 @@ int atc_phy_set_route(atc_phy_t *phy, bool enable) return -1; int ret = atc_route_start(phy); if (!ret) - mdelay(5); + mdelay(ATC_ROUTE_SETTLE_MS); return ret; } @@ -530,16 +549,16 @@ int atc_phy_deactivate(atc_phy_t *phy) if (phy->aux_started) { u64 aux = phy->base + ATC_AON + 0x24; set32(aux, BIT(10)); - udelay(1); + udelay(ATC_STEP_DELAY_US); clear32(aux, BIT(6)); - udelay(1); + udelay(ATC_STEP_DELAY_US); clear32(aux, BIT(8)); - udelay(1); + udelay(ATC_STEP_DELAY_US); phy->aux_started = false; } /* Assert main reset before restoring volatile setup. */ clear32(cfg, BIT(4)); - udelay(1); + udelay(ATC_STEP_DELAY_US); while (phy->saved_count) { struct atc_saved *s = &phy->saved[--phy->saved_count]; if (atc_restore(phy, s)) @@ -585,7 +604,7 @@ int atc_phy_activate(atc_phy_t *phy) goto fail; phy->started = true; set32(cfg + 0x30, BIT(0)); - udelay(1); + udelay(ATC_STEP_DELAY_US); set32(cfg, BIT(0)); if (poll32(cfg + 4, BIT(0), BIT(0), ATC_TIMEOUT)) goto fail; @@ -593,7 +612,7 @@ int atc_phy_activate(atc_phy_t *phy) if (poll32(cfg + 4, BIT(1), BIT(1), ATC_TIMEOUT)) goto fail; clear32(cfg, BIT(2)); - udelay(1); + udelay(ATC_STEP_DELAY_US); set32(cfg, BIT(3)); for (unsigned int i = 0; i < 2; i++) @@ -616,13 +635,13 @@ int atc_phy_activate(atc_phy_t *phy) phy->aux_started = true; if (atc_update(phy, ATC_AON + 0x24, BIT(8), BIT(8))) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, ATC_AON + 0x24, BIT(6), BIT(6))) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, ATC_AON + 0x24, BIT(10), 0)) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); phy->active = true; printf("ATC3: common PHY and AUX initialized, %u registers saved\n", phy->saved_count); return 0; @@ -696,24 +715,23 @@ int atc_phy_set_link_rate(atc_phy_t *phy, u32 rate) for (u32 i = 0; i < 2; i++) { if (atc_update(phy, ATC_AON + 0x24, BIT(12 + 2 * i), BIT(12 + 2 * i))) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, ATC_AON + 0x24, BIT(16 + 2 * i), BIT(16 + 2 * i))) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); u32 offset = 0x1e18 + 4 * i; if (atc_update(phy, offset, BIT(1), BIT(1))) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, offset, BIT(2), BIT(2))) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, offset, BIT(0), 0)) goto fail; - udelay(1); + udelay(ATC_STEP_DELAY_US); if (atc_update(phy, offset, BIT(5), BIT(5))) goto fail; - udelay(1); - udelay(1); + udelay(ATC_STEP_DELAY_US); } if (atc_update(phy, 0x2000, 0x380000, r << 19)) goto fail; diff --git a/src/dcp/dptx_phy.c b/src/dcp/dptx_phy.c index ce439d8e9..ca42e5efb 100644 --- a/src/dcp/dptx_phy.c +++ b/src/dcp/dptx_phy.c @@ -228,6 +228,15 @@ static int dptx_t8152_impedance_calibration(dptx_phy_t *phy) return 0; } +/* Settle time after each power-enable request in regs[1] + 0x48 and after + * turning the PLL and PHY on. No ready or acknowledge bit for these requests + * is known; the T8112/T602x sequence in dptx_phy_activate() writes the same + * 0x48 bits with no wait at all. Kept as qualified on J873g HDMI until a + * status bit is found or the sequence is shown to work without it (tracking: + * aura#344). PLL lock and impedance calibration are + * polled separately. */ +#define DPTX_T8152_POWER_SETTLE_MS 1 + /* T8152 activation: power up, apply the adapter tunables from the devicetree, * then activate the PLL, PHY and AUX. */ static int dptx_t8152_activate(dptx_phy_t *phy) @@ -237,15 +246,15 @@ static int dptx_t8152_activate(dptx_phy_t *phy) /* PLL, AUX and PHY power enables */ set32(phy->regs[1] + 0x48, 0x010); - mdelay(1); + mdelay(DPTX_T8152_POWER_SETTLE_MS); set32(phy->regs[1] + 0x48, 0x020); clear32(phy->regs[1] + 0x48, 0x040); set32(phy->regs[1] + 0x48, 0x100); - mdelay(1); + mdelay(DPTX_T8152_POWER_SETTLE_MS); set32(phy->regs[1] + 0x48, 0x200); clear32(phy->regs[1] + 0x48, 0x400); set32(phy->regs[1] + 0x48, 0x001); - mdelay(1); + mdelay(DPTX_T8152_POWER_SETTLE_MS); set32(phy->regs[1] + 0x48, 0x002); clear32(phy->regs[1] + 0x48, 0x004); @@ -258,7 +267,7 @@ static int dptx_t8152_activate(dptx_phy_t *phy) dptx_t8152_set_bits(phy, 0x1400, 0, 0x01, 0); /* AUX common control */ dptx_t8152_set_bits(phy, 0x1c00, 4, 0x3f0, 0x3c); /* PHY on */ dptx_t8152_set_bits(phy, 0x0000, 0, 0x01, 1); - mdelay(1); + mdelay(DPTX_T8152_POWER_SETTLE_MS); int ret = dptx_phy_set_active_lane_count(phy, DPTX_MAX_LANES); if (ret) From 40803bd5048e0c5abd664c4245cc57e765439b00 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 03:40:05 +1100 Subject: [PATCH 20/29] dcp: dptx-port: name the IOReturn codes and the virtual device call The DPTX port service replies used 22 bare 0xe00002xx literals, only some of them commented. Name the four codes after the public IOKit IOReturn.h values and give apcall 26 an enum entry instead of a bare case label. No functional change. Signed-off-by: Ryan Murray --- src/dcp/dptx_port_ep.c | 53 ++++++++++++++++++++++++------------------ src/dcp/dptx_port_ep.h | 1 + 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/dcp/dptx_port_ep.c b/src/dcp/dptx_port_ep.c index 2e76a558d..852f8aafd 100644 --- a/src/dcp/dptx_port_ep.c +++ b/src/dcp/dptx_port_ep.c @@ -19,6 +19,13 @@ #define TXBUF_LEN 0x4000 #define RXBUF_LEN 0x4000 +/* IOReturn status codes for EPIC service call replies, from the public IOKit + * IOReturn.h (sys_iokit | sub_iokit_common | code). */ +#define IORETURN_ERROR 0xe00002bc /* kIOReturnError */ +#define IORETURN_BAD_ARGUMENT 0xe00002c2 /* kIOReturnBadArgument */ +#define IORETURN_UNSUPPORTED 0xe00002c7 /* kIOReturnUnsupported */ +#define IORETURN_NOT_READY 0xe00002d8 /* kIOReturnNotReady */ + struct dcpdptx_connection_cmd { u32 unk; u32 target; @@ -537,7 +544,7 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, size_t data_size, void *reply, size_t reply_size) { if (group && !(group == 1 && command == DPTX_APCALL_SET_DRIVE_SETTINGS)) - return 0xe00002c7; /* kIOReturnUnsupported */ + return IORETURN_UNSUPPORTED; dptx_port_t *port = service->cookie; dprintf("DPTXV2: port %d group %u cmd %u size %zu\n", port ? (int)port->unit : -1, group, @@ -549,25 +556,25 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, command == DPTX_APCALL_SET_DOWN_SPREAD || command == DPTX_APCALL_SET_ACTIVE_LANE_COUNT || command == DPTX_APCALL_SET_DRIVE_SETTINGS || command == DPTX_APCALL_GET_LINK_RATE || command == DPTX_APCALL_GET_ACTIVE_LANE_COUNT)) - return 0xe00002d8; /* kIOReturnNotReady */ + return IORETURN_NOT_READY; /* Only the hardware-qualified HBR/four-lane configuration is exposed. * Training changes still require their own implementation. */ u64 value; switch (command) { case DPTX_APCALL_ACTIVATE: if (dptx_phy_activate(port->phy)) - return 0xe00002bc; /* kIOReturnError */ + return IORETURN_ERROR; port->link_rate = port->pending_link_rate = LINK_RATE_HBR; return 0; case DPTX_APCALL_DEACTIVATE: if (dptx_phy_deactivate(port->phy)) - return 0xe00002bc; + return IORETURN_ERROR; port->link_rate = port->pending_link_rate = 0; return 0; case DPTX_APCALL_WILL_CHANGE_LINKG_CONFIG: case DPTX_APCALL_DID_CHANGE_LINK_CONFIG: return dptx_phy_set_route(port->phy, command == DPTX_APCALL_DID_CHANGE_LINK_CONFIG) - ? 0xe00002bc + ? IORETURN_ERROR : 0; case DPTX_APCALL_GET_MAX_LINK_RATE: value = port->phy ? dptx_phy_max_link_rate(port->phy) / 270 : LINK_RATE_HBR; @@ -578,16 +585,16 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, case DPTX_APCALL_SET_LINK_RATE: { u8 rate; if (!data || data_size < 17) - return 0xe00002c2; + return IORETURN_BAD_ARGUMENT; rate = ((const u8 *)data)[16]; if (rate * 270 > dptx_phy_max_link_rate(port->phy)) - return 0xe00002c7; + return IORETURN_UNSUPPORTED; /* A phy qualified only at HBR keeps that single rate. */ if (dptx_phy_max_link_rate(port->phy) == 2700 && (rate != LINK_RATE_HBR || port->link_rate != LINK_RATE_HBR)) - return 0xe00002c7; + return IORETURN_UNSUPPORTED; if (dptx_phy_set_link_rate(port->phy, rate * 270)) - return 0xe00002bc; + return IORETURN_ERROR; port->link_rate = rate; return 0; } @@ -600,15 +607,15 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, case DPTX_APCALL_SET_ACTIVE_LANE_COUNT: { u32 count; if (!data || data_size < 20) - return 0xe00002c2; + return IORETURN_BAD_ARGUMENT; memcpy(&count, data + 16, sizeof(count)); printf("DPTXV2: active lane count %u\n", count); - return dptx_phy_set_active_lane_count(port->phy, count) ? 0xe00002c2 : 0; + return dptx_phy_set_active_lane_count(port->phy, count) ? IORETURN_BAD_ARGUMENT : 0; } case DPTX_APCALL_GET_MAX_DRIVE_SETTINGS: { const dptx_drive_settings_t maximum = {0, 3, 3}; if (reply_size < 32) - return 0xe00002c2; + return IORETURN_BAD_ARGUMENT; memcpy(reply + 16, &maximum, sizeof(maximum)); return 0; } @@ -616,25 +623,25 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, u32 count; dptx_drive_settings_t settings[4]; if (group != 1) - return 0xe00002c7; + return IORETURN_UNSUPPORTED; if (!data || data_size < 32) - return 0xe00002c2; + return IORETURN_BAD_ARGUMENT; memcpy(&count, data + 16, sizeof(count)); if (count > 4 || data_size < 32 + count * sizeof(*settings)) - return 0xe00002c2; + return IORETURN_BAD_ARGUMENT; memcpy(settings, data + 32, count * sizeof(*settings)); - return dptx_phy_set_drive_settings(port->phy, settings, count) ? 0xe00002bc : 0; + return dptx_phy_set_drive_settings(port->phy, settings, count) ? IORETURN_ERROR : 0; } case DPTX_APCALL_SET_DOWN_SPREAD: if (!data || data_size < 17) - return 0xe00002c2; + return IORETURN_BAD_ARGUMENT; /* Matching ATC port code records this request for the following * rate setup. The qualified setup uses downspread disabled. */ - return ((const u8 *)data)[16] ? 0xe00002c7 : 0; + return ((const u8 *)data)[16] ? IORETURN_UNSUPPORTED : 0; case DPTX_APCALL_GET_SUPPORTS_DOWN_SPREAD: case DPTX_APCALL_GET_DOWN_SPREAD: case DPTX_APCALL_GET_SUPPORTS_LANE_MAPPING: - case 26: /* GetVirtualDeviceMode: this is a physical display path. */ + case DPTX_APCALL_GET_VIRTUAL_DEVICE_MODE: /* A physical display path. */ value = 0; break; case DPTX_APCALL_GET_SUPPORTS_HPD: @@ -648,7 +655,7 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, * with a zeroed retcode; they are not requests we can answer. * Other machines keep answering them as unsupported. */ if (chip_id != T8152) - return 0xe00002c7; + return IORETURN_UNSUPPORTED; dprintf("DPTXPort: firmware reports link fault %u\n", command); memcpy(reply, data, min(reply_size, data_size)); if (reply_size > 4) @@ -656,17 +663,17 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, return 0; case DPTX_APCALL_SET_TILED_DISPLAY_HINTS: { if (chip_id != T8152) - return 0xe00002c7; + return IORETURN_UNSUPPORTED; u32 retcode = 1; memcpy(reply, data, min(reply_size, data_size)); memcpy(reply, &retcode, min(reply_size, sizeof(retcode))); return 0; } default: - return 0xe00002c7; + return IORETURN_UNSUPPORTED; } if (reply_size < 32) - return 0xe00002c2; /* kIOReturnBadArgument */ + return IORETURN_BAD_ARGUMENT; memcpy(reply + 16, &value, sizeof(value)); return 0; } diff --git a/src/dcp/dptx_port_ep.h b/src/dcp/dptx_port_ep.h index 2acd57653..a98f36129 100644 --- a/src/dcp/dptx_port_ep.h +++ b/src/dcp/dptx_port_ep.h @@ -38,6 +38,7 @@ enum dptx_apcall { DPTX_APCALL_DEVICE_NOT_RESPONDING = 22, DPTX_APCALL_DEVICE_BUSY_TIMEOUT = 23, DPTX_APCALL_DEVICE_NOT_STARTED = 24, + DPTX_APCALL_GET_VIRTUAL_DEVICE_MODE = 26, }; #define DCPDPTX_REMOTE_PORT_CORE GENMASK(3, 0) From 517eefe724c149a1f872062555e116522aa1b401 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 20 Nov 2026 03:49:20 +1100 Subject: [PATCH 21/29] dcp: describe the T8152 ATC steps without driver-internal names Some comments in atc_phy.c and dptx_port_ep.c named another driver's build, its internal tables and function names, and an attribute field name. Describe what each step does instead. Comment changes only. Signed-off-by: Ryan Murray --- src/dcp/atc_phy.c | 18 +++++++++--------- src/dcp/dptx_port_ep.c | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/dcp/atc_phy.c b/src/dcp/atc_phy.c index 53f9ae0ae..f2aa9d0e8 100644 --- a/src/dcp/atc_phy.c +++ b/src/dcp/atc_phy.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -/* T8152 ATC common/AUX initialization, derived from the matching 26A428 - * driver and qualified incrementally on J873g. DP lanes and crossbar clocks - * are separate stages; common initialization alone does not establish a link. */ +/* T8152 ATC common/AUX initialization, qualified incrementally on J873g. + * DP lanes and crossbar clocks are separate stages; common initialization + * alone does not establish a link. */ #include "atc_phy.h" #include "dptx_phy.h" #include "malloc.h" @@ -70,7 +70,7 @@ struct atc_tunable { bool optional; }; -/* _sRegisters table indices 4, 8, 2, 82, 6, 5, 78 respectively. */ +/* The ADT tunables applied by the common initialization, in order. */ static const struct atc_tunable tunables[] = { {"tunable_ACIOPHY_CMN_DFLT", 0x800, 0x6c, false}, {"tunable_CIOPLL_CORE_DFLT", 0x1600, 0x280, false}, @@ -414,8 +414,8 @@ static int atc_route_stop(atc_phy_t *phy) return -1; for (unsigned int i = phy->saved_count; i > 0; i--) { struct atc_saved *s = &phy->saved[i - 1]; - /* This output latch ignores an ordinary clear. The matching driver's - * teardown leaves it set; it is not a reversible configuration field. */ + /* This output latch ignores an ordinary clear and stays set after + * teardown; it is not a reversible configuration field. */ if (s->offset == ATC_XBAR + 0x4c) { s->mask = 0; continue; @@ -618,8 +618,8 @@ int atc_phy_activate(atc_phy_t *phy) for (unsigned int i = 0; i < 2; i++) if (atc_tunable_check(phy, i, true)) goto fail; - /* aciophy_xbar_config(mode=3): both PMAs carry DisplayPort. This is the - * PHY's lane mapping, not the DCP-to-ATC display crossbar. */ + /* PHY lane mapping: both PMAs carry DisplayPort. This is not the + * DCP-to-ATC display crossbar. */ if (atc_update(phy, 0x4c, 0x1f, 0x14) || atc_update(phy, 0x4c, 0x3ffe0, phy->swap ? 0x100 : 0x22000)) goto fail; @@ -631,7 +631,7 @@ int atc_phy_activate(atc_phy_t *phy) set32(cfg, BIT(4)); if ((read32(cfg) & 0x1f) != 0x1b) goto fail; - /* aciophy_aux_init: release small/big sleep, then AUX clamp. */ + /* AUX init: release small/big sleep, then AUX clamp. */ phy->aux_started = true; if (atc_update(phy, ATC_AON + 0x24, BIT(8), BIT(8))) goto fail; diff --git a/src/dcp/dptx_port_ep.c b/src/dcp/dptx_port_ep.c index 852f8aafd..8a07c69d3 100644 --- a/src/dcp/dptx_port_ep.c +++ b/src/dcp/dptx_port_ep.c @@ -193,7 +193,7 @@ int dptxport_connect(afk_epic_service_t *service, u8 core, u8 atc, u8 die) cmd.target = target; if (afk_epic_is_v2(service->epic)) - cmd.unk = 0x100; /* IODPTXPortAttributes supportsHPD */ + cmd.unk = 0x100; /* port attribute: HPD supported */ ret = afk_service_call(service, 0, 11, &cmd, sizeof(cmd), 24, &resp, sizeof(resp), 0); if (ret) return ret; From c88906db12ad060ef8cc7c634d90032cd0e1ad4e Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 20 Nov 2026 07:34:47 +1100 Subject: [PATCH 22/29] tests: give the PCIe sequence harness pcie.h's constants and the timeout helpers test_pcie_sequence.py compiles src/pcie.c without its includes against host stand-ins. pcie.c now names its reset waits in pcie.h and polls config readiness with timeout_calculate()/timeout_expired(), so the harness no longer built. Take the PCIE_/APCIE_ defines from pcie.h itself (so they cannot drift) and add max() and poll-counting timeout stand-ins. Signed-off-by: Ryan Murray --- tests/python/test_pcie_sequence.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/python/test_pcie_sequence.py b/tests/python/test_pcie_sequence.py index 3e8660ca4..e9aeca3b7 100644 --- a/tests/python/test_pcie_sequence.py +++ b/tests/python/test_pcie_sequence.py @@ -19,6 +19,9 @@ def test_t8140_sequence(tmp_path): source = (repo / "src/pcie.c").read_text() source = "\n".join(line for line in source.splitlines() if not line.startswith("#include")) + # pcie.c's own constants (the include above is dropped): take them from pcie.h so they can't drift + pcie_h = "\n".join(line for line in (repo / "src/pcie.h").read_text().splitlines() + if line.startswith("#define PCIE_") or line.startswith("#define APCIE_")) harness = r''' #include #include @@ -38,6 +41,12 @@ def test_t8140_sequence(tmp_path): #define T6050 0x6050 #define T8142 0x8142 #define ADT_GETPROP(a, n, p, v) adt_getprop_copy(a, n, p, v, sizeof(*(v))) +#define max(a, b) ((a) > (b) ? (a) : (b)) +/* Timeouts count polls, not time: a bound of N us allows N polls */ +static uint64_t timeout_calculate(uint64_t us) { return us; } +static uint64_t timeout_polls; +static bool timeout_expired(uint64_t timeout) { return ++timeout_polls > timeout; } +''' + pcie_h + r''' static void *adt; static int chip_id = T8140; static const char *controller_compat = "apcie,t8140"; From e6295e144991a241c1ff0ed4a2fa3161c3e872f2 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 20 Nov 2026 07:34:47 +1100 Subject: [PATCH 23/29] usb_cdc: keep the flag mask out of the aligned flag block USB_CDC_FLAGS_ALL is too long to align with the flag definitions above it, and clang-format 20 and 22 disagree about how to lay it out inside that block. Give it its own comment and paragraph. No code change. Signed-off-by: Ryan Murray --- src/usb_cdc_state.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/usb_cdc_state.h b/src/usb_cdc_state.h index b696144a7..73185982a 100644 --- a/src/usb_cdc_state.h +++ b/src/usb_cdc_state.h @@ -15,6 +15,8 @@ typedef uint64_t u64; #define USB_CDC_FLAG_FORCE_SWAPPED (1u << 0) /* drive the ATC lanes flipped */ #define USB_CDC_FLAG_GEN1 (1u << 1) /* Gen1 descriptors and link: required */ #define USB_CDC_FLAG_HPM_RESET (1u << 2) /* cold-reset HPM0 first, retiring KIS */ + +/* Every flag usb_cdc_schedule() accepts */ #define USB_CDC_FLAGS_ALL (USB_CDC_FLAG_FORCE_SWAPPED | USB_CDC_FLAG_GEN1 | USB_CDC_FLAG_HPM_RESET) /* Accepted delay before a scheduled transition starts (policy, not a hardware wait). */ From 570a76a94ac167b82acfe84abeceb0c38b46dff3 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:46:41 +1100 Subject: [PATCH 24/29] pcie: print the config address as unsigned long The config-readiness timeout message printed a u64 with %lx. On aura u64 is unsigned long, but the PCIe sequence host test builds pcie.c with the host's uint64_t, which is unsigned long long on macOS, and -Werror=format stops it there. Cast the address to unsigned long. No change on aura. Signed-off-by: Ryan Murray --- src/pcie.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pcie.c b/src/pcie.c index 5384e18ad..eff875124 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -1119,7 +1119,7 @@ int pcie_wait_config_ready(u64 cfg, u32 *id) if (vendor != 0xffff && vendor != 0x0001) return 0; if (timeout_expired(timeout)) { - printf("pcie: function at 0x%lx not ready after %u ms (0x%08x)\n", cfg, + printf("pcie: function at 0x%lx not ready after %u ms (0x%08x)\n", (unsigned long)cfg, PCIE_RESET_READY_MS, *id); return -1; } From f0f68682c0f80daca7d5d912cb438d73408baab2 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:46:41 +1100 Subject: [PATCH 25/29] ci: install qemu-user for the host tests qemu-user-static is only a virtual package on the current ubuntu-latest image, so apt refused it. qemu-user ships the qemu-aarch64 binary the start.S move gate looks for. Signed-off-by: Ryan Murray --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 74b6f4a12..e2e5a88f3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -89,7 +89,7 @@ jobs: if: matrix.os == 'ubuntu-latest' run: | sudo apt-get update - sudo apt-get install --no-install-recommends -y device-tree-compiler qemu-user-static + sudo apt-get install --no-install-recommends -y device-tree-compiler qemu-user # The host tests of tests/run-*.sh that need no files from a Mac - name: Run host tests From e668b43a0744a325ba06c1854e60d0b883fc89f3 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 21 Nov 2026 17:58:37 +1100 Subject: [PATCH 26/29] tps6598x: fail the reset wait when the controller rejects Gaid tps6598x_reset_wait_execute() took any CMD1 value other than Gaid as the reset having started, including !CMD, the controller's answer to a command it refused. MODE then still reads APP from the firmware that never reset, the wait returned 0, and the debugusb path went on to LOCK a controller that was not reset. The old tps6598x_command() returned -1 there. Return -1 when CMD1 reads !CMD. The host test answers Gaid with !CMD and APP in MODE and expects -1 after the single CMD1 read. Signed-off-by: Ryan Murray --- src/tps6598x_command_core.c | 7 +++++-- tests/python/test_tps6598x_command.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/tps6598x_command_core.c b/src/tps6598x_command_core.c index cb12c0ee9..cef0bd815 100644 --- a/src/tps6598x_command_core.c +++ b/src/tps6598x_command_core.c @@ -75,8 +75,11 @@ int tps6598x_reset_wait_execute(const struct tps6598x_command_ops *ops, void *ct if (!reset_seen) { if (ops->read(ctx, TPS_REG_CMD1, bytes, sizeof(bytes)) != sizeof(bytes)) transport_ok = false; - if (!transport_ok || - TPS6598X_TAG(bytes[0], bytes[1], bytes[2], bytes[3]) != TPS_CMD_GAID) + u32 cmd = transport_ok ? TPS6598X_TAG(bytes[0], bytes[1], bytes[2], bytes[3]) : 0; + /* The controller rejected Gaid: it never resets, and MODE still reads the old APP. */ + if (cmd == TPS_CMD_INVALID) + return -1; + if (!transport_ok || cmd != TPS_CMD_GAID) reset_seen = true; } if (reset_seen && transport_ok) { diff --git a/tests/python/test_tps6598x_command.py b/tests/python/test_tps6598x_command.py index df4fcda1f..3837cb7a9 100644 --- a/tests/python/test_tps6598x_command.py +++ b/tests/python/test_tps6598x_command.py @@ -114,6 +114,30 @@ def test_reset_wait_fails_when_reset_never_happens(tmp_path): assert mode.value == 0 +def test_reset_wait_fails_when_gaid_is_rejected(tmp_path): + """CMD1 reads !CMD: the controller refused Gaid and never resets, so the + old firmware's APP in MODE must not count as ready.""" + lib = command_lib(tmp_path) + clock = [0] + reads = [] + + def read(ctx, reg, data, length): + reads.append(reg) + ctypes.memmove(data, b"!CMD" if reg == 0x08 else b"APP ", 4) + return length + + def delay(ctx, usec): + clock[0] += usec // 1000 + + ops = Ops(WRITE(lambda ctx, reg, data, length: length), READ(read), + NOW(lambda ctx: clock[0]), DELAY(delay)) + mode = ctypes.c_uint32() + assert lib.tps6598x_reset_wait_execute(ctypes.byref(ops), None, + ctypes.byref(mode)) == -1 + assert mode.value == 0 + assert reads == [0x08] + + def test_stuck_register_selection_uses_whole_command_deadline(tmp_path): lib = command_lib(tmp_path) clock = [0] From c648d29a785259931e54f8a39da46d098b788b7d Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 21 Nov 2026 17:58:37 +1100 Subject: [PATCH 27/29] pcie: cap the ADT's perst-to-config at the 1 s reset-ready bound pcie_adt_perst_to_config_ms() raised the ADT bridge's perst-to-config to the 100 ms minimum but passed any larger value through, straight into mdelay() on the N1 and T6050 paths. A function completes configuration requests within 1.0 s of a conventional reset (PCIe Base r6.0 6.6.1, PCIE_RESET_READY_MS), so a longer value is bad data: use the bound and say so in the log. Signed-off-by: Ryan Murray --- src/pcie.c | 6 ++++++ src/pcie.h | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pcie.c b/src/pcie.c index eff875124..4e0431c3e 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -1105,6 +1105,12 @@ u32 pcie_adt_perst_to_config_ms(const char *bridge) if (node < 0 || ADT_GETPROP(adt, node, "perst-to-config", &ms) < 0) ms = 0; + /* A function answers config space within PCIE_RESET_READY_MS of reset; a longer ADT value is bad data. */ + if (ms > PCIE_RESET_READY_MS) { + printf("pcie: %s perst-to-config %u ms is over the %u ms reset-ready bound; using %u ms\n", + bridge, ms, PCIE_RESET_READY_MS, PCIE_RESET_READY_MS); + ms = PCIE_RESET_READY_MS; + } return max(ms, (u32)PCIE_RESET_TO_CONFIG_MS); } diff --git a/src/pcie.h b/src/pcie.h index ce39318a1..90a6e37c8 100644 --- a/src/pcie.h +++ b/src/pcie.h @@ -27,7 +27,8 @@ int pcie_ans_power_on(void); * * PCIE_RESET_TO_CONFIG_MS: PCIe Base r6.0 6.6.1: with a port faster than 5 GT/s, at least 100 ms * after link training completes before the first Configuration Request. The ADT bridge states it - * too, as perst-to-config (ms); pcie_adt_perst_to_config_ms() takes the longer of the two. + * too, as perst-to-config (ms); pcie_adt_perst_to_config_ms() takes the longer of the two, capped at + * PCIE_RESET_READY_MS. * * PCIE_RESET_READY_MS: PCIe Base r6.0 6.6.1: a function completes Configuration Requests * successfully within 1.0 s of a Conventional Reset. Until then it answers with Request Retry @@ -37,7 +38,7 @@ int pcie_ans_power_on(void); #define PCIE_RESET_TO_CONFIG_MS 100 #define PCIE_RESET_READY_MS 1000 -/* The ADT bridge's perst-to-config in ms, at least PCIE_RESET_TO_CONFIG_MS */ +/* The ADT bridge's perst-to-config in ms, at least PCIE_RESET_TO_CONFIG_MS and at most PCIE_RESET_READY_MS */ u32 pcie_adt_perst_to_config_ms(const char *bridge); /* Wait up to PCIE_RESET_READY_MS for the function at `cfg` to answer config space; *id gets its * vendor/device word. -1, and a log line, if it never does. */ From 0d5dca01e10d7a44d22dc373e52ae90f21fe419e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 21 Nov 2026 17:58:37 +1100 Subject: [PATCH 28/29] kboot: AOP: disable a failed handoff in place when the DT is full When reserving the AOP's firmware segments or DAPF windows failed, the loader grew the node's status to "disabled" and refused the whole boot if that failed too. The reservation usually fails because the DT is full, so growing the property is likely to fail the same way. Overwrite "okay" in place with "fail" (same length, no new space; Linux treats any status other than "okay" as unavailable), and grow the property only when there is no "okay" to overwrite. The boot is refused only if neither works. Both AOP paths (T8132 and the ADT-segment path) share the helper. Signed-off-by: Ryan Murray --- src/kboot.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/kboot.c b/src/kboot.c index e20188293..192e8b0ed 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -9105,10 +9105,27 @@ static int dt_reserve_t8132_aop_windows(const char *path) * DAPF windows. Without all of it the firmware cannot run, so on any failure * the AOP is disabled. */ +/* + * Take an AOP node out of the OS's tree after its handoff failed. That failure is often a full + * DT, where growing status to "disabled" fails too: overwrite "okay" in place with "fail" (same + * length, no new space; Linux treats anything but "okay" as unavailable) and only grow the + * property when the node has no "okay" to overwrite. + */ +static int dt_disable_aop(const char *path) +{ + int node = fdt_path_offset(dt, path); + if (node < 0) + return -1; + int len; + const char *status = fdt_getprop(dt, node, "status", &len); + if (status && len == sizeof("okay") && !memcmp(status, "okay", len)) + return fdt_setprop_inplace(dt, node, "status", "fail", sizeof("fail")) ? -1 : 0; + return fdt_setprop_string(dt, node, "status", "disabled") ? -1 : 0; +} + static int dt_set_t8132_aop(const char *path) { const char *why; - int node; if (fdt_node_check_compatible(dt, 0, "apple,j713") && fdt_node_check_compatible(dt, 0, "apple,j604")) @@ -9151,8 +9168,7 @@ static int dt_set_t8132_aop(const char *path) disable: printf("FDT: %s: disabling the AOP: %s\n", path, why); - node = fdt_path_offset(dt, path); - if (node < 0 || fdt_setprop_string(dt, node, "status", "disabled")) + if (dt_disable_aop(path)) bail("FDT: %s: cannot disable the AOP\n", path); return 0; } @@ -9279,8 +9295,7 @@ static int dt_set_aop_firmware(void) */ if (dt_reserve_asc_segments("/arm-io/aop", NULL, path, true, 0, true)) { printf("FDT: %s: reserving the AOP firmware segments failed, disabling the AOP\n", path); - int node = fdt_path_offset(dt, path); - if (node < 0 || fdt_setprop_string(dt, node, "status", "disabled")) + if (dt_disable_aop(path)) bail("FDT: %s: cannot disable the AOP\n", path); return 0; } From 50c21b015c2a621ffa14c169afa951bdeee701d4 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 21 Nov 2026 18:37:32 +1100 Subject: [PATCH 29/29] pcie: wrap the perst-to-config comments at 100 columns Signed-off-by: Ryan Murray --- src/pcie.c | 3 ++- src/pcie.h | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/pcie.c b/src/pcie.c index 4e0431c3e..c6588cfe5 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -1105,7 +1105,8 @@ u32 pcie_adt_perst_to_config_ms(const char *bridge) if (node < 0 || ADT_GETPROP(adt, node, "perst-to-config", &ms) < 0) ms = 0; - /* A function answers config space within PCIE_RESET_READY_MS of reset; a longer ADT value is bad data. */ + /* A function answers config space within PCIE_RESET_READY_MS of reset; a longer ADT value is + * bad data. */ if (ms > PCIE_RESET_READY_MS) { printf("pcie: %s perst-to-config %u ms is over the %u ms reset-ready bound; using %u ms\n", bridge, ms, PCIE_RESET_READY_MS, PCIE_RESET_READY_MS); diff --git a/src/pcie.h b/src/pcie.h index 90a6e37c8..8cbfac17b 100644 --- a/src/pcie.h +++ b/src/pcie.h @@ -27,8 +27,8 @@ int pcie_ans_power_on(void); * * PCIE_RESET_TO_CONFIG_MS: PCIe Base r6.0 6.6.1: with a port faster than 5 GT/s, at least 100 ms * after link training completes before the first Configuration Request. The ADT bridge states it - * too, as perst-to-config (ms); pcie_adt_perst_to_config_ms() takes the longer of the two, capped at - * PCIE_RESET_READY_MS. + * too, as perst-to-config (ms); pcie_adt_perst_to_config_ms() takes the longer of the two, capped + * at PCIE_RESET_READY_MS. * * PCIE_RESET_READY_MS: PCIe Base r6.0 6.6.1: a function completes Configuration Requests * successfully within 1.0 s of a Conventional Reset. Until then it answers with Request Retry @@ -38,7 +38,8 @@ int pcie_ans_power_on(void); #define PCIE_RESET_TO_CONFIG_MS 100 #define PCIE_RESET_READY_MS 1000 -/* The ADT bridge's perst-to-config in ms, at least PCIE_RESET_TO_CONFIG_MS and at most PCIE_RESET_READY_MS */ +/* The ADT bridge's perst-to-config in ms, at least PCIE_RESET_TO_CONFIG_MS and at most + * PCIE_RESET_READY_MS */ u32 pcie_adt_perst_to_config_ms(const char *bridge); /* Wait up to PCIE_RESET_READY_MS for the function at `cfg` to answer config space; *id gets its * vendor/device word. -1, and a log line, if it never does. */