From nobody Thu Apr 2 17:09:59 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F20D03F787A for ; Fri, 27 Mar 2026 13:18:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774617487; cv=none; b=Rq/SB3+VToo9wR/TJ2YHDiD7m3CLbZBK8NKuzlauo1EnaqPC8yhKZGa+mMs1CmSGPz23A8DUFYZYlHg1BnvX9cvGQ3JMRKRjT4OFyAv8OQ5NHBe7AAT57mmLw2K3nZvPiyxrKjZWCzmgcHuCWg5E3CPD5NleMKyqYUlCAk+fNhs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774617487; c=relaxed/simple; bh=uVuNAXkly6zyPTtUMNsbq0s0vIqZ8JSZTBK2WsISH7Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=K2MQejcnq2Fxo1us2agTvnwakpiANstrQg1/AYrSesuncjO6zHBkEALVED0eO1AxUZI/tFAmEM4tIfgmh8BxXYPXLkUM09BZtVN9efVm2QI45y6uo2ET3msGO9wR1oU/qqqv75e4tSAJ8B0vWUXatzcjqSTOBjKAvsgzobe3c94= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h1qjWYvy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="h1qjWYvy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99145C2BC9E; Fri, 27 Mar 2026 13:18:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774617486; bh=uVuNAXkly6zyPTtUMNsbq0s0vIqZ8JSZTBK2WsISH7Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h1qjWYvy/aFTsbXKSnu15xzIJjEBHzQ/x0IrkZscmG1+jGe6+sqqNWxoU9cX/J5wv X8Ur0aQeKkFLP9muDfRj0y7Jfz5aTtRZ6rsnKUDJLSh6vfIf47CUAgdbmjX8wIS9l3 iWgqnHkvlPbQwSnmX/ox+okwqmJ3IvSTyLmubFMpW1h2QaLCgGeitFk86n/2VuDGA2 V4+0mVcqLuIwxQpKf6Dp0hERPdZjLyE7RrvmVXIm5YZLmrPYggjsl0CoN7c2N58dn5 CL9CYKTzZGhd3PRZdvXZ0FcD1f75W+s0Klubx6xQeT2TRX7Lu9cu0KzGHbb9QXstl1 u00zCRKvqiXtg== From: srini@kernel.org To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Jonas Karlman , Willy Tarreau , Heiko Stuebner , Srinivas Kandagatla Subject: [PATCH 6/9] nvmem: rockchip-otp: Handle internal word_size in main reg_read op Date: Fri, 27 Mar 2026 13:17:48 +0000 Message-ID: <20260327131751.3026030-7-srini@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260327131751.3026030-1-srini@kernel.org> References: <20260327131751.3026030-1-srini@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jonas Karlman Rockchip SoCs RK3576 and RK3588 read data from the OTP using 32-bit words instead of normal 8-bit bytes. Similar RK3506, RK3528, RK3562 and RK3568 will read data from OTP using 16-bit words. The nvmem core stride and word_size cannot fully be used as cells is not always aligned. Continue to report a stride=3D1 and word_size=3D1 in nvmem_config and instead handle use of SoC specific word_size internally in the driver. Move current SoC specific word_size handling from the RK3588 read_reg operation to the main read_reg operation to help simplify the SoC specific read_reg operation and allow code reuse in a future RK3568 reg_read operation. Signed-off-by: Jonas Karlman Tested-by: Willy Tarreau Signed-off-by: Heiko Stuebner Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/rockchip-otp.c | 72 ++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/drivers/nvmem/rockchip-otp.c b/drivers/nvmem/rockchip-otp.c index d88f12c53242..45bbb6147fb7 100644 --- a/drivers/nvmem/rockchip-otp.c +++ b/drivers/nvmem/rockchip-otp.c @@ -59,7 +59,6 @@ #define RK3588_OTPC_AUTO_EN 0x08 #define RK3588_OTPC_INT_ST 0x84 #define RK3588_OTPC_DOUT0 0x20 -#define RK3588_NBYTES 4 #define RK3588_BURST_NUM 1 #define RK3588_BURST_SHIFT 8 #define RK3588_ADDR_SHIFT 16 @@ -69,6 +68,7 @@ struct rockchip_data { int size; int read_offset; + int word_size; const char * const *clks; int num_clks; nvmem_reg_read_t reg_read; @@ -185,48 +185,28 @@ static int px30_otp_read(void *context, unsigned int = offset, } =20 static int rk3588_otp_read(void *context, unsigned int offset, - void *val, size_t bytes) + void *val, size_t count) { struct rockchip_otp *otp =3D context; - unsigned int addr_start, addr_end, addr_len; - int ret, i =3D 0; - u32 data; - u8 *buf; - - addr_start =3D round_down(offset, RK3588_NBYTES) / RK3588_NBYTES; - addr_end =3D round_up(offset + bytes, RK3588_NBYTES) / RK3588_NBYTES; - addr_len =3D addr_end - addr_start; - addr_start +=3D otp->data->read_offset / RK3588_NBYTES; - - buf =3D kzalloc(array_size(addr_len, RK3588_NBYTES), GFP_KERNEL); - if (!buf) - return -ENOMEM; + u32 *buf =3D val; + int ret; =20 - while (addr_len--) { - writel((addr_start << RK3588_ADDR_SHIFT) | + while (count--) { + writel((offset++ << RK3588_ADDR_SHIFT) | (RK3588_BURST_NUM << RK3588_BURST_SHIFT), otp->base + RK3588_OTPC_AUTO_CTRL); writel(RK3588_AUTO_EN, otp->base + RK3588_OTPC_AUTO_EN); =20 ret =3D rockchip_otp_wait_status(otp, RK3588_OTPC_INT_ST, RK3588_RD_DONE); - if (ret < 0) { + if (ret) { dev_err(otp->dev, "timeout during read setup\n"); - goto read_end; + return ret; } =20 - data =3D readl(otp->base + RK3588_OTPC_DOUT0); - memcpy(&buf[i], &data, RK3588_NBYTES); - - i +=3D RK3588_NBYTES; - addr_start++; + *buf++ =3D readl(otp->base + RK3588_OTPC_DOUT0); } =20 - memcpy(val, buf + offset % RK3588_NBYTES, bytes); - -read_end: - kfree(buf); - return ret; } =20 @@ -234,7 +214,7 @@ static int rockchip_otp_read(void *context, unsigned in= t offset, void *val, size_t bytes) { struct rockchip_otp *otp =3D context; - int ret; + int ret, word_size; =20 if (!otp->data || !otp->data->reg_read) return -EINVAL; @@ -245,8 +225,34 @@ static int rockchip_otp_read(void *context, unsigned i= nt offset, return ret; } =20 - ret =3D otp->data->reg_read(context, offset, val, bytes); + offset +=3D otp->data->read_offset; + word_size =3D otp->data->word_size; + + if (word_size > 1) { + unsigned int addr_start, addr_end; + size_t count; + u8 *buf; + + addr_start =3D offset / word_size; + addr_end =3D DIV_ROUND_UP(offset + bytes, word_size); + count =3D addr_end - addr_start; + + buf =3D kzalloc(array_size(count, word_size), GFP_KERNEL); + if (!buf) { + ret =3D -ENOMEM; + goto err; + } + + ret =3D otp->data->reg_read(context, addr_start, buf, count); + if (!ret) + memcpy(val, buf + (offset % word_size), bytes); + + kfree(buf); + } else { + ret =3D otp->data->reg_read(context, offset, val, bytes); + } =20 +err: clk_bulk_disable_unprepare(otp->data->num_clks, otp->clks); =20 return ret; @@ -259,7 +265,7 @@ static struct nvmem_config otp_config =3D { .type =3D NVMEM_TYPE_OTP, .read_only =3D true, .stride =3D 1, - .word_size =3D 1, + .word_size =3D sizeof(u8), .reg_read =3D rockchip_otp_read, }; =20 @@ -277,6 +283,7 @@ static const struct rockchip_data px30_data =3D { static const struct rockchip_data rk3576_data =3D { .size =3D 0x100, .read_offset =3D 0x700, + .word_size =3D sizeof(u32), .clks =3D px30_otp_clocks, .num_clks =3D ARRAY_SIZE(px30_otp_clocks), .reg_read =3D rk3588_otp_read, @@ -289,6 +296,7 @@ static const char * const rk3588_otp_clocks[] =3D { static const struct rockchip_data rk3588_data =3D { .size =3D 0x400, .read_offset =3D 0xc00, + .word_size =3D sizeof(u32), .clks =3D rk3588_otp_clocks, .num_clks =3D ARRAY_SIZE(rk3588_otp_clocks), .reg_read =3D rk3588_otp_read, --=20 2.47.3