From 3f09f2d2925839c8196f3b4f2dc0fd73ea05c48a Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Wed, 11 Nov 2026 14:58:31 -0500 Subject: [PATCH 1/4] arm64: dts: apple: t8122: Fix the fingerprint SPI input clock The J613 ADT gives SPI2 clock ID 0x198 a 24 MHz boot frequency. On hardware, divider 25 produces 0.96 MHz: six 29184-byte frames take 243.2 ms and overrun the 229 ms receive deadline. Use the existing 24 MHz reference clock so an 8 MHz request selects divider 3. Leave SPI1 unchanged pending evidence for that controller. Signed-off-by: Satya Benson --- arch/arm64/boot/dts/apple/t8122.dtsi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t8122.dtsi b/arch/arm64/boot/dts/apple/t8122.dtsi index ba3e893a234f5..b25c70ccda570 100644 --- a/arch/arm64/boot/dts/apple/t8122.dtsi +++ b/arch/arm64/boot/dts/apple/t8122.dtsi @@ -591,13 +591,13 @@ status = "disabled"; }; - /* Touch ID: pads muxed by iBoot, native chip select, 200 MHz. */ + /* Touch ID: pads muxed by iBoot, native chip select. */ spi2: spi@2a1108000 { compatible = "apple,t8122-spi", "apple,t8103-spi"; reg = <0x2 0xa1108000 0x0 0x4000>; interrupt-parent = <&aic>; interrupts = ; - clocks = <&clk_200m>; + clocks = <&clkref>; power-domains = <&ps_spi2>; #address-cells = <1>; #size-cells = <0>; From e8d0e9a80477ce2bab817c0dce2e0954342a9a92 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Wed, 11 Nov 2026 14:59:56 -0500 Subject: [PATCH 2/4] spi: apple: Account for the programmed clock in receive timeouts The divider rounds up, so a requested transfer rate can exceed the achieved rate even when the input clock is described correctly. Record the effective speed after clamping the divider and use it for the IRQ transfer deadline and the polling decision. Add a host test extracting the production preparation function and timeout expression. Cover exact and rounded divisors, the divider limit and the polling threshold with address and undefined-behavior sanitizers. Signed-off-by: Satya Benson --- MAINTAINERS | 1 + drivers/spi/spi-apple.c | 8 ++-- tools/spi/apple-spi-clock-test.py | 78 +++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 tools/spi/apple-spi-clock-test.py diff --git a/MAINTAINERS b/MAINTAINERS index 30f3d7801ea57..0b29a734a3592 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2803,6 +2803,7 @@ F: tools/asahi/pmp-bootargs-tests.rs F: drivers/spi/spi-apple-test.c F: tools/asahi/test-m3-uapi.py F: drivers/spi/spi-apple.c +F: tools/spi/apple-spi-clock-test.py F: drivers/spmi/spmi-apple-controller.c F: drivers/thermal/apple-pmp* F: drivers/thunderbolt/.kunitconfig.apple-dpin diff --git a/drivers/spi/spi-apple.c b/drivers/spi/spi-apple.c index fefc559c81d09..17d07085ad66f 100644 --- a/drivers/spi/spi-apple.c +++ b/drivers/spi/spi-apple.c @@ -326,7 +326,9 @@ static bool apple_spi_prep_transfer(struct apple_spi *spi, struct spi_transfer * /* Calculate and program the clock rate */ cr = DIV_ROUND_UP(clk_get_rate(spi->clk), t->speed_hz); - reg_write(spi, APPLE_SPI_CLKDIV, min_t(u32, cr, APPLE_SPI_CLKDIV_MAX)); + cr = min_t(u32, cr, APPLE_SPI_CLKDIV_MAX); + reg_write(spi, APPLE_SPI_CLKDIV, cr); + t->effective_speed_hz = clk_get_rate(spi->clk) / cr; /* Update bits per word */ reg_mask(spi, APPLE_SPI_SHIFTCFG, APPLE_SPI_SHIFTCFG_BITS, @@ -339,7 +341,7 @@ static bool apple_spi_prep_transfer(struct apple_spi *spi, struct spi_transfer * * 200000 * bits_per_word * fifo_threshold <= hz */ fifo_threshold = APPLE_SPI_FIFO_DEPTH / 2; - return (200000 * t->bits_per_word * fifo_threshold) <= t->speed_hz; + return (200000 * t->bits_per_word * fifo_threshold) <= t->effective_speed_hz; } static int apple_spi_poll(struct apple_spi *spi, u32 fifo_bit, u32 xfer_bit) @@ -615,7 +617,7 @@ static int apple_spi_transfer_one(struct spi_controller *ctlr, struct spi_device /* Allow for the transfer itself on top of the usual timeout. */ unsigned long ms = APPLE_SPI_TIMEOUT_MS + DIV_ROUND_UP_ULL((u64)words * t->bits_per_word * 1000, - max(t->speed_hz, 1U)); + max(t->effective_speed_hz, 1U)); reinit_completion(&spi->done); spin_lock_irqsave(&spi->lock, flags); diff --git a/tools/spi/apple-spi-clock-test.py b/tools/spi/apple-spi-clock-test.py new file mode 100644 index 0000000000000..3b53d7a832b06 --- /dev/null +++ b/tools/spi/apple-spi-clock-test.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Host regression test for the production Apple SPI clock/timeout arithmetic. + +Run from the repository root. Requires a C compiler; uses no hardware. +""" +import os +from pathlib import Path +import re +import subprocess +import tempfile + +source = Path("drivers/spi/spi-apple.c").read_text() +prep = re.search(r"static bool apple_spi_prep_transfer\(.*?\n}", source, re.S)[0] +timeout = re.search(r"unsigned long ms = APPLE_SPI_TIMEOUT_MS \+.*?;", source, re.S)[0] +program = r""" +#include +#include +#include +#include +typedef uint32_t u32; +typedef uint64_t u64; +#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_UP_ULL DIV_ROUND_UP +#define min_t(t, a, b) ((t)(a) < (t)(b) ? (t)(a) : (t)(b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define FIELD_PREP(a, b) (b) +#define APPLE_SPI_CLKDIV 0x30 +#define APPLE_SPI_CLKDIV_MAX 0x7ff +#define APPLE_SPI_SHIFTCFG 0 +#define APPLE_SPI_SHIFTCFG_BITS 0 +#define APPLE_SPI_FIFO_DEPTH 16 +#define APPLE_SPI_TIMEOUT_MS 200 +struct apple_spi { unsigned long clk; u32 div; }; +struct spi_transfer { u32 speed_hz, bits_per_word, effective_speed_hz; }; +#define clk_get_rate(c) (c) +#define reg_write(s, r, v) ((s)->div = (v)) +#define reg_mask(s, r, m, v) ((void)0) +""" + prep + r""" +static unsigned long budget(u32 words, struct spi_transfer *t) +{ +""" + timeout + r""" +return ms; +} +int main(void) +{ + struct apple_spi s; + struct spi_transfer t = { .bits_per_word = 8 }; + const struct { unsigned long clk; u32 hz, div, rate, ms; } cases[] = { + {24000000, 8000000, 3, 8000000, 230}, + {200000000, 8000000, 25, 8000000, 230}, + {24000000, 7000000, 4, 6000000, 239}, + {24000000, 23000000, 2, 12000000, 220}, + {24000000, 1, 2047, 11724, 20115}, + }; + for (unsigned int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + s.clk = cases[i].clk; + t.speed_hz = cases[i].hz; + bool poll = apple_spi_prep_transfer(&s, &t); + assert(s.div == cases[i].div); + assert(t.effective_speed_hz == cases[i].rate); + assert(budget(29184, &t) == cases[i].ms); + assert(poll == (12800000U <= cases[i].rate)); + } + /* A rounded divider must not receive the shorter requested-rate budget. */ + s.clk = 24000000; t.speed_hz = 7000000; + apple_spi_prep_transfer(&s, &t); + assert(budget(29184, &t) > 200 + DIV_ROUND_UP_ULL(29184ULL * 8000, 7000000)); + puts("PASS: 5 divider/rate/deadline/poll cases and rounding regression"); +} +""" +with tempfile.TemporaryDirectory(prefix="apple-spi-clock-") as work: + c = Path(work) / "test.c" + exe = Path(work) / "test" + c.write_text(program) + subprocess.run([os.environ.get("CC", "cc"), "-Wall", "-Wextra", "-Werror", + "-fsanitize=undefined,address", str(c), "-o", str(exe)], check=True) + subprocess.run([str(exe)], check=True) From f1c9d85fa75a39fe936392243c6fb470d2670c61 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Wed, 11 Nov 2026 15:01:56 -0500 Subject: [PATCH 3/4] arm64: dts: apple: t8122: Correct the SPI4 reference clock The J613 boot clock table also describes SPI4 clock ID 0x19a as 24 MHz. Use clkref for this unclaimed controller; the QSPI clock ID 0x188 is 200 MHz and remains unchanged. Signed-off-by: Satya Benson --- arch/arm64/boot/dts/apple/t8122-peripheral-buses.dtsi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/boot/dts/apple/t8122-peripheral-buses.dtsi b/arch/arm64/boot/dts/apple/t8122-peripheral-buses.dtsi index a4a4affe0147d..1bdbd8356541f 100644 --- a/arch/arm64/boot/dts/apple/t8122-peripheral-buses.dtsi +++ b/arch/arm64/boot/dts/apple/t8122-peripheral-buses.dtsi @@ -7,7 +7,7 @@ reg = <0x2 0xa1110000 0 0x4000>; interrupt-parent = <&aic>; interrupts = ; - clocks = <&clk_200m>; + clocks = <&clkref>; power-domains = <&ps_spi4>; #address-cells = <1>; #size-cells = <0>; From b8761033979a53ae01cd3877c4953574b9ba57d3 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Fri, 13 Nov 2026 03:41:14 -0500 Subject: [PATCH 4/4] spi: apple: Clamp the clock divider to at least one If clk_get_rate() returns zero, the calculated divider is zero and computing effective_speed_hz divides by zero. Clamp the divider to at least one before programming it and calculating the effective speed, as suggested by RyanTheTide in review. Non-zero clock rates retain their existing behavior. Signed-off-by: Satya Benson --- drivers/spi/spi-apple.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/spi/spi-apple.c b/drivers/spi/spi-apple.c index 17d07085ad66f..0f7d5d7b156df 100644 --- a/drivers/spi/spi-apple.c +++ b/drivers/spi/spi-apple.c @@ -327,6 +327,7 @@ static bool apple_spi_prep_transfer(struct apple_spi *spi, struct spi_transfer * /* Calculate and program the clock rate */ cr = DIV_ROUND_UP(clk_get_rate(spi->clk), t->speed_hz); cr = min_t(u32, cr, APPLE_SPI_CLKDIV_MAX); + cr = max(cr, 1U); reg_write(spi, APPLE_SPI_CLKDIV, cr); t->effective_speed_hz = clk_get_rate(spi->clk) / cr;