diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3f8bf7980..e2e5a88f3 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 + + # 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 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/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/dcp/atc_phy.c b/src/dcp/atc_phy.c index 6283753c5..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" @@ -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; @@ -51,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}, @@ -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)) { @@ -395,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; @@ -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,14 +612,14 @@ 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++) 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; @@ -612,17 +631,17 @@ 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; - 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 d2dd33fd6..ca42e5efb 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,25 +205,38 @@ 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; } +/* 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) @@ -232,19 +246,20 @@ 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); - 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); @@ -252,16 +267,14 @@ 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) 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) diff --git a/src/dcp/dptx_port_ep.c b/src/dcp/dptx_port_ep.c index 2e76a558d..8a07c69d3 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; @@ -186,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; @@ -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) diff --git a/src/j813_n1.c b/src/j813_n1.c index 837554b52..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. * @@ -14,6 +14,7 @@ #include "j813_n1.h" #include "adt.h" +#include "pcie.h" #include "pmgr.h" #include "smc.h" #include "tunables.h" @@ -35,12 +36,12 @@ #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 +/* 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) { @@ -69,21 +70,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]; @@ -97,10 +83,20 @@ 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; - /* _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; @@ -129,19 +125,21 @@ 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")) 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; } - 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 */ + /* 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}, @@ -154,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; @@ -165,16 +163,17 @@ 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")) 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) || @@ -187,8 +186,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/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); diff --git a/src/kboot.c b/src/kboot.c index e5696139f..192e8b0ed 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; } /* @@ -9101,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")) @@ -9147,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; } @@ -9268,10 +9288,18 @@ 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); + if (dt_disable_aop(path)) + bail("FDT: %s: cannot disable the AOP\n", path); + return 0; + } + printf("FDT: %s: AOP firmware segments reserved\n", path); return 0; } @@ -11909,7 +11937,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); 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/src/pcie.c b/src/pcie.c index e50691219..c6588cfe5 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); @@ -1090,6 +1098,66 @@ 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; + /* 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); +} + +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", (unsigned long)cfg, + PCIE_RESET_READY_MS, *id); + return -1; + } + udelay(100); + } +} + +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; @@ -1172,7 +1240,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. diff --git a/src/pcie.h b/src/pcie.h index 172e1f568..8cbfac17b 100644 --- a/src/pcie.h +++ b/src/pcie.h @@ -19,6 +19,41 @@ 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, 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 + * 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 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. */ +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 f0bc22f3d..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) @@ -562,7 +550,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 +600,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 +641,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)); 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; } 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; diff --git a/src/tps6598x.c b/src/tps6598x.c index 3de6393b6..b0c8dc2ca 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; @@ -354,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); @@ -377,7 +407,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..cef0bd815 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,53 @@ 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; + 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) { + 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/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.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.c b/src/usb_cdc.c index 849475ab5..24b69e54d 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) @@ -67,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; @@ -117,17 +120,16 @@ 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; - mdelay(1000); } cdc.step = 2; 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_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_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..73185982a 100644 --- a/src/usb_cdc_state.h +++ b/src/usb_cdc_state.h @@ -11,6 +11,23 @@ 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 */ + +/* 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). */ +#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, 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); 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/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"; diff --git a/tests/python/test_tps6598x_command.py b/tests/python/test_tps6598x_command.py index 01d2c6db5..3837cb7a9 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,114 @@ 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_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] 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" 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) {