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>
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
15 #include <linux/mbus.h>
17 #if defined(CONFIG_MVEBU_EFUSE_FAKE)
23 #define MBUS_EFUSE_BASE 0xF6000000
24 #define MBUS_EFUSE_SIZE BIT(20)
26 #define MVEBU_EFUSE_CONTROL (MVEBU_REGISTER(0xE4008))
29 MVEBU_EFUSE_CTRL_PROGRAM_ENABLE = (1 << 31),
32 struct mvebu_hd_efuse {
40 static struct mvebu_hd_efuse *efuses =
41 (struct mvebu_hd_efuse *)(MBUS_EFUSE_BASE + 0xF9000);
43 static struct mvebu_hd_efuse efuses[EFUSE_LINE_MAX + 1];
46 static int efuse_initialised;
48 static struct mvebu_hd_efuse *get_efuse_line(int nr)
50 if (nr < 0 || nr > 63 || !efuse_initialised)
56 static void enable_efuse_program(void)
59 setbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_CTRL_PROGRAM_ENABLE);
63 static void disable_efuse_program(void)
66 clrbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_CTRL_PROGRAM_ENABLE);
70 static int do_prog_efuse(struct mvebu_hd_efuse *efuse,
71 struct efuse_val *new_val, u32 mask0, u32 mask1)
75 val.dwords.d[0] = readl(&efuse->bits_31_0);
76 val.dwords.d[1] = readl(&efuse->bits_63_32);
77 val.lock = readl(&efuse->bit64);
82 val.dwords.d[0] |= (new_val->dwords.d[0] & mask0);
83 val.dwords.d[1] |= (new_val->dwords.d[1] & mask1);
84 val.lock |= new_val->lock;
86 writel(val.dwords.d[0], &efuse->bits_31_0);
88 writel(val.dwords.d[1], &efuse->bits_63_32);
90 writel(val.lock, &efuse->bit64);
96 static int prog_efuse(int nr, struct efuse_val *new_val, u32 mask0, u32 mask1)
98 struct mvebu_hd_efuse *efuse;
101 res = mvebu_efuse_init_hw();
105 efuse = get_efuse_line(nr);
112 /* only write a fuse line with lock bit */
116 /* according to specs ECC protection bits must be 0 on write */
117 if (new_val->bytes.d[7] & 0xFE)
120 if (!new_val->dwords.d[0] && !new_val->dwords.d[1] && (mask0 | mask1))
123 enable_efuse_program();
125 res = do_prog_efuse(efuse, new_val, mask0, mask1);
127 disable_efuse_program();
132 int mvebu_efuse_init_hw(void)
136 if (efuse_initialised)
139 ret = mvebu_mbus_add_window_by_id(
140 CPU_TARGET_SATA23_DFX, 0xA, MBUS_EFUSE_BASE, MBUS_EFUSE_SIZE);
145 efuse_initialised = 1;
150 int mvebu_read_efuse(int nr, struct efuse_val *val)
152 struct mvebu_hd_efuse *efuse;
155 res = mvebu_efuse_init_hw();
159 efuse = get_efuse_line(nr);
166 val->dwords.d[0] = readl(&efuse->bits_31_0);
167 val->dwords.d[1] = readl(&efuse->bits_63_32);
168 val->lock = readl(&efuse->bit64);
172 int mvebu_write_efuse(int nr, struct efuse_val *val)
174 return prog_efuse(nr, val, ~0, ~0);
177 int mvebu_lock_efuse(int nr)
179 struct efuse_val val = {
183 return prog_efuse(nr, &val, 0, 0);
187 * wrapper funcs providing the fuse API
189 * we use the following mapping:
190 * "bank" -> eFuse line
191 * "word" -> 0: bits 0-31
196 static struct efuse_val prog_val;
197 static int valid_prog_words;
199 int fuse_read(u32 bank, u32 word, u32 *val)
201 struct efuse_val fuse_line;
204 if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2)
207 res = mvebu_read_efuse(bank, &fuse_line);
212 *val = fuse_line.dwords.d[word];
214 *val = fuse_line.lock;
219 int fuse_sense(u32 bank, u32 word, u32 *val)
225 int fuse_prog(u32 bank, u32 word, u32 val)
230 * NOTE: Fuse line should be written as whole.
231 * So how can we do that with this API?
232 * For now: remember values for word == 0 and word == 1 and write the
233 * whole line when word == 2.
234 * This implies that we always require all 3 fuse prog cmds (one for
235 * for each word) to write a single fuse line.
236 * Exception is a single write to word 2 which will lock the fuse line.
238 * Hope that will be OK.
241 if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2)
245 prog_val.dwords.d[word] = val;
246 valid_prog_words |= (1 << word);
247 } else if ((valid_prog_words & 3) == 0 && val) {
248 res = mvebu_lock_efuse(bank);
249 valid_prog_words = 0;
250 } else if ((valid_prog_words & 3) != 3 || !val) {
253 prog_val.lock = val != 0;
254 res = mvebu_write_efuse(bank, &prog_val);
255 valid_prog_words = 0;
261 int fuse_override(u32 bank, u32 word, u32 val)