blob: a23cedba90feb1018d66753a9c23ef7a52afe605 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Benoît Thébaudeaua8e88552013-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ébaudeaua8e88552013-04-23 10:17:40 +00009 */
10
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000011#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000013#include <fuse.h>
Angus Ainslief08743f2021-11-28 08:02:53 -080014#include <mapmem.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060015#include <vsprintf.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000017
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000018static int confirm_prog(void)
19{
20 puts("Warning: Programming fuses is an irreversible operation!\n"
21 " This may brick your system.\n"
22 " Use this command only if you are sure of "
23 "what you are doing!\n"
24 "\nReally perform this fuse programming? <y/N>\n");
25
Pierre Aubert5fef9d32014-04-24 10:30:07 +020026 if (confirm_yesno())
27 return 1;
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000028
29 puts("Fuse programming aborted\n");
30 return 0;
31}
32
Simon Glassed38aef2020-05-10 11:40:03 -060033static int do_fuse(struct cmd_tbl *cmdtp, int flag, int argc,
34 char *const argv[])
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000035{
Simon Glass793a98e2023-11-18 14:05:20 -070036 const char *op = cmd_arg1(argc, argv);
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000037 int confirmed = argc >= 3 && !strcmp(argv[2], "-y");
Angus Ainslie5149bca2021-11-28 08:02:52 -080038 u32 bank, word, cnt, val, cmp;
Angus Ainslief08743f2021-11-28 08:02:53 -080039 ulong addr;
40 void *buf, *start;
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000041 int ret, i;
42
43 argc -= 2 + confirmed;
44 argv += 2 + confirmed;
45
Harsha Vardhan V M56e9d2b2025-03-19 14:17:10 +053046 if (argc < 2)
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000047 return CMD_RET_USAGE;
48
Harsha Vardhan V M56e9d2b2025-03-19 14:17:10 +053049 bank = simple_strtoul(argv[0], NULL, 0);
50 word = simple_strtoul(argv[1], NULL, 0);
51
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000052 if (!strcmp(op, "read")) {
53 if (argc == 2)
54 cnt = 1;
Harsha Vardhan V M56e9d2b2025-03-19 14:17:10 +053055 else if (argc == 3)
56 cnt = simple_strtoul(argv[2], NULL, 0);
57 else
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +000058 return CMD_RET_USAGE;
59
60 printf("Reading bank %u:\n", bank);
61 for (i = 0; i < cnt; i++, word++) {
62 if (!(i % 4))
63 printf("\nWord 0x%.8x:", word);
64
65 ret = fuse_read(bank, word, &val);
66 if (ret)
67 goto err;
68
69 printf(" %.8x", val);
70 }
71 putc('\n');
Angus Ainslief08743f2021-11-28 08:02:53 -080072 } else if (!strcmp(op, "readm")) {
73 if (argc == 3)
74 cnt = 1;
Harsha Vardhan V M56e9d2b2025-03-19 14:17:10 +053075 else if (argc == 4)
76 cnt = simple_strtoul(argv[3], NULL, 0);
77 else
Angus Ainslief08743f2021-11-28 08:02:53 -080078 return CMD_RET_USAGE;
79
80 addr = simple_strtoul(argv[2], NULL, 16);
81
82 start = map_sysmem(addr, 4);
83 buf = start;
84
85 printf("Reading bank %u len %u to 0x%lx\n", bank, cnt, addr);
86 for (i = 0; i < cnt; i++, word++) {
87 ret = fuse_read(bank, word, &val);
88 if (ret)
89 goto err;
90
91 *((u32 *)buf) = val;
92 buf += 4;
93 }
94
95 unmap_sysmem(start);
Angus Ainslie5149bca2021-11-28 08:02:52 -080096 } else if (!strcmp(op, "cmp")) {
Harsha Vardhan V M56e9d2b2025-03-19 14:17:10 +053097 if (argc == 3)
98 cmp = simple_strtoul(argv[2], NULL, 0);
99 else
Angus Ainslie5149bca2021-11-28 08:02:52 -0800100 return CMD_RET_USAGE;
101
102 printf("Comparing bank %u:\n", bank);
103 printf("\nWord 0x%.8x:", word);
104 printf("\nValue 0x%.8x:", cmp);
105
106 ret = fuse_read(bank, word, &val);
107 if (ret)
108 goto err;
109
110 printf("0x%.8x\n", val);
111 if (val != cmp) {
112 printf("failed\n");
113 return CMD_RET_FAILURE;
114 }
115 printf("passed\n");
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +0000116 } else if (!strcmp(op, "sense")) {
117 if (argc == 2)
118 cnt = 1;
Harsha Vardhan V M56e9d2b2025-03-19 14:17:10 +0530119 else if (argc == 3)
120 cnt = simple_strtoul(argv[2], NULL, 0);
121 else
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +0000122 return CMD_RET_USAGE;
123
124 printf("Sensing bank %u:\n", bank);
125 for (i = 0; i < cnt; i++, word++) {
126 if (!(i % 4))
127 printf("\nWord 0x%.8x:", word);
128
129 ret = fuse_sense(bank, word, &val);
130 if (ret)
131 goto err;
132
133 printf(" %.8x", val);
134 }
135 putc('\n');
136 } else if (!strcmp(op, "prog")) {
137 if (argc < 3)
138 return CMD_RET_USAGE;
139
140 for (i = 2; i < argc; i++, word++) {
Harsha Vardhan V M56e9d2b2025-03-19 14:17:10 +0530141 val = simple_strtoul(argv[i], NULL, 16);
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +0000142
143 printf("Programming bank %u word 0x%.8x to 0x%.8x...\n",
144 bank, word, val);
145 if (!confirmed && !confirm_prog())
146 return CMD_RET_FAILURE;
147 ret = fuse_prog(bank, word, val);
148 if (ret)
149 goto err;
150 }
151 } else if (!strcmp(op, "override")) {
152 if (argc < 3)
153 return CMD_RET_USAGE;
154
155 for (i = 2; i < argc; i++, word++) {
Harsha Vardhan V M56e9d2b2025-03-19 14:17:10 +0530156 val = simple_strtoul(argv[i], NULL, 16);
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +0000157
158 printf("Overriding bank %u word 0x%.8x with "
159 "0x%.8x...\n", bank, word, val);
160 ret = fuse_override(bank, word, val);
161 if (ret)
162 goto err;
163 }
164 } else {
165 return CMD_RET_USAGE;
166 }
167
168 return 0;
169
170err:
171 puts("ERROR\n");
Hector Palacios17e99cc2014-11-20 09:27:42 +0100172 return CMD_RET_FAILURE;
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +0000173}
174
175U_BOOT_CMD(
176 fuse, CONFIG_SYS_MAXARGS, 0, do_fuse,
177 "Fuse sub-system",
178 "read <bank> <word> [<cnt>] - read 1 or 'cnt' fuse words,\n"
179 " starting at 'word'\n"
Angus Ainslie5149bca2021-11-28 08:02:52 -0800180 "fuse cmp <bank> <word> <hexval> - compare 'hexval' to fuse\n"
181 " at 'word'\n"
Angus Ainslief08743f2021-11-28 08:02:53 -0800182 "fuse readm <bank> <word> <addr> [<cnt>] - read 1 or 'cnt' fuse words,\n"
183 " starting at 'word' into memory at 'addr'\n"
Benoît Thébaudeaua8e88552013-04-23 10:17:40 +0000184 "fuse sense <bank> <word> [<cnt>] - sense 1 or 'cnt' fuse words,\n"
185 " starting at 'word'\n"
186 "fuse prog [-y] <bank> <word> <hexval> [<hexval>...] - program 1 or\n"
187 " several fuse words, starting at 'word' (PERMANENT)\n"
188 "fuse override <bank> <word> <hexval> [<hexval>...] - override 1 or\n"
189 " several fuse words, starting at 'word'"
190);