wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * FLASH support |
| 26 | */ |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 29 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 30 | |
wdenk | 381669a | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 31 | #ifdef CONFIG_HAS_DATAFLASH |
| 32 | #include <dataflash.h> |
| 33 | #endif |
| 34 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 35 | #if (CONFIG_COMMANDS & CFG_CMD_FLASH) |
| 36 | |
| 37 | extern flash_info_t flash_info[]; /* info for FLASH chips */ |
| 38 | |
| 39 | /* |
| 40 | * The user interface starts numbering for Flash banks with 1 |
| 41 | * for historical reasons. |
| 42 | */ |
| 43 | |
| 44 | /* |
| 45 | * this routine looks for an abbreviated flash range specification. |
| 46 | * the syntax is B:SF[-SL], where B is the bank number, SF is the first |
| 47 | * sector to erase, and SL is the last sector to erase (defaults to SF). |
| 48 | * bank numbers start at 1 to be consistent with other specs, sector numbers |
| 49 | * start at zero. |
| 50 | * |
| 51 | * returns: 1 - correct spec; *pinfo, *psf and *psl are |
| 52 | * set appropriately |
| 53 | * 0 - doesn't look like an abbreviated spec |
| 54 | * -1 - looks like an abbreviated spec, but got |
| 55 | * a parsing error, a number out of range, |
| 56 | * or an invalid flash bank. |
| 57 | */ |
| 58 | static int |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 59 | abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl) |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 60 | { |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 61 | flash_info_t *fp; |
| 62 | int bank, first, last; |
| 63 | char *p, *ep; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 64 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 65 | if ((p = strchr (str, ':')) == NULL) |
| 66 | return 0; |
| 67 | *p++ = '\0'; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 68 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 69 | bank = simple_strtoul (str, &ep, 10); |
| 70 | if (ep == str || *ep != '\0' || |
| 71 | bank < 1 || bank > CFG_MAX_FLASH_BANKS || |
| 72 | (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN) |
| 73 | return -1; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 74 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 75 | str = p; |
| 76 | if ((p = strchr (str, '-')) != NULL) |
| 77 | *p++ = '\0'; |
| 78 | |
| 79 | first = simple_strtoul (str, &ep, 10); |
| 80 | if (ep == str || *ep != '\0' || first >= fp->sector_count) |
| 81 | return -1; |
| 82 | |
| 83 | if (p != NULL) { |
| 84 | last = simple_strtoul (p, &ep, 10); |
| 85 | if (ep == p || *ep != '\0' || |
| 86 | last < first || last >= fp->sector_count) |
| 87 | return -1; |
| 88 | } else { |
| 89 | last = first; |
| 90 | } |
| 91 | |
| 92 | *pinfo = fp; |
| 93 | *psf = first; |
| 94 | *psl = last; |
| 95 | |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | static int |
| 100 | flash_fill_sect_ranges (ulong addr_first, ulong addr_last, |
| 101 | int *s_first, int *s_last, |
| 102 | int *s_count ) |
| 103 | { |
| 104 | flash_info_t *info; |
| 105 | ulong bank; |
| 106 | int rcode = 0; |
| 107 | |
| 108 | *s_count = 0; |
| 109 | |
| 110 | for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) { |
| 111 | s_first[bank] = -1; /* first sector to erase */ |
| 112 | s_last [bank] = -1; /* last sector to erase */ |
| 113 | } |
| 114 | |
| 115 | for (bank=0,info=&flash_info[0]; |
| 116 | (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last); |
| 117 | ++bank, ++info) { |
| 118 | ulong b_end; |
| 119 | int sect; |
| 120 | short s_end; |
| 121 | |
| 122 | if (info->flash_id == FLASH_UNKNOWN) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | b_end = info->start[0] + info->size - 1; /* bank end addr */ |
| 127 | s_end = info->sector_count - 1; /* last sector */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 128 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 129 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 130 | for (sect=0; sect < info->sector_count; ++sect) { |
| 131 | ulong end; /* last address in current sect */ |
| 132 | |
| 133 | end = (sect == s_end) ? b_end : info->start[sect + 1] - 1; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 134 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 135 | if (addr_first > end) |
| 136 | continue; |
| 137 | if (addr_last < info->start[sect]) |
| 138 | continue; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 139 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 140 | if (addr_first == info->start[sect]) { |
| 141 | s_first[bank] = sect; |
| 142 | } |
| 143 | if (addr_last == end) { |
| 144 | s_last[bank] = sect; |
| 145 | } |
| 146 | } |
| 147 | if (s_first[bank] >= 0) { |
| 148 | if (s_last[bank] < 0) { |
| 149 | if (addr_last > b_end) { |
| 150 | s_last[bank] = s_end; |
| 151 | } else { |
| 152 | printf ("Error: end address" |
| 153 | " not on sector boundary\n"); |
| 154 | rcode = 1; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | if (s_last[bank] < s_first[bank]) { |
| 159 | printf ("Error: end sector" |
| 160 | " precedes start sector\n"); |
| 161 | rcode = 1; |
| 162 | break; |
| 163 | } |
| 164 | sect = s_last[bank]; |
| 165 | addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1]; |
| 166 | (*s_count) += s_last[bank] - s_first[bank] + 1; |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 167 | } else if (s_last[bank] >= 0) { |
| 168 | printf("Error: cannot span across banks when they are" |
| 169 | " mapped in reverse order\n"); |
| 170 | rcode = 1; |
| 171 | break; |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | return rcode; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 176 | } |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 177 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 178 | int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 179 | { |
| 180 | ulong bank; |
| 181 | |
wdenk | 381669a | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 182 | #ifdef CONFIG_HAS_DATAFLASH |
| 183 | dataflash_print_info(); |
| 184 | #endif |
| 185 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 186 | if (argc == 1) { /* print info for all FLASH banks */ |
| 187 | for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) { |
| 188 | printf ("\nBank # %ld: ", bank+1); |
| 189 | |
| 190 | flash_print_info (&flash_info[bank]); |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | bank = simple_strtoul(argv[1], NULL, 16); |
| 196 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 197 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 198 | CFG_MAX_FLASH_BANKS); |
| 199 | return 1; |
| 200 | } |
| 201 | printf ("\nBank # %ld: ", bank); |
| 202 | flash_print_info (&flash_info[bank-1]); |
| 203 | return 0; |
| 204 | } |
| 205 | int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 206 | { |
| 207 | flash_info_t *info; |
| 208 | ulong bank, addr_first, addr_last; |
| 209 | int n, sect_first, sect_last; |
| 210 | int rcode = 0; |
| 211 | |
| 212 | if (argc < 2) { |
| 213 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | if (strcmp(argv[1], "all") == 0) { |
| 218 | for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) { |
| 219 | printf ("Erase Flash Bank # %ld ", bank); |
| 220 | info = &flash_info[bank-1]; |
| 221 | rcode = flash_erase (info, 0, info->sector_count-1); |
| 222 | } |
| 223 | return rcode; |
| 224 | } |
| 225 | |
| 226 | if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) { |
| 227 | if (n < 0) { |
| 228 | printf("Bad sector specification\n"); |
| 229 | return 1; |
| 230 | } |
| 231 | printf ("Erase Flash Sectors %d-%d in Bank # %d ", |
| 232 | sect_first, sect_last, (info-flash_info)+1); |
| 233 | rcode = flash_erase(info, sect_first, sect_last); |
| 234 | return rcode; |
| 235 | } |
| 236 | |
| 237 | if (argc != 3) { |
| 238 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 239 | return 1; |
| 240 | } |
| 241 | |
| 242 | if (strcmp(argv[1], "bank") == 0) { |
| 243 | bank = simple_strtoul(argv[2], NULL, 16); |
| 244 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 245 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 246 | CFG_MAX_FLASH_BANKS); |
| 247 | return 1; |
| 248 | } |
| 249 | printf ("Erase Flash Bank # %ld ", bank); |
| 250 | info = &flash_info[bank-1]; |
| 251 | rcode = flash_erase (info, 0, info->sector_count-1); |
| 252 | return rcode; |
| 253 | } |
| 254 | |
| 255 | addr_first = simple_strtoul(argv[1], NULL, 16); |
| 256 | addr_last = simple_strtoul(argv[2], NULL, 16); |
| 257 | |
| 258 | if (addr_first >= addr_last) { |
| 259 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 260 | return 1; |
| 261 | } |
| 262 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 263 | rcode = flash_sect_erase(addr_first, addr_last); |
| 264 | return rcode; |
| 265 | } |
| 266 | |
| 267 | int flash_sect_erase (ulong addr_first, ulong addr_last) |
| 268 | { |
| 269 | flash_info_t *info; |
| 270 | ulong bank; |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 271 | int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS]; |
| 272 | int erased = 0; |
| 273 | int planned; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 274 | int rcode = 0; |
| 275 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 276 | rcode = flash_fill_sect_ranges (addr_first, addr_last, |
| 277 | s_first, s_last, &planned ); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 278 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 279 | if (planned && (rcode == 0)) { |
| 280 | for (bank=0,info=&flash_info[0]; |
| 281 | (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0); |
| 282 | ++bank, ++info) { |
| 283 | if (s_first[bank]>=0) { |
| 284 | erased += s_last[bank] - s_first[bank] + 1; |
wdenk | 3f08e20 | 2003-08-07 14:52:18 +0000 | [diff] [blame] | 285 | debug ("Erase Flash from 0x%08lx to 0x%08lx " |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 286 | "in Bank # %ld ", |
| 287 | info->start[s_first[bank]], |
| 288 | (s_last[bank] == info->sector_count) ? |
| 289 | info->start[0] + info->size - 1: |
| 290 | info->start[s_last[bank]+1] - 1, |
| 291 | bank+1); |
| 292 | rcode = flash_erase (info, s_first[bank], s_last[bank]); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 293 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 294 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 295 | printf ("Erased %d sectors\n", erased); |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 296 | } else if (rcode == 0) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 297 | printf ("Error: start and/or end address" |
| 298 | " not on sector boundary\n"); |
| 299 | rcode = 1; |
| 300 | } |
| 301 | return rcode; |
| 302 | } |
| 303 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 304 | int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 305 | { |
| 306 | flash_info_t *info; |
| 307 | ulong bank, addr_first, addr_last; |
| 308 | int i, p, n, sect_first, sect_last; |
| 309 | int rcode = 0; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 310 | #ifdef CONFIG_HAS_DATAFLASH |
| 311 | int status; |
| 312 | #endif |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 313 | if (argc < 3) { |
| 314 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 315 | return 1; |
| 316 | } |
| 317 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 318 | if (strcmp(argv[1], "off") == 0) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 319 | p = 0; |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 320 | } else if (strcmp(argv[1], "on") == 0) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 321 | p = 1; |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 322 | } else { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 323 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 324 | return 1; |
| 325 | } |
| 326 | |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 327 | #ifdef CONFIG_HAS_DATAFLASH |
| 328 | if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) { |
| 329 | addr_first = simple_strtoul(argv[2], NULL, 16); |
| 330 | addr_last = simple_strtoul(argv[3], NULL, 16); |
| 331 | |
| 332 | if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) { |
| 333 | status = dataflash_real_protect(p,addr_first,addr_last); |
| 334 | if (status < 0){ |
| 335 | printf("Bad DataFlash sector specification\n"); |
wdenk | 1ebf41e | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 336 | return 1; |
| 337 | } |
| 338 | printf("%sProtect %d DataFlash Sectors\n", |
| 339 | p ? "" : "Un-", status); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | } |
| 343 | #endif |
wdenk | 1ebf41e | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 344 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 345 | if (strcmp(argv[2], "all") == 0) { |
| 346 | for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) { |
| 347 | info = &flash_info[bank-1]; |
| 348 | if (info->flash_id == FLASH_UNKNOWN) { |
| 349 | continue; |
| 350 | } |
| 351 | printf ("%sProtect Flash Bank # %ld\n", |
| 352 | p ? "" : "Un-", bank); |
| 353 | |
| 354 | for (i=0; i<info->sector_count; ++i) { |
| 355 | #if defined(CFG_FLASH_PROTECTION) |
| 356 | if (flash_real_protect(info, i, p)) |
| 357 | rcode = 1; |
| 358 | putc ('.'); |
| 359 | #else |
| 360 | info->protect[i] = p; |
| 361 | #endif /* CFG_FLASH_PROTECTION */ |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | #if defined(CFG_FLASH_PROTECTION) |
| 366 | if (!rcode) puts (" done\n"); |
| 367 | #endif /* CFG_FLASH_PROTECTION */ |
| 368 | |
| 369 | return rcode; |
| 370 | } |
| 371 | |
| 372 | if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) { |
| 373 | if (n < 0) { |
| 374 | printf("Bad sector specification\n"); |
| 375 | return 1; |
| 376 | } |
| 377 | printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", |
| 378 | p ? "" : "Un-", sect_first, sect_last, |
| 379 | (info-flash_info)+1); |
| 380 | for (i = sect_first; i <= sect_last; i++) { |
| 381 | #if defined(CFG_FLASH_PROTECTION) |
| 382 | if (flash_real_protect(info, i, p)) |
| 383 | rcode = 1; |
| 384 | putc ('.'); |
| 385 | #else |
| 386 | info->protect[i] = p; |
| 387 | #endif /* CFG_FLASH_PROTECTION */ |
| 388 | } |
| 389 | |
| 390 | #if defined(CFG_FLASH_PROTECTION) |
| 391 | if (!rcode) puts (" done\n"); |
| 392 | #endif /* CFG_FLASH_PROTECTION */ |
| 393 | |
| 394 | return rcode; |
| 395 | } |
| 396 | |
| 397 | if (argc != 4) { |
| 398 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | if (strcmp(argv[2], "bank") == 0) { |
| 403 | bank = simple_strtoul(argv[3], NULL, 16); |
| 404 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 405 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 406 | CFG_MAX_FLASH_BANKS); |
| 407 | return 1; |
| 408 | } |
| 409 | printf ("%sProtect Flash Bank # %ld\n", |
| 410 | p ? "" : "Un-", bank); |
| 411 | info = &flash_info[bank-1]; |
| 412 | |
| 413 | if (info->flash_id == FLASH_UNKNOWN) { |
| 414 | printf ("missing or unknown FLASH type\n"); |
| 415 | return 1; |
| 416 | } |
| 417 | for (i=0; i<info->sector_count; ++i) { |
| 418 | #if defined(CFG_FLASH_PROTECTION) |
| 419 | if (flash_real_protect(info, i, p)) |
| 420 | rcode = 1; |
| 421 | putc ('.'); |
| 422 | #else |
| 423 | info->protect[i] = p; |
| 424 | #endif /* CFG_FLASH_PROTECTION */ |
| 425 | } |
| 426 | |
| 427 | #if defined(CFG_FLASH_PROTECTION) |
| 428 | if (!rcode) puts (" done\n"); |
| 429 | #endif /* CFG_FLASH_PROTECTION */ |
| 430 | |
| 431 | return rcode; |
| 432 | } |
| 433 | |
| 434 | addr_first = simple_strtoul(argv[2], NULL, 16); |
| 435 | addr_last = simple_strtoul(argv[3], NULL, 16); |
| 436 | |
| 437 | if (addr_first >= addr_last) { |
| 438 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 439 | return 1; |
| 440 | } |
| 441 | rcode = flash_sect_protect (p, addr_first, addr_last); |
| 442 | return rcode; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | int flash_sect_protect (int p, ulong addr_first, ulong addr_last) |
| 447 | { |
| 448 | flash_info_t *info; |
| 449 | ulong bank; |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 450 | int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS]; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 451 | int protected, i; |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 452 | int planned; |
| 453 | int rcode; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 454 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 455 | rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned ); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 456 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 457 | protected = 0; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 458 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 459 | if (planned && (rcode == 0)) { |
| 460 | for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) { |
| 461 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 462 | continue; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 463 | } |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 464 | |
| 465 | if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) { |
stroese | eb4a439 | 2003-12-09 14:58:22 +0000 | [diff] [blame] | 466 | debug ("%sProtecting sectors %d..%d in bank %ld\n", |
| 467 | p ? "" : "Un-", |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 468 | s_first[bank], s_last[bank], bank+1); |
| 469 | protected += s_last[bank] - s_first[bank] + 1; |
| 470 | for (i=s_first[bank]; i<=s_last[bank]; ++i) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 471 | #if defined(CFG_FLASH_PROTECTION) |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 472 | if (flash_real_protect(info, i, p)) |
| 473 | rcode = 1; |
| 474 | putc ('.'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 475 | #else |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 476 | info->protect[i] = p; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 477 | #endif /* CFG_FLASH_PROTECTION */ |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 478 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 479 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 480 | #if defined(CFG_FLASH_PROTECTION) |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 481 | if (!rcode) putc ('\n'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 482 | #endif /* CFG_FLASH_PROTECTION */ |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 483 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 484 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 485 | printf ("%sProtected %d sectors\n", |
| 486 | p ? "" : "Un-", protected); |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 487 | } else if (rcode == 0) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 488 | printf ("Error: start and/or end address" |
| 489 | " not on sector boundary\n"); |
| 490 | rcode = 1; |
| 491 | } |
| 492 | return rcode; |
| 493 | } |
| 494 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 495 | |
| 496 | /**************************************************/ |
| 497 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 498 | U_BOOT_CMD( |
| 499 | flinfo, 2, 1, do_flinfo, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 500 | "flinfo - print FLASH memory information\n", |
| 501 | "\n - print information for all FLASH memory banks\n" |
| 502 | "flinfo N\n - print information for FLASH memory bank # N\n" |
| 503 | ); |
| 504 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 505 | U_BOOT_CMD( |
| 506 | erase, 3, 1, do_flerase, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 507 | "erase - erase FLASH memory\n", |
| 508 | "start end\n" |
| 509 | " - erase FLASH from addr 'start' to addr 'end'\n" |
| 510 | "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n" |
| 511 | "erase bank N\n - erase FLASH bank # N\n" |
| 512 | "erase all\n - erase all FLASH banks\n" |
| 513 | ); |
| 514 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 515 | U_BOOT_CMD( |
| 516 | protect, 4, 1, do_protect, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 517 | "protect - enable or disable FLASH write protection\n", |
| 518 | "on start end\n" |
| 519 | " - protect FLASH from addr 'start' to addr 'end'\n" |
| 520 | "protect on N:SF[-SL]\n" |
| 521 | " - protect sectors SF-SL in FLASH bank # N\n" |
| 522 | "protect on bank N\n - protect FLASH bank # N\n" |
| 523 | "protect on all\n - protect all FLASH banks\n" |
| 524 | "protect off start end\n" |
| 525 | " - make FLASH from addr 'start' to addr 'end' writable\n" |
| 526 | "protect off N:SF[-SL]\n" |
| 527 | " - make sectors SF-SL writable in FLASH bank # N\n" |
| 528 | "protect off bank N\n - make FLASH bank # N writable\n" |
| 529 | "protect off all\n - make all FLASH banks writable\n" |
| 530 | ); |
| 531 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 532 | #endif /* CFG_CMD_FLASH */ |