Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Freescale MXS On-Chip OTP driver
0004  *
0005  * Copyright (C) 2015 Stefan Wahren <stefan.wahren@i2se.com>
0006  *
0007  * Based on the driver from Huang Shijie and Christoph G. Baumann
0008  */
0009 #include <linux/clk.h>
0010 #include <linux/delay.h>
0011 #include <linux/device.h>
0012 #include <linux/err.h>
0013 #include <linux/io.h>
0014 #include <linux/module.h>
0015 #include <linux/nvmem-provider.h>
0016 #include <linux/of_device.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/slab.h>
0019 #include <linux/stmp_device.h>
0020 
0021 /* OCOTP registers and bits */
0022 
0023 #define BM_OCOTP_CTRL_RD_BANK_OPEN  BIT(12)
0024 #define BM_OCOTP_CTRL_ERROR     BIT(9)
0025 #define BM_OCOTP_CTRL_BUSY      BIT(8)
0026 
0027 #define OCOTP_TIMEOUT       10000
0028 #define OCOTP_DATA_OFFSET   0x20
0029 
0030 struct mxs_ocotp {
0031     struct clk *clk;
0032     void __iomem *base;
0033     struct nvmem_device *nvmem;
0034 };
0035 
0036 static int mxs_ocotp_wait(struct mxs_ocotp *otp)
0037 {
0038     int timeout = OCOTP_TIMEOUT;
0039     unsigned int status = 0;
0040 
0041     while (timeout--) {
0042         status = readl(otp->base);
0043 
0044         if (!(status & (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)))
0045             break;
0046 
0047         cpu_relax();
0048     }
0049 
0050     if (status & BM_OCOTP_CTRL_BUSY)
0051         return -EBUSY;
0052     else if (status & BM_OCOTP_CTRL_ERROR)
0053         return -EIO;
0054 
0055     return 0;
0056 }
0057 
0058 static int mxs_ocotp_read(void *context, unsigned int offset,
0059               void *val, size_t bytes)
0060 {
0061     struct mxs_ocotp *otp = context;
0062     u32 *buf = val;
0063     int ret;
0064 
0065     ret = clk_enable(otp->clk);
0066     if (ret)
0067         return ret;
0068 
0069     writel(BM_OCOTP_CTRL_ERROR, otp->base + STMP_OFFSET_REG_CLR);
0070 
0071     ret = mxs_ocotp_wait(otp);
0072     if (ret)
0073         goto disable_clk;
0074 
0075     /* open OCOTP banks for read */
0076     writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_SET);
0077 
0078     /* approximately wait 33 hclk cycles */
0079     udelay(1);
0080 
0081     ret = mxs_ocotp_wait(otp);
0082     if (ret)
0083         goto close_banks;
0084 
0085     while (bytes) {
0086         if ((offset < OCOTP_DATA_OFFSET) || (offset % 16)) {
0087             /* fill up non-data register */
0088             *buf++ = 0;
0089         } else {
0090             *buf++ = readl(otp->base + offset);
0091         }
0092 
0093         bytes -= 4;
0094         offset += 4;
0095     }
0096 
0097 close_banks:
0098     /* close banks for power saving */
0099     writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_CLR);
0100 
0101 disable_clk:
0102     clk_disable(otp->clk);
0103 
0104     return ret;
0105 }
0106 
0107 static struct nvmem_config ocotp_config = {
0108     .name = "mxs-ocotp",
0109     .stride = 16,
0110     .word_size = 4,
0111     .reg_read = mxs_ocotp_read,
0112 };
0113 
0114 struct mxs_data {
0115     int size;
0116 };
0117 
0118 static const struct mxs_data imx23_data = {
0119     .size = 0x220,
0120 };
0121 
0122 static const struct mxs_data imx28_data = {
0123     .size = 0x2a0,
0124 };
0125 
0126 static const struct of_device_id mxs_ocotp_match[] = {
0127     { .compatible = "fsl,imx23-ocotp", .data = &imx23_data },
0128     { .compatible = "fsl,imx28-ocotp", .data = &imx28_data },
0129     { /* sentinel */},
0130 };
0131 MODULE_DEVICE_TABLE(of, mxs_ocotp_match);
0132 
0133 static void mxs_ocotp_action(void *data)
0134 {
0135     clk_unprepare(data);
0136 }
0137 
0138 static int mxs_ocotp_probe(struct platform_device *pdev)
0139 {
0140     struct device *dev = &pdev->dev;
0141     const struct mxs_data *data;
0142     struct mxs_ocotp *otp;
0143     const struct of_device_id *match;
0144     int ret;
0145 
0146     match = of_match_device(dev->driver->of_match_table, dev);
0147     if (!match || !match->data)
0148         return -EINVAL;
0149 
0150     otp = devm_kzalloc(dev, sizeof(*otp), GFP_KERNEL);
0151     if (!otp)
0152         return -ENOMEM;
0153 
0154     otp->base = devm_platform_ioremap_resource(pdev, 0);
0155     if (IS_ERR(otp->base))
0156         return PTR_ERR(otp->base);
0157 
0158     otp->clk = devm_clk_get(&pdev->dev, NULL);
0159     if (IS_ERR(otp->clk))
0160         return PTR_ERR(otp->clk);
0161 
0162     ret = clk_prepare(otp->clk);
0163     if (ret < 0) {
0164         dev_err(dev, "failed to prepare clk: %d\n", ret);
0165         return ret;
0166     }
0167 
0168     ret = devm_add_action_or_reset(&pdev->dev, mxs_ocotp_action, otp->clk);
0169     if (ret)
0170         return ret;
0171 
0172     data = match->data;
0173 
0174     ocotp_config.size = data->size;
0175     ocotp_config.priv = otp;
0176     ocotp_config.dev = dev;
0177     otp->nvmem = devm_nvmem_register(dev, &ocotp_config);
0178     if (IS_ERR(otp->nvmem))
0179         return PTR_ERR(otp->nvmem);
0180 
0181     platform_set_drvdata(pdev, otp);
0182 
0183     return 0;
0184 }
0185 
0186 static struct platform_driver mxs_ocotp_driver = {
0187     .probe = mxs_ocotp_probe,
0188     .driver = {
0189         .name = "mxs-ocotp",
0190         .of_match_table = mxs_ocotp_match,
0191     },
0192 };
0193 
0194 module_platform_driver(mxs_ocotp_driver);
0195 MODULE_AUTHOR("Stefan Wahren <wahrenst@gmx.net");
0196 MODULE_DESCRIPTION("driver for OCOTP in i.MX23/i.MX28");
0197 MODULE_LICENSE("GPL v2");