MLK-16204-4 nvmem: imx-ocotp: add i.mx8mq support and fix read
authorPeng Fan <peng.fan@nxp.com>
Fri, 18 Aug 2017 03:19:08 +0000 (11:19 +0800)
committerLeonard Crestez <leonard.crestez@nxp.com>
Thu, 18 Apr 2019 00:00:38 +0000 (03:00 +0300)
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>
(cherry picked from commit 23bcfaac856f3f0de9f3113c9c447a6133157019)

drivers/nvmem/imx-ocotp.c

index 56242c7..45f3daf 100644 (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
@@ -125,22 +127,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->params->nregs - index))
                count = priv->params->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;
        }
 
@@ -151,7 +160,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
@@ -160,14 +169,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;
 }
 
@@ -392,7 +409,7 @@ static struct nvmem_config imx_ocotp_nvmem_config = {
        .name = "imx-ocotp",
        .read_only = false,
        .word_size = 4,
-       .stride = 4,
+       .stride = 1,
        .reg_read = imx_ocotp_read,
        .reg_write = imx_ocotp_write,
 };
@@ -433,6 +450,12 @@ static const struct ocotp_params imx7d_params = {
        .set_timing = imx_ocotp_set_imx7_timing,
 };
 
+static const struct ocotp_params imx8mq_params = {
+       .nregs = 256,
+       .bank_address_words = 0,
+       .set_timing = imx_ocotp_set_imx7_timing,
+};
+
 static const struct of_device_id imx_ocotp_dt_ids[] = {
        { .compatible = "fsl,imx6q-ocotp",  .data = &imx6q_params },
        { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
@@ -441,6 +464,7 @@ static const struct of_device_id imx_ocotp_dt_ids[] = {
        { .compatible = "fsl,imx6ull-ocotp", .data = &imx6ul_params },
        { .compatible = "fsl,imx7d-ocotp",  .data = &imx7d_params },
        { .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
+       { .compatible = "fsl,imx8mq-ocotp", .data = &imx8mq_params },
        { },
 };
 MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
@@ -467,6 +491,7 @@ static int imx_ocotp_probe(struct platform_device *pdev)
        if (IS_ERR(priv->clk))
                return PTR_ERR(priv->clk);
 
+       priv->dev = dev;
        priv->params = of_device_get_match_data(&pdev->dev);
        imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
        imx_ocotp_nvmem_config.dev = dev;