3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #if defined(CONFIG_CMD_FLASH)
33 extern flash_info_t flash_info[]; /* info for FLASH chips */
36 * The user interface starts numbering for Flash banks with 1
37 * for historical reasons.
41 * this routine looks for an abbreviated flash range specification.
42 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
43 * sector to erase, and SL is the last sector to erase (defaults to SF).
44 * bank numbers start at 1 to be consistent with other specs, sector numbers
47 * returns: 1 - correct spec; *pinfo, *psf and *psl are
49 * 0 - doesn't look like an abbreviated spec
50 * -1 - looks like an abbreviated spec, but got
51 * a parsing error, a number out of range,
52 * or an invalid flash bank.
55 abbrev_spec(char *str, flash_info_t **pinfo, int *psf, int *psl)
58 int bank, first, last;
61 if ((p = strchr(str, ':')) == NULL)
65 bank = simple_strtoul(str, &ep, 10);
66 if (ep == str || *ep != '\0' ||
67 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
68 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
72 if ((p = strchr(str, '-')) != NULL)
75 first = simple_strtoul(str, &ep, 10);
76 if (ep == str || *ep != '\0' || first >= fp->sector_count)
80 last = simple_strtoul(p, &ep, 10);
81 if (ep == p || *ep != '\0' ||
82 last < first || last >= fp->sector_count)
94 int do_flinfo (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
98 if (argc == 1) { /* print info for all FLASH banks */
99 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
100 printf ("\nBank # %ld: ", bank+1);
102 flash_print_info (&flash_info[bank]);
107 bank = simple_strtoul(argv[1], NULL, 16);
108 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
109 printf ("Only FLASH Banks # 1 ... # %d supported\n",
110 CONFIG_SYS_MAX_FLASH_BANKS);
113 printf ("\nBank # %ld: ", bank);
114 flash_print_info (&flash_info[bank-1]);
117 int do_flerase(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
120 ulong bank, addr_first, addr_last;
121 int n, sect_first, sect_last;
125 return cmd_usage(cmdtp);
127 if (strcmp(argv[1], "all") == 0) {
128 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
129 printf ("Erase Flash Bank # %ld ", bank);
130 info = &flash_info[bank-1];
131 rcode = flash_erase (info, 0, info->sector_count-1);
136 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
138 printf("Bad sector specification\n");
141 printf ("Erase Flash Sectors %d-%d in Bank # %d ",
142 sect_first, sect_last, (info-flash_info)+1);
143 rcode = flash_erase(info, sect_first, sect_last);
148 return cmd_usage(cmdtp);
150 if (strcmp(argv[1], "bank") == 0) {
151 bank = simple_strtoul(argv[2], NULL, 16);
152 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
153 printf ("Only FLASH Banks # 1 ... # %d supported\n",
154 CONFIG_SYS_MAX_FLASH_BANKS);
157 printf ("Erase Flash Bank # %ld ", bank);
158 info = &flash_info[bank-1];
159 rcode = flash_erase (info, 0, info->sector_count-1);
163 addr_first = simple_strtoul(argv[1], NULL, 16);
164 addr_last = simple_strtoul(argv[2], NULL, 16);
166 if (addr_first >= addr_last)
167 return cmd_usage(cmdtp);
169 printf ("Erase Flash from 0x%08lx to 0x%08lx ", addr_first, addr_last);
170 rcode = flash_sect_erase(addr_first, addr_last);
174 int flash_sect_erase (ulong addr_first, ulong addr_last)
184 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
188 if (info->flash_id == FLASH_UNKNOWN) {
192 b_end = info->start[0] + info->size - 1; /* bank end addr */
194 s_first = -1; /* first sector to erase */
195 s_last = -1; /* last sector to erase */
197 for (sect=0; sect < info->sector_count; ++sect) {
198 ulong end; /* last address in current sect */
201 s_end = info->sector_count - 1;
203 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
205 if (addr_first > end)
207 if (addr_last < info->start[sect])
210 if (addr_first == info->start[sect]) {
213 if (addr_last == end) {
217 if (s_first>=0 && s_first<=s_last) {
218 erased += s_last - s_first + 1;
219 rcode = flash_erase (info, s_first, s_last);
223 /* printf ("Erased %d sectors\n", erased); */
225 printf ("Error: start and/or end address"
226 " not on sector boundary\n");
233 int do_protect(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
236 ulong bank, addr_first, addr_last;
237 int i, p, n, sect_first, sect_last;
241 return cmd_usage(cmdtp);
243 if (strcmp(argv[1], "off") == 0)
245 else if (strcmp(argv[1], "on") == 0)
248 return cmd_usage(cmdtp);
250 if (strcmp(argv[2], "all") == 0) {
251 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
252 info = &flash_info[bank-1];
253 if (info->flash_id == FLASH_UNKNOWN) {
256 /*printf ("%sProtect Flash Bank # %ld\n", */
257 /* p ? "" : "Un-", bank); */
259 for (i=0; i<info->sector_count; ++i) {
260 #if defined(CONFIG_SYS_FLASH_PROTECTION)
261 if (flash_real_protect(info, i, p))
265 info->protect[i] = p;
266 #endif /* CONFIG_SYS_FLASH_PROTECTION */
270 #if defined(CONFIG_SYS_FLASH_PROTECTION)
271 if (!rcode) puts (" done\n");
272 #endif /* CONFIG_SYS_FLASH_PROTECTION */
277 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
279 printf("Bad sector specification\n");
282 /*printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", */
283 /* p ? "" : "Un-", sect_first, sect_last, */
284 /* (info-flash_info)+1); */
285 for (i = sect_first; i <= sect_last; i++) {
286 #if defined(CONFIG_SYS_FLASH_PROTECTION)
287 if (flash_real_protect(info, i, p))
291 info->protect[i] = p;
292 #endif /* CONFIG_SYS_FLASH_PROTECTION */
295 #if defined(CONFIG_SYS_FLASH_PROTECTION)
296 if (!rcode) puts (" done\n");
297 #endif /* CONFIG_SYS_FLASH_PROTECTION */
303 return cmd_usage(cmdtp);
305 if (strcmp(argv[2], "bank") == 0) {
306 bank = simple_strtoul(argv[3], NULL, 16);
307 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
308 printf ("Only FLASH Banks # 1 ... # %d supported\n",
309 CONFIG_SYS_MAX_FLASH_BANKS);
312 printf ("%sProtect Flash Bank # %ld\n",
313 p ? "" : "Un-", bank);
314 info = &flash_info[bank-1];
316 if (info->flash_id == FLASH_UNKNOWN) {
317 printf ("missing or unknown FLASH type\n");
320 for (i=0; i<info->sector_count; ++i) {
321 #if defined(CONFIG_SYS_FLASH_PROTECTION)
322 if (flash_real_protect(info, i, p))
326 info->protect[i] = p;
327 #endif /* CONFIG_SYS_FLASH_PROTECTION */
330 #if defined(CONFIG_SYS_FLASH_PROTECTION)
333 #endif /* CONFIG_SYS_FLASH_PROTECTION */
338 addr_first = simple_strtoul(argv[2], NULL, 16);
339 addr_last = simple_strtoul(argv[3], NULL, 16);
341 if (addr_first >= addr_last)
342 return cmd_usage(cmdtp);
344 return flash_sect_protect (p, addr_first, addr_last);
346 int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
356 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
360 if (info->flash_id == FLASH_UNKNOWN) {
364 b_end = info->start[0] + info->size - 1; /* bank end addr */
366 s_first = -1; /* first sector to erase */
367 s_last = -1; /* last sector to erase */
369 for (sect=0; sect < info->sector_count; ++sect) {
370 ulong end; /* last address in current sect */
373 s_end = info->sector_count - 1;
375 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
377 if (addr_first > end)
379 if (addr_last < info->start[sect])
382 if (addr_first == info->start[sect]) {
385 if (addr_last == end) {
389 if (s_first>=0 && s_first<=s_last) {
390 protected += s_last - s_first + 1;
391 for (i=s_first; i<=s_last; ++i) {
392 #if defined(CONFIG_SYS_FLASH_PROTECTION)
393 if (flash_real_protect(info, i, p))
397 info->protect[i] = p;
398 #endif /* CONFIG_SYS_FLASH_PROTECTION */
401 #if defined(CONFIG_SYS_FLASH_PROTECTION)
402 if (!rcode) putc ('\n');
403 #endif /* CONFIG_SYS_FLASH_PROTECTION */
407 /* printf ("%sProtected %d sectors\n", */
408 /* p ? "" : "Un-", protected); */
410 printf ("Error: start and/or end address"
411 " not on sector boundary\n");