1
0
Fork 0

MLK-16204-4: nvmem: imx-ocotp: add i.mx8mq support and fix read

Add i.MX8MQ support and Fix read.

When offset is not 4 bytes aligned, directly shift righty by 2 bits
will cause reading out wrong data. Since imx ocotp only supports
4 bytes reading once, we need handle offset is not 4 bytes aligned
and enlarge the bytes to 4 bytes aligned. After finished reading,
copy the needed data from buffer to caller and free buffer.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
pull/10/head
Peng Fan 2017-08-18 11:19:08 +08:00 committed by Jason Liu
parent 55bddc9061
commit 03fb1ed5af
2 changed files with 26 additions and 7 deletions

View File

@ -26,7 +26,7 @@ config NVMEM_IMX_IIM
config NVMEM_IMX_OCOTP
tristate "i.MX6 On-Chip OTP Controller support"
depends on SOC_IMX6 || COMPILE_TEST
depends on SOC_IMX6 || ARCH_MXC_ARM64 || COMPILE_TEST
depends on HAS_IOMEM
help
This is a driver for the On-Chip OTP Controller (OCOTP) available on

View File

@ -3,6 +3,8 @@
*
* Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
*
* Copyright 2017 NXP
*
* Based on the barebox ocotp driver,
* Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
* Orex Computed Radiography
@ -114,22 +116,29 @@ static int imx_ocotp_read(void *context, unsigned int offset,
{
struct ocotp_priv *priv = context;
unsigned int count;
u32 *buf = val;
u8 *buf, *p;
int i, ret;
u32 index;
u32 index, num_bytes;
index = offset >> 2;
count = bytes >> 2;
num_bytes = round_up((offset % 4) + bytes, 4);
count = num_bytes >> 2;
if (count > (priv->nregs - index))
count = priv->nregs - index;
mutex_lock(&ocotp_mutex);
p = kzalloc(num_bytes, GFP_KERNEL);
if (!p)
return -ENOMEM;
buf = p;
ret = clk_prepare_enable(priv->clk);
if (ret < 0) {
mutex_unlock(&ocotp_mutex);
dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
kfree(p);
return ret;
}
@ -140,7 +149,7 @@ static int imx_ocotp_read(void *context, unsigned int offset,
}
for (i = index; i < (index + count); i++) {
*buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
*(u32 *)buf = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
i * IMX_OCOTP_OFFSET_PER_WORD);
/* 47.3.1.2
@ -149,14 +158,22 @@ static int imx_ocotp_read(void *context, unsigned int offset,
* software before any new write, read or reload access can be
* issued
*/
if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL)
if (*((u32*)buf) == IMX_OCOTP_READ_LOCKED_VAL)
imx_ocotp_clr_err_if_set(priv->base);
buf += 4;
}
ret = 0;
index = offset % 4;
memcpy(val, &p[index], bytes);
read_end:
clk_disable_unprepare(priv->clk);
mutex_unlock(&ocotp_mutex);
kfree(p);
return ret;
}
@ -302,7 +319,7 @@ static struct nvmem_config imx_ocotp_nvmem_config = {
.name = "imx-ocotp",
.read_only = false,
.word_size = 4,
.stride = 4,
.stride = 1,
.owner = THIS_MODULE,
.reg_read = imx_ocotp_read,
.reg_write = imx_ocotp_write,
@ -314,6 +331,7 @@ static const struct of_device_id imx_ocotp_dt_ids[] = {
{ .compatible = "fsl,imx6sx-ocotp", (void *)128 },
{ .compatible = "fsl,imx6ul-ocotp", (void *)128 },
{ .compatible = "fsl,imx7d-ocotp", (void *)64 },
{ .compatible = "fsl,imx8mq-ocotp", (void *)256 },
{ },
};
MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
@ -343,6 +361,7 @@ static int imx_ocotp_probe(struct platform_device *pdev)
of_id = of_match_device(imx_ocotp_dt_ids, dev);
priv->nregs = (unsigned long)of_id->data;
priv->dev = dev;
imx_ocotp_nvmem_config.size = 4 * priv->nregs;
imx_ocotp_nvmem_config.dev = dev;
imx_ocotp_nvmem_config.priv = priv;