blob: 0676bb7a812f7cf9afb6b8b4a88698b267394ed1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Benoît Thébaudeauccca7df2013-04-23 10:17:40 +00002/*
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ébaudeauccca7df2013-04-23 10:17:40 +00009 */
10
11#include <common.h>
12#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070013#include <console.h>
Benoît Thébaudeauccca7df2013-04-23 10:17:40 +000014#include <fuse.h>
Angus Ainslieb4f0c7f2021-11-28 08:02:53 -080015#include <mapmem.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090016#include <linux/errno.h>
Benoît Thébaudeauccca7df2013-04-23 10:17:40 +000017
18static 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
29static 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 Auberta5dffa42014-04-24 10:30:07 +020037 if (confirm_yesno())
38 return 1;
Benoît Thébaudeauccca7df2013-04-23 10:17:40 +000039
40 puts("Fuse programming aborted\n");
41 return 0;
42}
43
Simon Glass09140112020-05-10 11:40:03 -060044static int do_fuse(struct cmd_tbl *cmdtp, int flag, int argc,
45 char *const argv[])
Benoît Thébaudeauccca7df2013-04-23 10:17:40 +000046{
47 const char *op = argc >= 2 ? argv[1] : NULL;
48 int confirmed = argc >= 3 && !strcmp(argv[2], "-y");
Angus Ainsliec2a4af52021-11-28 08:02:52 -080049 u32 bank, word, cnt, val, cmp;
Angus Ainslieb4f0c7f2021-11-28 08:02:53 -080050 ulong addr;
51 void *buf, *start;
Benoît Thébaudeauccca7df2013-04-23 10:17:40 +000052 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 Ainslieb4f0c7f2021-11-28 08:02:53 -080079 } 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 Ainsliec2a4af52021-11-28 08:02:52 -0800101 } 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ébaudeauccca7df2013-04-23 10:17:40 +0000119 } 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
173err:
174 puts("ERROR\n");
Hector Palacios814b6612014-11-20 09:27:42 +0100175 return CMD_RET_FAILURE;
Benoît Thébaudeauccca7df2013-04-23 10:17:40 +0000176}
177
178U_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 Ainsliec2a4af52021-11-28 08:02:52 -0800183 "fuse cmp <bank> <word> <hexval> - compare 'hexval' to fuse\n"
184 " at 'word'\n"
Angus Ainslieb4f0c7f2021-11-28 08:02:53 -0800185 "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ébaudeauccca7df2013-04-23 10:17:40 +0000187 "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);