imx-ocotp-fsb-s400.c
5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2021 NXP
* Author: Alice Guo <alice.guo@nxp.com>
*/
#include <linux/dev_printk.h>
#include <linux/errno.h>
#include <linux/firmware/imx/ele_base_msg.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/nvmem-consumer.h>
#include <linux/nvmem-provider.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#define LOCK_CFG 0x01
#define ECID 0x02
#define UNIQ_ID 0x07
#define OTFAD_CFG 0x17
#define MAPPING_SIZE 0x20
enum soc_type {
IMX8ULP,
IMX93,
};
struct bank_2_reg {
unsigned int bank;
unsigned int reg;
bool flag;
};
struct imx_fsb_s400_hw {
enum soc_type soc;
unsigned int fsb_otp_shadow;
const struct bank_2_reg fsb_bank_reg[MAPPING_SIZE];
};
struct imx_fsb_s400_fuse {
void __iomem *regs;
struct nvmem_config config;
struct mutex lock;
const struct imx_fsb_s400_hw *hw;
};
static int read_words_via_s400_api(u32 *buf, unsigned int fuse_base, unsigned int num)
{
unsigned int i;
int err = 0;
for (i = 0; i < num; i++)
err = read_common_fuse(fuse_base + i, buf + i);
return err;
}
static int read_words_via_fsb(void *priv, unsigned int bank, u32 *buf)
{
struct imx_fsb_s400_fuse *fuse = priv;
void __iomem *regs = fuse->regs + fuse->hw->fsb_otp_shadow;
unsigned int i;
unsigned int reg_id = UINT_MAX;
unsigned int size = ARRAY_SIZE(fuse->hw->fsb_bank_reg);
for (i = 0; i < size; i++) {
if (fuse->hw->fsb_bank_reg[i].bank == bank) {
reg_id = fuse->hw->fsb_bank_reg[i].reg;
break;
}
}
if (reg_id != UINT_MAX) {
size = fuse->hw->fsb_bank_reg[i].flag ? 4 : 8;
for (i = 0; i < size; i++) {
*buf = readl_relaxed(regs + (reg_id + i) * 4);
buf = buf + 1;
}
}
return 0;
}
static int read_nwords_via_fsb(void __iomem *regs, u32 *buf, u32 fuse_base, u32 num)
{
unsigned int i;
for (i = 0; i < num; i++) {
*buf = readl_relaxed(regs + (fuse_base + i) * 4);
buf = buf + 1;
}
return 0;
}
static int fsb_s400_fuse_read(void *priv, unsigned int offset, void *val,
size_t bytes)
{
struct imx_fsb_s400_fuse *fuse = priv;
void __iomem *regs = fuse->regs + fuse->hw->fsb_otp_shadow;
unsigned int num_bytes, bank;
u32 *buf;
int err;
num_bytes = round_up(2048, 4);
buf = kzalloc(num_bytes, GFP_KERNEL);
if (!buf)
return -ENOMEM;
err = -EINVAL;
mutex_lock(&fuse->lock);
if (fuse->hw->soc == IMX8ULP) {
for (bank = 0; bank < 63; bank++) {
switch (bank) {
case 0:
break;
case LOCK_CFG:
err = read_words_via_s400_api(&buf[8], 8, 8);
if (err)
goto ret;
break;
case ECID:
err = read_words_via_s400_api(&buf[16], 16, 8);
if (err)
goto ret;
break;
case UNIQ_ID:
err = read_common_fuse(OTP_UNIQ_ID, &buf[56]);
if (err)
goto ret;
break;
case OTFAD_CFG:
err = read_common_fuse(OTFAD_CONFIG, &buf[184]);
if (err)
goto ret;
break;
default:
err = read_words_via_fsb(priv, bank, &buf[bank * 8]);
break;
}
}
} else if (fuse->hw->soc == IMX93) {
for (bank = 0; bank < 6; bank++)
read_nwords_via_fsb(regs, &buf[bank * 8], bank * 8, 8);
read_nwords_via_fsb(regs, &buf[48], 48, 4); /* OTP_UNIQ_ID */
err = read_words_via_s400_api(&buf[63], 63, 1);
if (err)
goto ret;
err = read_words_via_s400_api(&buf[128], 128, 16);
if (err)
goto ret;
err = read_words_via_s400_api(&buf[182], 182, 1);
if (err)
goto ret;
err = read_words_via_s400_api(&buf[188], 188, 1);
if (err)
goto ret;
for (bank = 39; bank < 64; bank++)
read_nwords_via_fsb(regs, &buf[bank * 8], bank * 8, 8);
}
memcpy(val, (u8 *)(buf + offset), bytes);
ret:
kfree(buf);
mutex_unlock(&fuse->lock);
return err;
}
static int imx_fsb_s400_fuse_probe(struct platform_device *pdev)
{
struct imx_fsb_s400_fuse *fuse;
struct nvmem_device *nvmem;
fuse = devm_kzalloc(&pdev->dev, sizeof(*fuse), GFP_KERNEL);
if (!fuse)
return -ENOMEM;
fuse->regs = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(fuse->regs))
return PTR_ERR(fuse->regs);
fuse->config.dev = &pdev->dev;
fuse->config.name = "fsb_s400_fuse";
fuse->config.id = NVMEM_DEVID_AUTO;
fuse->config.owner = THIS_MODULE;
fuse->config.size = 2048; /* 64 Banks */
fuse->config.reg_read = fsb_s400_fuse_read;
fuse->config.priv = fuse;
nvmem = devm_nvmem_register(&pdev->dev, &fuse->config);
if (IS_ERR(nvmem)) {
dev_err(&pdev->dev, "failed to register fuse nvmem device\n");
return PTR_ERR(nvmem);
}
mutex_init(&fuse->lock);
fuse->hw = of_device_get_match_data(&pdev->dev);
dev_dbg(&pdev->dev, "fuse nvmem device registered successfully\n");
return 0;
}
static const struct imx_fsb_s400_hw imx8ulp_fsb_s400_hw = {
.soc = IMX8ULP,
.fsb_otp_shadow = 0x800,
.fsb_bank_reg = {
[0] = { 3, 0 },
[1] = { 4, 8 },
[2] = { 5, 16 },
[3] = { 6, 24 },
[4] = { 8, 80, true },
[5] = { 24, 84, true },
[6] = { 26, 88, true },
[7] = { 27, 92, true },
[8] = { 28, 96 },
[9] = { 29, 104 },
[10] = { 30, 112 },
[11] = { 31, 120 },
[12] = { 37, 128 },
[13] = { 38, 136 },
[14] = { 39, 144 },
[15] = { 40, 152 },
[16] = { 41, 160 },
[17] = { 42, 168 },
[18] = { 43, 176 },
[19] = { 44, 184 },
[20] = { 45, 192 },
[21] = { 46, 200 },
},
};
static const struct imx_fsb_s400_hw imx93_fsb_s400_hw = {
.soc = IMX93,
.fsb_otp_shadow = 0x8000,
};
static const struct of_device_id imx_fsb_s400_fuse_match[] = {
{ .compatible = "fsl,imx8ulp-ocotp", .data = &imx8ulp_fsb_s400_hw, },
{ .compatible = "fsl,imx93-ocotp", .data = &imx93_fsb_s400_hw, },
{},
};
static struct platform_driver imx_fsb_s400_fuse_driver = {
.driver = {
.name = "fsl-ocotp-fsb-s400",
.of_match_table = imx_fsb_s400_fuse_match,
},
.probe = imx_fsb_s400_fuse_probe,
};
MODULE_DEVICE_TABLE(of, imx_fsb_s400_fuse_match);
module_platform_driver(imx_fsb_s400_fuse_driver);
MODULE_AUTHOR("Alice Guo <alice.guo@nxp.com>");
MODULE_DESCRIPTION("i.MX FSB/S400-API ocotp fuse box driver");
MODULE_LICENSE("GPL v2");