blob: 80318c339eb7bc56b7fbaef259e9747a99acb52c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mario Six10d14492017-01-11 16:01:00 +01002/*
3 * Copyright (C) 2015-2016 Reinhard Pfau <reinhard.pfau@gdsys.cc>
Mario Six10d14492017-01-11 16:01:00 +01004 */
5
6#include <config.h>
7#include <common.h>
8#include <errno.h>
9#include <asm/io.h>
10#include <asm/arch/cpu.h>
11#include <asm/arch/efuse.h>
12#include <asm/arch/soc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060013#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Mario Six10d14492017-01-11 16:01:00 +010015#include <linux/mbus.h>
16
17#if defined(CONFIG_MVEBU_EFUSE_FAKE)
18#define DRY_RUN
19#else
20#undef DRY_RUN
21#endif
22
23#define MBUS_EFUSE_BASE 0xF6000000
24#define MBUS_EFUSE_SIZE BIT(20)
25
26#define MVEBU_EFUSE_CONTROL (MVEBU_REGISTER(0xE4008))
27
28enum {
29 MVEBU_EFUSE_CTRL_PROGRAM_ENABLE = (1 << 31),
Pali Rohárdf1da732022-04-06 14:18:18 +020030 MVEBU_EFUSE_LD1_SELECT = (1 << 6),
Mario Six10d14492017-01-11 16:01:00 +010031};
32
33struct mvebu_hd_efuse {
34 u32 bits_31_0;
35 u32 bits_63_32;
36 u32 bit64;
37 u32 reserved0;
38};
39
40#ifndef DRY_RUN
41static struct mvebu_hd_efuse *efuses =
42 (struct mvebu_hd_efuse *)(MBUS_EFUSE_BASE + 0xF9000);
Pali Rohárdf1da732022-04-06 14:18:18 +020043static u32 *ld_efuses = (void *)MBUS_EFUSE_BASE + 0xF8F00;
Mario Six10d14492017-01-11 16:01:00 +010044#else
45static struct mvebu_hd_efuse efuses[EFUSE_LINE_MAX + 1];
Pali Rohárdf1da732022-04-06 14:18:18 +020046static u32 ld_efuses[EFUSE_LD_WORDS];
Mario Six10d14492017-01-11 16:01:00 +010047#endif
48
49static int efuse_initialised;
50
51static struct mvebu_hd_efuse *get_efuse_line(int nr)
52{
53 if (nr < 0 || nr > 63 || !efuse_initialised)
54 return NULL;
55
56 return efuses + nr;
57}
58
59static void enable_efuse_program(void)
60{
61#ifndef DRY_RUN
62 setbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_CTRL_PROGRAM_ENABLE);
63#endif
64}
65
66static void disable_efuse_program(void)
67{
68#ifndef DRY_RUN
69 clrbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_CTRL_PROGRAM_ENABLE);
70#endif
71}
72
73static int do_prog_efuse(struct mvebu_hd_efuse *efuse,
74 struct efuse_val *new_val, u32 mask0, u32 mask1)
75{
76 struct efuse_val val;
77
78 val.dwords.d[0] = readl(&efuse->bits_31_0);
79 val.dwords.d[1] = readl(&efuse->bits_63_32);
80 val.lock = readl(&efuse->bit64);
81
82 if (val.lock & 1)
83 return -EPERM;
84
85 val.dwords.d[0] |= (new_val->dwords.d[0] & mask0);
86 val.dwords.d[1] |= (new_val->dwords.d[1] & mask1);
87 val.lock |= new_val->lock;
88
89 writel(val.dwords.d[0], &efuse->bits_31_0);
90 mdelay(1);
91 writel(val.dwords.d[1], &efuse->bits_63_32);
92 mdelay(1);
93 writel(val.lock, &efuse->bit64);
94 mdelay(5);
95
96 return 0;
97}
98
99static int prog_efuse(int nr, struct efuse_val *new_val, u32 mask0, u32 mask1)
100{
101 struct mvebu_hd_efuse *efuse;
102 int res = 0;
103
104 res = mvebu_efuse_init_hw();
105 if (res)
106 return res;
107
108 efuse = get_efuse_line(nr);
109 if (!efuse)
110 return -ENODEV;
111
112 if (!new_val)
113 return -EINVAL;
114
115 /* only write a fuse line with lock bit */
116 if (!new_val->lock)
117 return -EINVAL;
118
119 /* according to specs ECC protection bits must be 0 on write */
120 if (new_val->bytes.d[7] & 0xFE)
121 return -EINVAL;
122
123 if (!new_val->dwords.d[0] && !new_val->dwords.d[1] && (mask0 | mask1))
124 return 0;
125
126 enable_efuse_program();
127
128 res = do_prog_efuse(efuse, new_val, mask0, mask1);
129
130 disable_efuse_program();
131
132 return res;
133}
134
135int mvebu_efuse_init_hw(void)
136{
137 int ret;
138
139 if (efuse_initialised)
140 return 0;
141
142 ret = mvebu_mbus_add_window_by_id(
143 CPU_TARGET_SATA23_DFX, 0xA, MBUS_EFUSE_BASE, MBUS_EFUSE_SIZE);
144
145 if (ret)
146 return ret;
147
148 efuse_initialised = 1;
149
150 return 0;
151}
152
153int mvebu_read_efuse(int nr, struct efuse_val *val)
154{
155 struct mvebu_hd_efuse *efuse;
156 int res;
157
158 res = mvebu_efuse_init_hw();
159 if (res)
160 return res;
161
162 efuse = get_efuse_line(nr);
163 if (!efuse)
164 return -ENODEV;
165
166 if (!val)
167 return -EINVAL;
168
169 val->dwords.d[0] = readl(&efuse->bits_31_0);
170 val->dwords.d[1] = readl(&efuse->bits_63_32);
171 val->lock = readl(&efuse->bit64);
172 return 0;
173}
174
Pali Rohárdf1da732022-04-06 14:18:18 +0200175void mvebu_read_ld_efuse(int ld1, u32 *line)
176{
177 int i;
178
179#ifndef DRY_RUN
180 if (ld1)
181 setbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_LD1_SELECT);
182 else
183 clrbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_LD1_SELECT);
184#endif
185
186 for (i = 0; i < EFUSE_LD_WORDS; i++)
187 line[i] = readl(ld_efuses + i);
188}
189
Mario Six10d14492017-01-11 16:01:00 +0100190int mvebu_write_efuse(int nr, struct efuse_val *val)
191{
192 return prog_efuse(nr, val, ~0, ~0);
193}
194
195int mvebu_lock_efuse(int nr)
196{
197 struct efuse_val val = {
198 .lock = 1,
199 };
200
201 return prog_efuse(nr, &val, 0, 0);
202}
203
204/*
205 * wrapper funcs providing the fuse API
206 *
207 * we use the following mapping:
208 * "bank" -> eFuse line
209 * "word" -> 0: bits 0-31
210 * 1: bits 32-63
211 * 2: bit 64 (lock)
212 */
213
214static struct efuse_val prog_val;
215static int valid_prog_words;
216
217int fuse_read(u32 bank, u32 word, u32 *val)
218{
219 struct efuse_val fuse_line;
Pali Rohárdf1da732022-04-06 14:18:18 +0200220 u32 ld_line[EFUSE_LD_WORDS];
Mario Six10d14492017-01-11 16:01:00 +0100221 int res;
222
Pali Rohárdf1da732022-04-06 14:18:18 +0200223 if ((bank == EFUSE_LD0_LINE || bank == EFUSE_LD1_LINE) && word < EFUSE_LD_WORDS) {
224 res = mvebu_efuse_init_hw();
225 if (res)
226 return res;
227 mvebu_read_ld_efuse(bank == EFUSE_LD1_LINE, ld_line);
228 *val = ld_line[word];
229 return 0;
230 }
231
Mario Six10d14492017-01-11 16:01:00 +0100232 if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2)
233 return -EINVAL;
234
235 res = mvebu_read_efuse(bank, &fuse_line);
236 if (res)
237 return res;
238
239 if (word < 2)
240 *val = fuse_line.dwords.d[word];
241 else
242 *val = fuse_line.lock;
243
244 return res;
245}
246
247int fuse_sense(u32 bank, u32 word, u32 *val)
248{
249 /* not supported */
250 return -ENOSYS;
251}
252
253int fuse_prog(u32 bank, u32 word, u32 val)
254{
255 int res = 0;
256
257 /*
258 * NOTE: Fuse line should be written as whole.
259 * So how can we do that with this API?
260 * For now: remember values for word == 0 and word == 1 and write the
261 * whole line when word == 2.
262 * This implies that we always require all 3 fuse prog cmds (one for
263 * for each word) to write a single fuse line.
264 * Exception is a single write to word 2 which will lock the fuse line.
265 *
266 * Hope that will be OK.
267 */
268
269 if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2)
270 return -EINVAL;
271
272 if (word < 2) {
273 prog_val.dwords.d[word] = val;
274 valid_prog_words |= (1 << word);
275 } else if ((valid_prog_words & 3) == 0 && val) {
276 res = mvebu_lock_efuse(bank);
277 valid_prog_words = 0;
278 } else if ((valid_prog_words & 3) != 3 || !val) {
279 res = -EINVAL;
280 } else {
281 prog_val.lock = val != 0;
282 res = mvebu_write_efuse(bank, &prog_val);
283 valid_prog_words = 0;
284 }
285
286 return res;
287}
288
289int fuse_override(u32 bank, u32 word, u32 val)
290{
291 /* not supported */
292 return -ENOSYS;
293}