1 // SPDX-License-Identifier: GPL-2.0-only
3 * i.MX9 OCOTP fusebox driver
8 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/nvmem-provider.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
23 struct ocotp_map_entry {
24 u32 start; /* start word */
25 u32 num; /* num words */
29 struct ocotp_devtype_data {
35 nvmem_reg_read_t reg_read;
36 struct ocotp_map_entry entry[];
39 struct imx_ocotp_priv {
42 struct nvmem_config config;
44 const struct ocotp_devtype_data *data;
47 static enum fuse_type imx_ocotp_fuse_type(void *context, u32 index)
49 struct imx_ocotp_priv *priv = context;
50 const struct ocotp_devtype_data *data = priv->data;
54 for (i = 0; i < data->num_entry; i++) {
55 start = data->entry[i].start;
56 end = data->entry[i].start + data->entry[i].num;
58 if (index >= start && index < end)
59 return data->entry[i].type;
65 static int imx_ocotp_reg_read(void *context, unsigned int offset, void *val, size_t bytes)
67 struct imx_ocotp_priv *priv = context;
68 void __iomem *reg = priv->base + priv->data->reg_off;
69 u32 count, index, num_bytes;
76 if (offset + bytes > priv->data->size)
77 bytes = priv->data->size - offset;
80 skipbytes = offset - (index << 2);
81 num_bytes = round_up(bytes + skipbytes, 4);
82 count = num_bytes >> 2;
84 p = kzalloc(num_bytes, GFP_KERNEL);
88 mutex_lock(&priv->lock);
92 for (i = index; i < (index + count); i++) {
93 type = imx_ocotp_fuse_type(context, i);
94 if (type == FUSE_INVALID || type == FUSE_ELE) {
100 *buf++ = readl_relaxed(reg + (i << 2)) & GENMASK(15, 0);
102 *buf++ = readl_relaxed(reg + (i << 2));
105 memcpy(val, ((u8 *)p) + skipbytes, bytes);
107 mutex_unlock(&priv->lock);
114 static int imx_ocotp_cell_pp(void *context, const char *id, int index,
115 unsigned int offset, void *data, size_t bytes)
120 /* Deal with some post processing of nvmem cell data */
121 if (id && !strcmp(id, "mac-address"))
122 for (i = 0; i < bytes / 2; i++)
123 swap(buf[i], buf[bytes - i - 1]);
128 static void imx_ocotp_fixup_dt_cell_info(struct nvmem_device *nvmem,
129 struct nvmem_cell_info *cell)
131 cell->read_post_process = imx_ocotp_cell_pp;
134 static int imx_ele_ocotp_probe(struct platform_device *pdev)
136 struct device *dev = &pdev->dev;
137 struct imx_ocotp_priv *priv;
138 struct nvmem_device *nvmem;
140 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
144 priv->data = of_device_get_match_data(dev);
146 priv->base = devm_platform_ioremap_resource(pdev, 0);
147 if (IS_ERR(priv->base))
148 return PTR_ERR(priv->base);
150 priv->config.dev = dev;
151 priv->config.name = "ELE-OCOTP";
152 priv->config.id = NVMEM_DEVID_AUTO;
153 priv->config.owner = THIS_MODULE;
154 priv->config.size = priv->data->size;
155 priv->config.reg_read = priv->data->reg_read;
156 priv->config.word_size = 1;
157 priv->config.stride = 1;
158 priv->config.priv = priv;
159 priv->config.read_only = true;
160 priv->config.add_legacy_fixed_of_cells = true;
161 priv->config.fixup_dt_cell_info = imx_ocotp_fixup_dt_cell_info;
162 mutex_init(&priv->lock);
164 nvmem = devm_nvmem_register(dev, &priv->config);
166 return PTR_ERR(nvmem);
171 static const struct ocotp_devtype_data imx93_ocotp_data = {
173 .reg_read = imx_ocotp_reg_read,
179 { 128, 16, FUSE_ELE },
180 { 182, 1, FUSE_ELE },
181 { 188, 1, FUSE_ELE },
182 { 312, 200, FUSE_FSB }
186 static const struct ocotp_devtype_data imx95_ocotp_data = {
188 .reg_read = imx_ocotp_reg_read,
192 { 0, 1, FUSE_FSB | FUSE_ECC },
193 { 7, 1, FUSE_FSB | FUSE_ECC },
194 { 9, 3, FUSE_FSB | FUSE_ECC },
195 { 12, 24, FUSE_FSB },
196 { 36, 2, FUSE_FSB | FUSE_ECC },
197 { 38, 14, FUSE_FSB },
199 { 128, 16, FUSE_ELE },
200 { 188, 1, FUSE_ELE },
201 { 317, 2, FUSE_FSB | FUSE_ECC },
202 { 320, 7, FUSE_FSB },
203 { 328, 184, FUSE_FSB }
207 static const struct of_device_id imx_ele_ocotp_dt_ids[] = {
208 { .compatible = "fsl,imx93-ocotp", .data = &imx93_ocotp_data, },
209 { .compatible = "fsl,imx95-ocotp", .data = &imx95_ocotp_data, },
212 MODULE_DEVICE_TABLE(of, imx_ele_ocotp_dt_ids);
214 static struct platform_driver imx_ele_ocotp_driver = {
216 .name = "imx_ele_ocotp",
217 .of_match_table = imx_ele_ocotp_dt_ids,
219 .probe = imx_ele_ocotp_probe,
221 module_platform_driver(imx_ele_ocotp_driver);
223 MODULE_DESCRIPTION("i.MX OCOTP/ELE driver");
225 MODULE_LICENSE("GPL");