mirror of https://lore.kernel.org/linux-amlogic/
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Xianwei Zhao" <xianwei.zhao@amlogic.com>
Cc: linux-amlogic@lists.infradead.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org, neil.armstrong@linaro.org
Subject: Re: [PATCH v12 2/2] i2c: amlogic: Add Amlogic A9 I2C controller driver
Date: Wed, 18 Nov 2026 10:20:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-220673@kernel.org> (raw)
In-Reply-To: <20261118-a9-i2c-v12-2-fc83c53121e6@amlogic.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] i2c: amlogic-a9: transfer started before IRQ setup in
  aml_i2c_do_start()
- [Medium] i2c: amlogic-a9: stale irq_state and edge IRQ miss in
  aml_i2c_irq()
--

commit ce6e05692c16fd50e5f34df38eaf42209813110e
Author: Junyi Zhao <junyi.zhao@amlogic.com>

i2c: amlogic: Add Amlogic A9 I2C controller driver

This commit adds the I2C controller driver for Amlogic A9 SoCs along with its
Kconfig, Makefile, and MAINTAINERS entries.

> diff --git a/drivers/i2c/busses/i2c-amlogic-a9.c b/drivers/i2c/busses/i2c-amlogic-a9.c
> new file mode 100644
> index 0000000000000..d25bd50e78f6d
> --- /dev/null
> +++ b/drivers/i2c/busses/i2c-amlogic-a9.c
[ ... ]
> +static irqreturn_t aml_i2c_irq(int irqno, void *dev_id)
> +{
> +	struct aml_i2c *i2c = dev_id;
> +	u8 *buf = NULL;
> +	u32 rd_len, i;
> +	u32 rd_val;
> +	u32 irq_state;
> +
> +	guard(spinlock)(&i2c->lock);
> +	irq_state = readl(i2c->regs + REG_CFG_IRQ_STATE);
> +	if (i2c->state == A9_STATE_IDLE) {
> +		dev_warn(i2c->dev, "i2c irq triggered abnormally, irq_state:0x%x,irq_en:0x%x\n",
> +			 irq_state, readl(i2c->regs + REG_CFG_IRQ_ENABLE));
> +		goto irq_trans_out;
> +	}
> +
> +	if (irq_state & A9_TRANS_ERROR) {
> +		i2c->error = -ENXIO;
> +		goto complete_irq_out;
> +	}
> +
> +	switch (i2c->state) {
> +	case A9_STATE_READ:
> +		buf = i2c->msg->buf;
> +		rd_len = min(i2c->msg->len - i2c->pos, A9_I2C_FIFO_DEPTH);
> +		for (i = 0; i < rd_len; i++) {
> +			rd_val = readl(i2c->regs + REG_CFG_RX);
> +			buf[i2c->pos++] = rd_val & TX_RX_DATA;
> +		}
> +		/* Rx must clear thresh after read fifo */
> +		clear_irq_status(i2c, A9_RX_THRESH_READ);
> +		break;
> +	case A9_STATE_WRITE:
> +		aml_i2c_prepare_xfer(i2c, A9_FIFO_FILL_HALF);
> +		if (i2c->pos == i2c->msg->len)
> +			aml_i2c_set_mask(i2c, REG_CFG_IRQ_ENABLE, A9_TX_THRESH_WRITE, 0);
> +		/* Tx must clear thresh after write fifo */
> +		clear_irq_status(i2c, A9_TX_THRESH_WRITE);
> +		break;
> +	default:
> +		break;
> +	}
> +
> +	if (!(irq_state & A9_TRANS_DONE) ||
> +	    (i2c->state == A9_STATE_READ && i2c->pos < i2c->msg->len))
> +		return IRQ_HANDLED;

[Severity: Medium]
Can aml_i2c_irq() miss A9_TRANS_DONE on multi-chunk transfers and cause
wait_for_completion_timeout() to time out?

The binding specifies a rising-edge interrupt (IRQ_TYPE_EDGE_RISING), which
requires (REG_CFG_IRQ_STATE & REG_CFG_IRQ_ENABLE) to drop to 0 and transition
back to 1 to latch a new interrupt in the GIC:

Documentation/devicetree/bindings/i2c/amlogic,a9-i2c.yaml:
    ...
        interrupts = <0 164 1>;
    ...

On a multi-chunk read whose length is a multiple of A9_I2C_FIFO_DEPTH (32),
such as 64 bytes, A9_RX_THRESH_READ remains enabled for the final 32-byte
chunk and fires as soon as the 32nd byte enters the RX FIFO, before the final
NACK and STOP finish on the bus:

CPU0                              I2C HW
aml_i2c_irq()
  irq_state = readl(REG_CFG_IRQ_STATE)
  /* A9_RX_THRESH_READ only */
  read 32 bytes from REG_CFG_RX
                                  STOP finishes:
                                  REG_CFG_IRQ_STATE |= A9_PHY_DONE
  clear_irq_status(A9_RX_THRESH_READ)
  /* A9_PHY_DONE keeps IRQ line high; no new rising edge */
  !(irq_state & A9_TRANS_DONE)
    return IRQ_HANDLED            <-- completion missed, hangs 500 ms

Because irq_state is not re-read after draining or filling the FIFO, the same
miss happens if A9_PHY_DONE or A9_NCK_ERROR asserts while A9_STATE_WRITE is
filling REG_CFG_TX before clear_irq_status(i2c, A9_TX_THRESH_WRITE).

Similarly, if both A9_RX_THRESH_READ and A9_PHY_DONE are already set in
irq_state when more than 32 bytes remain to be read, aml_i2c_irq() reads only
32 bytes, clears only A9_RX_THRESH_READ, and returns early because
i2c->pos < i2c->msg->len, leaving A9_PHY_DONE continuously asserted with no
further rising edge to read the remaining bytes or complete the transfer.

> +
> +complete_irq_out:
> +	i2c->state = A9_STATE_IDLE;
> +	reset_fifo_pos(i2c);
> +	aml_i2c_set_mask(i2c, REG_CFG_IRQ_ENABLE, A9_RX_THRESH_READ | A9_TX_THRESH_WRITE, 0);
> +	complete(&i2c->done);
> +irq_trans_out:
> +	clear_irq_status(i2c, IRQ_ALL_MASK);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static void aml_i2c_do_start(struct aml_i2c *i2c, struct i2c_msg *msg)
> +{
> +	unsigned int reg_val;
> +	unsigned int trans_len;
> +
> +	trans_len = msg->len;
> +	reg_val = (unsigned int)(((msg->addr & 0x7f) << START_SADDR_SHIFT) |
> +				((msg->flags & I2C_M_RD) ? START_READ : 0) |
> +				((trans_len & 0xfff) << START_LEN_SHIFT) | START_START);
> +	aml_i2c_set_mask(i2c, REG_CFG_START,
> +			 START_START | START_SLAVE_ADDR | START_READ | START_LEN, reg_val);
> +	clear_irq_status(i2c, IRQ_ALL_MASK);
> +	/* enable thresh mode */
> +	if (trans_len > A9_I2C_FIFO_DEPTH) {
> +		reg_val = (msg->flags & I2C_M_RD) ? A9_RX_THRESH_READ : A9_TX_THRESH_WRITE;
> +		aml_i2c_set_mask(i2c, REG_CFG_IRQ_ENABLE,
> +				 A9_RX_THRESH_READ | A9_TX_THRESH_WRITE, reg_val);
> +	}
> +}

[Severity: Medium]
Can clear_irq_status(i2c, IRQ_ALL_MASK) wipe out interrupt status bits for
the transfer that was just started by writing START_START to REG_CFG_START?

CPU0                              I2C HW
aml_i2c_xfer_msg()
  aml_i2c_do_start()
    writel(..., REG_CFG_START)
                                  address NACK or fast transfer finishes:
                                  REG_CFG_IRQ_STATE |= A9_NCK_ERROR
                                  GIC latches rising-edge IRQ
    clear_irq_status(IRQ_ALL_MASK)
  spin_unlock_irqrestore()
aml_i2c_irq()
  irq_state = readl(REG_CFG_IRQ_STATE) /* 0 */
  switch (i2c->state)
    /* reads empty RX FIFO or writes extra TX FIFO bytes */
  !(irq_state & A9_TRANS_DONE)
    return IRQ_HANDLED            <-- never completes, times out

If the CPU is delayed between writing REG_CFG_START and clearing
REG_CFG_IRQ_STATE (for example under CONFIG_PREEMPT_RT where spinlock_t is
preemptible, or on a vCPU), clearing IRQ_ALL_MASK after asserting START_START
discards the newly asserted status bits.  When aml_i2c_irq() runs for the
already-latched edge interrupt, it sees irq_state == 0, misses A9_TRANS_ERROR,
unconditionally accesses the FIFO in the switch (i2c->state) block, and
returns without completing i2c->done.

Clearing REG_CFG_IRQ_STATE and enabling the threshold interrupts in
REG_CFG_IRQ_ENABLE before asserting START_START in REG_CFG_START would avoid
this race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-a9-i2c-v12-0-fc83c53121e6@amlogic.com?part=2

_______________________________________________
linux-amlogic mailing list
linux-amlogic@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-amlogic

      reply	other threads:[~2026-11-18 10:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  9:59 [PATCH v12 0/2] i2c: Add driver support Amlogic A9 I2C Xianwei Zhao via B4 Relay
2026-11-18  9:59 ` [PATCH v12 1/2] dt-bindings: i2c: add Amlogic A9 I2C controller Xianwei Zhao via B4 Relay
2026-11-18  9:59 ` [PATCH v12 2/2] i2c: amlogic: Add Amlogic A9 I2C controller driver Xianwei Zhao via B4 Relay
2026-11-18 10:20   ` sashiko-bot [this message]

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=sashiko-outbox-220673@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=neil.armstrong@linaro.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xianwei.zhao@amlogic.com \
    /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

all inboxes | Powered by JetHome®