1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * i.MX9 OCOTP fusebox driver
4 *
5 * Copyright 2023 NXP
6 */
7
8 #include <linux/device.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/nvmem-provider.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15
16 enum fuse_type {
17 FUSE_FSB = BIT(0),
18 FUSE_ELE = BIT(1),
19 FUSE_ECC = BIT(2),
20 FUSE_INVALID = -1
21 };
22
23 struct ocotp_map_entry {
24 u32 start; /* start word */
25 u32 num; /* num words */
26 enum fuse_type type;
27 };
28
29 struct ocotp_devtype_data {
30 u32 reg_off;
31 char *name;
32 u32 size;
33 u32 num_entry;
34 u32 flag;
35 nvmem_reg_read_t reg_read;
36 struct ocotp_map_entry entry[];
37 };
38
39 struct imx_ocotp_priv {
40 struct device *dev;
41 void __iomem *base;
42 struct nvmem_config config;
43 struct mutex lock;
44 const struct ocotp_devtype_data *data;
45 };
46
imx_ocotp_fuse_type(void * context,u32 index)47 static enum fuse_type imx_ocotp_fuse_type(void *context, u32 index)
48 {
49 struct imx_ocotp_priv *priv = context;
50 const struct ocotp_devtype_data *data = priv->data;
51 u32 start, end;
52 int i;
53
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;
57
58 if (index >= start && index < end)
59 return data->entry[i].type;
60 }
61
62 return FUSE_INVALID;
63 }
64
imx_ocotp_reg_read(void * context,unsigned int offset,void * val,size_t bytes)65 static int imx_ocotp_reg_read(void *context, unsigned int offset, void *val, size_t bytes)
66 {
67 struct imx_ocotp_priv *priv = context;
68 void __iomem *reg = priv->base + priv->data->reg_off;
69 u32 count, index, num_bytes;
70 enum fuse_type type;
71 u32 *buf;
72 void *p;
73 int i;
74
75 index = offset;
76 num_bytes = round_up(bytes, 4);
77 count = num_bytes >> 2;
78
79 if (count > ((priv->data->size >> 2) - index))
80 count = (priv->data->size >> 2) - index;
81
82 p = kzalloc(num_bytes, GFP_KERNEL);
83 if (!p)
84 return -ENOMEM;
85
86 mutex_lock(&priv->lock);
87
88 buf = p;
89
90 for (i = index; i < (index + count); i++) {
91 type = imx_ocotp_fuse_type(context, i);
92 if (type == FUSE_INVALID || type == FUSE_ELE) {
93 *buf++ = 0;
94 continue;
95 }
96
97 if (type & FUSE_ECC)
98 *buf++ = readl_relaxed(reg + (i << 2)) & GENMASK(15, 0);
99 else
100 *buf++ = readl_relaxed(reg + (i << 2));
101 }
102
103 memcpy(val, (u8 *)p, bytes);
104
105 mutex_unlock(&priv->lock);
106
107 kfree(p);
108
109 return 0;
110 };
111
imx_ele_ocotp_probe(struct platform_device * pdev)112 static int imx_ele_ocotp_probe(struct platform_device *pdev)
113 {
114 struct device *dev = &pdev->dev;
115 struct imx_ocotp_priv *priv;
116 struct nvmem_device *nvmem;
117
118 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
119 if (!priv)
120 return -ENOMEM;
121
122 priv->data = of_device_get_match_data(dev);
123
124 priv->base = devm_platform_ioremap_resource(pdev, 0);
125 if (IS_ERR(priv->base))
126 return PTR_ERR(priv->base);
127
128 priv->config.dev = dev;
129 priv->config.name = "ELE-OCOTP";
130 priv->config.id = NVMEM_DEVID_AUTO;
131 priv->config.owner = THIS_MODULE;
132 priv->config.size = priv->data->size;
133 priv->config.reg_read = priv->data->reg_read;
134 priv->config.word_size = 4;
135 priv->config.stride = 1;
136 priv->config.priv = priv;
137 priv->config.read_only = true;
138 mutex_init(&priv->lock);
139
140 nvmem = devm_nvmem_register(dev, &priv->config);
141 if (IS_ERR(nvmem))
142 return PTR_ERR(nvmem);
143
144 return 0;
145 }
146
147 static const struct ocotp_devtype_data imx93_ocotp_data = {
148 .reg_off = 0x8000,
149 .reg_read = imx_ocotp_reg_read,
150 .size = 2048,
151 .num_entry = 6,
152 .entry = {
153 { 0, 52, FUSE_FSB },
154 { 63, 1, FUSE_ELE},
155 { 128, 16, FUSE_ELE },
156 { 182, 1, FUSE_ELE },
157 { 188, 1, FUSE_ELE },
158 { 312, 200, FUSE_FSB }
159 },
160 };
161
162 static const struct ocotp_devtype_data imx95_ocotp_data = {
163 .reg_off = 0x8000,
164 .reg_read = imx_ocotp_reg_read,
165 .size = 2048,
166 .num_entry = 12,
167 .entry = {
168 { 0, 1, FUSE_FSB | FUSE_ECC },
169 { 7, 1, FUSE_FSB | FUSE_ECC },
170 { 9, 3, FUSE_FSB | FUSE_ECC },
171 { 12, 24, FUSE_FSB },
172 { 36, 2, FUSE_FSB | FUSE_ECC },
173 { 38, 14, FUSE_FSB },
174 { 63, 1, FUSE_ELE },
175 { 128, 16, FUSE_ELE },
176 { 188, 1, FUSE_ELE },
177 { 317, 2, FUSE_FSB | FUSE_ECC },
178 { 320, 7, FUSE_FSB },
179 { 328, 184, FUSE_FSB }
180 },
181 };
182
183 static const struct of_device_id imx_ele_ocotp_dt_ids[] = {
184 { .compatible = "fsl,imx93-ocotp", .data = &imx93_ocotp_data, },
185 { .compatible = "fsl,imx95-ocotp", .data = &imx95_ocotp_data, },
186 {},
187 };
188 MODULE_DEVICE_TABLE(of, imx_ele_ocotp_dt_ids);
189
190 static struct platform_driver imx_ele_ocotp_driver = {
191 .driver = {
192 .name = "imx_ele_ocotp",
193 .of_match_table = imx_ele_ocotp_dt_ids,
194 },
195 .probe = imx_ele_ocotp_probe,
196 };
197 module_platform_driver(imx_ele_ocotp_driver);
198
199 MODULE_DESCRIPTION("i.MX OCOTP/ELE driver");
200 MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
201 MODULE_LICENSE("GPL");
202