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 = 1,
19 FUSE_ELE = 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 u8 skipbytes;
75
76 if (offset + bytes > priv->data->size)
77 bytes = priv->data->size - offset;
78
79 index = offset >> 2;
80 skipbytes = offset - (index << 2);
81 num_bytes = round_up(bytes + skipbytes, 4);
82 count = num_bytes >> 2;
83
84 p = kzalloc(num_bytes, GFP_KERNEL);
85 if (!p)
86 return -ENOMEM;
87
88 mutex_lock(&priv->lock);
89
90 buf = p;
91
92 for (i = index; i < (index + count); i++) {
93 type = imx_ocotp_fuse_type(context, i);
94 if (type == FUSE_INVALID || type == FUSE_ELE) {
95 *buf++ = 0;
96 continue;
97 }
98
99 *buf++ = readl_relaxed(reg + (i << 2));
100 }
101
102 memcpy(val, ((u8 *)p) + skipbytes, bytes);
103
104 mutex_unlock(&priv->lock);
105
106 kfree(p);
107
108 return 0;
109 };
110
imx_ocotp_cell_pp(void * context,const char * id,int index,unsigned int offset,void * data,size_t bytes)111 static int imx_ocotp_cell_pp(void *context, const char *id, int index,
112 unsigned int offset, void *data, size_t bytes)
113 {
114 u8 *buf = data;
115 int i;
116
117 /* Deal with some post processing of nvmem cell data */
118 if (id && !strcmp(id, "mac-address")) {
119 bytes = min(bytes, ETH_ALEN);
120 for (i = 0; i < bytes / 2; i++)
121 swap(buf[i], buf[bytes - i - 1]);
122 }
123
124 return 0;
125 }
126
imx_ocotp_fixup_dt_cell_info(struct nvmem_device * nvmem,struct nvmem_cell_info * cell)127 static void imx_ocotp_fixup_dt_cell_info(struct nvmem_device *nvmem,
128 struct nvmem_cell_info *cell)
129 {
130 cell->read_post_process = imx_ocotp_cell_pp;
131 }
132
imx_ele_ocotp_probe(struct platform_device * pdev)133 static int imx_ele_ocotp_probe(struct platform_device *pdev)
134 {
135 struct device *dev = &pdev->dev;
136 struct imx_ocotp_priv *priv;
137 struct nvmem_device *nvmem;
138
139 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
140 if (!priv)
141 return -ENOMEM;
142
143 priv->data = of_device_get_match_data(dev);
144
145 priv->base = devm_platform_ioremap_resource(pdev, 0);
146 if (IS_ERR(priv->base))
147 return PTR_ERR(priv->base);
148
149 priv->config.dev = dev;
150 priv->config.name = "ELE-OCOTP";
151 priv->config.id = NVMEM_DEVID_AUTO;
152 priv->config.owner = THIS_MODULE;
153 priv->config.size = priv->data->size;
154 priv->config.reg_read = priv->data->reg_read;
155 priv->config.word_size = 1;
156 priv->config.stride = 1;
157 priv->config.priv = priv;
158 priv->config.read_only = true;
159 priv->config.add_legacy_fixed_of_cells = true;
160 priv->config.fixup_dt_cell_info = imx_ocotp_fixup_dt_cell_info;
161 mutex_init(&priv->lock);
162
163 nvmem = devm_nvmem_register(dev, &priv->config);
164 if (IS_ERR(nvmem))
165 return PTR_ERR(nvmem);
166
167 return 0;
168 }
169
170 static const struct ocotp_devtype_data imx93_ocotp_data = {
171 .reg_off = 0x8000,
172 .reg_read = imx_ocotp_reg_read,
173 .size = 2048,
174 .num_entry = 6,
175 .entry = {
176 { 0, 52, FUSE_FSB },
177 { 63, 1, FUSE_ELE},
178 { 128, 16, FUSE_ELE },
179 { 182, 1, FUSE_ELE },
180 { 188, 1, FUSE_ELE },
181 { 312, 200, FUSE_FSB }
182 },
183 };
184
185 static const struct of_device_id imx_ele_ocotp_dt_ids[] = {
186 { .compatible = "fsl,imx93-ocotp", .data = &imx93_ocotp_data, },
187 {},
188 };
189 MODULE_DEVICE_TABLE(of, imx_ele_ocotp_dt_ids);
190
191 static struct platform_driver imx_ele_ocotp_driver = {
192 .driver = {
193 .name = "imx_ele_ocotp",
194 .of_match_table = imx_ele_ocotp_dt_ids,
195 },
196 .probe = imx_ele_ocotp_probe,
197 };
198 module_platform_driver(imx_ele_ocotp_driver);
199
200 MODULE_DESCRIPTION("i.MX OCOTP/ELE driver");
201 MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
202 MODULE_LICENSE("GPL");
203