[PATCH can-next v1 2/3] can: flexcan: support 32-bit writes when initializing ECC RAM

From: Liu Changjie

Date: Sun Nov 29 2026 - 08:56:07 EST


On RV64, memset_io() uses 64-bit stores for aligned I/O ranges. The vendor
driver for the SpacemiT X1 initializes FlexCAN ECC RAM with 32-bit writes,
including the second RAM window when CAN FD is enabled.

Select the access width per device so FlexCAN can use its 32-bit register
accessor on this hardware and retain memset_io() elsewhere.

Link: https://github.com/orangepi-xunlong/linux-orangepi/blob/ae9e974d3e19f460b6397bfe8f0f1417a073ce05/drivers/net/can/flexcan/flexcan-core.c#L1382
Signed-off-by: Liu Changjie <liucj1228@xxxxxxxxxxx>
---
drivers/net/can/flexcan/flexcan-core.c | 18 ++++++++++++++++--
drivers/net/can/flexcan/flexcan.h | 3 +++
2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/flexcan/flexcan-core.c b/drivers/net/can/flexcan/flexcan-core.c
index 86b40abe9..a05928d5e 100644
--- a/drivers/net/can/flexcan/flexcan-core.c
+++ b/drivers/net/can/flexcan/flexcan-core.c
@@ -292,6 +292,8 @@ struct flexcan_regs {
};

static_assert(sizeof(struct flexcan_regs) == 0x4 * 18 + 0xfb8);
+static_assert(IS_ALIGNED(sizeof_field(struct flexcan_regs, init), sizeof(u32)));
+static_assert(IS_ALIGNED(sizeof_field(struct flexcan_regs, init_fd), sizeof(u32)));

static const struct flexcan_devtype_data fsl_mcf5441x_devtype_data = {
.quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE |
@@ -1354,6 +1356,18 @@ static void flexcan_set_bittiming(struct net_device *dev)
return flexcan_set_bittiming_ctrl(dev);
}

+static void flexcan_clear_ram(const struct flexcan_priv *priv, void __iomem *addr,
+ size_t size)
+{
+ if (!(priv->devtype_data.quirks & FLEXCAN_QUIRK_ECC_WRITE32)) {
+ memset_io(addr, 0, size);
+ return;
+ }
+
+ for (size_t offset = 0; offset < size; offset += sizeof(u32))
+ priv->write(0, addr + offset);
+}
+
static void flexcan_ram_init(struct net_device *dev)
{
struct flexcan_priv *priv = netdev_priv(dev);
@@ -1372,10 +1386,10 @@ static void flexcan_ram_init(struct net_device *dev)
reg_ctrl2 |= FLEXCAN_CTRL2_WRMFRZ;
priv->write(reg_ctrl2, &regs->ctrl2);

- memset_io(&regs->init, 0, sizeof(regs->init));
+ flexcan_clear_ram(priv, &regs->init, sizeof(regs->init));

if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
- memset_io(&regs->init_fd, 0, sizeof(regs->init_fd));
+ flexcan_clear_ram(priv, &regs->init_fd, sizeof(regs->init_fd));

reg_ctrl2 &= ~FLEXCAN_CTRL2_WRMFRZ;
priv->write(reg_ctrl2, &regs->ctrl2);
diff --git a/drivers/net/can/flexcan/flexcan.h b/drivers/net/can/flexcan/flexcan.h
index 16692a250..06a7bd545 100644
--- a/drivers/net/can/flexcan/flexcan.h
+++ b/drivers/net/can/flexcan/flexcan.h
@@ -75,6 +75,9 @@
*/
#define FLEXCAN_QUIRK_SECONDARY_MB_IRQ BIT(18)

+/* Initialize ECC-protected RAM through 32-bit writes. */
+#define FLEXCAN_QUIRK_ECC_WRITE32 BIT(19)
+
struct flexcan_devtype_data {
u32 quirks; /* quirks needed for different IP cores */
};