i2c: ma35d1: Add support for MA35D1 I2C controller
Subsystems (4)
v12 cover i2c: ma35d1: Add support for MA35D1 I2C controller
Resending the v12 series for review.
No changes since the previous submission.
The Sashiko AI findings were addressed in the previous discussion.
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 v12:
- Synchronize IRQs while initializing transfer state and issuing START.
- Clear wake-up control if enabling IRQ wake fails.
v11: https://lore.kernel.org/r/[email protected]/
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 | 800 ++++++++++++++++++ 6 files changed, 969 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
PATCH RESEND v12 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
@@ -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
PATCH RESEND v12 2/3 i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support
In reply to Zi-Yu ChenAdd 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 | 800 ++++++++++++++++++++++++++++++++ 3 files changed, 814 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ma35d1.c
drivers/i2c/busses/Kconfig +13 -0
@@ -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
@@ -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 +800 -0
800 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH RESEND v12 3/3 arm64: dts: nuvoton: Add I2C nodes for MA35D1 SoC
In reply to Zi-Yu ChenAdd 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
@@ -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
[email protected]
AI reviewRe: [PATCH RESEND v12 1/3] dt-bindings: i2c: nuvoton,ma35d1-i2c: Add MA35D1 I2C controller
In reply to Zi-Yu ChenThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] dt-bindings: i2c: ma35d1: interrupts listed before clocks --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] dt-bindings: i2c: ma35d1: interrupts listed before clocks --
commit 2066300098d16c8d475b20ab2c4f0d189c765526
Author: Zi-Yu Chen <[email protected]>
dt-bindings: i2c: nuvoton,ma35d1-i2c: Add MA35D1 I2C controller
This commit adds the Device Tree binding schema for the Nuvoton MA35D1 I2C controller.
Quoted 6 lines
Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml new file +0
-0@@ -0,0 +1,63 @@
[ ... ]
Quoted 31 lines
Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml new file +31
-0+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:
Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml line 58
Quoted 10 lines
Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml new file +10
-0+ - | + #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>;
[Severity: Low]
In properties, required, and the i2c0 example node, interrupts is listed before clocks. Under standard DT binding property ordering (compatible, reg, clocks, interrupts, ...), clocks should precede interrupts in all three sections.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH RESEND v12 2/3] i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support
In reply to Zi-Yu ChenThank you for your contribution! Sashiko AI review found 11 potential issues to consider: - [High] i2c: ma35d1: failure to clear STA and STO bits in ma35d1_i2c_write_ctl() - [High] i2c: ma35d1: NULL…
Thank you for your contribution! Sashiko AI review found 11 potential issues to consider:
- [High] i2c: ma35d1: failure to clear STA and STO bits in ma35d1_i2c_write_ctl()
- [High] i2c: ma35d1: NULL i2c->target dereference and race in ma35d1_i2c_irq_target_trx()
- [Medium] i2c: ma35d1: clearing of AA bit during controller start breaks target mode
- [Medium] i2c: ma35d1: premature wakeup on arbitration loss corrupts active target xfer
- [Medium] i2c: ma35d1: unhandled 0xc8/0xf8 statuses and missing target read STOP event
- [Medium] i2c: ma35d1: ignored I2C_SLAVE_WRITE_RECEIVED error in ma35d1_i2c_irq_target_trx()
- [Medium] i2c: ma35d1: uncleared MA35_WKSTS in ma35d1_i2c_irq() causes resume IRQ storm
- [Medium] i2c: ma35d1: stale target_reject state after ma35d1_i2c_reset()
- [Low] i2c: ma35d1: missing I2C_FUNC_SLAVE flag in ma35d1_i2c_func()
- [Low] i2c: ma35d1: missing pm_runtime_dont_use_autosuspend() in ma35d1_i2c_pm_cleanup()
- [Low] i2c: ma35d1: mishandled pm_runtime_put_sync() return in ma35d1_unreg_target()
--
commit 0a5481a7c6218eeb6b2a9112888fe1a57dc147c6
Author: Zi-Yu Chen <[email protected]>
i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support
This commit adds the I2C bus driver for the Nuvoton MA35D1 SoC with controller and target mode support.
Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0
[ ... ]
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: High]
Does ma35d1_i2c_write_ctl() also need to clear MA35_CTL_STA and MA35_CTL_STO from val before ORing in ctl?
After ma35d1_i2c_doxfer() sets MA35_CTL_STA, the MA35_M_START handler writes the target address and calls ma35d1_i2c_write_ctl(i2c, MA35_CTL_SI) to clear STA and SI and shift out the address byte. Because val &= ~(MA35_CTL_SI_AA) leaves MA35_CTL_STA set in MA35_CTL0, the controller keeps re-asserting START instead of transmitting the address, causing controller transfers to time out and reset.
[Severity: Medium]
When i2c->target is registered, does clearing MA35_CTL_AA here on ma35d1_i2c_write_ctl(i2c, MA35_CTL_STA) in ma35d1_i2c_doxfer() and ma35d1_i2c_write_ctl(i2c, MA35_CTL_SI) in MA35_M_START prevent the controller from ACKing its own target address if multi-master arbitration is lost? Without MA35_CTL_AA set during the START and address phase, the hardware will not enter the MA35_S_RECE_ARB_LOST or MA35_ADDR_TRAN_ARB_LOST states handled in ma35d1_i2c_irq_target_trx().
[ ... ]
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10
-0+static void ma35d1_i2c_reset (struct ma35d1_i2c *i2c) +{ +unsigned int clkdiv, slvaddr; + + clkdiv =readl (i2c->regs +MA35_CLKDIV ); + slvaddr =readl (i2c->regs +MA35_ADDR0 ); + +disable_irq (i2c->irq); + i2c->msg =NULL ; +reset_control_assert (i2c->rst);
drivers/i2c/busses/i2c-ma35d1.c line 177
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10
-0+usleep_range (10 ,20 ); +reset_control_deassert (i2c->rst); +enable_irq (i2c->irq); + +writel (clkdiv, (i2c->regs +MA35_CLKDIV )); +ma35d1_i2c_write_ctl (i2c,MA35_CTL_I2CEN |MA35_CTL_INTEN ); + +if (i2c->target) +writel (slvaddr, i2c->regs +MA35_ADDR0 ); +}
[Severity: Medium]
Should ma35d1_i2c_reset() also reset i2c->target_reject = false (and restore MA35_CTL_AA when i2c->target is active), or should MA35_S_RECE_ADDR_ACK and MA35_S_TRAN_ADDR_ACK clear i2c->target_reject at the start of a target transfer?
If a target write sets i2c->target_reject = true and a controller transfer then times out into ma35d1_i2c_reset(), the hardware reset aborts the target transaction before MA35_S_RECE_DATA_NACK or MA35_S_REPEAT_START_STOP can clear i2c->target_reject. The next target transaction sees the stale true value and clears MA35_CTL_AA at the end of ma35d1_i2c_irq_target_trx().
[ ... ]
Quoted 36 lines
drivers/i2c/busses/i2c-ma35d1.c new file +36
-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) { +case MA35_S_RECE_ARB_LOST : +/* +* Arbitration lost during address transmission phase. +* The hardware switches to Target Transmitter mode when +* our own SLA+W is detected on the bus. +*/ + i2c->err = -EAGAIN ; +ma35d1_i2c_controller_complete (i2c); + ret =i2c_slave_event (i2c->target,I2C_SLAVE_WRITE_REQUESTED , + &byte); +if (ret) + i2c->target_reject = true; +break ; + +case MA35_S_RECE_ADDR_ACK : +/* Own SLA+W has been receive; ACK has been return */ + ret =i2c_slave_event (i2c->target,I2C_SLAVE_WRITE_REQUESTED , + &byte); +if (ret) + i2c->target_reject = true; +break ; + +case MA35_S_TRAN_DATA_NACK : +case MA35_S_RECE_DATA_NACK : +/* +* Data byte or last data in I2CDAT has been transmitted and NACK received, +* or previously addressed with own SLA address and NACK returned. +*/ + i2c->target_reject = false;
drivers/i2c/busses/i2c-ma35d1.c line 277
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10
-0+break ; + +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]
As reported by Andi Shyti on v10 [1], should MA35_S_RECE_DATA_ACK check the return value of i2c_slave_event() and set i2c->target_reject = true on error? Otherwise the controller re-arms MA35_CTL_SI_AA and continues ACKing subsequent bytes after the target backend signals an error or full buffer.
drivers/i2c/busses/i2c-ma35d1.c line 289
Quoted 12 lines
drivers/i2c/busses/i2c-ma35d1.c new file +12
-0+ +case MA35_ADDR_TRAN_ARB_LOST : +/* +* Arbitration lost during address transmission phase. +* The hardware switches to Target Receiver mode when +* our own SLA+R is detected on the bus. +*/ + i2c->err = -EAGAIN ; +ma35d1_i2c_controller_complete (i2c); +i2c_slave_event (i2c->target,I2C_SLAVE_READ_REQUESTED , &byte); +writel (byte, i2c->regs +MA35_DAT ); +break ;
[Severity: Medium]
Can waking up ma35d1_i2c_doxfer() via ma35d1_i2c_controller_complete() before updating MA35_CTL0 in MA35_S_RECE_ARB_LOST, MA35_ADDR_TRAN_ARB_LOST, and MA35_M_ARB_LOST race with ma35d1_i2c_xfer() and automatic retries?
CPU0 CPU1 (<hardirq>)ma35d1_i2c_xfer()
ma35d1_i2c_doxfer()
wait_event_timeout()
ma35d1_i2c_irq_target_trx()
case MA35_S_RECE_ARB_LOST:
i2c->err = -EAGAIN;
ma35d1_i2c_controller_complete()
/* woken up, STO is 0 */
if (i2c->target)
ma35d1_i2c_write_ctl(MA35_CTL_AA)
return -EAGAIN;
__i2c_transfer() /* retries = 2 */
ma35d1_i2c_xfer()
ma35d1_i2c_doxfer()
ma35d1_i2c_write_ctl(MA35_CTL_STA)
i2c_slave_event(...)
ma35d1_i2c_write_ctl(...)In MA35_S_RECE_ARB_LOST and MA35_ADDR_TRAN_ARB_LOST the hardware has just entered target mode at the start of an incoming transfer, so returning -EAGAIN immediately causes __i2c_transfer() to retry ma35d1_i2c_doxfer() and assert MA35_CTL_STA in the middle of the active target transaction. In MA35_M_ARB_LOST, if CPU0 runs ma35d1_i2c_write_ctl(i2c, MA35_CTL_AA) in ma35d1_i2c_xfer() before CPU1 runs ma35d1_i2c_write_ctl(i2c, MA35_CTL_SI), CPU1 clears MA35_CTL_AA and leaves target mode disabled. Also, the comments on MA35_S_RECE_ARB_LOST and MA35_ADDR_TRAN_ARB_LOST have Target Transmitter and Target Receiver swapped.
Quoted 23 lines
drivers/i2c/busses/i2c-ma35d1.c new file +23
-0+ +case MA35_S_TRAN_ADDR_ACK : +/* Own SLA+R has been receive; ACK has been return */ +i2c_slave_event (i2c->target,I2C_SLAVE_READ_REQUESTED , &byte); +writel (byte, i2c->regs +MA35_DAT ); +break ; + +case MA35_S_TRAN_DATA_ACK : +i2c_slave_event (i2c->target,I2C_SLAVE_READ_PROCESSED , &byte); +writel (byte, i2c->regs +MA35_DAT ); +break ; + +case MA35_S_REPEAT_START_STOP : +/* +* A STOP or repeated START has been received +* while still addressed as Target/Receiver +*/ +i2c_slave_event (i2c->target,I2C_SLAVE_STOP , &byte); + i2c->target_reject = false; +break ; + +default : + dev_err(i2c->dev, "Status 0x%02lx is NOT processed\n",
drivers/i2c/busses/i2c-ma35d1.c line 322
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10
-0+ i2c_status); +ma35d1_i2c_restore_idle (i2c); +return IRQ_NONE ; + } +if (i2c->target_reject) +ma35d1_i2c_write_ctl (i2c,MA35_CTL_SI ); +else +ma35d1_i2c_write_ctl (i2c,MA35_CTL_SI_AA ); +return IRQ_HANDLED ; +}
[Severity: Medium]
What happens when the controller reports MA35_S_TRAN_LAST_DATA_ACK (0xc8) or MA35_BUS_RELEASED (0xf8), which are defined at the top of the file but not handled in this switch?
Both statuses fall outside ma35d1_is_controller_status() and land in
default: here, logging an error, calling ma35d1_i2c_restore_idle() (which aborts any active controller transfer with -EIO), and returning IRQ_NONE. In addition, when an external controller ends a target read with NACK (MA35_S_TRAN_DATA_NACK) or after MA35_S_TRAN_LAST_DATA_ACK, the hardware is no longer addressed and does not raise MA35_S_REPEAT_START_STOP on the subsequent STOP, so the target backend never receives I2C_SLAVE_STOP after a read transaction.
[ ... ]
Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8
-0+static irqreturn_t ma35d1_i2c_irq (int irqno,void *dev_id) +{ +struct ma35d1_i2c *i2c = dev_id; +unsigned long status; + + status =readl (i2c->regs +MA35_STATUS0 ); + +if (status ==MA35_BUS_ERROR ) {
drivers/i2c/busses/i2c-ma35d1.c line 470
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10
-0+dev_err (i2c->dev,"bus error\n" ); +ma35d1_i2c_restore_idle (i2c); +return IRQ_HANDLED ; + } + +if (ma35d1_is_controller_status (status)) +return ma35d1_i2c_irq_controller_trx (i2c, status); +else +return ma35d1_i2c_irq_target_trx (i2c, status); +}
[Severity: Medium]
Can an I2C target wakeup event cause an interrupt storm during system resume because ma35d1_i2c_irq() never clears MA35_WKSTS?
ma35d1_i2c_suspend() enables wake-up in MA35_WKCTL and calls enable_irq_wake(i2c->irq). On resume, dpm_resume_noirq() unmasks i2c->irq before ma35d1_i2c_resume() runs in dpm_resume(), so the latched wakeup flag in MA35_WKSTS keeps the level-triggered interrupt asserted while ma35d1_i2c_irq() only reads MA35_STATUS0.
[ ... ]
Quoted 11 lines
drivers/i2c/busses/i2c-ma35d1.c new file +11
-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 );
drivers/i2c/busses/i2c-ma35d1.c line 585
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10
-0+ +synchronize_irq (i2c->irq); + i2c->target =NULL ; + + ret =pm_runtime_put_sync (i2c->dev); +if (ret <0 ) +dev_err (i2c->dev,"failed to suspend i2c controller" ); + +return 0 ; +}
[Severity: High]
Can ma35d1_i2c_irq_target_trx() dereference a NULL i2c->target in i2c_slave_event() or re-enable MA35_CTL_AA during ma35d1_unreg_target()?
CPU0 CPU1 (<hardirq>)ma35d1_unreg_target()
ma35d1_i2c_irq_target_trx()
i2c_slave_event(i2c->target, ...)
writel(0, i2c->regs + MA35_ADDR0)
ma35d1_i2c_write_ctl(i2c, 0)
ma35d1_i2c_write_ctl(MA35_CTL_SI_AA)
synchronize_irq(i2c->irq)
i2c->target = NULL
ma35d1_i2c_irq_target_trx()
i2c_slave_event(NULL, ...)
<-- NULL pointer dereferenceEven if no IRQ is running when ma35d1_i2c_write_ctl(i2c, 0) executes, an external controller that already addressed this device before MA35_ADDR0 and MA35_CTL_AA were cleared will still raise a target interrupt on the in-flight byte or STOP condition after i2c->target is set to NULL, and ma35d1_i2c_irq_target_trx() never checks i2c->target before calling i2c_slave_event() or re-setting MA35_CTL_AA.
[Severity: Low]
Should ma35d1_unreg_target() call pm_runtime_put_autosuspend(i2c->dev) like ma35d1_i2c_xfer() instead of checking pm_runtime_put_sync() for ret < 0? pm_runtime_put_sync() returns negative values such as -ENOSYS (when CONFIG_PM is disabled) or -EBUSY even when the usage count was decremented normally, logging a false error (which is also missing a trailing newline).
drivers/i2c/busses/i2c-ma35d1.c line 591
Quoted 6 lines
drivers/i2c/busses/i2c-ma35d1.c new file +6
-0+ +/* Declare Our I2C Functionality */ +static u32 ma35d1_i2c_func (struct i2c_adapter *adap) +{ +return I2C_FUNC_I2C |I2C_FUNC_SMBUS_EMUL ; +}
[Severity: Low]
Should ma35d1_i2c_func() also advertise I2C_FUNC_SLAVE since the driver selects I2C_SLAVE and implements .reg_target and .unreg_target?
[ ... ]
drivers/i2c/busses/i2c-ma35d1.c line 681
Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8
-0+static void ma35d1_i2c_pm_cleanup (void *data) +{ +struct device *dev = data; + +pm_runtime_get_sync (dev); +pm_runtime_disable (dev); +pm_runtime_put_noidle (dev); +}
[Severity: Low]
Should ma35d1_i2c_pm_cleanup() also call pm_runtime_dont_use_autosuspend(dev) to pair with pm_runtime_use_autosuspend(dev) in ma35d1_i2c_probe()?
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2