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