From: Janne Grunau <j@jannau.net>
To: Sasha Finkelstein <k@chaosmail.tech>
Cc: Sven Peter <sven@kernel.org>, Neal Gompa <neal@gompa.dev>,
Stephen Boyd <sboyd@kernel.org>, Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
Alba Mendez <me@alba.sh>
Subject: Re: [PATCH v4 2/6] spmi: apple: Validate FIFO state
Date: Wed, 5 Aug 2026 14:03:02 +0200 [thread overview]
Message-ID: <20260805120302.GB832571@robin.jannau.net> (raw)
In-Reply-To: <20260805-t603x-spmi-v4-2-c15a12d9a7d1@chaosmail.tech>
On Wed, Aug 05, 2026 at 12:11:12PM +0200, Sasha Finkelstein wrote:
> From: Alba Mendez <me@alba.sh>
>
> Check for data before reading the body of a reply, and check for
> end of data afterwards.
>
> Signed-off-by: Alba Mendez <me@alba.sh>
> Signed-off-by: Sasha Finkelstein <k@chaosmail.tech>
> ---
> drivers/spmi/spmi-apple-controller.c | 29 +++++++++++++++++++++++++++++
> 1 file changed, 29 insertions(+)
>
> diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c
> index 376cf682c43e..4d5356bf9bbf 100644
> --- a/drivers/spmi/spmi-apple-controller.c
> +++ b/drivers/spmi/spmi-apple-controller.c
> @@ -21,7 +21,9 @@
> #define SPMI_STATUS_REG 0
> #define SPMI_CMD_REG 0x4
> #define SPMI_RSP_REG 0x8
> +#define SPMI_ACT_REG 0xa4
>
> +#define SPMI_ACT_FIFO_FLUSH BIT(0)
> #define SPMI_RX_FIFO_EMPTY BIT(24)
>
> #define REG_POLL_INTERVAL_US 10000
> @@ -29,6 +31,7 @@
>
> struct apple_spmi {
> void __iomem *regs;
> + bool prev_fail;
> };
>
> #define poll_reg(spmi, reg, val, cond) \
> @@ -49,6 +52,7 @@ static int apple_spmi_wait_rx_not_empty(struct spmi_controller *ctrl)
>
> ret = poll_reg(spmi, SPMI_STATUS_REG, status, !(status & SPMI_RX_FIFO_EMPTY));
> if (ret) {
> + spmi->prev_fail = true;
> dev_err(&ctrl->dev,
> "failed to wait for RX FIFO not empty\n");
> return ret;
> @@ -67,6 +71,11 @@ static int spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> u8 i;
> int ret;
>
> + if (spmi->prev_fail) {
> + writel(SPMI_ACT_FIFO_FLUSH, spmi->regs + SPMI_RSP_REG);
> + spmi->prev_fail = false;
> + }
> +
> writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
>
> ret = apple_spmi_wait_rx_not_empty(ctrl);
> @@ -78,6 +87,11 @@ static int spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
>
> /* Read SPMI data reply */
> while (len_read < len) {
> + if (readl(spmi->regs + SPMI_STATUS_REG) & SPMI_RX_FIFO_EMPTY) {
> + dev_err_ratelimited(&ctrl->dev,
> + "FIFO lacks reply data, controller stuck?\n");
> + return -EIO;
> + }
> rsp = readl(spmi->regs + SPMI_RSP_REG);
> i = 0;
> while ((len_read < len) && (i < 4)) {
> @@ -86,6 +100,11 @@ static int spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> }
> }
>
> + if (!(readl(spmi->regs + SPMI_STATUS_REG) & SPMI_RX_FIFO_EMPTY)) {
> + dev_warn(&ctrl->dev, "FIFO has extra data\n");
> + spmi->prev_fail = true;
> + }
> +
> return 0;
> }
>
> @@ -97,6 +116,11 @@ static int spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> size_t i = 0, j;
> int ret;
>
> + if (spmi->prev_fail) {
> + writel(SPMI_ACT_FIFO_FLUSH, spmi->regs + SPMI_RSP_REG);
> + spmi->prev_fail = false;
> + }
> +
> writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
>
> while (i < len) {
> @@ -115,6 +139,11 @@ static int spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> /* Discard */
> readl(spmi->regs + SPMI_RSP_REG);
>
> + if (!(readl(spmi->regs + SPMI_STATUS_REG) & SPMI_RX_FIFO_EMPTY)) {
> + dev_warn(&ctrl->dev, "FIFO has extra data\n");
> + spmi->prev_fail = true;
> + }
> +
> return 0;
> }
Reviewed-by: Janne Grunau <j@jannau.net>
Janne
next prev parent reply other threads:[~2026-08-05 12:03 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-05 10:11 [PATCH v4 0/6] spmi: apple: Additional commands and interrupt support Sasha Finkelstein
2026-08-05 10:11 ` [PATCH v4 1/6] dt-bindings: spmi: apple,spmi: Add t603x Sasha Finkelstein
2026-08-05 10:19 ` sashiko-bot
2026-08-05 12:01 ` Janne Grunau
2026-08-05 10:11 ` [PATCH v4 2/6] spmi: apple: Validate FIFO state Sasha Finkelstein
2026-08-05 10:21 ` sashiko-bot
2026-08-05 12:03 ` Janne Grunau [this message]
2026-08-05 10:11 ` [PATCH v4 3/6] spmi: apple: check transaction status Sasha Finkelstein
2026-08-05 10:27 ` sashiko-bot
2026-08-05 12:06 ` Janne Grunau
2026-08-05 10:11 ` [PATCH v4 4/6] spmi: apple: Implement remaining commands Sasha Finkelstein
2026-08-05 10:24 ` sashiko-bot
2026-08-05 12:19 ` Janne Grunau
2026-08-05 16:54 ` Sasha Finkelstein
2026-08-05 10:11 ` [PATCH v4 5/6] spmi: apple: lock around FIFOs Sasha Finkelstein
2026-08-05 10:25 ` sashiko-bot
2026-08-05 12:25 ` Janne Grunau
2026-08-05 10:11 ` [PATCH v4 6/6] spmi: apple: Add interrupt functionality Sasha Finkelstein
2026-08-05 10:22 ` sashiko-bot
2026-08-05 12:42 ` Janne Grunau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260805120302.GB832571@robin.jannau.net \
--to=j@jannau.net \
--cc=asahi@lists.linux.dev \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=k@chaosmail.tech \
--cc=krzk+dt@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=me@alba.sh \
--cc=neal@gompa.dev \
--cc=robh@kernel.org \
--cc=sboyd@kernel.org \
--cc=sven@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox