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