2 * Copyright (C) 2015-2016 Reinhard Pfau <reinhard.pfau@gdsys.cc>
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/arch/cpu.h>
12 #include <asm/arch/efuse.h>
13 #include <asm/arch/soc.h>
14 #include <linux/mbus.h>
16 #if defined(CONFIG_MVEBU_EFUSE_FAKE)
22 #define MBUS_EFUSE_BASE 0xF6000000
23 #define MBUS_EFUSE_SIZE BIT(20)
25 #define MVEBU_EFUSE_CONTROL (MVEBU_REGISTER(0xE4008))
28 MVEBU_EFUSE_CTRL_PROGRAM_ENABLE = (1 << 31),
31 struct mvebu_hd_efuse {
39 static struct mvebu_hd_efuse *efuses =
40 (struct mvebu_hd_efuse *)(MBUS_EFUSE_BASE + 0xF9000);
42 static struct mvebu_hd_efuse efuses[EFUSE_LINE_MAX + 1];
45 static int efuse_initialised;
47 static struct mvebu_hd_efuse *get_efuse_line(int nr)
49 if (nr < 0 || nr > 63 || !efuse_initialised)
55 static void enable_efuse_program(void)
58 setbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_CTRL_PROGRAM_ENABLE);
62 static void disable_efuse_program(void)
65 clrbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_CTRL_PROGRAM_ENABLE);
69 static int do_prog_efuse(struct mvebu_hd_efuse *efuse,
70 struct efuse_val *new_val, u32 mask0, u32 mask1)
74 val.dwords.d[0] = readl(&efuse->bits_31_0);
75 val.dwords.d[1] = readl(&efuse->bits_63_32);
76 val.lock = readl(&efuse->bit64);
81 val.dwords.d[0] |= (new_val->dwords.d[0] & mask0);
82 val.dwords.d[1] |= (new_val->dwords.d[1] & mask1);
83 val.lock |= new_val->lock;
85 writel(val.dwords.d[0], &efuse->bits_31_0);
87 writel(val.dwords.d[1], &efuse->bits_63_32);
89 writel(val.lock, &efuse->bit64);
95 static int prog_efuse(int nr, struct efuse_val *new_val, u32 mask0, u32 mask1)
97 struct mvebu_hd_efuse *efuse;
100 res = mvebu_efuse_init_hw();
104 efuse = get_efuse_line(nr);
111 /* only write a fuse line with lock bit */
115 /* according to specs ECC protection bits must be 0 on write */
116 if (new_val->bytes.d[7] & 0xFE)
119 if (!new_val->dwords.d[0] && !new_val->dwords.d[1] && (mask0 | mask1))
122 enable_efuse_program();
124 res = do_prog_efuse(efuse, new_val, mask0, mask1);
126 disable_efuse_program();
131 int mvebu_efuse_init_hw(void)
135 if (efuse_initialised)
138 ret = mvebu_mbus_add_window_by_id(
139 CPU_TARGET_SATA23_DFX, 0xA, MBUS_EFUSE_BASE, MBUS_EFUSE_SIZE);
144 efuse_initialised = 1;
149 int mvebu_read_efuse(int nr, struct efuse_val *val)
151 struct mvebu_hd_efuse *efuse;
154 res = mvebu_efuse_init_hw();
158 efuse = get_efuse_line(nr);
165 val->dwords.d[0] = readl(&efuse->bits_31_0);
166 val->dwords.d[1] = readl(&efuse->bits_63_32);
167 val->lock = readl(&efuse->bit64);
171 int mvebu_write_efuse(int nr, struct efuse_val *val)
173 return prog_efuse(nr, val, ~0, ~0);
176 int mvebu_lock_efuse(int nr)
178 struct efuse_val val = {
182 return prog_efuse(nr, &val, 0, 0);
186 * wrapper funcs providing the fuse API
188 * we use the following mapping:
189 * "bank" -> eFuse line
190 * "word" -> 0: bits 0-31
195 static struct efuse_val prog_val;
196 static int valid_prog_words;
198 int fuse_read(u32 bank, u32 word, u32 *val)
200 struct efuse_val fuse_line;
203 if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2)
206 res = mvebu_read_efuse(bank, &fuse_line);
211 *val = fuse_line.dwords.d[word];
213 *val = fuse_line.lock;
218 int fuse_sense(u32 bank, u32 word, u32 *val)
224 int fuse_prog(u32 bank, u32 word, u32 val)
229 * NOTE: Fuse line should be written as whole.
230 * So how can we do that with this API?
231 * For now: remember values for word == 0 and word == 1 and write the
232 * whole line when word == 2.
233 * This implies that we always require all 3 fuse prog cmds (one for
234 * for each word) to write a single fuse line.
235 * Exception is a single write to word 2 which will lock the fuse line.
237 * Hope that will be OK.
240 if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2)
244 prog_val.dwords.d[word] = val;
245 valid_prog_words |= (1 << word);
246 } else if ((valid_prog_words & 3) == 0 && val) {
247 res = mvebu_lock_efuse(bank);
248 valid_prog_words = 0;
249 } else if ((valid_prog_words & 3) != 3 || !val) {
252 prog_val.lock = val != 0;
253 res = mvebu_write_efuse(bank, &prog_val);
254 valid_prog_words = 0;
260 int fuse_override(u32 bank, u32 word, u32 val)