1 From 3311bf18467272388039922a5e29c4925b291f73 Mon Sep 17 00:00:00 2001
2 From: Peng Fan <peng.fan@nxp.com>
3 Date: Fri, 27 Nov 2020 10:28:37 +0000
4 Subject: [PATCH] nvmem: imx-ocotp: add support for the unaliged word count
6 When offset is not 4 bytes aligned, directly shift righty by 2 bits
7 will cause reading out wrong data. Since imx ocotp only supports
8 4 bytes reading once, we need handle offset is not 4 bytes aligned
9 and enlarge the bytes to 4 bytes aligned. After reading finished,
10 copy the needed data from buffer to caller and free buffer.
12 Signed-off-by: Peng Fan <peng.fan@nxp.com>
13 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
14 Link: https://lore.kernel.org/r/20201127102837.19366-6-srinivas.kandagatla@linaro.org
15 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
17 drivers/nvmem/imx-ocotp.c | 30 ++++++++++++++++++++++++------
18 1 file changed, 24 insertions(+), 6 deletions(-)
20 --- a/drivers/nvmem/imx-ocotp.c
21 +++ b/drivers/nvmem/imx-ocotp.c
24 * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
26 + * Copyright 2019 NXP
28 * Based on the barebox ocotp driver,
29 * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
30 * Orex Computed Radiography
31 @@ -158,22 +160,30 @@ static int imx_ocotp_read(void *context,
33 struct ocotp_priv *priv = context;
39 + u32 index, num_bytes;
43 + num_bytes = round_up((offset % 4) + bytes, 4);
44 + count = num_bytes >> 2;
46 if (count > (priv->params->nregs - index))
47 count = priv->params->nregs - index;
49 + p = kzalloc(num_bytes, GFP_KERNEL);
53 mutex_lock(&ocotp_mutex);
57 ret = clk_prepare_enable(priv->clk);
59 mutex_unlock(&ocotp_mutex);
60 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
65 @@ -184,7 +194,7 @@ static int imx_ocotp_read(void *context,
68 for (i = index; i < (index + count); i++) {
69 - *buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
70 + *(u32 *)buf = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
71 i * IMX_OCOTP_OFFSET_PER_WORD);
74 @@ -193,13 +203,21 @@ static int imx_ocotp_read(void *context,
75 * software before any new write, read or reload access can be
78 - if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL)
79 + if (*((u32 *)buf) == IMX_OCOTP_READ_LOCKED_VAL)
80 imx_ocotp_clr_err_if_set(priv);
86 + memcpy(val, &p[index], bytes);
89 clk_disable_unprepare(priv->clk);
90 mutex_unlock(&ocotp_mutex);
97 @@ -447,7 +465,7 @@ static struct nvmem_config imx_ocotp_nvm
103 .reg_read = imx_ocotp_read,
104 .reg_write = imx_ocotp_write,