Skip to contentSkip to conversation

i2c: ma35d1: Add support for MA35D1 I2C controller

PATCH Superseded Zi-Yu Chen 3 participants 8 messages Oct 1 – Oct 16

Subsystems (4)

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Zi-Yu Chen

author
#0 lore raw

v11 cover i2c: ma35d1: Add support for MA35D1 I2C controller

This series adds support for the I2C controller found in the Nuvoton MA35D1 SoC. The driver supports controller and optional target mode and runtime power management.

The implementation has been tested on the Nuvoton MA35D1 SOM board.

---
Changes in v11:
According to Andi's comments:
- Prefix the remaining timeout macros with MA35_.
- Handle errors returned by i2c_slave_event() by sending NACK when the target driver rejects a request.
- Use dev_err_probe() consistently in probe error paths.
v10: https://lore.kernel.org/r/[email protected]/

Changes in v10:
- Handle enable_irq_wake() return value in suspend.
- Correct clock divider mask and maximum limit to 10-bit.
v9: https://lore.kernel.org/r/[email protected]/

Changes in v9:
- Centralize SI handling in the interrupt handler.
- Fix target mode comments.
- Initialize the target transmit byte.
- Add reset_control_deassert() during probe.
v8: https://lore.kernel.org/r/[email protected]/

Changes in v8:
- Remove rpm_put avoid duplicate processing.
- Add synchronize_irq wait for interrupt to finish before i2c->target = NULL.
- Add ma35d1_i2c_restore_idle() to handle bus errors or spurious interrupts.
- Change return types of TRX helper functions to irqreturn_t, returning IRQ_NONE for unhandled status codes.
v7: https://lore.kernel.org/r/[email protected]/

Changes in v7:
- Disable IRQ before clearing i2c->msg.
- Support CONFIG_PM=n, and simplify runtime PM setup accordingly.
- Remove redundant register access in runtime suspend/resume callbacks.
- Defer clearing i2c->target until after pm_runtime_put_sync() in ma35d1_unreg_target().
- Enable interrupts alongside I2C enable in ma35d1_i2c_reset().
v6: https://lore.kernel.org/r/[email protected]/

Changes in v6:
- Patch 2 (driver):
- Removed dynamic IRQ toggling helpers for simpler interrupt management. Based on Sashiko AI Feedback:
- Added zero-length read quirk.
- Set i2c->msg to NULL on timeout to avoid use-after-free.
- Cleared hardware registers on target unregistration to release the bus.
- Protected suspend-path register writes with a spinlock.
- Validated clock-frequency to prevent division-by-zero.
- Removed incorrect I2C_M_IGNORE_NAK handling.
- Corrected a comment typo in the receive data path.
- Patch 3 (dts):
- use a generic functional name audio-codec@1a
v5: https://lore.kernel.org/r/[email protected]/

Changes in v5:
- Patch 2 (driver):
- Add spinlock protection for MA35_CTL0 register access.
- Add NULL pointer check for i2c->msg in the ISR.
- Use devres-managed cleanup (devm_add_action_or_reset) for runtime PM.
- Use pm_runtime_force_suspend/resume() for Controller mode system sleep.
- Control I2CEN bit enabling/disabling via runtime PM.
- Refactor arbitration lost handling in state machines.
- Remove unsupported I2C_FUNC_PROTOCOL_MANGLING flag.
- Clean up readl_poll_timeout() parameters, retry loop, and unused macros.
v4: https://lore.kernel.org/r/[email protected]/

Changes in v4:
- Patch 1 (dt-bindings):
- Update example interrupt number.
- Patch 2 (driver):
- Remove redundant spinlocks and fix 'irq' type to signed.
- Fix target address recovery in ma35d1_i2c_reset().
- Refactor IRQ handler for multi-msg reads and NACK/IGNORE_NAK.
- Fix timeout UAF via explicit IRQ disabling during recovery.
- Drop IRQF_SHARED and fix PM paths in probe/unregistration.
- Limit suspend/resume wakeup logic to target mode only.
- Patch 3 (dts):
- Add missing i2c0 node to dtsi.
- Switch SoM configuration from i2c1 to i2c2 with updated pinctrl.
- Add nau8822 audio codec node under i2c2 on the SoM board.
- Drop redundant i2c0 alias and clean up dtsi property ordering.
v3: https://lore.kernel.org/r/[email protected]/

