From bfb41e6ea0a9818507f893e7ef2f54af4767813b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:51 +0100 Subject: [PATCH 01/16] ARM: apple: Support the S5L8950X (A6) and its Swift cores MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The A6 (S5L8950X, iPhone 5) has two Apple Swift ARMv7s cores, the A5's PMGR core control and the same AIC. Add it to the S5L machine: - apple.c: match "apple,s5l8950x". The A5 boards' memblock carve-out under iBoot's scanout (0x9f600000..0x9f6fc000) is skipped on the A6, where that address is ordinary RAM; the A6 boards reserve their scanout in the DT. - headsmp.S: run the Cortex-A9 errata fixups and the SCU tag invalidation only on a Cortex-A9 (MIDR part 0xc09). Swift has an integrated L2 and no A9 SCU, CBAR or diagnostic register. - platsmp.c: look up "apple,s5l8950x-pmgr" first. Its core control registers sit at the A5's offsets (+0x1210 stop, +0x1214 start, +0x1220 run; iOS 8.4.1 AppleS5L8950XPerformanceController). On a core that is not a Cortex-A9, power down by switching the D-cache off and cleaning L1, without the A9's ACTLR.SMP write. The A5 boards run the same code as before. Tested on an iPhone5,2: both cores come up and the USB shell is reached. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/mach-apple/apple.c | 5 +++++ arch/arm/mach-apple/headsmp.S | 14 +++++++++++-- arch/arm/mach-apple/platsmp.c | 39 ++++++++++++++++++++++++++++------- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/arch/arm/mach-apple/apple.c b/arch/arm/mach-apple/apple.c index 647bfecf9fc315..044d64d9a78c82 100644 --- a/arch/arm/mach-apple/apple.c +++ b/arch/arm/mach-apple/apple.c @@ -29,6 +29,10 @@ static void __init apple_s5l_reserve(void) { phys_addr_t phys = P105_FB_PHYS & ~(SZ_1M - 1); + /* The A6 boards describe iBoot's scanout in reserved-memory. */ + if (of_machine_is_compatible("apple,s5l8950x")) + return; + memblock_remove(phys, P105_FB_PHYS - phys); } @@ -121,6 +125,7 @@ static void __init apple_s5l_init_time(void) static const char * const apple_s5l_dt_compat[] __initconst = { "apple,s5l8940x", "apple,s5l8942x", + "apple,s5l8950x", NULL }; diff --git a/arch/arm/mach-apple/headsmp.S b/arch/arm/mach-apple/headsmp.S index 50cc1d53b2e57d..56c6d4221e255d 100644 --- a/arch/arm/mach-apple/headsmp.S +++ b/arch/arm/mach-apple/headsmp.S @@ -13,9 +13,15 @@ * Invalidate this core's duplicate tags in the SCU (SCU + 0x0c, four bits * per core), as iBoot does when it resets a core: nothing the SCU remembers * about this core's L1 from before the power went is true any more. + * Cortex-A9 only (Apple Swift has an integrated L2 and no A9 SCU/CBAR). * Corrupts r0-r2. */ apple_s5l_scu_inv_self: + mrc p15, 0, r0, c0, c0, 0 @ MIDR + ubfx r1, r0, #4, #12 @ primary part number + movw r2, #0xc09 @ ARM_CPU_PART_CORTEX_A9 >> 4 + cmp r1, r2 + retne lr mrc p15, 4, r0, c15, c0, 0 @ CBAR: the SCU mrc p15, 0, r1, c0, c0, 5 @ MPIDR and r1, r1, #3 @@ -29,11 +35,15 @@ apple_s5l_scu_inv_self: /* * Cortex-A9 errata 743622 (r2p*, diagnostic bit 6) and 751472 (before r3p0, * bit 11), which multi_v7 leaves out: the diagnostic register is secure-only, - * and this kernel runs secure. apple.c does the same for CPU0. Corrupts - * r0-r2. + * and this kernel runs secure. apple.c does the same for CPU0. Skipped on + * non-A9 cores such as Apple Swift. Corrupts r0-r2. */ apple_s5l_a9_errata: mrc p15, 0, r0, c0, c0, 0 @ MIDR + ubfx r1, r0, #4, #12 @ primary part number + movw r2, #0xc09 @ ARM_CPU_PART_CORTEX_A9 >> 4 + cmp r1, r2 + retne lr ubfx r1, r0, #20, #4 @ variant mrc p15, 0, r2, c15, c0, 1 @ diagnostic register cmp r1, #2 diff --git a/arch/arm/mach-apple/platsmp.c b/arch/arm/mach-apple/platsmp.c index 6245ac72c0bf9b..1e2d89aed8fe98 100644 --- a/arch/arm/mach-apple/platsmp.c +++ b/arch/arm/mach-apple/platsmp.c @@ -96,17 +96,38 @@ static void tramp_target(phys_addr_t pa) dsb(st); } -/* On the core going down: clean L1, leave coherency, WFI. Returns only if - * the core was not powered off after all (an interrupt already pending). */ +/* On the core going down: clean L1, leave coherency (on Cortex-A9), WFI. + * Returns only if the core was not powered off after all (an interrupt already + * pending). Apple Swift (S5L8950X) does not use Cortex-A9 ACTLR.SMP bit 6. */ static int apple_s5l_powerdown(unsigned long arg) { - v7_exit_coherency_flush(louis); - wfi(); + if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) { + v7_exit_coherency_flush(louis); + wfi(); + asm volatile( + " mrc p15, 0, r0, c1, c0, 1\n" + " orr r0, r0, #(1 << 6)\n" /* ACTLR.SMP */ + " mcr p15, 0, r0, c1, c0, 1\n" + " isb\n" + " mrc p15, 0, r0, c1, c0, 0\n" + " orr r0, r0, #(1 << 2)\n" /* SCTLR.C */ + " mcr p15, 0, r0, c1, c0, 0\n" + " isb\n" + : : : "r0", "memory"); + return 1; + } + asm volatile( - " mrc p15, 0, r0, c1, c0, 1\n" - " orr r0, r0, #(1 << 6)\n" /* ACTLR.SMP */ - " mcr p15, 0, r0, c1, c0, 1\n" + " mrc p15, 0, r0, c1, c0, 0\n" + " bic r0, r0, #(1 << 2)\n" /* SCTLR.C */ + " mcr p15, 0, r0, c1, c0, 0\n" " isb\n" + " bl v7_flush_dcache_louis\n" + " dsb\n" + : : : "r0", "r1", "r2", "r3", "r4", "r5", "r6", + "r9", "r10", "ip", "lr", "memory"); + wfi(); + asm volatile( " mrc p15, 0, r0, c1, c0, 0\n" " orr r0, r0, #(1 << 2)\n" /* SCTLR.C */ " mcr p15, 0, r0, c1, c0, 0\n" @@ -144,7 +165,9 @@ static void __init apple_smp_prepare_cpus(unsigned int max_cpus) of_node_put(page); of_node_put(cpus); - pmgr = of_find_compatible_node(NULL, NULL, "apple,s5l8940x-pmgr"); + pmgr = of_find_compatible_node(NULL, NULL, "apple,s5l8950x-pmgr"); + if (!pmgr) + pmgr = of_find_compatible_node(NULL, NULL, "apple,s5l8940x-pmgr"); if (pmgr) pmgr_base = of_iomap(pmgr, 0); of_node_put(pmgr); From 90b5ca3efa6a9b16cd7fadd92733339f98d5ce14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:51 +0100 Subject: [PATCH 02/16] ARM: apple: Skip the cycle counter and PMU tick on cores without an ARM PMU MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The A6's Swift cores are Apple's own (MIDR 0x613f0001, implementer 'a') and have no ARM PMU. time_init enabled the Cortex-A9 cycle counter unconditionally, and its first CP15 c9 write (PMUSERENR) was an undefined instruction: the iPhone 5 panicked at time 0 ("Attempted to kill the idle task"). Use the cycle counter, its calibration and the PMU overflow tick only on a Cortex-A9 (the A5). Other cores get the 24 MHz watchdog counter as clocksource and sched_clock, and the AIC timer as the tick. The USB SOF tick no longer reads the cycle counter before its checks. With this the iPhone 5 boots to /init in 5.4 s. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/mach-apple/apple.c | 15 +++++++++++++++ arch/arm/mach-apple/apple_sof_clkevt.c | 10 +++++++++- arch/arm/mach-apple/pmccntr.c | 11 +++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/arch/arm/mach-apple/apple.c b/arch/arm/mach-apple/apple.c index 044d64d9a78c82..9ff189c6bd5daa 100644 --- a/arch/arm/mach-apple/apple.c +++ b/arch/arm/mach-apple/apple.c @@ -17,6 +17,7 @@ #include void __init apple_s5l_pmccntr_enable_counter(void); +bool apple_s5l_has_pmccntr(void); void __init apple_s5l_pmccntr_init(unsigned long rate); unsigned long __init apple_s5l_calibrate_cpu_hz(void); void __init apple_s5l_wdt_clocksource_init(void); @@ -104,6 +105,20 @@ static void __init apple_s5l_init_time(void) { unsigned long cpu_hz; + /* + * The A6's Swift cores are Apple's own (MIDR implementer 'a', 0x613f0001 + * on the iPhone 5) and have no ARM PMU: the first CP15 c9 write is an + * undefined instruction, and Linux died on it in time_init. They get the + * 24 MHz watchdog counter only (clocksource, sched_clock); the tick + * comes from the AIC timer. + */ + if (!apple_s5l_has_pmccntr()) { + pr_info("No ARM PMU on this core (MIDR %#x): no cycle counter, no PMU tick\n", + read_cpuid_id()); + apple_s5l_wdt_clocksource_init(); + return; + } + /* Enable the cycle counter first, then measure it against the watchdog's * exact 24 MHz reference, and only then register the clocksource -- its * rate used to be a hardcoded 1 GHz guess, which made all wall-clock time diff --git a/arch/arm/mach-apple/apple_sof_clkevt.c b/arch/arm/mach-apple/apple_sof_clkevt.c index 6c4b4694810922..5dc3b35243e941 100644 --- a/arch/arm/mach-apple/apple_sof_clkevt.c +++ b/arch/arm/mach-apple/apple_sof_clkevt.c @@ -53,6 +53,8 @@ #include #include +bool apple_s5l_has_pmccntr(void); /* pmccntr.c */ + /* One SOF per 125 us microframe at high speed. */ #define SOF_TICK_HZ 8000 @@ -171,13 +173,19 @@ static struct clock_event_device sof_clkevt = { static int __init apple_sof_clkevt_register(void) { unsigned int before = READ_ONCE(sof_seen); - u32 start = pmccntr(); unsigned long cpu_hz = 1000000000UL; /* only used to size the spin */ + u32 start; if (sof_superseded) { pr_err("SOF-TIMER: not needed -- the AIC timer is the tick, SOF interrupts stay off.\n"); return 0; } + /* The spin below counts CPU cycles; the A6 has no cycle counter */ + if (!apple_s5l_has_pmccntr()) { + pr_err("SOF-TIMER: no cycle counter on this core, not registering\n"); + return 0; + } + start = pmccntr(); while ((u32)(pmccntr() - start) < cpu_hz * 2) { /* up to ~2 s */ if (READ_ONCE(sof_seen) != before) diff --git a/arch/arm/mach-apple/pmccntr.c b/arch/arm/mach-apple/pmccntr.c index dc1e349453b28b..d53f5b88c5e846 100644 --- a/arch/arm/mach-apple/pmccntr.c +++ b/arch/arm/mach-apple/pmccntr.c @@ -15,6 +15,7 @@ #include #include +#include #include /* Fallback only. apple_s5l_pmccntr_init() is handed the rate measured against @@ -85,6 +86,16 @@ static void __init apple_pmccntr_enable(void) isb(); } +/* + * Only the A5's Cortex-A9 has the ARM PMU and so this counter. The A6's + * Swift cores (implementer 'a') take an undefined instruction on any CP15 + * c9 PMU access. + */ +bool apple_s5l_has_pmccntr(void) +{ + return read_cpuid_part() == ARM_CPU_PART_CORTEX_A9; +} + /* Split in two so the caller can enable the counter, measure the real CPU * frequency against the watchdog's 24 MHz reference, and only then register * everything with a rate that is not a guess. */ From 8823999516696fc239ed4ed3c68e414deb5b7c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:51 +0100 Subject: [PATCH 03/16] ARM: apple: Run the S5L8950X watchdog counter and restart through it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three differences on the A6 watchdog: - WDT_CUR_TIME only counts while WDT_CTRL bit 3 (enable) is set. If the boot loader left CTRL at 0, set bit 3 without the reset enable, so the counter runs; a watchdog the boot loader armed (0xc) is left alone. The calibration loop is bounded, so a counter that never moves cannot hang the boot. - A reset needs enable and reset enable together (0xc), programmed the way iBEC's wdt_enable() does: CTRL 0, bite time, count, CTRL 0xc, count 0. The restart handler and the reboot notifier do that on the S5L8950X. - The A6 skips the CPU calibration (no cycle counter), which was the only place that mapped the counter, so the clocksource found nothing mapped and the phone ran on jiffies. Map the counter in a helper that both call. The A5 boards keep their sequence. Tested on an iPhone5,2: the watchdog counter is the clocksource, and panic=10 and reboot reset the phone through the watchdog. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/mach-apple/apple_wdt_clkevt.c | 91 ++++++++++++++++++++------ 1 file changed, 71 insertions(+), 20 deletions(-) diff --git a/arch/arm/mach-apple/apple_wdt_clkevt.c b/arch/arm/mach-apple/apple_wdt_clkevt.c index bd58731f7ceaec..6863ba9ba95481 100644 --- a/arch/arm/mach-apple/apple_wdt_clkevt.c +++ b/arch/arm/mach-apple/apple_wdt_clkevt.c @@ -56,6 +56,7 @@ #define WDT_BITE_TIME 0x04 #define WDT_CTRL 0x0c #define WDT_CTRL_RESET_EN BIT(2) +#define WDT_CTRL_ENABLE BIT(3) #define APPLE_WDT_REF_HZ 24000000UL static inline u32 apple_pmccntr(void) @@ -110,6 +111,47 @@ static struct delay_timer apple_wdt_delay = { .freq = APPLE_WDT_REF_HZ, }; +/* + * Map the watchdog's 24 MHz counter (once). The A5 does this from the CPU + * calibration; the A6 has no cycle counter to calibrate, so its clocksource + * init maps it here directly. + */ +static bool __init apple_wdt_map(void) +{ + struct device_node *np; + + if (wdt_base) + return true; + np = of_find_compatible_node(NULL, NULL, "apple,s5l8940x-wdt-timer"); + if (!np) { + pr_err("no wdt node\n"); + return false; + } + + wdt_base = of_iomap(np, 0); + of_node_put(np); + if (!wdt_base) { + pr_err("cannot map wdt counter\n"); + return false; + } + + /* + * On S5L8950X (A6), WDT_CUR_TIME only ticks while WDT_CTRL_ENABLE (bit 3) + * is set. If the bootloader left CTRL=0 (no watchdog armed), enable the + * counter without RESET_EN so calibration and the clocksource tick; if + * a boot watchdog is already armed (0xc), leave it intact. + */ + if (of_machine_is_compatible("apple,s5l8950x")) { + u32 ctrl = readl_relaxed(wdt_base + WDT_CTRL); + + if (!(ctrl & WDT_CTRL_ENABLE)) { + writel(ctrl | WDT_CTRL_ENABLE, wdt_base + WDT_CTRL); + (void)readl(wdt_base + WDT_CTRL); + } + } + return true; +} + /** * apple_s5l_wdt_clocksource_init - wall time that survives idle * @@ -119,7 +161,7 @@ static struct delay_timer apple_wdt_delay = { */ void __init apple_s5l_wdt_clocksource_init(void) { - if (!wdt_base) { + if (!apple_wdt_map()) { pr_err("CS: counter not mapped, wall time stays on PMCCNTR (wrong in idle)\n"); return; } @@ -138,28 +180,22 @@ void __init apple_s5l_wdt_clocksource_init(void) */ unsigned long __init apple_s5l_calibrate_cpu_hz(void) { - struct device_node *np; u32 w0, w1, c0, c1, wd, cd; u64 hz; - np = of_find_compatible_node(NULL, NULL, "apple,s5l8940x-wdt-timer"); - if (!np) { - pr_err("CALIB: no wdt node; CPU rate stays a guess\n"); + if (!apple_wdt_map()) return 0; - } - - wdt_base = of_iomap(np, 0); - of_node_put(np); - if (!wdt_base) { - pr_err("CALIB: cannot map wdt counter\n"); - return 0; - } /* ~50 ms of reference ticks is plenty and costs nothing at boot. */ w0 = readl_relaxed(wdt_base + WDT_CUR_TIME); c0 = apple_pmccntr(); - while ((u32)(readl_relaxed(wdt_base + WDT_CUR_TIME) - w0) < APPLE_WDT_REF_HZ / 20) - cpu_relax(); + { + u32 guard = 0; + + while ((u32)(readl_relaxed(wdt_base + WDT_CUR_TIME) - w0) < APPLE_WDT_REF_HZ / 20 && + ++guard < 50000000U) + cpu_relax(); + } w1 = readl_relaxed(wdt_base + WDT_CUR_TIME); c1 = apple_pmccntr(); /* deliberately not unmapped: the clocksource lives on this counter */ @@ -191,11 +227,24 @@ unsigned long __init apple_s5l_calibrate_cpu_hz(void) */ static int apple_wdt_restart(struct sys_off_data *data) { - u32 now = readl_relaxed(wdt_base + WDT_CUR_TIME); + if (of_machine_is_compatible("apple,s5l8950x")) { + /* + * S5L8950X (A6) requires both WDT_CTRL_ENABLE and WDT_CTRL_RESET_EN + * (0xc), following iBEC's wdt_enable() sequence. + */ + writel(0, wdt_base + WDT_CTRL); + writel(APPLE_WDT_REF_HZ / 1000, wdt_base + WDT_BITE_TIME); + writel(0x80000000U, wdt_base + WDT_CUR_TIME); + writel(WDT_CTRL_ENABLE | WDT_CTRL_RESET_EN, wdt_base + WDT_CTRL); + writel(0, wdt_base + WDT_CUR_TIME); + (void)readl(wdt_base + WDT_CTRL); + } else { + u32 now = readl_relaxed(wdt_base + WDT_CUR_TIME); - writel_relaxed(now + APPLE_WDT_REF_HZ / 1000, wdt_base + WDT_BITE_TIME); - writel(WDT_CTRL_RESET_EN, wdt_base + WDT_CTRL); - (void)readl(wdt_base + WDT_CTRL); + writel_relaxed(now + APPLE_WDT_REF_HZ / 1000, wdt_base + WDT_BITE_TIME); + writel(WDT_CTRL_RESET_EN, wdt_base + WDT_CTRL); + (void)readl(wdt_base + WDT_CTRL); + } mdelay(500); pr_emerg("the watchdog did not reset the machine\n"); return NOTIFY_DONE; @@ -212,9 +261,11 @@ static int apple_wdt_reboot_notify(struct notifier_block *nb, unsigned long acti { if (action == SYS_RESTART) { u32 now = readl_relaxed(wdt_base + WDT_CUR_TIME); + u32 ctrl = of_machine_is_compatible("apple,s5l8950x") ? + (WDT_CTRL_ENABLE | WDT_CTRL_RESET_EN) : WDT_CTRL_RESET_EN; writel_relaxed(now + 10 * APPLE_WDT_REF_HZ, wdt_base + WDT_BITE_TIME); - writel(WDT_CTRL_RESET_EN, wdt_base + WDT_CTRL); + writel(ctrl, wdt_base + WDT_CTRL); pr_info("restart: the watchdog will reset the machine in 10 s at the latest\n"); } return NOTIFY_DONE; From c4229ae52837d1e6a562da728e13bf70bb3a02fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:52 +0100 Subject: [PATCH 04/16] irqchip/apple-aic1: Give each A6 core its own AIC timer tick MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On the iPhone 5 the only clockevent was the AIC timer of CPU0. CPU1 got the dummy clockevent, and with no broadcast device it never ticked: with idle=poll RCU stalled on it ("timer wakeup didn't happen"), and with WFI idle the whole system froze at about 10.8 s, the first time something waited for a timer on CPU1. The AIC timer is a per-core register: the 0x2000 window is banked by the accessing core, like WHOAMI, EVENT/IACK and IPI_SEND, and the timer fires as a local event (type 7) in that core's EVENT. iOS 10.3.4 on the N42 does not use it (XNU ticks each core from the Swift generic timer), but iBoot runs on it, and so does Linux on CPU0. On the A6, once a secondary core is online (CPUHP_AP_ONLINE_DYN, interrupts on), arm a 10 ms shot in its banked window, then in its explicit window (0x5000 + (cpu << 7)), and wait up to 200 ms of AIC timebase for the event on that core. The first window that fires becomes the core's oneshot clockevent, rating 400, which replaces the dummy. A failed test disarms through the core's explicit window only, so it cannot stop CPU0's tick, and logs FAIL. Per-CPU timer event counts are in /sys/module/aic1/parameters/timer_events. There is no offline teardown: going offline, the core's timer stays its tick device until the tick code lets go of it, and apple_aic1_secondary_init() quiesces the window before the core comes back. The A5 (P105, J1) keeps its PMU tick and CPU0-only AIC timer: the online state is installed only on "apple,s5l8950x". Tested on an iPhone5,2: "PASS -- CPU1 has its own clockevent", and a two-core boot reaches the USB shell with no nohlt or irqaffinity. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/irqchip/irq-apple-aic1.c | 187 ++++++++++++++++++++++++++++--- 1 file changed, 174 insertions(+), 13 deletions(-) diff --git a/drivers/irqchip/irq-apple-aic1.c b/drivers/irqchip/irq-apple-aic1.c index f6fda81687d418..31219f99f7f3e0 100644 --- a/drivers/irqchip/irq-apple-aic1.c +++ b/drivers/irqchip/irq-apple-aic1.c @@ -774,8 +774,33 @@ static bool aic1_tmr_registered; static bool aic1_tmr_percpu; static DEFINE_PER_CPU(struct clock_event_device, aic1_clkevts); +/* + * The timer is one of the per-core registers of the AIC: the window at 0x2000 + * is banked by the core that accesses it (WHOAMI, the EVENT/IACK and the IPI + * registers are, and IPI_SEND there already tells the two cores apart), and + * its timer event is a local event (type 7) in that core's own EVENT. So each + * core has a timer of its own, and the A6's CPU1 is given its own (see + * aic1_tmr_cpu_online). A window picked for one CPU overrides the global + * choice; 0 means none picked. + */ +static DEFINE_PER_CPU(u32, aic1_tmr_cpu_win); +/* The window a CPU's own self-test saw fire, kept across CPU hotplug. */ +static u32 aic1_tmr_cpu_ok[AIC1_NR_CPUS]; +static u64 aic1_tmr_cpu_last[AIC1_NR_CPUS]; + +/* Timer events taken per CPU: /sys/module/aic1/parameters/timer_events. + * Both counts must grow; CPU1's staying at 0 means CPU1 has no tick. */ +#undef MODULE_PARAM_PREFIX +#define MODULE_PARAM_PREFIX "aic1." +static unsigned int timer_events[AIC1_NR_CPUS]; +module_param_array(timer_events, uint, NULL, 0444); + static inline u32 aic1_tmr_base(void) { + u32 win = this_cpu_read(aic1_tmr_cpu_win); + + if (win) + return win; return aic1_tmr_percpu ? AIC1_CPU_WINDOW(smp_processor_id()) : aic1_tmr_win; } @@ -829,8 +854,14 @@ static void aic1_tmr_arm(struct apple_aic1 *aic, u32 delta) */ static void aic1_tmr_event(struct apple_aic1 *aic) { + unsigned int cpu = smp_processor_id(); + aic1_tmr_events++; aic1_tmr_last = aic1_time(aic); + if (cpu < AIC1_NR_CPUS) { + aic1_tmr_cpu_last[cpu] = aic1_tmr_last; + WRITE_ONCE(timer_events[cpu], timer_events[cpu] + 1); + } aic1_tmr_disarm(aic); if (aic1_tmr_registered) { @@ -854,6 +885,23 @@ static int aic1_ce_shutdown(struct clock_event_device *ce) return 0; } +/* The CPU's own clockevent, registered on that CPU. */ +static void aic1_tmr_register_ce(unsigned int cpu) +{ + struct clock_event_device *ce = per_cpu_ptr(&aic1_clkevts, cpu); + + ce->name = "apple-aic1-timer"; + ce->features = CLOCK_EVT_FEAT_ONESHOT; + /* Above the USB SOF tick (250), which stays registered as a spare. */ + ce->rating = 400; + ce->set_next_event = aic1_ce_set_next_event; + ce->set_state_shutdown = aic1_ce_shutdown; + ce->set_state_oneshot = aic1_ce_shutdown; + ce->tick_resume = aic1_ce_shutdown; + ce->cpumask = cpumask_of(cpu); + clockevents_config_and_register(ce, AIC1_TIMER_HZ, 0xf, 0x7fffffff); +} + /* * CPU hotplug STARTING, on the CPU itself with interrupts off: its window's * timer set up the way the probe found it working, and its clockevent. @@ -862,23 +910,13 @@ static int aic1_ce_shutdown(struct clock_event_device *ce) */ static int aic1_tmr_cpu_starting(unsigned int cpu) { - struct clock_event_device *ce = per_cpu_ptr(&aic1_clkevts, cpu); struct apple_aic1 *aic = apple_aic1; if (cpu) { aic1_tmr_write(aic, AIC1_TMR_CFG, AIC1_TMR_CFG_IBOOT); aic1_tmr_disarm(aic); } - ce->name = "apple-aic1-timer"; - ce->features = CLOCK_EVT_FEAT_ONESHOT; - /* Above the USB SOF tick (250), which stays registered as a spare. */ - ce->rating = 400; - ce->set_next_event = aic1_ce_set_next_event; - ce->set_state_shutdown = aic1_ce_shutdown; - ce->set_state_oneshot = aic1_ce_shutdown; - ce->tick_resume = aic1_ce_shutdown; - ce->cpumask = cpumask_of(cpu); - clockevents_config_and_register(ce, AIC1_TIMER_HZ, 0xf, 0x7fffffff); + aic1_tmr_register_ce(cpu); return 0; } @@ -888,6 +926,124 @@ static int aic1_tmr_cpu_dying(unsigned int cpu) return 0; } +#ifdef CONFIG_SMP +/* + * On a secondary CPU, from its own hotplug thread with interrupts on: put + * this CPU's timer in @win, arm a 10 ms shot and wait up to 200 ms of AIC + * timebase for the event to come back to THIS CPU (timer_events[cpu]). An + * event that went to the other core instead is only an early tick there. + */ +static bool aic1_tmr_cpu_try(struct apple_aic1 *aic, unsigned int cpu, u32 win, + const char *what) +{ + unsigned int before = READ_ONCE(timer_events[cpu]); + unsigned int other = READ_ONCE(timer_events[!cpu]); + u32 cfg0, cnt_a, cnt_b; + u64 t0, t; + + this_cpu_write(aic1_tmr_cpu_win, win); + cfg0 = aic1_tmr_read(aic, AIC1_TMR_CFG); + aic1_tmr_write(aic, AIC1_TMR_CFG, AIC1_TMR_CFG_IBOOT); + + t0 = aic1_time(aic); + aic1_tmr_arm(aic, AIC1_TIMER_HZ / 100); + cnt_a = aic1_tmr_read(aic, AIC1_TMR_CNT); + do { + if (READ_ONCE(timer_events[cpu]) != before) + break; + cpu_relax(); + t = aic1_time(aic); + } while (t - t0 < AIC1_TIMER_HZ / 5); + cnt_b = aic1_tmr_read(aic, AIC1_TMR_CNT); + + if (READ_ONCE(timer_events[cpu]) != before) { + pr_info("AIC-TIMER: CPU%u %s window %#x: a 10 ms shot fired on CPU%u after %llu us\n", + cpu, what, win, cpu, + div_u64((aic1_tmr_cpu_last[cpu] - t0) * 1000000ULL, + AIC1_TIMER_HZ)); + return true; + } + + pr_err("AIC-TIMER: CPU%u %s window %#x: nothing on CPU%u in 200 ms (CPU%u took %u timer events meanwhile). CFG was %#x, CNT %#x -> %#x, STAT %#x\n", + cpu, what, win, cpu, !cpu, READ_ONCE(timer_events[!cpu]) - other, + cfg0, cnt_a, cnt_b, aic1_tmr_read(aic, AIC1_TMR_STAT)); + /* Stop it through the window that is this CPU's for certain. Not + * through @win: if the banked window turned out to reach CPU0's timer, + * a disarm there would kill CPU0's tick, while the shot it got was + * only an early tick that CPU0 has re-armed since. */ + this_cpu_write(aic1_tmr_cpu_win, AIC1_CPU_WINDOW(cpu)); + aic1_tmr_disarm(aic); + this_cpu_write(aic1_tmr_cpu_win, 0); + return false; +} + +/* + * CPU hotplug ONLINE (dynamic state), A6 only, on a secondary CPU: give it + * the AIC timer of its own core as its tick. Until this runs the CPU has + * only the dummy clockevent, and with no broadcast device on this SoC a CPU + * left like that never ticks: RCU stalls on it with idle=poll, and in WFI + * the first wait for it hangs the system. The timer is tested before it is + * trusted, through the banked window first (the one CPU0 runs on), then the + * CPU's explicit window. + */ +static int aic1_tmr_cpu_online(unsigned int cpu) +{ + struct apple_aic1 *aic = apple_aic1; + u32 win; + + if (!cpu || cpu >= AIC1_NR_CPUS || !aic) + return 0; + + win = aic1_tmr_cpu_ok[cpu]; + if (!win) { + if (aic1_tmr_cpu_try(aic, cpu, AIC1_ALIAS_WINDOW, "banked")) + win = AIC1_ALIAS_WINDOW; + else if (aic1_tmr_cpu_try(aic, cpu, AIC1_CPU_WINDOW(cpu), "explicit")) + win = AIC1_CPU_WINDOW(cpu); + } + if (!win) { + pr_err("AIC-TIMER: FAIL -- CPU%u: its own AIC timer never fired; CPU%u has no tick (expect RCU stalls; boot with maxcpus=1)\n", + cpu, cpu); + return 0; + } + + aic1_tmr_cpu_ok[cpu] = win; + this_cpu_write(aic1_tmr_cpu_win, win); + aic1_tmr_write(aic, AIC1_TMR_CFG, AIC1_TMR_CFG_IBOOT); + aic1_tmr_disarm(aic); + aic1_tmr_register_ce(cpu); + pr_info("AIC-TIMER: PASS -- CPU%u has its own clockevent (window %#x)\n", + cpu, win); + return 0; +} + +/* + * No teardown: going offline, the CPU's timer is still its tick device until + * the tick code lets go of it, and apple_aic1_secondary_init() quiesces the + * window before the CPU comes back. + */ +static bool __init aic1_tmr_setup_secondaries(void) +{ + int ret; + + /* The A5 keeps its PMU tick and CPU0-only AIC timer as they were. */ + if (num_possible_cpus() < 2 || !of_machine_is_compatible("apple,s5l8950x")) + return false; + + ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, + "irqchip/apple-aic1/timer:online", + aic1_tmr_cpu_online, NULL); + if (ret < 0) { + pr_err("AIC-TIMER: no hotplug state for the secondary timers (%d)\n", + ret); + return false; + } + return true; +} +#else +static inline bool aic1_tmr_setup_secondaries(void) { return false; } +#endif + /* * Arm a 10 ms shot in one window and wait up to 200 ms of timebase for the * event to come back through the dispatcher. IRQs are on before any initcall; @@ -941,6 +1097,7 @@ static bool aic1_tmr_tried __initdata; static int __init aic1_timer_init(void) { struct apple_aic1 *aic = apple_aic1; + bool secondaries = false; if (!aic || aic1_tmr_tried) return 0; @@ -963,8 +1120,10 @@ static int __init aic1_timer_init(void) "irqchip/apple-aic1/timer:starting", aic1_tmr_cpu_starting, aic1_tmr_cpu_dying); } else { - /* The alias fired: one timer, CPU0's. */ + /* The banked window fired: CPU0's timer. On the A6 each further + * CPU tests and takes its own as it comes online. */ aic1_tmr_cpu_starting(0); + secondaries = aic1_tmr_setup_secondaries(); } #ifdef CONFIG_ARCH_APPLE_S5L { @@ -976,7 +1135,9 @@ static int __init aic1_timer_init(void) } #endif pr_err("AIC-TIMER: PASS -- clockevent registered at 24 MHz, %s. The tick no longer needs a USB host.\n", - aic1_tmr_percpu ? "one per CPU" : "CPU0 only"); + aic1_tmr_percpu ? "one per CPU" : + secondaries ? "CPU0 now, each other CPU its own once online" : + "CPU0 only"); return 0; } From 9dda332e69038eb81b27c725207b80ed9d8697cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:52 +0100 Subject: [PATCH 05/16] irqchip/apple-aic1: Report device IRQs as CPU0-only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit aic1_irq_unmask() routes every line to CPU0 (TARGET = BIT(0)) and the chip has no irq_set_affinity, so no device interrupt ever reaches another core. The IRQ core did not know that: /proc/irq/N claimed affinity 0-1, and irqaffinity=0 on the command line only changed what it claimed. Set the affinity and effective affinity of each line to CPU0 when it is mapped, mark it single-target and exclude it from balancing. Routing is unchanged, on the A5 as well; irqaffinity=0 is no longer needed. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/irqchip/irq-apple-aic1.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/drivers/irqchip/irq-apple-aic1.c b/drivers/irqchip/irq-apple-aic1.c index 31219f99f7f3e0..71e0f3cb1a799b 100644 --- a/drivers/irqchip/irq-apple-aic1.c +++ b/drivers/irqchip/irq-apple-aic1.c @@ -559,6 +559,20 @@ static int aic1_irq_domain_map(struct irq_domain *d, unsigned int virq, NULL, NULL); irq_set_probe(virq); + /* Every line is routed to CPU0 (aic1_irq_unmask writes TARGET = CPU0) + * and the chip has no irq_set_affinity, so say so: affinity and + * effective affinity CPU0, and no balancing. /proc/irq/N then tells + * the truth, irqaffinity= is not needed, and nothing expects a + * device interrupt on CPU1. */ + { + struct irq_data *irqd = irq_get_irq_data(virq); + + irqd_set_single_target(irqd); + irq_data_update_affinity(irqd, cpumask_of(0)); + irq_data_update_effective_affinity(irqd, cpumask_of(0)); + irq_set_status_flags(virq, IRQ_NO_BALANCING); + } + return 0; } From 9e8e14ae53d519a13d88c430ad1fd22f5b6cee7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:53 +0100 Subject: [PATCH 06/16] dt-bindings: phy: apple,s5l8942x-usb-phy: Add the S5L8950X MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The A6 (S5L8950X, iPhone 5) has the A5's OTG and HSIC PHY with other PMGR offsets and tuning values, so it gets its own compatible and no S5L8940X fallback. On the S5L8950X the PHY can also take the PMGR gates of the OTG block (UPERF, USB-OTG, USBREG) as clocks, after the reference clock, and holds them from probe. Allow up to four clocks, named in that order. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- .../bindings/phy/apple,s5l8942x-usb-phy.yaml | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/Documentation/devicetree/bindings/phy/apple,s5l8942x-usb-phy.yaml b/Documentation/devicetree/bindings/phy/apple,s5l8942x-usb-phy.yaml index b9cf8b7d2a9a2f..cc30150ab9e07c 100644 --- a/Documentation/devicetree/bindings/phy/apple,s5l8942x-usb-phy.yaml +++ b/Documentation/devicetree/bindings/phy/apple,s5l8942x-usb-phy.yaml @@ -4,7 +4,7 @@ $id: http://devicetree.org/schemas/phy/apple,s5l8942x-usb-phy.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: Apple S5L8942X USB and HSIC PHY +title: Apple S5L8942X and S5L8950X USB and HSIC PHY maintainers: - Shayne Legault <8741557+shaynelegault@users.noreply.github.com> @@ -14,19 +14,28 @@ description: | and reset. The first PHY argument selects the output: 0 for USB OTG, 1 for the HSIC host. The HSIC supplies power the attached peripheral. + On the S5L8950X (A6) the PHY can also hold the PMGR power-state gates + of the OTG block (UPERF, USB-OTG and USBREG), which it switches on at + probe and never off again. Its PMGR offsets and tuning differ from the + A5's, so it has no S5L8940X fallback. + select: properties: compatible: contains: - const: apple,s5l8942x-otgphy + enum: + - apple,s5l8942x-otgphy + - apple,s5l8950x-otgphy required: - compatible properties: compatible: - items: - - const: apple,s5l8942x-otgphy - - const: apple,s5l8940x-otgphy + oneOf: + - items: + - const: apple,s5l8942x-otgphy + - const: apple,s5l8940x-otgphy + - const: apple,s5l8950x-otgphy reg: items: @@ -34,11 +43,20 @@ properties: - description: USB complex control registers clocks: - maxItems: 1 - description: PHY reference clock, at 12, 24 or 48 MHz + minItems: 1 + items: + - description: PHY reference clock, at 12, 24 or 48 MHz + - description: UPERF power-state gate + - description: USB-OTG power-state gate + - description: USBREG power-state gate clock-names: - const: ref + minItems: 1 + items: + - const: ref + - const: uperf + - const: otg + - const: usbreg '#phy-cells': const: 1 @@ -67,3 +85,11 @@ examples: clock-names = "ref"; #phy-cells = <1>; }; + - | + phy@36000000 { + compatible = "apple,s5l8950x-otgphy"; + reg = <0x36000000 0x1000>, <0x3f108000 0x1000>; + clocks = <&refclk24mhz>, <&uperf_ps>, <&usb_otg_ps>, <&usbreg_ps>; + clock-names = "ref", "uperf", "otg", "usbreg"; + #phy-cells = <1>; + }; From 1a13a00df2acb31ad4df9d6f85d412b94420fe41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:54 +0100 Subject: [PATCH 07/16] phy: apple-s5l-usb: Add the S5L8950X OTG PHY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The A6's OTG PHY is the A5's, with its PMGR power states at other offsets: UPERF +0x1084, USB-OTG +0x1088, USB-EHCI +0x108c, USB-OHCI0 +0x1094 and USBREG +0x109c. Its tuning differs too: uotgtune1 is 0x749. Move the gate offsets and the tuning values into per-SoC match data and add "apple,s5l8950x-otgphy" (iPhone5,2 iOS 8.4.1 iBEC and ADT). The S5L8940X and S5L8942X entries carry the values the driver used before. Tested on an iPhone5,2: the dwc2 gadget comes up with CDC ACM and networking. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/phy/phy-apple-s5l-usb.c | 91 +++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 22 deletions(-) diff --git a/drivers/phy/phy-apple-s5l-usb.c b/drivers/phy/phy-apple-s5l-usb.c index 91f50b21df104e..2de812f98e08b9 100644 --- a/drivers/phy/phy-apple-s5l-usb.c +++ b/drivers/phy/phy-apple-s5l-usb.c @@ -35,6 +35,7 @@ #define OPHY_UOTGTUNE1 0x40 #define OPHY_UOTGTUNE2 0x44 #define OPHY_UOTGTUNE1_DEVICE 0x549 +#define OPHY_UOTGTUNE1_S5L8950X_DEVICE 0x749 #define OPHY_UOTGTUNE2_DEVICE 0x2ff3 #define PMGR_BASE 0x3F100000 @@ -111,10 +112,52 @@ struct s5l_usbphy_soc_data { bool cold_init; + u32 pmgr_uperf; + u32 pmgr_usb_otg; + u32 pmgr_usbreg; + u32 pmgr_usb_ehci; + u32 pmgr_usb_ohci0; + u32 uotgtune1_device; +}; + +static const struct s5l_usbphy_soc_data s5l8940x_usbphy_data = { + .cold_init = false, + .pmgr_uperf = PMGR_UPERF, + .pmgr_usb_otg = PMGR_USB_OTG, + .pmgr_usbreg = PMGR_USBREG, + .pmgr_usb_ehci = PMGR_USB_EHCI, + .pmgr_usb_ohci0 = PMGR_USB_OHCI0, + .uotgtune1_device = OPHY_UOTGTUNE1_DEVICE, }; static const struct s5l_usbphy_soc_data s5l8942x_usbphy_data = { .cold_init = true, + .pmgr_uperf = PMGR_UPERF, + .pmgr_usb_otg = PMGR_USB_OTG, + .pmgr_usbreg = PMGR_USBREG, + .pmgr_usb_ehci = PMGR_USB_EHCI, + .pmgr_usb_ohci0 = PMGR_USB_OHCI0, + .uotgtune1_device = OPHY_UOTGTUNE1_DEVICE, +}; + +/* + * S5L8950X (Apple A6): verified from iPhone5,2 iOS 8.4.1 ADT device-clocks + * (81 entries x 44 bytes) and iBEC USB init: + * UPERF id 0x04, idx 0x21 -> PMGR + 0x1084 + * USB-OTG id 0x35, idx 0x22 -> PMGR + 0x1088 + * USB-EHCI id 0x36, idx 0x23 -> PMGR + 0x108c + * OHCI0 id 0x37, idx 0x25 -> PMGR + 0x1094 + * USBREG id 0x06, idx 0x27 -> PMGR + 0x109c + * OPHY_UOTGTUNE1 = 0x749, OPHY_UOTGTUNE2 = 0x2ff3. + */ +static const struct s5l_usbphy_soc_data s5l8950x_usbphy_data = { + .cold_init = true, + .pmgr_uperf = 0x1084, + .pmgr_usb_otg = 0x1088, + .pmgr_usbreg = 0x109c, + .pmgr_usb_ehci = 0x108c, + .pmgr_usb_ohci0 = 0x1094, + .uotgtune1_device = OPHY_UOTGTUNE1_S5L8950X_DEVICE, }; struct s5l_usbphy { @@ -173,7 +216,7 @@ static bool s5l_usbphy_is_ready(struct s5l_usbphy *p) (readl(p->base + OPHYCLK) & OPHYCLK_CLKSEL_MASK) == clksel && readl(p->base + OPHYPWR) == OPHYPWR_ACTIVE && !(readl(p->base + ORSTCON) & ORSTCON_PHYSWRESET) && - readl(p->base + OPHY_UOTGTUNE1) == OPHY_UOTGTUNE1_DEVICE && + readl(p->base + OPHY_UOTGTUNE1) == p->data->uotgtune1_device && readl(p->base + OPHY_UOTGTUNE2) == OPHY_UOTGTUNE2_DEVICE && readl(p->base + OPHYUNK4) == OPHYUNK4_START; } @@ -192,7 +235,7 @@ static int s5l_usbphy_cold_init(struct s5l_usbphy *p) writel(OPHYUNK4_START, p->base + OPHYUNK4); writel(OPHYPWR_ACTIVE, p->base + OPHYPWR); writel(OPHYUNK1_START, p->base + OPHYUNK1); - writel(OPHY_UOTGTUNE1_DEVICE, p->base + OPHY_UOTGTUNE1); + writel(p->data->uotgtune1_device, p->base + OPHY_UOTGTUNE1); writel(OPHY_UOTGTUNE2_DEVICE, p->base + OPHY_UOTGTUNE2); udelay(10); @@ -218,33 +261,33 @@ static int s5l_usbphy_init(struct phy *phy) goto out; } - if (p->data && p->data->cold_init) { + if (p->data->cold_init) { ret = clk_prepare_enable(p->refclk); if (ret) goto out; } /* Native firmware enables these parents in this order. */ - if ((readl(p->pmgr + PMGR_UPERF) & 0xff) != 0xff || - (readl(p->pmgr + PMGR_USB_OTG) & 0xff) != 0xff || - (readl(p->pmgr + PMGR_USBREG) & 0xff) != 0xff) + if ((readl(p->pmgr + p->data->pmgr_uperf) & 0xff) != 0xff || + (readl(p->pmgr + p->data->pmgr_usb_otg) & 0xff) != 0xff || + (readl(p->pmgr + p->data->pmgr_usbreg) & 0xff) != 0xff) usleep_range(10000, 11000); /* The legacy compatibles also name the A5X, whose power-state registers * are elsewhere: for them a parent that does not answer is not fatal. */ - ret = s5l_pmgr_on(p, PMGR_UPERF); - if (ret && p->data && p->data->cold_init) + ret = s5l_pmgr_on(p, p->data->pmgr_uperf); + if (ret && p->data->cold_init) goto disable_clock; - ret = s5l_pmgr_on(p, PMGR_USB_OTG); + ret = s5l_pmgr_on(p, p->data->pmgr_usb_otg); if (ret) goto disable_clock; - ret = s5l_pmgr_on(p, PMGR_USBREG); - if (ret && p->data && p->data->cold_init) + ret = s5l_pmgr_on(p, p->data->pmgr_usbreg); + if (ret && p->data->cold_init) goto disable_clock; ret = 0; udelay(100); - if (p->data && p->data->cold_init) { + if (p->data->cold_init) { if (!s5l_usbphy_is_ready(p)) { ret = s5l_usbphy_cold_init(p); if (ret) @@ -259,7 +302,7 @@ static int s5l_usbphy_init(struct phy *phy) writel(0xc, p->base + 0x14); writel(OPHYUNK1_START | OPHYUNK1_OTG_ENABLE, p->base + OPHYUNK1); writel(0xc00, p->base + 0x20); - writel(OPHY_UOTGTUNE1_DEVICE, p->base + OPHY_UOTGTUNE1); + writel(p->data->uotgtune1_device, p->base + OPHY_UOTGTUNE1); writel(OPHY_UOTGTUNE2_DEVICE, p->base + OPHY_UOTGTUNE2); writel(OPHYUNK4_START, p->base + OPHYUNK4); writel(OPHYPWR_ACTIVE, p->base + OPHYPWR); @@ -279,7 +322,7 @@ static int s5l_usbphy_init(struct phy *phy) dev_err(p->dev, "PHY reset did not clear: 0x%08x\n", v); goto disable_clock; } - if (p->data && p->data->cold_init && !s5l_usbphy_is_ready(p)) { + if (p->data->cold_init && !s5l_usbphy_is_ready(p)) { dev_err(p->dev, "PHY power or tuning did not latch\n"); ret = -EIO; goto disable_clock; @@ -288,7 +331,7 @@ static int s5l_usbphy_init(struct phy *phy) goto out; disable_clock: - if (p->data && p->data->cold_init) + if (p->data->cold_init) clk_disable_unprepare(p->refclk); out: mutex_unlock(&p->lock); @@ -406,17 +449,18 @@ static int s5l_hostphy_power_on(struct phy *phy) msleep(10); } - ret = s5l_pmgr_on(p, PMGR_USB_EHCI); + ret = s5l_pmgr_on(p, p->data->pmgr_usb_ehci); if (ret) goto err_disable_hsic; - ret = s5l_pmgr_on(p, PMGR_USB_OHCI0); + ret = s5l_pmgr_on(p, p->data->pmgr_usb_ohci0); if (ret) goto err_disable_hsic; if (p->usbcplx) writel(readl(p->usbcplx) | USBCPLX_HSIC_EN, p->usbcplx); - dev_dbg(p->dev, "host PHY on: EHCI(89)=0x%08x OHCI0(90)=0x%08x usb-complex=0x%08x\n", - readl(p->pmgr + PMGR_USB_EHCI), readl(p->pmgr + PMGR_USB_OHCI0), + dev_dbg(p->dev, "host PHY on: EHCI=0x%08x OHCI0=0x%08x usb-complex=0x%08x\n", + readl(p->pmgr + p->data->pmgr_usb_ehci), + readl(p->pmgr + p->data->pmgr_usb_ohci0), p->usbcplx ? readl(p->usbcplx) : 0); return 0; @@ -620,8 +664,10 @@ static int s5l_usbphy_probe(struct platform_device *pdev) p->dev = &pdev->dev; p->data = device_get_match_data(&pdev->dev); + if (!p->data) + p->data = &s5l8940x_usbphy_data; mutex_init(&p->lock); - if (p->data && p->data->cold_init) { + if (p->data->cold_init) { p->refclk = devm_clk_get(&pdev->dev, "ref"); if (IS_ERR(p->refclk)) return dev_err_probe(&pdev->dev, PTR_ERR(p->refclk), @@ -672,9 +718,10 @@ static int s5l_usbphy_probe(struct platform_device *pdev) } static const struct of_device_id s5l_usbphy_of_match[] = { + { .compatible = "apple,s5l8950x-otgphy", .data = &s5l8950x_usbphy_data }, { .compatible = "apple,s5l8942x-otgphy", .data = &s5l8942x_usbphy_data }, - { .compatible = "apple,s5l8940x-otgphy" }, - { .compatible = "apple,s5l8930x-otgphy" }, + { .compatible = "apple,s5l8940x-otgphy", .data = &s5l8940x_usbphy_data }, + { .compatible = "apple,s5l8930x-otgphy", .data = &s5l8940x_usbphy_data }, { .compatible = "apple,s5l8945x-usb1-phy" }, { }, }; From 28674e8e26d4b843783401917bf9bcc1c5234afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:55 +0100 Subject: [PATCH 08/16] dt-bindings: usb: dwc2: Add the Apple S5L8950X MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The A6's OTG controller is the A5's dwc2 and runs with the S5L8942X quirks and parameters. Add "apple,s5l8950x-dwc2" in front of the S5L8942X and S5L8940X compatibles, and require the PHY like theirs. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- Documentation/devicetree/bindings/usb/dwc2.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Documentation/devicetree/bindings/usb/dwc2.yaml b/Documentation/devicetree/bindings/usb/dwc2.yaml index 759eb83a36fb39..a838702328e789 100644 --- a/Documentation/devicetree/bindings/usb/dwc2.yaml +++ b/Documentation/devicetree/bindings/usb/dwc2.yaml @@ -20,6 +20,7 @@ allOf: enum: - apple,s5l8940x-dwc2 - apple,s5l8942x-dwc2 + - apple,s5l8950x-dwc2 then: required: - phys @@ -32,6 +33,11 @@ allOf: properties: compatible: oneOf: + - items: + - const: apple,s5l8950x-dwc2 + - const: apple,s5l8942x-dwc2 + - const: apple,s5l8940x-dwc2 + - const: snps,dwc2 - items: - const: apple,s5l8942x-dwc2 - const: apple,s5l8940x-dwc2 From 683349db425475e6e097a91be7e9924f44af7e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:56 +0100 Subject: [PATCH 09/16] ARM: dts: apple: Add the S5L8950X and the iPhone 5 (N41, N42) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit s5l8950x.dtsi describes the A6 as the iPhone5,2 iOS 8.4.1 (12H321) ADT, iBEC and kernelcache give it: two Swift cores started through PMGR, the AIC, the 24 MHz watchdog counter, PMGR, GPIO (232 pins), UART1-6, the OTG PHY and the dwc2 gadget, and simple-framebuffer over iBoot's 640x1136 scanout. The boot loader fills memory and framebuffer. Every PMGR gate is disabled in the SoC file and enabled by the board next to its consumer. A registered gate that nobody holds is switched off by clk_disable_unused, and iBoot leaves most of them on. When the SoC file registered the CDMA, SPI1, UART1-5 and I2C0-2 gates with no consumer, the N42 booted into clk: Disabling unused clocks phy-apple-s5l-usb 36000000.phy: PHY reference clock selection did not latch udc 36100000.usb: failed to start ...: -5 although no USB gate is among them. A gate that is not registered keeps the state iBoot left. CDMA has no Linux consumer and stays unregistered. s5l8950x-5.dtsi holds what the N41 and N42 share; so far UART6, the debug port. The two boards differ in cellular bands, which Linux does not drive. The N41 is untested. Tested on an iPhone5,2: both cores, the framebuffer console and the USB gadget. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/boot/dts/apple/Makefile | 2 + arch/arm/boot/dts/apple/s5l8950x-5.dtsi | 30 ++ arch/arm/boot/dts/apple/s5l8950x-n41.dts | 24 ++ arch/arm/boot/dts/apple/s5l8950x-n42.dts | 29 ++ arch/arm/boot/dts/apple/s5l8950x.dtsi | 384 +++++++++++++++++++++++ 5 files changed, 469 insertions(+) create mode 100644 arch/arm/boot/dts/apple/s5l8950x-5.dtsi create mode 100644 arch/arm/boot/dts/apple/s5l8950x-n41.dts create mode 100644 arch/arm/boot/dts/apple/s5l8950x-n42.dts create mode 100644 arch/arm/boot/dts/apple/s5l8950x.dtsi diff --git a/arch/arm/boot/dts/apple/Makefile b/arch/arm/boot/dts/apple/Makefile index ba7ec8501b0577..a60735b0905136 100644 --- a/arch/arm/boot/dts/apple/Makefile +++ b/arch/arm/boot/dts/apple/Makefile @@ -2,3 +2,5 @@ dtb-$(CONFIG_ARCH_APPLE_S5L) += s5l8942x-p105.dtb dtb-$(CONFIG_ARCH_APPLE_S5L) += s5l8945x-j1.dtb dtb-$(CONFIG_ARCH_APPLE_S5L) += s5l8940x-k93.dtb +dtb-$(CONFIG_ARCH_APPLE_S5L) += s5l8950x-n41.dtb +dtb-$(CONFIG_ARCH_APPLE_S5L) += s5l8950x-n42.dtb diff --git a/arch/arm/boot/dts/apple/s5l8950x-5.dtsi b/arch/arm/boot/dts/apple/s5l8950x-5.dtsi new file mode 100644 index 00000000000000..0d8858f3ece075 --- /dev/null +++ b/arch/arm/boot/dts/apple/s5l8950x-5.dtsi @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* + * Apple iPhone 5 (N41 / iPhone5,1 and N42 / iPhone5,2, S5L8950X / Apple A6): + * what the two models share. Apple sells them as one phone that differs in + * its cellular bands, which Linux does not drive, and the N42's ADT names + * its touch controller "multi-touch,n41". Everything below was read from + * the N42: the iPhone5,2 iOS 8.4.1 (12H321) ADT as iBoot passes it, and the + * iPhone5,2 10.3.4 (14G61) ADT and kernelcache. N41 is untested. + */ + +#include "s5l8950x.dtsi" + +/ { + aliases { + serial0 = &uart6; + }; +}; + +/* + * UART6 is the debug port. Production iBEC leaves it gated off unless + * debug-uarts is set, so there is no earlycon on it: the serial driver + * switches the gate on before its first register access. + */ +&uart6_ps { + status = "okay"; +}; + +&uart6 { + status = "okay"; +}; diff --git a/arch/arm/boot/dts/apple/s5l8950x-n41.dts b/arch/arm/boot/dts/apple/s5l8950x-n41.dts new file mode 100644 index 00000000000000..d1b4f7622346e6 --- /dev/null +++ b/arch/arm/boot/dts/apple/s5l8950x-n41.dts @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* + * Apple iPhone 5 (GSM) -- iPhone5,1 / N41AP / S5L8950X (Apple A6). + * + * The same phone as the N42 for everything Linux drives (s5l8950x-5.dtsi). + * Untested: every value was read from an N42. + */ + +/dts-v1/; + +#include "s5l8950x-5.dtsi" + +/ { + compatible = "apple,n41ap", "apple,s5l8950x", "apple,arm"; + model = "Apple iPhone 5 (GSM) (iPhone5,1)"; + + chosen { + bootargs = "console=tty0 console=ttyGS0,115200 lpj=6500000 root=/dev/ram0 rw rootwait panic=10 loglevel=8 ignore_loglevel g_cdc.host_addr=02:10:5a:51:00:01 g_cdc.dev_addr=02:10:5a:51:00:02"; + + /* Overwritten by m1n1 kboot_prepare_dt() when an initrd is loaded. */ + linux,initrd-start = <0x88000000>; + linux,initrd-end = <0x88000000>; + }; +}; diff --git a/arch/arm/boot/dts/apple/s5l8950x-n42.dts b/arch/arm/boot/dts/apple/s5l8950x-n42.dts new file mode 100644 index 00000000000000..57acd61ab757f3 --- /dev/null +++ b/arch/arm/boot/dts/apple/s5l8950x-n42.dts @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* + * Apple iPhone 5 (Global) -- iPhone5,2 / N42AP / S5L8950X (Apple A6). + * + * Booted from a jailbroken iOS through kDFU: iBSS, then a patched iBEC + * starts m1n1 (armv7) with bootx, and m1n1 starts Linux. + */ + +/dts-v1/; + +#include "s5l8950x-5.dtsi" + +/ { + compatible = "apple,n42ap", "apple,s5l8950x", "apple,arm"; + model = "Apple iPhone 5 (Global) (iPhone5,2)"; + + chosen { + /* + * console=tty0 draws the boot log on the 640x1136 portrait + * simple-framebuffer; ttyGS0 stays last so /dev/console is the + * USB CDC ACM serial gadget. + */ + bootargs = "console=tty0 console=ttyGS0,115200 lpj=6500000 root=/dev/ram0 rw rootwait panic=10 loglevel=8 ignore_loglevel g_cdc.host_addr=02:10:5a:52:00:01 g_cdc.dev_addr=02:10:5a:52:00:02"; + + /* Overwritten by m1n1 kboot_prepare_dt() when an initrd is loaded. */ + linux,initrd-start = <0x88000000>; + linux,initrd-end = <0x88000000>; + }; +}; diff --git a/arch/arm/boot/dts/apple/s5l8950x.dtsi b/arch/arm/boot/dts/apple/s5l8950x.dtsi new file mode 100644 index 00000000000000..a5c3b0b7caef6f --- /dev/null +++ b/arch/arm/boot/dts/apple/s5l8950x.dtsi @@ -0,0 +1,384 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* + * Apple A6 (S5L8950X) SoC core device tree include. + * + * Shared by iPhone 5 (N41 / iPhone5,1 and N42 / iPhone5,2). + * + * Every address, IRQ, and PMGR power-state offset below was verified from the + * decrypted iPhone5,2 iOS 8.4.1 (12H321) Apple Device Tree, iBEC, and + * kernelcache (AppleS5L8950XPerformanceController + USB PHY init). + * + * What sits outside the SoC (PMU, touch, display panel, Wi-Fi, Bluetooth, + * battery) is described by the iPhone 5 include, s5l8950x-5.dtsi. + */ + +/ { + #address-cells = <1>; + #size-cells = <1>; + interrupt-parent = <&aic>; + + /* + * 1 GiB DRAM at 0x80000000..0xc0000000. m1n1 overwrites reg at boot + * from iBoot's boot_args (phys_base, mem_size). + */ + memory@80000000 { + device_type = "memory"; + reg = <0x80000000 0x3fc00000>; + }; + + reserved-memory { + #address-cells = <1>; + #size-cells = <1>; + ranges; + + /* + * iBoot's scanout buffer. Base and size are patched by m1n1 + * at boot from boot_args.video together with framebuffer0. + */ + fb_region: framebuffer@bf000000 { + reg = <0xbf000000 0x400000>; + no-map; + }; + + /* + * First page of DRAM: physical 0 aliases here when a secondary + * core leaves reset via PMGR+0x1214/+0x1220 (platsmp.c). + */ + cpu_reset_page: cpu-reset@80000000 { + reg = <0x80000000 0x1000>; + no-map; + }; + + /* + * iBoot's 16 KiB persistent RAM (its console and panic data), + * which the boot chain keeps across a reset. + */ + pram_region: pram@bfffc000 { + reg = <0xbfffc000 0x4000>; + no-map; + }; + }; + + /* + * simple-framebuffer over iBoot's live CLCD scanout (640x1136 portrait + * Retina panel on iPhone 5). m1n1 patches reg/width/height/stride + * from boot_args.video before entering Linux. + */ + framebuffer0: framebuffer@bf000000 { + compatible = "simple-framebuffer"; + reg = <0xbf000000 0x400000>; + width = <640>; + height = <1136>; + stride = <2560>; + format = "a8r8g8b8"; + memory-region = <&fb_region>; + status = "okay"; + }; + + /* + * Dual Apple Swift (ARMv7s) cores with integrated architectural L2 + * cache (no external PL310, no Cortex-A9 SCU). + * + * ADT /cpus/cpu0 reg = 0, /cpus/cpu1 reg = 1. XNU indexes cores by + * MPIDR Aff0 (0 and 1, Aff1 = 0). Secondary bring-up uses PMGR + * +0x1214 (START) and +0x1220 (RUN), verified from + * AppleS5L8950XPerformanceController in iOS 8.4.1 kernelcache. + */ + cpus { + #address-cells = <1>; + #size-cells = <0>; + apple,cpu-reset-page = <&cpu_reset_page>; + + cpu0: cpu@0 { + device_type = "cpu"; + compatible = "apple,swift", "arm,armv7"; + reg = <0x0>; + enable-method = "apple,pmgr-core"; + }; + + cpu1: cpu@1 { + device_type = "cpu"; + compatible = "apple,swift", "arm,armv7"; + reg = <0x1>; + enable-method = "apple,pmgr-core"; + }; + }; + + refclk24mhz: clock-24000000 { + compatible = "fixed-clock"; + #clock-cells = <0>; + clock-frequency = <24000000>; + }; + + soc: soc@30000000 { + compatible = "simple-bus"; + #address-cells = <1>; + #size-cells = <1>; + ranges = <0x0 0x30000000 0x10000000>; + + /* Apple Interrupt Controller rev 1 (ADT /arm-io/aic). */ + aic: interrupt-controller@f200000 { + compatible = "aic,1"; + reg = <0xf200000 0x10000>; + interrupt-controller; + #interrupt-cells = <1>; + #address-cells = <0>; + apple,main-cpus = <2>; + }; + + /* + * 24 MHz watchdog counter (ADT /arm-io/wdt, wdt-version 1): + * used as wall clocksource, sched_clock, udelay timer, and + * CPU frequency calibration reference. + */ + wdt: timer@f103020 { + compatible = "apple,s5l8940x-wdt-timer"; + reg = <0xf103020 0x10>; + interrupts = <4>; + clock-frequency = <24000000>; + status = "okay"; + }; + + /* + * Power/clock manager (ADT /arm-io/pmgr). Kept disabled so no + * generic driver gates boot-critical clocks; platsmp.c looks + * up the node directly for PMGR_CORE_STOP/START/RUN + * (+0x1210/+0x1214/+0x1220). + */ + pmgr: power-management@f100000 { + compatible = "apple,s5l8950x-pmgr", "apple,s5l8940x-pmgr", "syscon"; + reg = <0xf100000 0x7000>; + status = "disabled"; + }; + + /* + * Individual PMGR power-state gates (offset = 0x1000 + ps_idx * 4 + * from the 81-entry x 44-byte ADT pmgr device-clocks table). + * + * Every gate is disabled here and enabled by the board next to + * the consumer that claims it. A registered gate nobody holds + * is switched off by clk_disable_unused at late_initcall, and + * iBoot leaves most of these on: on N42 that cut took the USB + * OTG PHY with it ("PHY reference clock selection did not + * latch", gadget start -EIO) although no USB gate is among + * them. An unregistered gate keeps the state iBoot left. + */ + /* + * CDMA (ADT id 0x31, index 0x1f). No Linux driver claims it yet, + * so no board should enable it: it must stay as iBoot left it. + */ + cdma_ps: cdma-ps@f10107c { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf10107c 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "cdma_ps"; + status = "disabled"; + }; + + spi1_ps: spi1-ps@f1010e8 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf1010e8 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "spi1_ps"; + status = "disabled"; + }; + + uart1_ps: uart1-ps@f1010fc { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf1010fc 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "uart1_ps"; + status = "disabled"; + }; + + uart2_ps: uart2-ps@f101100 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101100 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "uart2_ps"; + status = "disabled"; + }; + + uart3_ps: uart3-ps@f101104 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101104 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "uart3_ps"; + status = "disabled"; + }; + + uart4_ps: uart4-ps@f101108 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101108 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "uart4_ps"; + status = "disabled"; + }; + + uart5_ps: uart5-ps@f10110c { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf10110c 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "uart5_ps"; + status = "disabled"; + }; + + uart6_ps: uart6-ps@f101110 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101110 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "uart6_ps"; + status = "disabled"; + }; + + i2c0_ps: i2c0-ps@f101118 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101118 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "i2c0_ps"; + status = "disabled"; + }; + + i2c1_ps: i2c1-ps@f10111c { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf10111c 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "i2c1_ps"; + status = "disabled"; + }; + + i2c2_ps: i2c2-ps@f101120 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101120 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "i2c2_ps"; + status = "disabled"; + }; + + /* + * GPIO / pinctrl block (ADT /arm-io/gpio, "gpio,s5l8950x", + * "gpio,s5l8930x"): 29 ports = 232 pins, IRQ 119. + */ + gpio: gpio@fa00000 { + compatible = "apple,s5l8950x-gpio", "apple,s5l8940x-gpio"; + reg = <0xfa00000 0x1000>; + interrupts = <119>; + gpio-controller; + #gpio-cells = <2>; + interrupt-controller; + #interrupt-cells = <2>; + #address-cells = <0>; + pin-count = <232>; + interrupt-groups = <1>; + status = "okay"; + }; + + /* + * UARTs (ADT /arm-io/uart1..uart6; S5L8950X has no uart0). + * uart6 is the board's boot-console / debug-console port. + */ + uart1: serial@2600000 { + compatible = "apple,s5l8940x-uart", "apple,s5l-uart", + "samsung,s3c6400-uart"; + reg = <0x2600000 0x1000>; + interrupts = <22>; + clocks = <&uart1_ps>, <&refclk24mhz>; + clock-names = "uart", "clk_uart_baud0"; + status = "disabled"; + }; + + uart2: serial@2700000 { + compatible = "apple,s5l8940x-uart", "apple,s5l-uart", + "samsung,s3c6400-uart"; + reg = <0x2700000 0x1000>; + interrupts = <23>; + clocks = <&uart2_ps>, <&refclk24mhz>; + clock-names = "uart", "clk_uart_baud0"; + status = "disabled"; + }; + + uart3: serial@2800000 { + compatible = "apple,s5l8940x-uart", "apple,s5l-uart", + "samsung,s3c6400-uart"; + reg = <0x2800000 0x1000>; + interrupts = <24>; + clocks = <&uart3_ps>, <&refclk24mhz>; + clock-names = "uart", "clk_uart_baud0"; + status = "disabled"; + }; + + uart4: serial@2900000 { + compatible = "apple,s5l8940x-uart", "apple,s5l-uart", + "samsung,s3c6400-uart"; + reg = <0x2900000 0x1000>; + interrupts = <25>; + clocks = <&uart4_ps>, <&refclk24mhz>; + clock-names = "uart", "clk_uart_baud0"; + status = "disabled"; + }; + + uart5: serial@2a00000 { + compatible = "apple,s5l8940x-uart", "apple,s5l-uart", + "samsung,s3c6400-uart"; + reg = <0x2a00000 0x1000>; + interrupts = <26>; + clocks = <&uart5_ps>, <&refclk24mhz>; + clock-names = "uart", "clk_uart_baud0"; + status = "disabled"; + }; + + uart6: serial@2b00000 { + compatible = "apple,s5l8940x-uart", "apple,s5l-uart", + "samsung,s3c6400-uart"; + reg = <0x2b00000 0x1000>; + interrupts = <27>; + clocks = <&uart6_ps>, <&refclk24mhz>; + clock-names = "uart", "clk_uart_baud0"; + status = "disabled"; + }; + + /* + * USB OTG PHY (ADT /arm-io/otgphyctrl + /arm-io/usb-complex). + * Uses S5L8950X PMGR gates (+0x1084/+0x1088/+0x109c) and + * OPHY_UOTGTUNE1 = 0x749. + */ + otgphy: phy@6000000 { + compatible = "apple,s5l8950x-otgphy"; + reg = <0x6000000 0x1000>, /* otgphyctrl */ + <0xf108000 0x1000>; /* usb-complex */ + #phy-cells = <1>; + clocks = <&refclk24mhz>; + clock-names = "ref"; + status = "okay"; + }; + + /* + * DWC2 USB-OTG gadget controller (ADT /arm-io/usb-complex/usb-device: + * reg 0x06100000, IRQ 11, num-of-eps 14, fifo-depth 0x820). + */ + dwc2: usb@6100000 { + compatible = "apple,s5l8950x-dwc2", "apple,s5l8942x-dwc2", + "apple,s5l8940x-dwc2", "snps,dwc2"; + reg = <0x6100000 0x10000>; + interrupts = <11>; + phys = <&otgphy 0>; + phy-names = "usb2-phy"; + dr_mode = "peripheral"; + g-rx-fifo-size = <512>; + g-np-tx-fifo-size = <128>; + g-tx-fifo-size = <128 128 128 128 128 128 128 128>; + status = "okay"; + }; + }; +}; From 738359a5bb8cf70c71f3c5f6ff226d74151cee89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:56 +0100 Subject: [PATCH 10/16] phy: apple-s5l-usb: Program the S5L8950X HSIC tuning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The N42's otgphyctrl is PHY version 4. Besides uotgtune1 = 0x749 and uotgtune2 = 0x2ff3, its ADT node has hsic-tuning-val = 0xa30a3a3, which AppleS5L8930XUSBPhy writes to +0x5c (iPhone5,2 iOS 10.3.4). Add it to the per-SoC data and write it with the other tuning values on a cold start, and on its own when the PHY was left running. HSIC port 4 carries the BCM4334 Wi-Fi. The other SoCs have no HSIC tuning value and write nothing new. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/phy/phy-apple-s5l-usb.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/phy/phy-apple-s5l-usb.c b/drivers/phy/phy-apple-s5l-usb.c index 2de812f98e08b9..5801e6d74ec508 100644 --- a/drivers/phy/phy-apple-s5l-usb.c +++ b/drivers/phy/phy-apple-s5l-usb.c @@ -37,6 +37,10 @@ #define OPHY_UOTGTUNE1_DEVICE 0x549 #define OPHY_UOTGTUNE1_S5L8950X_DEVICE 0x749 #define OPHY_UOTGTUNE2_DEVICE 0x2ff3 +/* PHY version 4 (S5L8950X): ADT otgphyctrl hsic-tuning-val, written to +0x5c + * by AppleS5L8930XUSBPhy (14G61). */ +#define OPHY_HSICTUNE 0x5c +#define OPHY_HSICTUNE_S5L8950X 0xa30a3a3 #define PMGR_BASE 0x3F100000 #define PMGR_GATE_ON 0xF @@ -118,6 +122,7 @@ struct s5l_usbphy_soc_data { u32 pmgr_usb_ehci; u32 pmgr_usb_ohci0; u32 uotgtune1_device; + u32 hsic_tuning; /* OPHY_HSICTUNE, when non-zero */ }; static const struct s5l_usbphy_soc_data s5l8940x_usbphy_data = { @@ -158,6 +163,7 @@ static const struct s5l_usbphy_soc_data s5l8950x_usbphy_data = { .pmgr_usb_ehci = 0x108c, .pmgr_usb_ohci0 = 0x1094, .uotgtune1_device = OPHY_UOTGTUNE1_S5L8950X_DEVICE, + .hsic_tuning = OPHY_HSICTUNE_S5L8950X, }; struct s5l_usbphy { @@ -237,6 +243,8 @@ static int s5l_usbphy_cold_init(struct s5l_usbphy *p) writel(OPHYUNK1_START, p->base + OPHYUNK1); writel(p->data->uotgtune1_device, p->base + OPHY_UOTGTUNE1); writel(OPHY_UOTGTUNE2_DEVICE, p->base + OPHY_UOTGTUNE2); + if (p->data->hsic_tuning) + writel(p->data->hsic_tuning, p->base + OPHY_HSICTUNE); udelay(10); v = readl(p->base + OPHYCLK); @@ -292,6 +300,8 @@ static int s5l_usbphy_init(struct phy *phy) ret = s5l_usbphy_cold_init(p); if (ret) goto disable_clock; + } else if (p->data->hsic_tuning) { + writel(p->data->hsic_tuning, p->base + OPHY_HSICTUNE); } } else if (readl(p->base + OPHYPWR) != OPHYPWR_ACTIVE) { /* Legacy compatibles keep the tuning firmware leaves, unless From 0bdd40d1cd641d18b08cfa54191a50433b774466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:57 +0100 Subject: [PATCH 11/16] phy: apple-s5l-usb: Hold the S5L8950X OTG gates from probe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The OTG PHY switched UPERF, USB-OTG and USBREG on with raw PMGR writes in phy_init, behind the clock framework's back. Describe the three as PMGR gate clocks in s5l8950x.dtsi (+0x1084, +0x1088, +0x109c; ADT device-clocks ids 0x04, 0x35, 0x06) and give them to the PHY, which switches them on at probe in the order the boot loader uses and keeps them on, the way the H2FMI driver holds its gates. The gadget is the console and the network, so nothing switches them off again: not a failed probe, not an unbind, and with the enable count held not clk_disable_unused either. The clocks are optional and looked up by name ("uperf", "otg", "usbreg"). P105, J1 and the other trees name only "ref", get none, and run the code they ran before; phy_init's register checks stay for them and are a no-op on the A6 once the gates are held. The host side's USB-EHCI and USB-OHCI0 gates stay with the host PHY's power-on and are not clocks, so nothing at late_initcall reaches them either. When the reference clock selection does not latch, the error now says what OPHYCLK, OPHYPWR and the three gate registers read. Tested on an iPhone5,2: the gadget comes up without clk_ignore_unused. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/boot/dts/apple/s5l8950x.dtsi | 40 ++++++++++++++++-- drivers/phy/phy-apple-s5l-usb.c | 60 ++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/arch/arm/boot/dts/apple/s5l8950x.dtsi b/arch/arm/boot/dts/apple/s5l8950x.dtsi index a5c3b0b7caef6f..84d98ff6de92ff 100644 --- a/arch/arm/boot/dts/apple/s5l8950x.dtsi +++ b/arch/arm/boot/dts/apple/s5l8950x.dtsi @@ -151,6 +151,38 @@ status = "disabled"; }; + /* + * USB OTG gates (ADT usb-complex clock-gates; device-clocks + * UPERF id 0x04 index 0x21, USB-OTG id 0x35 index 0x22, USBREG + * id 0x06 index 0x27). Registered with the OTG PHY, which holds + * them from probe. The host side's USB-EHCI (+0x108c) and + * USB-OHCI0 (+0x1094) stay with the PHY's host power-on and are + * not clocks, so nothing at late_initcall can switch them off. + */ + uperf_ps: uperf-ps@f101084 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101084 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "uperf_ps"; + }; + + usb_otg_ps: usb-otg-ps@f101088 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101088 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "usb_otg_ps"; + }; + + usbreg_ps: usbreg-ps@f10109c { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf10109c 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "usbreg_ps"; + }; + /* * Individual PMGR power-state gates (offset = 0x1000 + ps_idx * 4 * from the 81-entry x 44-byte ADT pmgr device-clocks table). @@ -351,15 +383,17 @@ /* * USB OTG PHY (ADT /arm-io/otgphyctrl + /arm-io/usb-complex). * Uses S5L8950X PMGR gates (+0x1084/+0x1088/+0x109c) and - * OPHY_UOTGTUNE1 = 0x749. + * OPHY_UOTGTUNE1 = 0x749. The PHY holds the three OTG gates + * from probe. */ otgphy: phy@6000000 { compatible = "apple,s5l8950x-otgphy"; reg = <0x6000000 0x1000>, /* otgphyctrl */ <0xf108000 0x1000>; /* usb-complex */ #phy-cells = <1>; - clocks = <&refclk24mhz>; - clock-names = "ref"; + clocks = <&refclk24mhz>, <&uperf_ps>, <&usb_otg_ps>, + <&usbreg_ps>; + clock-names = "ref", "uperf", "otg", "usbreg"; status = "okay"; }; diff --git a/drivers/phy/phy-apple-s5l-usb.c b/drivers/phy/phy-apple-s5l-usb.c index 5801e6d74ec508..97a6b4a068711d 100644 --- a/drivers/phy/phy-apple-s5l-usb.c +++ b/drivers/phy/phy-apple-s5l-usb.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -176,6 +177,7 @@ struct s5l_usbphy { bool lpo_supply_on; struct device *dev; struct clk *refclk; + struct clk_bulk_data gates[3]; /* uperf, otg, usbreg; optional */ const struct s5l_usbphy_soc_data *data; struct mutex lock; unsigned int init_users; @@ -200,6 +202,44 @@ static int s5l_pmgr_on(struct s5l_usbphy *p, u32 off) return -ETIMEDOUT; } +/* + * The OTG side's PMGR gates, when the tree names them as clocks (S5L8950X: + * UPERF, USB-OTG and USBREG, ADT ids 0x04, 0x35 and 0x06): switched on at + * probe in the order native firmware uses, and held from then on. The + * gadget is the console and the network, so they are never switched off, + * not on a failed probe and not on unbind; the enable count also keeps + * clk_disable_unused away from them. s5l_usbphy_init() still checks the + * same registers directly, which is all a tree without these clocks gets. + */ +static const char * const s5l_usbphy_gate_names[] = { "uperf", "otg", "usbreg" }; + +static int s5l_usbphy_hold_gates(struct s5l_usbphy *p) +{ + int i, n = ARRAY_SIZE(s5l_usbphy_gate_names), ret; + bool any = false; + + BUILD_BUG_ON(ARRAY_SIZE(s5l_usbphy_gate_names) != ARRAY_SIZE(p->gates)); + for (i = 0; i < n; i++) + p->gates[i].id = s5l_usbphy_gate_names[i]; + ret = devm_clk_bulk_get_optional(p->dev, n, p->gates); + if (ret) + return dev_err_probe(p->dev, ret, "PMGR gates\n"); + for (i = 0; i < n; i++) + any |= !!p->gates[i].clk; + if (!any) + return 0; + + for (i = 0; i < n; i++) + if (p->gates[i].clk) + dev_dbg(p->dev, "PMGR gate %s was %s\n", + __clk_get_name(p->gates[i].clk), + __clk_is_enabled(p->gates[i].clk) ? "on" : "off"); + ret = clk_bulk_prepare_enable(n, p->gates); + if (ret) + return dev_err_probe(p->dev, ret, "enabling the PMGR gates\n"); + return 0; +} + static int s5l_usbphy_clk_sel(unsigned long rate) { switch (rate) { @@ -249,8 +289,13 @@ static int s5l_usbphy_cold_init(struct s5l_usbphy *p) v = readl(p->base + OPHYCLK); writel((v & ~OPHYCLK_CLKSEL_MASK) | clksel, p->base + OPHYCLK); - if ((readl(p->base + OPHYCLK) & OPHYCLK_CLKSEL_MASK) != clksel) { - dev_err(p->dev, "PHY reference clock selection did not latch\n"); + v = readl(p->base + OPHYCLK); + if ((v & OPHYCLK_CLKSEL_MASK) != clksel) { + dev_err(p->dev, "PHY reference clock selection did not latch: OPHYCLK 0x%08x OPHYPWR 0x%08x PMGR uperf 0x%08x otg 0x%08x usbreg 0x%08x\n", + v, readl(p->base + OPHYPWR), + readl(p->pmgr + p->data->pmgr_uperf), + readl(p->pmgr + p->data->pmgr_usb_otg), + readl(p->pmgr + p->data->pmgr_usbreg)); return -EIO; } @@ -469,9 +514,9 @@ static int s5l_hostphy_power_on(struct phy *phy) writel(readl(p->usbcplx) | USBCPLX_HSIC_EN, p->usbcplx); dev_dbg(p->dev, "host PHY on: EHCI=0x%08x OHCI0=0x%08x usb-complex=0x%08x\n", - readl(p->pmgr + p->data->pmgr_usb_ehci), - readl(p->pmgr + p->data->pmgr_usb_ohci0), - p->usbcplx ? readl(p->usbcplx) : 0); + readl(p->pmgr + p->data->pmgr_usb_ehci), + readl(p->pmgr + p->data->pmgr_usb_ohci0), + p->usbcplx ? readl(p->usbcplx) : 0); return 0; err_disable_hsic: @@ -664,6 +709,7 @@ static int s5l_usbphy_probe(struct platform_device *pdev) struct s5l_usbphy *p; struct phy *phy; struct phy_provider *provider; + int ret; if (of_device_is_compatible(pdev->dev.of_node, "apple,s5l8945x-usb1-phy")) return s5l_usb1phy_probe(pdev); @@ -696,6 +742,10 @@ static int s5l_usbphy_probe(struct platform_device *pdev) return -ENOMEM; } + ret = s5l_usbphy_hold_gates(p); + if (ret) + return ret; + /* usb-complex, for the HSIC enable bit: optional, the second reg. */ if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) { p->usbcplx = devm_platform_ioremap_resource(pdev, 1); From 32b3b6953cc2254d8c67a01d049c25d541db4eef Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Wed, 2 Dec 2026 21:44:10 +0800 Subject: [PATCH 12/16] irqchip/apple-aic1: Give secondary CPUs their AIC timer on every Swift SoC The per-CPU AIC timer for secondary cores was switched on only for boards compatible with "apple,s5l8950x". What decides it is the core, not the board: the A5's Cortex-A9 has the ARM PMU whose tick serves the other core, and Apple's Swift cores have none. The A6X (S5L8955X) uses Swift cores and the same "aic,1" controller, so key the choice on the boot CPU's part number, as pmccntr.c already does, and every Swift board gets a tick on each core it brings up. Assisted-by: LLM Signed-off-by: Henrik Klee --- drivers/irqchip/irq-apple-aic1.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/irqchip/irq-apple-aic1.c b/drivers/irqchip/irq-apple-aic1.c index 71e0f3cb1a799b..3216d7de00d1d4 100644 --- a/drivers/irqchip/irq-apple-aic1.c +++ b/drivers/irqchip/irq-apple-aic1.c @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -1040,8 +1041,13 @@ static bool __init aic1_tmr_setup_secondaries(void) { int ret; - /* The A5 keeps its PMU tick and CPU0-only AIC timer as they were. */ - if (num_possible_cpus() < 2 || !of_machine_is_compatible("apple,s5l8950x")) + /* + * Only the A5's Cortex-A9 has the ARM PMU whose tick covers the other + * core; it keeps that and the CPU0-only AIC timer as they were. Apple's + * own cores (Swift on the A6 and A6X) have no PMU, so every further + * CPU takes its own AIC timer, whatever the board. + */ + if (num_possible_cpus() < 2 || read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) return false; ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, From 2559443b2df9ff99266819ece1ac25a9547c8ff7 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Wed, 2 Dec 2026 21:44:31 +0800 Subject: [PATCH 13/16] ARM: apple: Run the A6's watchdog counter handling on the A6X too The watchdog counter of the A6 runs only while WDT_CTRL_ENABLE is set, and its bite needs ENABLE together with RESET_EN; that was decided by the "apple,s5l8950x" board compatible alone. The A6X (S5L8955X) is of the same family (its ADT pmgr lists "pmgr,s5l8950x" after its own) with the same wdt-version 1 block, and having no cycle counter it relies on this counter for all its time. Put the check in one helper that names both chips, so the A6X's clocksource ticks and its restart bites as the A6's do. Assisted-by: LLM Signed-off-by: Henrik Klee --- arch/arm/mach-apple/apple_wdt_clkevt.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/arch/arm/mach-apple/apple_wdt_clkevt.c b/arch/arm/mach-apple/apple_wdt_clkevt.c index 6863ba9ba95481..a6ae4ced9c1ffe 100644 --- a/arch/arm/mach-apple/apple_wdt_clkevt.c +++ b/arch/arm/mach-apple/apple_wdt_clkevt.c @@ -111,6 +111,18 @@ static struct delay_timer apple_wdt_delay = { .freq = APPLE_WDT_REF_HZ, }; +/* + * The A6 family's watchdog counter runs only while WDT_CTRL_ENABLE is set, + * and its bite needs ENABLE with RESET_EN. The A6X (S5L8955X) is of that + * family -- its ADT pmgr names "pmgr,s5l8950x" after its own -- and has the + * same wdt-version 1 block, so it gets the A6's handling too. + */ +static bool apple_wdt_counter_gated(void) +{ + return of_machine_is_compatible("apple,s5l8950x") || + of_machine_is_compatible("apple,s5l8955x"); +} + /* * Map the watchdog's 24 MHz counter (once). The A5 does this from the CPU * calibration; the A6 has no cycle counter to calibrate, so its clocksource @@ -141,7 +153,7 @@ static bool __init apple_wdt_map(void) * counter without RESET_EN so calibration and the clocksource tick; if * a boot watchdog is already armed (0xc), leave it intact. */ - if (of_machine_is_compatible("apple,s5l8950x")) { + if (apple_wdt_counter_gated()) { u32 ctrl = readl_relaxed(wdt_base + WDT_CTRL); if (!(ctrl & WDT_CTRL_ENABLE)) { @@ -227,7 +239,7 @@ unsigned long __init apple_s5l_calibrate_cpu_hz(void) */ static int apple_wdt_restart(struct sys_off_data *data) { - if (of_machine_is_compatible("apple,s5l8950x")) { + if (apple_wdt_counter_gated()) { /* * S5L8950X (A6) requires both WDT_CTRL_ENABLE and WDT_CTRL_RESET_EN * (0xc), following iBEC's wdt_enable() sequence. @@ -261,7 +273,7 @@ static int apple_wdt_reboot_notify(struct notifier_block *nb, unsigned long acti { if (action == SYS_RESTART) { u32 now = readl_relaxed(wdt_base + WDT_CUR_TIME); - u32 ctrl = of_machine_is_compatible("apple,s5l8950x") ? + u32 ctrl = apple_wdt_counter_gated() ? (WDT_CTRL_ENABLE | WDT_CTRL_RESET_EN) : WDT_CTRL_RESET_EN; writel_relaxed(now + 10 * APPLE_WDT_REF_HZ, wdt_base + WDT_BITE_TIME); From e9db7c6b23896b652f887366c1047413f3a16e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Tue, 1 Dec 2026 18:07:57 +0100 Subject: [PATCH 14/16] ARM: apple: Pet the S5L watchdog from Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Register the wdt-version 1 watchdog (the 24 MHz counter that is already the clocksource) as a watchdog device, so a hung system is reset within the timeout and a healthy one runs for ever: the kernel pets it from boot until userspace (systemd's RuntimeWatchdogSec=) opens /dev/watchdog. A ping moves BITE_TIME to CUR_TIME + timeout; CUR_TIME is never written, being the clocksource. The comparator fires when the counter reaches BITE_TIME, not while it is above it (iBoot enables RESET_EN with CUR_TIME at 0x80000000 and a small BITE_TIME), so a bite past the 32-bit wrap is correct and timeouts go up to 178 s. On the A6, ENABLE stays set: the counter only runs with it. It is started at boot whatever the boot loader armed, with no way out: this hardware has no power off, so a halt ends in the bite as well. The restart path keeps its own bite. apple_wdt_timeout= (60 s) sets the timeout; 0 leaves the watchdog as the boot loader left it. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/mach-apple/apple_wdt_clkevt.c | 110 +++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/arch/arm/mach-apple/apple_wdt_clkevt.c b/arch/arm/mach-apple/apple_wdt_clkevt.c index a6ae4ced9c1ffe..66974d157c77df 100644 --- a/arch/arm/mach-apple/apple_wdt_clkevt.c +++ b/arch/arm/mach-apple/apple_wdt_clkevt.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include @@ -304,3 +305,112 @@ static int __init apple_wdt_restart_init(void) return 0; } device_initcall(apple_wdt_restart_init); + +/* + * The watchdog as a watchdog: /dev/watchdog for systemd (RuntimeWatchdogSec=) + * and the kernel's own pinger until userspace opens it, so a healthy system + * runs for ever and a hung one is reset -- into iOS, on a phone started from + * it -- within the timeout, with no button pressed. + * + * A ping moves the bite forward: BITE_TIME = CUR_TIME + timeout. CUR_TIME is + * never written: it is the clocksource. The comparator fires when the counter + * reaches BITE_TIME, not while it is above it (iBoot enables RESET_EN with + * CUR_TIME at 0x80000000 and a small BITE_TIME, and nothing resets), so a bite + * past the 32-bit wrap (about every 179 s) is simply where the counter gets to + * after wrapping; the reboot notifier above relies on the same. Timeouts stop + * at 178 s, one turn of the counter. + * + * It is started at boot whatever the boot loader left (m1n1 hands over with a + * window armed), and it has no way out: a poweroff or halt, which this + * hardware cannot do, ends in the bite too. apple_wdt_timeout=0 on the + * command line leaves the watchdog as the boot loader left it, for debugging. + */ +static unsigned int apple_wdt_timeout = 60; +core_param(apple_wdt_timeout, apple_wdt_timeout, uint, 0444); + +static u32 apple_wdt_ctrl_on(void) +{ + /* the A6's counter only runs with ENABLE (see apple_wdt_map()) */ + return of_machine_is_compatible("apple,s5l8950x") ? WDT_CTRL_ENABLE : 0; +} + +static int apple_wdt_ping(struct watchdog_device *wdd) +{ + u32 now = readl_relaxed(wdt_base + WDT_CUR_TIME); + + writel(now + wdd->timeout * APPLE_WDT_REF_HZ, wdt_base + WDT_BITE_TIME); + return 0; +} + +static int apple_wdt_start(struct watchdog_device *wdd) +{ + apple_wdt_ping(wdd); + writel(apple_wdt_ctrl_on() | WDT_CTRL_RESET_EN, wdt_base + WDT_CTRL); + (void)readl(wdt_base + WDT_CTRL); + return 0; +} + +static int apple_wdt_stop(struct watchdog_device *wdd) +{ + writel(apple_wdt_ctrl_on(), wdt_base + WDT_CTRL); + (void)readl(wdt_base + WDT_CTRL); + return 0; +} + +static int apple_wdt_set_timeout(struct watchdog_device *wdd, unsigned int timeout) +{ + wdd->timeout = timeout; + return apple_wdt_ping(wdd); +} + +static const struct watchdog_ops apple_wdt_ops = { + .owner = THIS_MODULE, + .start = apple_wdt_start, + .stop = apple_wdt_stop, + .ping = apple_wdt_ping, + .set_timeout = apple_wdt_set_timeout, +}; + +static const struct watchdog_info apple_wdt_info = { + .identity = "Apple S5L wdt-version 1", + .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, +}; + +static struct watchdog_device apple_wdt_wdd = { + .info = &apple_wdt_info, + .ops = &apple_wdt_ops, + .min_timeout = 1, + .max_timeout = U32_MAX / APPLE_WDT_REF_HZ, /* 178 s */ +}; + +static int __init apple_wdt_watchdog_init(void) +{ + u32 ctrl; + int ret; + + if (!wdt_base) + return 0; + ctrl = readl_relaxed(wdt_base + WDT_CTRL); + if (!apple_wdt_timeout) { + pr_info("watchdog: left as the boot loader left it (CTRL %#x)\n", ctrl); + return 0; + } + apple_wdt_wdd.timeout = clamp(apple_wdt_timeout, apple_wdt_wdd.min_timeout, + apple_wdt_wdd.max_timeout); + watchdog_set_nowayout(&apple_wdt_wdd, true); + /* No watchdog_stop_on_reboot(): a halt or poweroff must still end in the + * bite, and a restart keeps the 10 s net the reboot notifier arms. */ + apple_wdt_start(&apple_wdt_wdd); + set_bit(WDOG_HW_RUNNING, &apple_wdt_wdd.status); + ret = watchdog_register_device(&apple_wdt_wdd); + if (ret) { + /* nobody would pet it: back to what the boot loader left */ + writel(ctrl, wdt_base + WDT_CTRL); + pr_err("watchdog: not registered (%d); CTRL back to %#x\n", ret, ctrl); + return 0; + } + pr_info("watchdog: armed, %u s, petted by the kernel until userspace opens /dev/watchdog (boot CTRL %#x)\n", + apple_wdt_wdd.timeout, ctrl); + return 0; +} +device_initcall(apple_wdt_watchdog_init); From c02b41a7832ce6e05c44a6bca50a13e658172a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Tue, 1 Dec 2026 18:07:57 +0100 Subject: [PATCH 15/16] ARM: apple: Power off and halt through the S5L watchdog too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The board has no way to cut its own power, so a power off halted and waited for the watchdog's timeout. A phone booted from iOS that powers off belongs back in iOS: restart through the watchdog at once, as a low-priority power-off handler that a real one would replace. Halt and power off also get the 10 s net the reboot notifier arms for a restart. For the flights, a ping whose bite lies past the counter's 32-bit wrap is logged, and debugfs apple-wdt/state shows the counter, the bite and CTRL. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/mach-apple/apple_wdt_clkevt.c | 39 ++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/arch/arm/mach-apple/apple_wdt_clkevt.c b/arch/arm/mach-apple/apple_wdt_clkevt.c index 66974d157c77df..78ed6f3d965d4e 100644 --- a/arch/arm/mach-apple/apple_wdt_clkevt.c +++ b/arch/arm/mach-apple/apple_wdt_clkevt.c @@ -37,6 +37,7 @@ #define pr_fmt(fmt) "apple-wdt: " fmt #include +#include #include #include #include @@ -47,6 +48,7 @@ #include #include #include +#include #include #include @@ -272,18 +274,30 @@ static int apple_wdt_restart(struct sys_off_data *data) */ static int apple_wdt_reboot_notify(struct notifier_block *nb, unsigned long action, void *data) { - if (action == SYS_RESTART) { + if (action == SYS_RESTART || action == SYS_POWER_OFF || action == SYS_HALT) { u32 now = readl_relaxed(wdt_base + WDT_CUR_TIME); u32 ctrl = apple_wdt_counter_gated() ? (WDT_CTRL_ENABLE | WDT_CTRL_RESET_EN) : WDT_CTRL_RESET_EN; writel_relaxed(now + 10 * APPLE_WDT_REF_HZ, wdt_base + WDT_BITE_TIME); writel(ctrl, wdt_base + WDT_CTRL); - pr_info("restart: the watchdog will reset the machine in 10 s at the latest\n"); + pr_info("%s: the watchdog will reset the machine in 10 s at the latest\n", + action == SYS_RESTART ? "restart" : action == SYS_POWER_OFF ? "power off" : "halt"); } return NOTIFY_DONE; } +/* + * Power off: this board has no way to cut its own power, and a phone booted + * from iOS that "powers off" belongs back in iOS. So a power off is the + * restart, at once -- at a low priority, so a real power-off driver wins. + */ +static int apple_wdt_power_off(struct sys_off_data *data) +{ + pr_emerg("power off: no power-off control here; restarting through the watchdog\n"); + return apple_wdt_restart(data); +} + static struct notifier_block apple_wdt_reboot_nb = { .notifier_call = apple_wdt_reboot_notify, .priority = INT_MAX, /* before anything else gets to hang */ @@ -302,6 +316,10 @@ static int __init apple_wdt_restart_init(void) pr_err("no restart handler: %ld\n", PTR_ERR(h)); else pr_info("restart through the watchdog's bite\n"); + h = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_LOW, + apple_wdt_power_off, NULL); + if (IS_ERR(h)) + pr_err("no power-off handler: %ld\n", PTR_ERR(h)); return 0; } device_initcall(apple_wdt_restart_init); @@ -337,10 +355,23 @@ static u32 apple_wdt_ctrl_on(void) static int apple_wdt_ping(struct watchdog_device *wdd) { u32 now = readl_relaxed(wdt_base + WDT_CUR_TIME); + u32 bite = now + wdd->timeout * APPLE_WDT_REF_HZ; - writel(now + wdd->timeout * APPLE_WDT_REF_HZ, wdt_base + WDT_BITE_TIME); + writel(bite, wdt_base + WDT_BITE_TIME); + /* the bite lies past the counter's wrap: said once per turn or so */ + if (bite < now) + pr_info("watchdog: ping across the wrap: CUR %#010x BITE %#010x\n", now, bite); + return 0; +} + +/* debugfs apple-wdt/state: the counter, the bite and CTRL, for the flights */ +static int apple_wdt_state_show(struct seq_file *s, void *unused) +{ + seq_printf(s, "cur %#010x bite %#010x ctrl %#x\n", readl_relaxed(wdt_base + WDT_CUR_TIME), + readl_relaxed(wdt_base + WDT_BITE_TIME), readl_relaxed(wdt_base + WDT_CTRL)); return 0; } +DEFINE_SHOW_ATTRIBUTE(apple_wdt_state); static int apple_wdt_start(struct watchdog_device *wdd) { @@ -411,6 +442,8 @@ static int __init apple_wdt_watchdog_init(void) } pr_info("watchdog: armed, %u s, petted by the kernel until userspace opens /dev/watchdog (boot CTRL %#x)\n", apple_wdt_wdd.timeout, ctrl); + debugfs_create_file("state", 0444, debugfs_create_dir("apple-wdt", NULL), NULL, + &apple_wdt_state_fops); return 0; } device_initcall(apple_wdt_watchdog_init); From 1da9dede76fc86f2f4615966b17ba1b5ffd2b9a0 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Wed, 2 Dec 2026 21:49:18 +0800 Subject: [PATCH 16/16] ARM: apple: Give the A6X the watchdog's ENABLE bit; select the watchdog core The watchdog that Linux now keeps armed set WDT_CTRL_ENABLE only on boards named "apple,s5l8950x". The A6X's counter needs the same bit to run, so use the A6-family helper the clocksource and restart paths use: on the A6X, starting or stopping the watchdog would otherwise stop its only clock. The watchdog registers with the watchdog core from code built into every S5L kernel, but nothing asked for that core: it was only there because unrelated watchdog drivers in the defconfig select it. Select WATCHDOG and WATCHDOG_CORE from ARCH_APPLE_S5L, so a configuration without those drivers still links. Assisted-by: LLM Signed-off-by: Henrik Klee --- arch/arm/mach-apple/Kconfig | 2 ++ arch/arm/mach-apple/apple_wdt_clkevt.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/arm/mach-apple/Kconfig b/arch/arm/mach-apple/Kconfig index 63f344f2985527..f0cb1cb44458e0 100644 --- a/arch/arm/mach-apple/Kconfig +++ b/arch/arm/mach-apple/Kconfig @@ -18,6 +18,8 @@ menuconfig ARCH_APPLE_S5L select COMMON_CLK select GENERIC_IRQ_CHIP select HAVE_ARM_TWD if SMP + select WATCHDOG + select WATCHDOG_CORE help Support for Apple's 32-bit ARM application processors, as used in iOS devices from the A4 through the A6X. diff --git a/arch/arm/mach-apple/apple_wdt_clkevt.c b/arch/arm/mach-apple/apple_wdt_clkevt.c index 78ed6f3d965d4e..19f86ae9fe2dce 100644 --- a/arch/arm/mach-apple/apple_wdt_clkevt.c +++ b/arch/arm/mach-apple/apple_wdt_clkevt.c @@ -348,8 +348,8 @@ core_param(apple_wdt_timeout, apple_wdt_timeout, uint, 0444); static u32 apple_wdt_ctrl_on(void) { - /* the A6's counter only runs with ENABLE (see apple_wdt_map()) */ - return of_machine_is_compatible("apple,s5l8950x") ? WDT_CTRL_ENABLE : 0; + /* the A6 family's counter only runs with ENABLE (see apple_wdt_map()) */ + return apple_wdt_counter_gated() ? WDT_CTRL_ENABLE : 0; } static int apple_wdt_ping(struct watchdog_device *wdd)