Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009-2013 ADVANSEE |
| 4 | * Benoît Thébaudeau <benoit.thebaudeau@advansee.com> |
| 5 | * |
| 6 | * Based on the mpc512x iim code: |
| 7 | * Copyright 2008 Silicon Turnkey Express, Inc. |
| 8 | * Martha Marx <mmarx@silicontkx.com> |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 12 | #include <console.h> |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 13 | #include <fuse.h> |
Angus Ainslie | f08743f | 2021-11-28 08:02:53 -0800 | [diff] [blame] | 14 | #include <mapmem.h> |
Tom Rini | 5c1444f | 2024-04-27 08:10:59 -0600 | [diff] [blame] | 15 | #include <vsprintf.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 16 | #include <linux/errno.h> |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 17 | |
| 18 | static int strtou32(const char *str, unsigned int base, u32 *result) |
| 19 | { |
| 20 | char *ep; |
| 21 | |
| 22 | *result = simple_strtoul(str, &ep, base); |
| 23 | if (ep == str || *ep != '\0') |
| 24 | return -EINVAL; |
| 25 | |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | static int confirm_prog(void) |
| 30 | { |
| 31 | puts("Warning: Programming fuses is an irreversible operation!\n" |
| 32 | " This may brick your system.\n" |
| 33 | " Use this command only if you are sure of " |
| 34 | "what you are doing!\n" |
| 35 | "\nReally perform this fuse programming? <y/N>\n"); |
| 36 | |
Pierre Aubert | 5fef9d3 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 37 | if (confirm_yesno()) |
| 38 | return 1; |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 39 | |
| 40 | puts("Fuse programming aborted\n"); |
| 41 | return 0; |
| 42 | } |
| 43 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 44 | static int do_fuse(struct cmd_tbl *cmdtp, int flag, int argc, |
| 45 | char *const argv[]) |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 46 | { |
Simon Glass | 793a98e | 2023-11-18 14:05:20 -0700 | [diff] [blame] | 47 | const char *op = cmd_arg1(argc, argv); |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 48 | int confirmed = argc >= 3 && !strcmp(argv[2], "-y"); |
Angus Ainslie | 5149bca | 2021-11-28 08:02:52 -0800 | [diff] [blame] | 49 | u32 bank, word, cnt, val, cmp; |
Angus Ainslie | f08743f | 2021-11-28 08:02:53 -0800 | [diff] [blame] | 50 | ulong addr; |
| 51 | void *buf, *start; |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 52 | int ret, i; |
| 53 | |
| 54 | argc -= 2 + confirmed; |
| 55 | argv += 2 + confirmed; |
| 56 | |
| 57 | if (argc < 2 || strtou32(argv[0], 0, &bank) || |
| 58 | strtou32(argv[1], 0, &word)) |
| 59 | return CMD_RET_USAGE; |
| 60 | |
| 61 | if (!strcmp(op, "read")) { |
| 62 | if (argc == 2) |
| 63 | cnt = 1; |
| 64 | else if (argc != 3 || strtou32(argv[2], 0, &cnt)) |
| 65 | return CMD_RET_USAGE; |
| 66 | |
| 67 | printf("Reading bank %u:\n", bank); |
| 68 | for (i = 0; i < cnt; i++, word++) { |
| 69 | if (!(i % 4)) |
| 70 | printf("\nWord 0x%.8x:", word); |
| 71 | |
| 72 | ret = fuse_read(bank, word, &val); |
| 73 | if (ret) |
| 74 | goto err; |
| 75 | |
| 76 | printf(" %.8x", val); |
| 77 | } |
| 78 | putc('\n'); |
Angus Ainslie | f08743f | 2021-11-28 08:02:53 -0800 | [diff] [blame] | 79 | } else if (!strcmp(op, "readm")) { |
| 80 | if (argc == 3) |
| 81 | cnt = 1; |
| 82 | else if (argc != 4 || strtou32(argv[3], 0, &cnt)) |
| 83 | return CMD_RET_USAGE; |
| 84 | |
| 85 | addr = simple_strtoul(argv[2], NULL, 16); |
| 86 | |
| 87 | start = map_sysmem(addr, 4); |
| 88 | buf = start; |
| 89 | |
| 90 | printf("Reading bank %u len %u to 0x%lx\n", bank, cnt, addr); |
| 91 | for (i = 0; i < cnt; i++, word++) { |
| 92 | ret = fuse_read(bank, word, &val); |
| 93 | if (ret) |
| 94 | goto err; |
| 95 | |
| 96 | *((u32 *)buf) = val; |
| 97 | buf += 4; |
| 98 | } |
| 99 | |
| 100 | unmap_sysmem(start); |
Angus Ainslie | 5149bca | 2021-11-28 08:02:52 -0800 | [diff] [blame] | 101 | } else if (!strcmp(op, "cmp")) { |
| 102 | if (argc != 3 || strtou32(argv[2], 0, &cmp)) |
| 103 | return CMD_RET_USAGE; |
| 104 | |
| 105 | printf("Comparing bank %u:\n", bank); |
| 106 | printf("\nWord 0x%.8x:", word); |
| 107 | printf("\nValue 0x%.8x:", cmp); |
| 108 | |
| 109 | ret = fuse_read(bank, word, &val); |
| 110 | if (ret) |
| 111 | goto err; |
| 112 | |
| 113 | printf("0x%.8x\n", val); |
| 114 | if (val != cmp) { |
| 115 | printf("failed\n"); |
| 116 | return CMD_RET_FAILURE; |
| 117 | } |
| 118 | printf("passed\n"); |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 119 | } else if (!strcmp(op, "sense")) { |
| 120 | if (argc == 2) |
| 121 | cnt = 1; |
| 122 | else if (argc != 3 || strtou32(argv[2], 0, &cnt)) |
| 123 | return CMD_RET_USAGE; |
| 124 | |
| 125 | printf("Sensing bank %u:\n", bank); |
| 126 | for (i = 0; i < cnt; i++, word++) { |
| 127 | if (!(i % 4)) |
| 128 | printf("\nWord 0x%.8x:", word); |
| 129 | |
| 130 | ret = fuse_sense(bank, word, &val); |
| 131 | if (ret) |
| 132 | goto err; |
| 133 | |
| 134 | printf(" %.8x", val); |
| 135 | } |
| 136 | putc('\n'); |
| 137 | } else if (!strcmp(op, "prog")) { |
| 138 | if (argc < 3) |
| 139 | return CMD_RET_USAGE; |
| 140 | |
| 141 | for (i = 2; i < argc; i++, word++) { |
| 142 | if (strtou32(argv[i], 16, &val)) |
| 143 | return CMD_RET_USAGE; |
| 144 | |
| 145 | printf("Programming bank %u word 0x%.8x to 0x%.8x...\n", |
| 146 | bank, word, val); |
| 147 | if (!confirmed && !confirm_prog()) |
| 148 | return CMD_RET_FAILURE; |
| 149 | ret = fuse_prog(bank, word, val); |
| 150 | if (ret) |
| 151 | goto err; |
| 152 | } |
| 153 | } else if (!strcmp(op, "override")) { |
| 154 | if (argc < 3) |
| 155 | return CMD_RET_USAGE; |
| 156 | |
| 157 | for (i = 2; i < argc; i++, word++) { |
| 158 | if (strtou32(argv[i], 16, &val)) |
| 159 | return CMD_RET_USAGE; |
| 160 | |
| 161 | printf("Overriding bank %u word 0x%.8x with " |
| 162 | "0x%.8x...\n", bank, word, val); |
| 163 | ret = fuse_override(bank, word, val); |
| 164 | if (ret) |
| 165 | goto err; |
| 166 | } |
| 167 | } else { |
| 168 | return CMD_RET_USAGE; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | |
| 173 | err: |
| 174 | puts("ERROR\n"); |
Hector Palacios | 17e99cc | 2014-11-20 09:27:42 +0100 | [diff] [blame] | 175 | return CMD_RET_FAILURE; |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | U_BOOT_CMD( |
| 179 | fuse, CONFIG_SYS_MAXARGS, 0, do_fuse, |
| 180 | "Fuse sub-system", |
| 181 | "read <bank> <word> [<cnt>] - read 1 or 'cnt' fuse words,\n" |
| 182 | " starting at 'word'\n" |
Angus Ainslie | 5149bca | 2021-11-28 08:02:52 -0800 | [diff] [blame] | 183 | "fuse cmp <bank> <word> <hexval> - compare 'hexval' to fuse\n" |
| 184 | " at 'word'\n" |
Angus Ainslie | f08743f | 2021-11-28 08:02:53 -0800 | [diff] [blame] | 185 | "fuse readm <bank> <word> <addr> [<cnt>] - read 1 or 'cnt' fuse words,\n" |
| 186 | " starting at 'word' into memory at 'addr'\n" |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 187 | "fuse sense <bank> <word> [<cnt>] - sense 1 or 'cnt' fuse words,\n" |
| 188 | " starting at 'word'\n" |
| 189 | "fuse prog [-y] <bank> <word> <hexval> [<hexval>...] - program 1 or\n" |
| 190 | " several fuse words, starting at 'word' (PERMANENT)\n" |
| 191 | "fuse override <bank> <word> <hexval> [<hexval>...] - override 1 or\n" |
| 192 | " several fuse words, starting at 'word'" |
| 193 | ); |