Changes in v3:
- Fix minor DTS formatting issues (whitespace, missing newline)
v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Overall:
- Rebase on linux-i2c/i2c-next
- Switched terminology from "master/slave" to "controller/target".
- Patch 1 (dt-bindings):
- Simplified description and fixed 'reg' size in example.
- Patch 2 (driver):
- Modernized using devm_*, generic device properties, and FIELD_PREP/GENMASK.
- Optimized power management by moving clock control to runtime PM.
- Simplified code by removing redundant .remove(), .owner, and inlines.
- Added dev_err_probe() and default bus frequency handling.
- Patch 3 (dts):
- Moved i2c aliases to board dts and reordered nodes alphabetically.
v1: https://lore.kernel.org/r/[email protected]

Zi-Yu Chen (3):
dt-bindings: i2c: nuvoton,ma35d1-i2c: Add MA35D1 I2C controller
i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support
arm64: dts: nuvoton: Add I2C nodes for MA35D1 SoC

 .../bindings/i2c/nuvoton,ma35d1-i2c.yaml      |  63 ++
 .../boot/dts/nuvoton/ma35d1-som-256m.dts      |  20 +
 arch/arm64/boot/dts/nuvoton/ma35d1.dtsi       |  72 ++
 drivers/i2c/busses/Kconfig                    |  13 +
 drivers/i2c/busses/Makefile                   |   1 +
 drivers/i2c/busses/i2c-ma35d1.c               | 796 ++++++++++++++++++
 6 files changed, 965 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml
 create mode 100644 drivers/i2c/busses/i2c-ma35d1.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v11 1/3 dt-bindings: i2c: nuvoton,ma35d1-i2c: Add MA35D1 I2C controller

Add device tree binding documentation for the I2C controller found in the Nuvoton MA35D1 SoC.

---
 .../bindings/i2c/nuvoton,ma35d1-i2c.yaml      | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml
Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml new file +63 -0
new file mode 100644
index 000000000000..0713e1c1ecb8
@@ -0,0 +1,63 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/i2c/nuvoton,ma35d1-i2c.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton MA35D1 I2C Controller
+
+maintainers:
+  - Zi-Yu Chen <[email protected]>
+
+description:
+  The Nuvoton MA35D1 I2C controller supports controller and optional target mode.
+
+allOf:
+  - $ref: /schemas/i2c/i2c-controller.yaml#
+
+properties:
+  compatible:
+    const: nuvoton,ma35d1-i2c
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  clock-frequency:
+    description:
+      Desired I2C bus clock frequency in Hz. The absence of this property
+      indicates the default frequency 100 kHz.
+
+  resets:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - resets
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/nuvoton,ma35d1-clk.h>
+    #include <dt-bindings/reset/nuvoton,ma35d1-reset.h>
+
+    i2c0: i2c@40800000 {
+      compatible = "nuvoton,ma35d1-i2c";
+      reg = <0x40800000 0x1000>;
+      interrupts = <GIC_SPI 13 IRQ_TYPE_LEVEL_HIGH>;
+      clocks = <&clk I2C0_GATE>;
+      clock-frequency = <100000>;
+      resets = <&sys MA35D1_RESET_I2C0>;
+      #address-cells = <1>;
+      #size-cells = <0>;
+    };
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v11 2/3 i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

In reply to Zi-Yu Chen

Add I2C support for Nuvoton MA35D1 SoC.
The controller supports standard, fast and fast-plus modes, and provides controller/target functionality.

