Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015-2016 Reinhard Pfau <reinhard.pfau@gdsys.cc> |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 4 | */ |
| 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 Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 15 | #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 | |
| 28 | enum { |
| 29 | MVEBU_EFUSE_CTRL_PROGRAM_ENABLE = (1 << 31), |
Pali Rohár | df1da73 | 2022-04-06 14:18:18 +0200 | [diff] [blame] | 30 | MVEBU_EFUSE_LD1_SELECT = (1 << 6), |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct 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 |
| 41 | static struct mvebu_hd_efuse *efuses = |
| 42 | (struct mvebu_hd_efuse *)(MBUS_EFUSE_BASE + 0xF9000); |
Pali Rohár | df1da73 | 2022-04-06 14:18:18 +0200 | [diff] [blame] | 43 | static u32 *ld_efuses = (void *)MBUS_EFUSE_BASE + 0xF8F00; |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 44 | #else |
| 45 | static struct mvebu_hd_efuse efuses[EFUSE_LINE_MAX + 1]; |
Pali Rohár | df1da73 | 2022-04-06 14:18:18 +0200 | [diff] [blame] | 46 | static u32 ld_efuses[EFUSE_LD_WORDS]; |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | static int efuse_initialised; |
| 50 | |
| 51 | static 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 | |
| 59 | static 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 | |
| 66 | static 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 | |
| 73 | static 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 | |
| 99 | static 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 | |
Pali Rohár | 666252d | 2022-09-22 13:43:44 +0200 | [diff] [blame^] | 135 | int mvebu_prog_ld_efuse(int ld1, u32 word, u32 val) |
| 136 | { |
| 137 | int i, res; |
| 138 | u32 line[EFUSE_LD_WORDS]; |
| 139 | |
| 140 | res = mvebu_efuse_init_hw(); |
| 141 | if (res) |
| 142 | return res; |
| 143 | |
| 144 | mvebu_read_ld_efuse(ld1, line); |
| 145 | |
| 146 | /* check if lock bit is already programmed */ |
| 147 | if (line[EFUSE_LD_WORDS - 1]) |
| 148 | return -EPERM; |
| 149 | |
| 150 | /* check if word is valid */ |
| 151 | if (word >= EFUSE_LD_WORDS) |
| 152 | return -EINVAL; |
| 153 | |
| 154 | /* check if there is some bit for programming */ |
| 155 | if (val == (line[word] & val)) |
| 156 | return 0; |
| 157 | |
| 158 | enable_efuse_program(); |
| 159 | |
| 160 | mvebu_read_ld_efuse(ld1, line); |
| 161 | line[word] |= val; |
| 162 | |
| 163 | for (i = 0; i < EFUSE_LD_WORDS; i++) { |
| 164 | writel(line[i], ld_efuses + i); |
| 165 | mdelay(1); |
| 166 | } |
| 167 | |
| 168 | mdelay(5); |
| 169 | |
| 170 | disable_efuse_program(); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 175 | int mvebu_efuse_init_hw(void) |
| 176 | { |
| 177 | int ret; |
| 178 | |
| 179 | if (efuse_initialised) |
| 180 | return 0; |
| 181 | |
| 182 | ret = mvebu_mbus_add_window_by_id( |
| 183 | CPU_TARGET_SATA23_DFX, 0xA, MBUS_EFUSE_BASE, MBUS_EFUSE_SIZE); |
| 184 | |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
| 188 | efuse_initialised = 1; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int mvebu_read_efuse(int nr, struct efuse_val *val) |
| 194 | { |
| 195 | struct mvebu_hd_efuse *efuse; |
| 196 | int res; |
| 197 | |
| 198 | res = mvebu_efuse_init_hw(); |
| 199 | if (res) |
| 200 | return res; |
| 201 | |
| 202 | efuse = get_efuse_line(nr); |
| 203 | if (!efuse) |
| 204 | return -ENODEV; |
| 205 | |
| 206 | if (!val) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | val->dwords.d[0] = readl(&efuse->bits_31_0); |
| 210 | val->dwords.d[1] = readl(&efuse->bits_63_32); |
| 211 | val->lock = readl(&efuse->bit64); |
| 212 | return 0; |
| 213 | } |
| 214 | |
Pali Rohár | df1da73 | 2022-04-06 14:18:18 +0200 | [diff] [blame] | 215 | void mvebu_read_ld_efuse(int ld1, u32 *line) |
| 216 | { |
| 217 | int i; |
| 218 | |
| 219 | #ifndef DRY_RUN |
| 220 | if (ld1) |
| 221 | setbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_LD1_SELECT); |
| 222 | else |
| 223 | clrbits_le32(MVEBU_EFUSE_CONTROL, MVEBU_EFUSE_LD1_SELECT); |
| 224 | #endif |
| 225 | |
| 226 | for (i = 0; i < EFUSE_LD_WORDS; i++) |
| 227 | line[i] = readl(ld_efuses + i); |
| 228 | } |
| 229 | |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 230 | int mvebu_write_efuse(int nr, struct efuse_val *val) |
| 231 | { |
| 232 | return prog_efuse(nr, val, ~0, ~0); |
| 233 | } |
| 234 | |
| 235 | int mvebu_lock_efuse(int nr) |
| 236 | { |
| 237 | struct efuse_val val = { |
| 238 | .lock = 1, |
| 239 | }; |
| 240 | |
| 241 | return prog_efuse(nr, &val, 0, 0); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * wrapper funcs providing the fuse API |
| 246 | * |
| 247 | * we use the following mapping: |
| 248 | * "bank" -> eFuse line |
| 249 | * "word" -> 0: bits 0-31 |
| 250 | * 1: bits 32-63 |
| 251 | * 2: bit 64 (lock) |
| 252 | */ |
| 253 | |
| 254 | static struct efuse_val prog_val; |
| 255 | static int valid_prog_words; |
| 256 | |
| 257 | int fuse_read(u32 bank, u32 word, u32 *val) |
| 258 | { |
| 259 | struct efuse_val fuse_line; |
Pali Rohár | df1da73 | 2022-04-06 14:18:18 +0200 | [diff] [blame] | 260 | u32 ld_line[EFUSE_LD_WORDS]; |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 261 | int res; |
| 262 | |
Pali Rohár | df1da73 | 2022-04-06 14:18:18 +0200 | [diff] [blame] | 263 | if ((bank == EFUSE_LD0_LINE || bank == EFUSE_LD1_LINE) && word < EFUSE_LD_WORDS) { |
| 264 | res = mvebu_efuse_init_hw(); |
| 265 | if (res) |
| 266 | return res; |
| 267 | mvebu_read_ld_efuse(bank == EFUSE_LD1_LINE, ld_line); |
| 268 | *val = ld_line[word]; |
| 269 | return 0; |
| 270 | } |
| 271 | |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 272 | if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2) |
| 273 | return -EINVAL; |
| 274 | |
| 275 | res = mvebu_read_efuse(bank, &fuse_line); |
| 276 | if (res) |
| 277 | return res; |
| 278 | |
| 279 | if (word < 2) |
| 280 | *val = fuse_line.dwords.d[word]; |
| 281 | else |
| 282 | *val = fuse_line.lock; |
| 283 | |
| 284 | return res; |
| 285 | } |
| 286 | |
| 287 | int fuse_sense(u32 bank, u32 word, u32 *val) |
| 288 | { |
| 289 | /* not supported */ |
| 290 | return -ENOSYS; |
| 291 | } |
| 292 | |
| 293 | int fuse_prog(u32 bank, u32 word, u32 val) |
| 294 | { |
| 295 | int res = 0; |
| 296 | |
Pali Rohár | 666252d | 2022-09-22 13:43:44 +0200 | [diff] [blame^] | 297 | if (bank == EFUSE_LD0_LINE || bank == EFUSE_LD1_LINE) |
| 298 | return mvebu_prog_ld_efuse(bank == EFUSE_LD1_LINE, word, val); |
| 299 | |
Mario Six | 10d1449 | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 300 | /* |
| 301 | * NOTE: Fuse line should be written as whole. |
| 302 | * So how can we do that with this API? |
| 303 | * For now: remember values for word == 0 and word == 1 and write the |
| 304 | * whole line when word == 2. |
| 305 | * This implies that we always require all 3 fuse prog cmds (one for |
| 306 | * for each word) to write a single fuse line. |
| 307 | * Exception is a single write to word 2 which will lock the fuse line. |
| 308 | * |
| 309 | * Hope that will be OK. |
| 310 | */ |
| 311 | |
| 312 | if (bank < EFUSE_LINE_MIN || bank > EFUSE_LINE_MAX || word > 2) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | if (word < 2) { |
| 316 | prog_val.dwords.d[word] = val; |
| 317 | valid_prog_words |= (1 << word); |
| 318 | } else if ((valid_prog_words & 3) == 0 && val) { |
| 319 | res = mvebu_lock_efuse(bank); |
| 320 | valid_prog_words = 0; |
| 321 | } else if ((valid_prog_words & 3) != 3 || !val) { |
| 322 | res = -EINVAL; |
| 323 | } else { |
| 324 | prog_val.lock = val != 0; |
| 325 | res = mvebu_write_efuse(bank, &prog_val); |
| 326 | valid_prog_words = 0; |
| 327 | } |
| 328 | |
| 329 | return res; |
| 330 | } |
| 331 | |
| 332 | int fuse_override(u32 bank, u32 word, u32 val) |
| 333 | { |
| 334 | /* not supported */ |
| 335 | return -ENOSYS; |
| 336 | } |