1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015-2016 Reinhard Pfau <reinhard.pfau@gdsys.cc>
10 #include <asm/arch/cpu.h>
11 #include <asm/arch/efuse.h>
12 #include <asm/arch/soc.h>
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
16 #include <linux/mbus.h>
18 #if defined(CONFIG_MVEBU_EFUSE_FAKE)
24 #define MBUS_EFUSE_BASE 0xF6000000
25 #define MBUS_EFUSE_SIZE BIT(20)
27 #define MVEBU_EFUSE_CONTROL (MVEBU_REGISTER(0xE4008))
30 MVEBU_EFUSE_CTRL_PROGRAM_ENABLE = (1 << 31),
31 MVEBU_EFUSE_LD1_SELECT = (1 << 6),
34 struct mvebu_hd_efuse {
42 static struct mvebu_hd_efuse *efuses =
43 (struct mvebu_hd_efuse *)(MBUS_EFUSE_BASE + 0xF9000);
44 static u32 *ld_efuses = (void *)MBUS_EFUSE_BASE + 0xF8F00;
46 static struct mvebu_hd_efuse efuses[EFUSE_LINE_MAX + 1];
47 static u32 ld_efuses[EFUSE_LD_WORDS];
50 static int efuse_initialised;
52 static struct mvebu_hd_efuse *get_efuse_line(int nr)
54 if (nr < 0 || nr > 63 || !efuse_initialised)
64 static int enable_efuse_program(void)
67 if (CONFIG_MVEBU_EFUSE_VHV_GPIO[0]) {
68 if (gpio_lookup_name(CONFIG_MVEBU_EFUSE_VHV_GPIO, NULL, NULL, &vhv_gpio)) {
69 printf("Error: VHV gpio lookup failed\n");
72 if (gpio_request(vhv_gpio, CONFIG_MVEBU_EFUSE_VHV_GPIO)) {
73 printf("Error: VHV gpio request failed\n");
76 if (gpio_direction_output(vhv_gpio,
77 IS_ENABLED(CONFIG_MVEBU_EFUSE_VHV_GPIO_ACTIVE_LOW) ? 0 : 1)) {
78 printf("Error: VHV gpio enable failed\n");
81 mdelay(5); /* Wait for the VHV power to stabilize */
84 setbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_CTRL_PROGRAM_ENABLE);
90 static void disable_efuse_program(void)
93 clrbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_CTRL_PROGRAM_ENABLE);
95 if (CONFIG_MVEBU_EFUSE_VHV_GPIO[0]) {
96 if (gpio_direction_output(vhv_gpio,
97 IS_ENABLED(CONFIG_MVEBU_EFUSE_VHV_GPIO_ACTIVE_LOW) ? 1 : 0))
98 printf("Error: VHV gpio disable failed\n");
105 static int do_prog_efuse(struct mvebu_hd_efuse *efuse,
106 struct efuse_val *new_val, u32 mask0, u32 mask1)
108 struct efuse_val val;
110 val.dwords.d[0] = readl(&efuse->bits_31_0);
111 val.dwords.d[1] = readl(&efuse->bits_63_32);
112 val.lock = readl(&efuse->bit64);
117 val.dwords.d[0] |= (new_val->dwords.d[0] & mask0);
118 val.dwords.d[1] |= (new_val->dwords.d[1] & mask1);
119 val.lock |= new_val->lock;
121 writel(val.dwords.d[0], &efuse->bits_31_0);
123 writel(val.dwords.d[1], &efuse->bits_63_32);
125 writel(val.lock, &efuse->bit64);
131 static int prog_efuse(int nr, struct efuse_val *new_val, u32 mask0, u32 mask1)
133 struct mvebu_hd_efuse *efuse;
136 res = mvebu_efuse_init_hw();
140 efuse = get_efuse_line(nr);
147 /* only write a fuse line with lock bit */
151 /* according to specs ECC protection bits must be 0 on write */
152 if (new_val->bytes.d[7] & 0xFE)
155 if (!new_val->dwords.d[0] && !new_val->dwords.d[1] && (mask0 | mask1))
158 res = enable_efuse_program();
162 res = do_prog_efuse(efuse, new_val, mask0, mask1);
164 disable_efuse_program();
169 int mvebu_prog_ld_efuse(int ld1, u32 word, u32 val)
172 u32 line[EFUSE_LD_WORDS];
174 res = mvebu_efuse_init_hw();
178 mvebu_read_ld_efuse(ld1, line);
180 /* check if lock bit is already programmed */
181 if (line[EFUSE_LD_WORDS - 1])
184 /* check if word is valid */
185 if (word >= EFUSE_LD_WORDS)
188 /* check if there is some bit for programming */
189 if (val == (line[word] & val))
192 res = enable_efuse_program();
196 mvebu_read_ld_efuse(ld1, line);
199 for (i = 0; i < EFUSE_LD_WORDS; i++) {
200 writel(line[i], ld_efuses + i);
206 disable_efuse_program();
211 int mvebu_efuse_init_hw(void)
215 if (efuse_initialised)
218 ret = mvebu_mbus_add_window_by_id(
219 CPU_TARGET_SATA23_DFX, 0xA, MBUS_EFUSE_BASE, MBUS_EFUSE_SIZE);
224 efuse_initialised = 1;
229 int mvebu_read_efuse(int nr, struct efuse_val *val)
231 struct mvebu_hd_efuse *efuse;
234 res = mvebu_efuse_init_hw();
238 efuse = get_efuse_line(nr);
245 val->dwords.d[0] = readl(&efuse->bits_31_0);
246 val->dwords.d[1] = readl(&efuse->bits_63_32);
247 val->lock = readl(&efuse->bit64);
251 void mvebu_read_ld_efuse(int ld1, u32 *line)
257 setbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_LD1_SELECT);
259 clrbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_LD1_SELECT);
262 for (i = 0; i < EFUSE_LD_WORDS; i++)
263 line[i] = readl(ld_efuses + i);
266 int mvebu_write_efuse(int nr, struct efuse_val *val)
268 return prog_efuse(nr, val, ~0, ~0);
271 int mvebu_lock_efuse(int nr)
273 struct efuse_val val = {
277 return prog_efuse(nr, &val, 0, 0);
281 * wrapper funcs providing the fuse API
283 * we use the following mapping:
284 * "bank" -> eFuse line
285 * "word" -> 0: bits 0-31
290 static struct efuse_val prog_val;
291 static int valid_prog_words;
293 int fuse_read(u32 bank, u32 word, u32 *val)
295 struct efuse_val fuse_line;
296 u32 ld_line[EFUSE_LD_WORDS];
299 if ((bank == EFUSE_LD0_LINE || bank == EFUSE_LD1_LINE) && word < EFUSE_LD_WORDS) {
300 res = mvebu_efuse_init_hw();
303 mvebu_read_ld_efuse(bank == EFUSE_LD1_LINE, ld_line);
304 *val = ld_line[word];
308 if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2)
311 res = mvebu_read_efuse(bank, &fuse_line);
316 *val = fuse_line.dwords.d[word];
318 *val = fuse_line.lock;
323 int fuse_sense(u32 bank, u32 word, u32 *val)
329 int fuse_prog(u32 bank, u32 word, u32 val)
333 if (bank == EFUSE_LD0_LINE || bank == EFUSE_LD1_LINE)
334 return mvebu_prog_ld_efuse(bank == EFUSE_LD1_LINE, word, val);
337 * NOTE: Fuse line should be written as whole.
338 * So how can we do that with this API?
339 * For now: remember values for word == 0 and word == 1 and write the
340 * whole line when word == 2.
341 * This implies that we always require all 3 fuse prog cmds (one for
342 * for each word) to write a single fuse line.
343 * Exception is a single write to word 2 which will lock the fuse line.
345 * Hope that will be OK.
348 if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2)
352 prog_val.dwords.d[word] = val;
353 valid_prog_words |= (1 << word);
354 } else if ((valid_prog_words & 3) == 0 && val) {
355 res = mvebu_lock_efuse(bank);
356 valid_prog_words = 0;
357 } else if ((valid_prog_words & 3) != 3 || !val) {
360 prog_val.lock = val != 0;
361 res = mvebu_write_efuse(bank, &prog_val);
362 valid_prog_words = 0;
368 int fuse_override(u32 bank, u32 word, u32 val)