---
 drivers/i2c/busses/Kconfig      |  13 +
 drivers/i2c/busses/Makefile     |   1 +
 drivers/i2c/busses/i2c-ma35d1.c | 796 ++++++++++++++++++++++++++++++++
 3 files changed, 810 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-ma35d1.c
drivers/i2c/busses/Kconfig +13 -0
index d7b89508311f..e69120d46977 100644
@@ -1602,4 +1602,17 @@ config I2C_VIRTIO
           This driver can also be built as a module. If so, the module
           will be called i2c-virtio.
 
+config I2C_MA35D1
+	tristate "Nuvoton MA35D1 I2C driver"
+	depends on ARCH_MA35 || COMPILE_TEST
+	select I2C_SLAVE
+	help
+	  If you say yes to this option, support will be included for the
+	  I2C controller in the Nuvoton MA35D1 SoC. This driver
+	  supports the standard I2C bus protocols, including master and
+	  slave modes.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-ma35d1.
+
 endmenu
drivers/i2c/busses/Makefile +1 -0
index 3755c54b3d82..ca75dae4955c 100644
@@ -131,6 +131,7 @@ obj-$(CONFIG_I2C_XILINX)	+= i2c-xiic.o
 obj-$(CONFIG_I2C_XLP9XX)	+= i2c-xlp9xx.o
 obj-$(CONFIG_I2C_RCAR)		+= i2c-rcar.o
 obj-$(CONFIG_I2C_GXP)		+= i2c-gxp.o
+obj-$(CONFIG_I2C_MA35D1)	+= i2c-ma35d1.o
 
 # External I2C/SMBus adapter drivers
 obj-$(CONFIG_I2C_DIOLAN_U2C)	+= i2c-diolan-u2c.o
drivers/i2c/busses/i2c-ma35d1.c new file +796 -0

796 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v11 2/3] i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] i2c: ma35d1: data race in transfer initialization corrupts state machine - [High] i2c: ma35d1…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] i2c: ma35d1: data race in transfer initialization corrupts state machine
- [High] i2c: ma35d1: TOCTOU race in target registration leads to NULL pointer dereference
- [Medium] i2c: ma35d1: ignored return value in target receive allows buffer overrun
- [Medium] i2c: ma35d1: inconsistent IO accessors for MA35_DAT register
- [Medium] i2c: ma35d1: resource leak on suspend error path leaves wake-up enabled
- [Medium] i2c: ma35d1: multi-master target mode broken due to unconditional AA bit clear --

commit deb266e66e11836eee266a00ecd16f32efa64cf8
Author: Zi-Yu Chen <[email protected]>

i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

Add I2C support for Nuvoton MA35D1 SoC.
The controller supports standard, fast and fast-plus modes, and provides controller/target functionality.

Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0 -0
new file mode 100644
index 0000000000000..2a4985dc22c6e

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 139

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+static void ma35d1_i2c_write_ctl(struct ma35d1_i2c *i2c, unsigned int ctl)
+{
+	unsigned int val;
+
+	guard(spinlock_irqsave)(&i2c->lock);
+	val = readl(i2c->regs + MA35_CTL0);
+	val &= ~(MA35_CTL_SI_AA);
+	val |= ctl;
+	writel(val, i2c->regs + MA35_CTL0);
+}

[Severity: Medium]
Will this unconditionally clear the Assert Acknowledge (AA) bit during controller transfers?

When ma35d1_i2c_doxfer() calls this with just MA35_CTL_STA, the AA bit is omitted and stripped from the register. In a multi-master environment, if arbitration is lost and another controller addresses this device as a target, won't it fail to ACK its target address and break the multi-master target fallback mechanism?

[ ... ]

