1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
22e192b24SSimon Glass /*
32e192b24SSimon Glass * (C) Copyright 2009-2013 ADVANSEE
42e192b24SSimon Glass * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
52e192b24SSimon Glass *
62e192b24SSimon Glass * Based on the mpc512x iim code:
72e192b24SSimon Glass * Copyright 2008 Silicon Turnkey Express, Inc.
82e192b24SSimon Glass * Martha Marx <mmarx@silicontkx.com>
92e192b24SSimon Glass */
102e192b24SSimon Glass
112e192b24SSimon Glass #include <common.h>
122e192b24SSimon Glass #include <command.h>
132e192b24SSimon Glass #include <console.h>
142e192b24SSimon Glass #include <fuse.h>
151221ce45SMasahiro Yamada #include <linux/errno.h>
162e192b24SSimon Glass
strtou32(const char * str,unsigned int base,u32 * result)172e192b24SSimon Glass static int strtou32(const char *str, unsigned int base, u32 *result)
182e192b24SSimon Glass {
192e192b24SSimon Glass char *ep;
202e192b24SSimon Glass
212e192b24SSimon Glass *result = simple_strtoul(str, &ep, base);
222e192b24SSimon Glass if (ep == str || *ep != '\0')
232e192b24SSimon Glass return -EINVAL;
242e192b24SSimon Glass
252e192b24SSimon Glass return 0;
262e192b24SSimon Glass }
272e192b24SSimon Glass
confirm_prog(void)282e192b24SSimon Glass static int confirm_prog(void)
292e192b24SSimon Glass {
302e192b24SSimon Glass puts("Warning: Programming fuses is an irreversible operation!\n"
312e192b24SSimon Glass " This may brick your system.\n"
322e192b24SSimon Glass " Use this command only if you are sure of "
332e192b24SSimon Glass "what you are doing!\n"
342e192b24SSimon Glass "\nReally perform this fuse programming? <y/N>\n");
352e192b24SSimon Glass
362e192b24SSimon Glass if (confirm_yesno())
372e192b24SSimon Glass return 1;
382e192b24SSimon Glass
392e192b24SSimon Glass puts("Fuse programming aborted\n");
402e192b24SSimon Glass return 0;
412e192b24SSimon Glass }
422e192b24SSimon Glass
do_fuse(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])432e192b24SSimon Glass static int do_fuse(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
442e192b24SSimon Glass {
452e192b24SSimon Glass const char *op = argc >= 2 ? argv[1] : NULL;
462e192b24SSimon Glass int confirmed = argc >= 3 && !strcmp(argv[2], "-y");
472e192b24SSimon Glass u32 bank, word, cnt, val;
482e192b24SSimon Glass int ret, i;
492e192b24SSimon Glass
502e192b24SSimon Glass argc -= 2 + confirmed;
512e192b24SSimon Glass argv += 2 + confirmed;
522e192b24SSimon Glass
532e192b24SSimon Glass if (argc < 2 || strtou32(argv[0], 0, &bank) ||
542e192b24SSimon Glass strtou32(argv[1], 0, &word))
552e192b24SSimon Glass return CMD_RET_USAGE;
562e192b24SSimon Glass
572e192b24SSimon Glass if (!strcmp(op, "read")) {
582e192b24SSimon Glass if (argc == 2)
592e192b24SSimon Glass cnt = 1;
602e192b24SSimon Glass else if (argc != 3 || strtou32(argv[2], 0, &cnt))
612e192b24SSimon Glass return CMD_RET_USAGE;
622e192b24SSimon Glass
632e192b24SSimon Glass printf("Reading bank %u:\n", bank);
642e192b24SSimon Glass for (i = 0; i < cnt; i++, word++) {
652e192b24SSimon Glass if (!(i % 4))
662e192b24SSimon Glass printf("\nWord 0x%.8x:", word);
672e192b24SSimon Glass
682e192b24SSimon Glass ret = fuse_read(bank, word, &val);
692e192b24SSimon Glass if (ret)
702e192b24SSimon Glass goto err;
712e192b24SSimon Glass
722e192b24SSimon Glass printf(" %.8x", val);
732e192b24SSimon Glass }
742e192b24SSimon Glass putc('\n');
752e192b24SSimon Glass } else if (!strcmp(op, "sense")) {
762e192b24SSimon Glass if (argc == 2)
772e192b24SSimon Glass cnt = 1;
782e192b24SSimon Glass else if (argc != 3 || strtou32(argv[2], 0, &cnt))
792e192b24SSimon Glass return CMD_RET_USAGE;
802e192b24SSimon Glass
812e192b24SSimon Glass printf("Sensing bank %u:\n", bank);
822e192b24SSimon Glass for (i = 0; i < cnt; i++, word++) {
832e192b24SSimon Glass if (!(i % 4))
842e192b24SSimon Glass printf("\nWord 0x%.8x:", word);
852e192b24SSimon Glass
862e192b24SSimon Glass ret = fuse_sense(bank, word, &val);
872e192b24SSimon Glass if (ret)
882e192b24SSimon Glass goto err;
892e192b24SSimon Glass
902e192b24SSimon Glass printf(" %.8x", val);
912e192b24SSimon Glass }
922e192b24SSimon Glass putc('\n');
932e192b24SSimon Glass } else if (!strcmp(op, "prog")) {
942e192b24SSimon Glass if (argc < 3)
952e192b24SSimon Glass return CMD_RET_USAGE;
962e192b24SSimon Glass
972e192b24SSimon Glass for (i = 2; i < argc; i++, word++) {
982e192b24SSimon Glass if (strtou32(argv[i], 16, &val))
992e192b24SSimon Glass return CMD_RET_USAGE;
1002e192b24SSimon Glass
1012e192b24SSimon Glass printf("Programming bank %u word 0x%.8x to 0x%.8x...\n",
1022e192b24SSimon Glass bank, word, val);
1032e192b24SSimon Glass if (!confirmed && !confirm_prog())
1042e192b24SSimon Glass return CMD_RET_FAILURE;
1052e192b24SSimon Glass ret = fuse_prog(bank, word, val);
1062e192b24SSimon Glass if (ret)
1072e192b24SSimon Glass goto err;
1082e192b24SSimon Glass }
1092e192b24SSimon Glass } else if (!strcmp(op, "override")) {
1102e192b24SSimon Glass if (argc < 3)
1112e192b24SSimon Glass return CMD_RET_USAGE;
1122e192b24SSimon Glass
1132e192b24SSimon Glass for (i = 2; i < argc; i++, word++) {
1142e192b24SSimon Glass if (strtou32(argv[i], 16, &val))
1152e192b24SSimon Glass return CMD_RET_USAGE;
1162e192b24SSimon Glass
1172e192b24SSimon Glass printf("Overriding bank %u word 0x%.8x with "
1182e192b24SSimon Glass "0x%.8x...\n", bank, word, val);
1192e192b24SSimon Glass ret = fuse_override(bank, word, val);
1202e192b24SSimon Glass if (ret)
1212e192b24SSimon Glass goto err;
1222e192b24SSimon Glass }
1232e192b24SSimon Glass } else {
1242e192b24SSimon Glass return CMD_RET_USAGE;
1252e192b24SSimon Glass }
1262e192b24SSimon Glass
1272e192b24SSimon Glass return 0;
1282e192b24SSimon Glass
1292e192b24SSimon Glass err:
1302e192b24SSimon Glass puts("ERROR\n");
1312e192b24SSimon Glass return CMD_RET_FAILURE;
1322e192b24SSimon Glass }
1332e192b24SSimon Glass
1342e192b24SSimon Glass U_BOOT_CMD(
1352e192b24SSimon Glass fuse, CONFIG_SYS_MAXARGS, 0, do_fuse,
1362e192b24SSimon Glass "Fuse sub-system",
1372e192b24SSimon Glass "read <bank> <word> [<cnt>] - read 1 or 'cnt' fuse words,\n"
1382e192b24SSimon Glass " starting at 'word'\n"
1392e192b24SSimon Glass "fuse sense <bank> <word> [<cnt>] - sense 1 or 'cnt' fuse words,\n"
1402e192b24SSimon Glass " starting at 'word'\n"
1412e192b24SSimon Glass "fuse prog [-y] <bank> <word> <hexval> [<hexval>...] - program 1 or\n"
1422e192b24SSimon Glass " several fuse words, starting at 'word' (PERMANENT)\n"
1432e192b24SSimon Glass "fuse override <bank> <word> <hexval> [<hexval>...] - override 1 or\n"
1442e192b24SSimon Glass " several fuse words, starting at 'word'"
1452e192b24SSimon Glass );
146