video: Split SPLASH_SCREEN_ALIGN from bmp command
[platform/kernel/u-boot.git] / cmd / fuse.c
1 // SPDX-License-Identifier: GPL-2.0+
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>
9  */
10
11 #include <common.h>
12 #include <command.h>
13 #include <console.h>
14 #include <fuse.h>
15 #include <mapmem.h>
16 #include <linux/errno.h>
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
37         if (confirm_yesno())
38                 return 1;
39
40         puts("Fuse programming aborted\n");
41         return 0;
42 }
43
44 static int do_fuse(struct cmd_tbl *cmdtp, int flag, int argc,
45                    char *const argv[])
46 {
47         const char *op = argc >= 2 ? argv[1] : NULL;
48         int confirmed = argc >= 3 && !strcmp(argv[2], "-y");
49         u32 bank, word, cnt, val, cmp;
50         ulong addr;
51         void *buf, *start;
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');
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);
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");
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");
175         return CMD_RET_FAILURE;
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"
183         "fuse cmp <bank> <word> <hexval> - compare 'hexval' to fuse\n"
184         "    at 'word'\n"
185         "fuse readm <bank> <word> <addr> [<cnt>] - read 1 or 'cnt' fuse words,\n"
186         "    starting at 'word' into memory at 'addr'\n"
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 );