Quoted 7 lines
drivers/i2c/busses/i2c-ma35d1.c new file +7 -0
+static irqreturn_t ma35d1_i2c_irq_target_trx(struct ma35d1_i2c *i2c,
+					     unsigned long i2c_status)
+{
+	unsigned char byte = 0;
+	int ret;
+
+	switch (i2c_status) {

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 277

Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8 -0
+	case MA35_S_RECE_DATA_ACK:
+		/*
+		 * Previously address with own SLA address Data has been received;
+		 * ACK has been returned
+		 */
+		byte = readb(i2c->regs + MA35_DAT);
+		i2c_slave_event(i2c->target, I2C_SLAVE_WRITE_RECEIVED, &byte);
+		break;

[Severity: Medium]
Is it possible for i2c_slave_event() to return an error here when the backend buffer is full?

By ignoring the return value, target_reject remains false, causing the controller to blindly ACK subsequent bytes and potentially overrun the backend buffer instead of NACKing the next byte.

[ ... ]

Quoted 4 lines
drivers/i2c/busses/i2c-ma35d1.c new file +4 -0
+static irqreturn_t ma35d1_i2c_irq_controller_trx(struct ma35d1_i2c *i2c,
+						 unsigned long i2c_status)
+{
+	unsigned char byte;

[ ... ]

Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +5 -0
+	case MA35_M_TRAN_ADDR_ACK:
+	case MA35_M_TRAN_DATA_ACK:
+		/* SLA+W has been transmitted and ACK has been received */
+		if (i2c_status == MA35_M_TRAN_ADDR_ACK) {
+			if (is_last_msg(i2c) && i2c->msg->len == 0) {

drivers/i2c/busses/i2c-ma35d1.c line 362

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+				ma35d1_i2c_stop(i2c, 0);
+				return IRQ_HANDLED;
+			}
+		}
+
+		if (!is_msgend(i2c)) {
+			byte = i2c->msg->buf[i2c->msg_ptr++];
+			writel(byte, i2c->regs + MA35_DAT);
+			ma35d1_i2c_write_ctl(i2c, MA35_CTL_SI);
+		} else if (!is_last_msg(i2c)) {

[Severity: Medium]
Why is 32-bit writel() used to write data but 8-bit readb() used to read from the exact same MA35_DAT register below?

If the ARM SoC's peripheral bus dictates 32-bit aligned accesses, could readb() trigger a bus fault or return invalid data? Both paths should use consistent accessor sizes (e.g. readl(reg) & 0xff).

[ ... ]

Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +5 -0
+	case MA35_M_RECE_DATA_ACK:
+	case MA35_M_RECE_DATA_NACK:
+		/* DATA has been received and ACK/NACK has been received */
+		byte = readb(i2c->regs + MA35_DAT);
+		i2c->msg->buf[i2c->msg_ptr++] = byte;

[ ... ]

Quoted 4 lines
drivers/i2c/busses/i2c-ma35d1.c new file +4 -0
+static int ma35d1_i2c_doxfer(struct ma35d1_i2c *i2c, struct i2c_msg *msgs,
+			     int num)
+{
+	unsigned long timeout;

drivers/i2c/busses/i2c-ma35d1.c line 485

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+	unsigned int val;
+	int ret, err;
+
+	i2c->msg = msgs;
+	i2c->msg_num = num;
+	i2c->msg_ptr = 0;
+	i2c->msg_idx = 0;
+	i2c->err = 0;
+
+	ma35d1_i2c_write_ctl(i2c, MA35_CTL_STA);

[Severity: High]
Does this expose the transfer state to the active interrupt handler before setup is fully complete and protected?

If a spurious interrupt or bus error fires during this window, the ISR could concurrently modify these fields. The ISR might set msg_num=0 and err=-EIO, which the process context then overwrites with err=0 before sending a START, potentially leaving the hardware hanging.

[ ... ]

Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8 -0
+static int ma35d1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
+			   int num)
+{
+	struct ma35d1_i2c *i2c = i2c_get_adapdata(adap);
+	int ret;
+
+	ret = pm_runtime_resume_and_get(i2c->dev);
+	if (ret)

drivers/i2c/busses/i2c-ma35d1.c line 531

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+		return ret;
+
+	ret = ma35d1_i2c_doxfer(i2c, msgs, num);
+
+	if (i2c->target)
+		ma35d1_i2c_write_ctl(i2c, MA35_CTL_AA);
+	pm_runtime_put_autosuspend(i2c->dev);
+
+	return ret;
+}

[Severity: High]
Is there a TOCTOU race condition here?

If ma35d1_unreg_target() clears i2c->target and calls synchronize_irq() right after ma35d1_i2c_xfer() checks it but before it sets the AA bit, this could enable target mode interrupts while i2c->target is NULL. A subsequent target transaction would then fire the ISR and dereference the NULL pointer.

[ ... ]

Quoted 14 lines
drivers/i2c/busses/i2c-ma35d1.c new file +14 -0
+static int ma35d1_unreg_target(struct i2c_client *target)
+{
+	struct ma35d1_i2c *i2c = i2c_get_adapdata(target->adapter);
+	int ret;
+
+	if (!i2c->target)
+		return -EINVAL;
+
+	writel(0, i2c->regs + MA35_ADDR0);
+	/* Clear AA bit */
+	ma35d1_i2c_write_ctl(i2c, 0);
+
+	synchronize_irq(i2c->irq);
+	i2c->target = NULL;

[ ... ]

Quoted 14 lines
drivers/i2c/busses/i2c-ma35d1.c new file +14 -0
+static int ma35d1_i2c_suspend(struct device *dev)
+{
+	struct ma35d1_i2c *i2c = dev_get_drvdata(dev);
+	unsigned int val;
+	int ret;
+
+	/* Prepare for wake-up from I2C events if target mode is active */
+	if (i2c->target) {
+		ma35d1_i2c_write_ctl(i2c, MA35_CTL_AA);
+
+		/* Setup wake-up control */
+		writel(0x1, i2c->regs + MA35_WKCTL);
+
+		/* Clear pending wake-up flags */

drivers/i2c/busses/i2c-ma35d1.c line 626

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+		val = readl(i2c->regs + MA35_WKSTS);
+		writel(val, i2c->regs + MA35_WKSTS);
+
+		ret = enable_irq_wake(i2c->irq);
+		if (ret)
+			return ret;
+		return 0;
+	}
+	return pm_runtime_force_suspend(dev);
+}

[Severity: Medium]
If enable_irq_wake() fails, this returns early without clearing MA35_WKCTL.

Will this leave the hardware wake-up control active during normal runtime, potentially causing spurious wakeups or interrupt storms?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#4 lore raw

Re: [PATCH v11 2/3] i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

[email protected] 於 2026/10/1 下午 04:08 寫道:

Quoted 39 lines

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] i2c: ma35d1: data race in transfer initialization corrupts state machine
- [High] i2c: ma35d1: TOCTOU race in target registration leads to NULL pointer dereference
- [Medium] i2c: ma35d1: ignored return value in target receive allows buffer overrun
- [Medium] i2c: ma35d1: inconsistent IO accessors for MA35_DAT register
- [Medium] i2c: ma35d1: resource leak on suspend error path leaves wake-up enabled
- [Medium] i2c: ma35d1: multi-master target mode broken due to unconditional AA bit clear --

commit deb266e66e11836eee266a00ecd16f32efa64cf8
Author: Zi-Yu Chen <[email protected]>

i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

Add I2C support for Nuvoton MA35D1 SoC.
The controller supports standard, fast and fast-plus modes, and provides controller/target functionality.

Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0 -0
new file mode 100644
index 0000000000000..2a4985dc22c6e

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 139

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+static void ma35d1_i2c_write_ctl(struct ma35d1_i2c *i2c, unsigned int ctl)
+{
+	unsigned int val;
+
+	guard(spinlock_irqsave)(&i2c->lock);
+	val = readl(i2c->regs + MA35_CTL0);
+	val &= ~(MA35_CTL_SI_AA);
+	val |= ctl;
+	writel(val, i2c->regs + MA35_CTL0);
+}

[Severity: Medium]
Will this unconditionally clear the Assert Acknowledge (AA) bit during controller transfers?

Quoted 6 lines

When ma35d1_i2c_doxfer() calls this with just MA35_CTL_STA, the AA bit is omitted and stripped from the register. In a multi-master environment, if arbitration is lost and another controller addresses this device as a target, won't it fail to ACK its target address and break the multi-master target fallback mechanism?

Clearing AA is intentional, not an unintended behavior of controller/target coexistence. MA35D1 prioritizes controller transactions, and ma35d1_i2c_write_ctl() clears AA when starting a controller transfer. Therefore, target requests received during a controller transaction are expected to be NACKed. Once the controller transaction completes, the driver re-enables AA if a target is registered. This is the intended controller-priority policy and does not require changes.

Quoted 22 lines

[ ... ]

Quoted 7 lines
drivers/i2c/busses/i2c-ma35d1.c new file +7 -0
+static irqreturn_t ma35d1_i2c_irq_target_trx(struct ma35d1_i2c *i2c,
+					     unsigned long i2c_status)
+{
+	unsigned char byte = 0;
+	int ret;
+
+	switch (i2c_status) {

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 277

Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8 -0
+	case MA35_S_RECE_DATA_ACK:
+		/*
+		 * Previously address with own SLA address Data has been received;
+		 * ACK has been returned
+		 */
+		byte = readb(i2c->regs + MA35_DAT);
+		i2c_slave_event(i2c->target, I2C_SLAVE_WRITE_RECEIVED, &byte);
+		break;

[Severity: Medium]
Is it possible for i2c_slave_event() to return an error here when the backend buffer is full?

Quoted 4 lines

By ignoring the return value, target_reject remains false, causing the controller to blindly ACK subsequent bytes and potentially overrun the backend buffer instead of NACKing the next byte.

When MA35D1 reports MA35_S_RECE_DATA_ACK, the byte has already been received and ACKed, so the driver can no longer NACK it. Propagating the callback error to the next byte is also incorrect, as I2C_SLAVE_WRITE_RECEIVED applies only to the current byte.

Quoted 26 lines

[ ... ]

Quoted 4 lines
drivers/i2c/busses/i2c-ma35d1.c new file +4 -0
+static irqreturn_t ma35d1_i2c_irq_controller_trx(struct ma35d1_i2c *i2c,
+						 unsigned long i2c_status)
+{
+	unsigned char byte;

[ ... ]

Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +5 -0
+	case MA35_M_TRAN_ADDR_ACK:
+	case MA35_M_TRAN_DATA_ACK:
+		/* SLA+W has been transmitted and ACK has been received */
+		if (i2c_status == MA35_M_TRAN_ADDR_ACK) {
+			if (is_last_msg(i2c) && i2c->msg->len == 0) {

drivers/i2c/busses/i2c-ma35d1.c line 362

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+				ma35d1_i2c_stop(i2c, 0);
+				return IRQ_HANDLED;
+			}
+		}
+
+		if (!is_msgend(i2c)) {
+			byte = i2c->msg->buf[i2c->msg_ptr++];
+			writel(byte, i2c->regs + MA35_DAT);
+			ma35d1_i2c_write_ctl(i2c, MA35_CTL_SI);
+		} else if (!is_last_msg(i2c)) {

[Severity: Medium]
Why is 32-bit writel() used to write data but 8-bit readb() used to read from the exact same MA35_DAT register below?

Quoted 4 lines

If the ARM SoC's peripheral bus dictates 32-bit aligned accesses, could readb() trigger a bus fault or return invalid data? Both paths should use consistent accessor sizes (e.g. readl(reg) & 0xff).

The peripheral bus supports byte accesses, and only bits [7:0] of I2C_DAT contain data. Therefore, using writel() to transmit and readb() to receive is valid on this hardware.

Quoted 26 lines

[ ... ]

Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +5 -0
+	case MA35_M_RECE_DATA_ACK:
+	case MA35_M_RECE_DATA_NACK:
+		/* DATA has been received and ACK/NACK has been received */
+		byte = readb(i2c->regs + MA35_DAT);
+		i2c->msg->buf[i2c->msg_ptr++] = byte;

[ ... ]

Quoted 4 lines
drivers/i2c/busses/i2c-ma35d1.c new file +4 -0
+static int ma35d1_i2c_doxfer(struct ma35d1_i2c *i2c, struct i2c_msg *msgs,
+			     int num)
+{
+	unsigned long timeout;

drivers/i2c/busses/i2c-ma35d1.c line 485

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+	unsigned int val;
+	int ret, err;
+
+	i2c->msg = msgs;
+	i2c->msg_num = num;
+	i2c->msg_ptr = 0;
+	i2c->msg_idx = 0;
+	i2c->err = 0;
+
+	ma35d1_i2c_write_ctl(i2c, MA35_CTL_STA);

[Severity: High]
Does this expose the transfer state to the active interrupt handler before setup is fully complete and protected?

Quoted 5 lines

If a spurious interrupt or bus error fires during this window, the ISR could concurrently modify these fields. The ISR might set msg_num=0 and err=-EIO, which the process context then overwrites with err=0 before sending a START, potentially leaving the hardware hanging.

The IRQ is dedicated to this I2C controller and is not shared with other devices. Disabling the IRQ while initializing the transfer state prevents the interrupt handler from accessing partially initialized transfer data. The IRQ is re-enabled after the transfer has been started.

Quoted 23 lines

[ ... ]

Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8 -0
+static int ma35d1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
+			   int num)
+{
+	struct ma35d1_i2c *i2c = i2c_get_adapdata(adap);
+	int ret;
+
+	ret = pm_runtime_resume_and_get(i2c->dev);
+	if (ret)

drivers/i2c/busses/i2c-ma35d1.c line 531

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+		return ret;
+
+	ret = ma35d1_i2c_doxfer(i2c, msgs, num);
+
+	if (i2c->target)
+		ma35d1_i2c_write_ctl(i2c, MA35_CTL_AA);
+	pm_runtime_put_autosuspend(i2c->dev);
+
+	return ret;
+}

[Severity: High]
Is there a TOCTOU race condition here?

Quoted 5 lines

If ma35d1_unreg_target() clears i2c->target and calls synchronize_irq() right after ma35d1_i2c_xfer() checks it but before it sets the AA bit, this could enable target mode interrupts while i2c->target is NULL. A subsequent target transaction would then fire the ISR and dereference the NULL pointer.

The I2C core serializes target registration and removal with controller transfers, so unreg_target() should not run concurrently with master_xfer().

Quoted 44 lines

[ ... ]

Quoted 14 lines
drivers/i2c/busses/i2c-ma35d1.c new file +14 -0
+static int ma35d1_unreg_target(struct i2c_client *target)
+{
+	struct ma35d1_i2c *i2c = i2c_get_adapdata(target->adapter);
+	int ret;
+
+	if (!i2c->target)
+		return -EINVAL;
+
+	writel(0, i2c->regs + MA35_ADDR0);
+	/* Clear AA bit */
+	ma35d1_i2c_write_ctl(i2c, 0);
+
+	synchronize_irq(i2c->irq);
+	i2c->target = NULL;

[ ... ]

Quoted 14 lines
drivers/i2c/busses/i2c-ma35d1.c new file +14 -0
+static int ma35d1_i2c_suspend(struct device *dev)
+{
+	struct ma35d1_i2c *i2c = dev_get_drvdata(dev);
+	unsigned int val;
+	int ret;
+
+	/* Prepare for wake-up from I2C events if target mode is active */
+	if (i2c->target) {
+		ma35d1_i2c_write_ctl(i2c, MA35_CTL_AA);
+
+		/* Setup wake-up control */
+		writel(0x1, i2c->regs + MA35_WKCTL);
+
+		/* Clear pending wake-up flags */

drivers/i2c/busses/i2c-ma35d1.c line 626

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+		val = readl(i2c->regs + MA35_WKSTS);
+		writel(val, i2c->regs + MA35_WKSTS);
+
+		ret = enable_irq_wake(i2c->irq);
+		if (ret)
+			return ret;
+		return 0;
+	}
+	return pm_runtime_force_suspend(dev);
+}

[Severity: Medium]
If enable_irq_wake() fails, this returns early without clearing MA35_WKCTL.

Quoted 3 lines

Will this leave the hardware wake-up control active during normal runtime, potentially causing spurious wakeups or interrupt storms?

Clear the wake-up control register if enabling IRQ wakeup fails, so the hardware state is rolled back when the suspend operation cannot proceed.

Regards,
Zi-Yu Chen

maintainer
#5 lore raw

Re: [PATCH v11 2/3] i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

In reply to Zi-Yu Chen

Hi Zi-Yi,

Quoted 5 lines · Zi-Yu Chen

Add I2C support for Nuvoton MA35D1 SoC.
The controller supports standard, fast and fast-plus modes, and provides controller/target functionality.

to me the patch is fine. Sashiko has pointed out a few things that require device knowledge. Are those concerns valid?

Thanks,
Andi

author
#6 lore raw

Re: [PATCH v11 2/3] i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

Hi Andi,

Thanks for your review.

I reviewed the Sashiko AI report and will address two findings in v12:
- Disable the dedicated IRQ during transfer state initialization to prevent the IRQ handler from accessing partially initialized state.
- Clear the wake-up control register if enable_irq_wake() fails.

For the other findings, I have provided the relevant hardware and driver design rationale in my inline replies.

I will send v12 with these changes.

Thanks,
Zi-Yu

Andi Shyti 於 2026/10/14 下午 06:29 寫道:

Quoted 14 lines

Hi Zi-Yi,

Quoted 5 lines · Zi-Yu Chen

Add I2C support for Nuvoton MA35D1 SoC.
The controller supports standard, fast and fast-plus modes, and provides controller/target functionality.

to me the patch is fine. Sashiko has pointed out a few things that require device knowledge. Are those concerns valid?

Thanks,
Andi

author
#7 lore raw

PATCH v11 3/3 arm64: dts: nuvoton: Add I2C nodes for MA35D1 SoC

In reply to Zi-Yu Chen

Add I2C controller nodes to the MA35D1 SoC dtsi file. Also, enable the I2C2 interface on the MA35D1 SOM board and configure its pinctrl to allow communication with the onboard NAU8822 audio codec

---
 .../boot/dts/nuvoton/ma35d1-som-256m.dts      | 20 ++++++
 arch/arm64/boot/dts/nuvoton/ma35d1.dtsi       | 72 +++++++++++++++++++
 2 files changed, 92 insertions(+)
arch/arm64/boot/dts/nuvoton/ma35d1-som-256m.dts +20 -0
index a029b660e8dc..488567f830a6 100644
@@ -57,6 +57,18 @@ &clk {
 			   "integer";
 };
 
+&i2c2 {
+	status = "okay";
+	pinctrl-names = "default";
+	pinctrl-0 = <&pinctrl_i2c2>;
+
+	nau8822: audio-codec@1a {
+		compatible = "nuvoton,nau8822";
+		reg = <0x1a>;
+		nuvoton,spk-btl;
+	};
+};
+
 &pinctrl {
 	uart-grp {
 		pinctrl_uart0: uart0-pins {
@@ -100,6 +112,14 @@ pinctrl_uart16: uart16-pins {
 			power-source = <1>;
 		};
 	};
+
+	i2c-grp {
+		pinctrl_i2c2: i2c2-pins {
+			nuvoton,pins = <1 8 4>,
+				       <1 9 4>;
+			bias-disable;
+		};
+	};
 };
 
 &uart0 {
arch/arm64/boot/dts/nuvoton/ma35d1.dtsi +72 -0

78 lines not on this page · raw message

Signature · 1 line
2.34.1