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/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>; 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>; diff --git a/drivers/spi/spi-apple.c b/drivers/spi/spi-apple.c index fefc559c81d09..0f7d5d7b156df 100644 --- a/drivers/spi/spi-apple.c +++ b/drivers/spi/spi-apple.c @@ -326,7 +326,10 @@ 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); + cr = max(cr, 1U); + 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 +342,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 +618,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)