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 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 13 | #include <console.h> |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 14 | #include <fuse.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 15 | #include <linux/errno.h> |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 16 | |
| 17 | static int strtou32(const char *str, unsigned int base, u32 *result) |
| 18 | { |
| 19 | char *ep; |
| 20 | |
| 21 | *result = simple_strtoul(str, &ep, base); |
| 22 | if (ep == str || *ep != '\0') |
| 23 | return -EINVAL; |
| 24 | |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | static int confirm_prog(void) |
| 29 | { |
| 30 | puts("Warning: Programming fuses is an irreversible operation!\n" |
| 31 | " This may brick your system.\n" |
| 32 | " Use this command only if you are sure of " |
| 33 | "what you are doing!\n" |
| 34 | "\nReally perform this fuse programming? <y/N>\n"); |
| 35 | |
Pierre Aubert | 5fef9d3 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 36 | if (confirm_yesno()) |
| 37 | return 1; |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 38 | |
| 39 | puts("Fuse programming aborted\n"); |
| 40 | return 0; |
| 41 | } |
| 42 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 43 | static int do_fuse(struct cmd_tbl *cmdtp, int flag, int argc, |
| 44 | char *const argv[]) |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 45 | { |
| 46 | const char *op = argc >= 2 ? argv[1] : NULL; |
| 47 | int confirmed = argc >= 3 && !strcmp(argv[2], "-y"); |
| 48 | u32 bank, word, cnt, val; |
| 49 | int ret, i; |
| 50 | |
| 51 | argc -= 2 + confirmed; |
| 52 | argv += 2 + confirmed; |
| 53 | |
| 54 | if (argc < 2 || strtou32(argv[0], 0, &bank) || |
| 55 | strtou32(argv[1], 0, &word)) |
| 56 | return CMD_RET_USAGE; |
| 57 | |
| 58 | if (!strcmp(op, "read")) { |
| 59 | if (argc == 2) |
| 60 | cnt = 1; |
| 61 | else if (argc != 3 || strtou32(argv[2], 0, &cnt)) |
| 62 | return CMD_RET_USAGE; |
| 63 | |
| 64 | printf("Reading bank %u:\n", bank); |
| 65 | for (i = 0; i < cnt; i++, word++) { |
| 66 | if (!(i % 4)) |
| 67 | printf("\nWord 0x%.8x:", word); |
| 68 | |
| 69 | ret = fuse_read(bank, word, &val); |
| 70 | if (ret) |
| 71 | goto err; |
| 72 | |
| 73 | printf(" %.8x", val); |
| 74 | } |
| 75 | putc('\n'); |
| 76 | } else if (!strcmp(op, "sense")) { |
| 77 | if (argc == 2) |
| 78 | cnt = 1; |
| 79 | else if (argc != 3 || strtou32(argv[2], 0, &cnt)) |
| 80 | return CMD_RET_USAGE; |
| 81 | |
| 82 | printf("Sensing bank %u:\n", bank); |
| 83 | for (i = 0; i < cnt; i++, word++) { |
| 84 | if (!(i % 4)) |
| 85 | printf("\nWord 0x%.8x:", word); |
| 86 | |
| 87 | ret = fuse_sense(bank, word, &val); |
| 88 | if (ret) |
| 89 | goto err; |
| 90 | |
| 91 | printf(" %.8x", val); |
| 92 | } |
| 93 | putc('\n'); |
| 94 | } else if (!strcmp(op, "prog")) { |
| 95 | if (argc < 3) |
| 96 | return CMD_RET_USAGE; |
| 97 | |
| 98 | for (i = 2; i < argc; i++, word++) { |
| 99 | if (strtou32(argv[i], 16, &val)) |
| 100 | return CMD_RET_USAGE; |
| 101 | |
| 102 | printf("Programming bank %u word 0x%.8x to 0x%.8x...\n", |
| 103 | bank, word, val); |
| 104 | if (!confirmed && !confirm_prog()) |
| 105 | return CMD_RET_FAILURE; |
| 106 | ret = fuse_prog(bank, word, val); |
| 107 | if (ret) |
| 108 | goto err; |
| 109 | } |
| 110 | } else if (!strcmp(op, "override")) { |
| 111 | if (argc < 3) |
| 112 | return CMD_RET_USAGE; |
| 113 | |
| 114 | for (i = 2; i < argc; i++, word++) { |
| 115 | if (strtou32(argv[i], 16, &val)) |
| 116 | return CMD_RET_USAGE; |
| 117 | |
| 118 | printf("Overriding bank %u word 0x%.8x with " |
| 119 | "0x%.8x...\n", bank, word, val); |
| 120 | ret = fuse_override(bank, word, val); |
| 121 | if (ret) |
| 122 | goto err; |
| 123 | } |
| 124 | } else { |
| 125 | return CMD_RET_USAGE; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | |
| 130 | err: |
| 131 | puts("ERROR\n"); |
Hector Palacios | 17e99cc | 2014-11-20 09:27:42 +0100 | [diff] [blame] | 132 | return CMD_RET_FAILURE; |
Benoît Thébaudeau | a8e8855 | 2013-04-23 10:17:40 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | U_BOOT_CMD( |
| 136 | fuse, CONFIG_SYS_MAXARGS, 0, do_fuse, |
| 137 | "Fuse sub-system", |
| 138 | "read <bank> <word> [<cnt>] - read 1 or 'cnt' fuse words,\n" |
| 139 | " starting at 'word'\n" |
| 140 | "fuse sense <bank> <word> [<cnt>] - sense 1 or 'cnt' fuse words,\n" |
| 141 | " starting at 'word'\n" |
| 142 | "fuse prog [-y] <bank> <word> <hexval> [<hexval>...] - program 1 or\n" |
| 143 | " several fuse words, starting at 'word' (PERMANENT)\n" |
| 144 | "fuse override <bank> <word> <hexval> [<hexval>...] - override 1 or\n" |
| 145 | " several fuse words, starting at 'word'" |
| 146 | ); |