Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/boot/dts/apple/t8122-peripheral-buses.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
reg = <0x2 0xa1110000 0 0x4000>;
interrupt-parent = <&aic>;
interrupts = <AIC_IRQ 769 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&clk_200m>;
clocks = <&clkref>;
power-domains = <&ps_spi4>;
#address-cells = <1>;
#size-cells = <0>;
Expand Down
4 changes: 2 additions & 2 deletions arch/arm64/boot/dts/apple/t8122.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <AIC_IRQ 767 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&clk_200m>;
clocks = <&clkref>;
power-domains = <&ps_spi2>;
#address-cells = <1>;
#size-cells = <0>;
Expand Down
9 changes: 6 additions & 3 deletions drivers/spi/spi-apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
78 changes: 78 additions & 0 deletions tools/spi/apple-spi-clock-test.py
Original file line number Diff line number Diff line change
@@ -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 <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
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)
Loading