From c0dee9d06be6b611a7c2c2a0a62bdfbbd2a390be Mon Sep 17 00:00:00 2001 From: Peng Fan Date: Fri, 18 Aug 2017 11:19:08 +0800 Subject: [PATCH] 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 --- drivers/nvmem/Kconfig | 2 +- drivers/nvmem/imx-ocotp.c | 30 ++++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index 29601e0b3e42..f165be7f65cf 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -15,7 +15,7 @@ if NVMEM 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 diff --git a/drivers/nvmem/imx-ocotp.c b/drivers/nvmem/imx-ocotp.c index 8e7b120696fa..d5c02174149d 100644 --- a/drivers/nvmem/imx-ocotp.c +++ b/drivers/nvmem/imx-ocotp.c @@ -3,6 +3,8 @@ * * Copyright (c) 2015 Pengutronix, Philipp Zabel * + * Copyright 2017 NXP + * * Based on the barebox ocotp driver, * Copyright (c) 2010 Baruch Siach , * Orex Computed Radiography @@ -37,26 +39,40 @@ 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; + p = kzalloc(num_bytes, GFP_KERNEL); + if (!p) + return -ENOMEM; + buf = p; + ret = clk_prepare_enable(priv->clk); if (ret < 0) { dev_err(priv->dev, "failed to prepare/enable ocotp clk\n"); + kfree(p); return ret; } - for (i = index; i < (index + count); i++) - *buf++ = readl(priv->base + 0x400 + i * 0x10); + for (i = index; i < (index + count); i++) { + *(u32 *)buf = readl(priv->base + 0x400 + i * 0x10); + buf += 4; + } clk_disable_unprepare(priv->clk); + index = offset % 4; + memcpy(val, &p[index], bytes); + + kfree(p); + return 0; } @@ -64,7 +80,7 @@ static struct nvmem_config imx_ocotp_nvmem_config = { .name = "imx-ocotp", .read_only = true, .word_size = 4, - .stride = 4, + .stride = 1, .owner = THIS_MODULE, .reg_read = imx_ocotp_read, }; @@ -73,6 +89,7 @@ static const struct of_device_id imx_ocotp_dt_ids[] = { { .compatible = "fsl,imx6q-ocotp", (void *)128 }, { .compatible = "fsl,imx6sl-ocotp", (void *)64 }, { .compatible = "fsl,imx6sx-ocotp", (void *)128 }, + { .compatible = "fsl,imx8mq-ocotp", (void *)256 }, { }, }; MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids); @@ -100,6 +117,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; -- 2.17.1