0a05e0a97f51fb2c6e8f6b1fee9e10a37747d589
[openwrt/staging/neocturne.git] /
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
5
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.
11
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>
16 ---
17 drivers/nvmem/imx-ocotp.c | 30 ++++++++++++++++++++++++------
18 1 file changed, 24 insertions(+), 6 deletions(-)
19
20 --- a/drivers/nvmem/imx-ocotp.c
21 +++ b/drivers/nvmem/imx-ocotp.c
22 @@ -4,6 +4,8 @@
23 *
24 * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
25 *
26 + * Copyright 2019 NXP
27 + *
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,
32 {
33 struct ocotp_priv *priv = context;
34 unsigned int count;
35 - u32 *buf = val;
36 + u8 *buf, *p;
37 int i, ret;
38 - u32 index;
39 + u32 index, num_bytes;
40
41 index = offset >> 2;
42 - count = bytes >> 2;
43 + num_bytes = round_up((offset % 4) + bytes, 4);
44 + count = num_bytes >> 2;
45
46 if (count > (priv->params->nregs - index))
47 count = priv->params->nregs - index;
48
49 + p = kzalloc(num_bytes, GFP_KERNEL);
50 + if (!p)
51 + return -ENOMEM;
52 +
53 mutex_lock(&ocotp_mutex);
54
55 + buf = p;
56 +
57 ret = clk_prepare_enable(priv->clk);
58 if (ret < 0) {
59 mutex_unlock(&ocotp_mutex);
60 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
61 + kfree(p);
62 return ret;
63 }
64
65 @@ -184,7 +194,7 @@ static int imx_ocotp_read(void *context,
66 }
67
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);
72
73 /* 47.3.1.2
74 @@ -193,13 +203,21 @@ static int imx_ocotp_read(void *context,
75 * software before any new write, read or reload access can be
76 * issued
77 */
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);
81 +
82 + buf += 4;
83 }
84
85 + index = offset % 4;
86 + memcpy(val, &p[index], bytes);
87 +
88 read_end:
89 clk_disable_unprepare(priv->clk);
90 mutex_unlock(&ocotp_mutex);
91 +
92 + kfree(p);
93 +
94 return ret;
95 }
96
97 @@ -447,7 +465,7 @@ static struct nvmem_config imx_ocotp_nvm
98 .name = "imx-ocotp",
99 .read_only = false,
100 .word_size = 4,
101 - .stride = 4,
102 + .stride = 1,
103 .reg_read = imx_ocotp_read,
104 .reg_write = imx_ocotp_write,
105 };