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 | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 29 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 30 | |
wdenk | 2abbe07 | 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 | bdccc4f | 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 | bdccc4f | 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 | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 65 | if ((p = strchr (str, ':')) == NULL) |
| 66 | return 0; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 67 | *p++ = '\0'; |
| 68 | |
wdenk | bdccc4f | 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 | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 75 | str = p; |
| 76 | if ((p = strchr (str, '-')) != NULL) |
| 77 | *p++ = '\0'; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 78 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 79 | first = simple_strtoul (str, &ep, 10); |
| 80 | if (ep == str || *ep != '\0' || first >= fp->sector_count) |
| 81 | return -1; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 82 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 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; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 97 | } |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 98 | |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 99 | /* |
| 100 | * This function computes the start and end addresses for both |
| 101 | * erase and protect commands. The range of the addresses on which |
| 102 | * either of the commands is to operate can be given in two forms: |
| 103 | * 1. <cmd> start end - operate on <'start', 'end') |
| 104 | * 2. <cmd> start +length - operate on <'start', start + length) |
| 105 | * If the second form is used and the end address doesn't fall on the |
| 106 | * sector boundary, than it will be adjusted to the next sector boundary. |
| 107 | * If it isn't in the flash, the function will fail (return -1). |
| 108 | * Input: |
| 109 | * arg1, arg2: address specification (i.e. both command arguments) |
| 110 | * Output: |
| 111 | * addr_first, addr_last: computed address range |
| 112 | * Return: |
| 113 | * 1: success |
| 114 | * -1: failure (bad format, bad address). |
| 115 | */ |
| 116 | static int |
| 117 | addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last) |
| 118 | { |
| 119 | char *ep; |
| 120 | *addr_first = simple_strtoul(arg1, &ep, 16); |
| 121 | if (ep == arg1 || *ep != '\0') |
| 122 | return -1; |
| 123 | |
| 124 | char len_used = 0; /* indicates if the "start +length" form used */ |
| 125 | if (arg2 && *arg2 == '+'){ |
| 126 | len_used = 1; |
| 127 | ++arg2; |
| 128 | } |
| 129 | |
| 130 | *addr_last = simple_strtoul(arg2, &ep, 16); |
| 131 | if (ep == arg2 || *ep != '\0') |
| 132 | return -1; |
| 133 | |
| 134 | if (len_used){ |
| 135 | /* |
| 136 | * *addr_last has the length, compute correct *addr_last |
| 137 | * XXX watch out for the integer overflow! Right now it is |
| 138 | * checked for in both the callers. |
| 139 | */ |
| 140 | *addr_last = *addr_first + *addr_last - 1; |
| 141 | |
| 142 | /* |
| 143 | * It may happen that *addr_last doesn't fall on the sector |
| 144 | * boundary. We want to round such an address to the next |
| 145 | * sector boundary, so that the commands don't fail later on. |
| 146 | */ |
| 147 | |
| 148 | /* find the end addr of the sector where the *addr_last is */ |
| 149 | char found = 0; |
| 150 | ulong bank; |
| 151 | for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank){ |
| 152 | int i; |
| 153 | flash_info_t *info = &flash_info[bank]; |
| 154 | for (i = 0; i < info->sector_count && !found; ++i){ |
| 155 | /* get the end address of the sector */ |
| 156 | ulong sector_end_addr; |
| 157 | if (i == info->sector_count - 1){ |
| 158 | sector_end_addr = |
| 159 | info->start[0] + info->size - 1; |
| 160 | } else { |
| 161 | sector_end_addr = |
| 162 | info->start[i+1] - 1; |
| 163 | } |
| 164 | if (*addr_last <= sector_end_addr && |
| 165 | *addr_last >= info->start[i]){ |
| 166 | /* sector found */ |
| 167 | found = 1; |
| 168 | /* adjust *addr_last if necessary */ |
| 169 | if (*addr_last < sector_end_addr){ |
| 170 | *addr_last = sector_end_addr; |
| 171 | } |
| 172 | } |
| 173 | } /* sector */ |
| 174 | } /* bank */ |
| 175 | if (!found){ |
| 176 | /* error, addres not in flash */ |
| 177 | printf("Error: end address (0x%08lx) not in flash!\n", |
| 178 | *addr_last); |
| 179 | return -1; |
| 180 | } |
| 181 | } /* "start +length" from used */ |
| 182 | |
| 183 | return 1; |
| 184 | } |
| 185 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 186 | static int |
| 187 | flash_fill_sect_ranges (ulong addr_first, ulong addr_last, |
| 188 | int *s_first, int *s_last, |
| 189 | int *s_count ) |
| 190 | { |
| 191 | flash_info_t *info; |
| 192 | ulong bank; |
| 193 | int rcode = 0; |
| 194 | |
| 195 | *s_count = 0; |
| 196 | |
| 197 | for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) { |
| 198 | s_first[bank] = -1; /* first sector to erase */ |
| 199 | s_last [bank] = -1; /* last sector to erase */ |
| 200 | } |
| 201 | |
| 202 | for (bank=0,info=&flash_info[0]; |
| 203 | (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last); |
| 204 | ++bank, ++info) { |
| 205 | ulong b_end; |
| 206 | int sect; |
| 207 | short s_end; |
| 208 | |
| 209 | if (info->flash_id == FLASH_UNKNOWN) { |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | b_end = info->start[0] + info->size - 1; /* bank end addr */ |
| 214 | s_end = info->sector_count - 1; /* last sector */ |
| 215 | |
| 216 | |
| 217 | for (sect=0; sect < info->sector_count; ++sect) { |
| 218 | ulong end; /* last address in current sect */ |
| 219 | |
| 220 | end = (sect == s_end) ? b_end : info->start[sect + 1] - 1; |
| 221 | |
| 222 | if (addr_first > end) |
| 223 | continue; |
| 224 | if (addr_last < info->start[sect]) |
| 225 | continue; |
| 226 | |
| 227 | if (addr_first == info->start[sect]) { |
| 228 | s_first[bank] = sect; |
| 229 | } |
| 230 | if (addr_last == end) { |
| 231 | s_last[bank] = sect; |
| 232 | } |
| 233 | } |
| 234 | if (s_first[bank] >= 0) { |
| 235 | if (s_last[bank] < 0) { |
| 236 | if (addr_last > b_end) { |
| 237 | s_last[bank] = s_end; |
| 238 | } else { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 239 | puts ("Error: end address" |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 240 | " not on sector boundary\n"); |
| 241 | rcode = 1; |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | if (s_last[bank] < s_first[bank]) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 246 | puts ("Error: end sector" |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 247 | " precedes start sector\n"); |
| 248 | rcode = 1; |
| 249 | break; |
| 250 | } |
| 251 | sect = s_last[bank]; |
| 252 | addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1]; |
| 253 | (*s_count) += s_last[bank] - s_first[bank] + 1; |
wdenk | e2ffd59 | 2004-12-31 09:32:47 +0000 | [diff] [blame] | 254 | } else if (addr_first >= info->start[0] && addr_first < b_end) { |
| 255 | puts ("Error: start address not on sector boundary\n"); |
| 256 | rcode = 1; |
| 257 | break; |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 258 | } else if (s_last[bank] >= 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 259 | puts ("Error: cannot span across banks when they are" |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 260 | " mapped in reverse order\n"); |
| 261 | rcode = 1; |
| 262 | break; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | return rcode; |
| 267 | } |
| 268 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 269 | int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 270 | { |
| 271 | ulong bank; |
| 272 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 273 | #ifdef CONFIG_HAS_DATAFLASH |
| 274 | dataflash_print_info(); |
| 275 | #endif |
| 276 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 277 | if (argc == 1) { /* print info for all FLASH banks */ |
| 278 | for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) { |
| 279 | printf ("\nBank # %ld: ", bank+1); |
| 280 | |
| 281 | flash_print_info (&flash_info[bank]); |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | bank = simple_strtoul(argv[1], NULL, 16); |
| 287 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 288 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 289 | CFG_MAX_FLASH_BANKS); |
| 290 | return 1; |
| 291 | } |
| 292 | printf ("\nBank # %ld: ", bank); |
| 293 | flash_print_info (&flash_info[bank-1]); |
| 294 | return 0; |
| 295 | } |
| 296 | int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 297 | { |
| 298 | flash_info_t *info; |
| 299 | ulong bank, addr_first, addr_last; |
| 300 | int n, sect_first, sect_last; |
| 301 | int rcode = 0; |
| 302 | |
| 303 | if (argc < 2) { |
| 304 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | if (strcmp(argv[1], "all") == 0) { |
| 309 | for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) { |
| 310 | printf ("Erase Flash Bank # %ld ", bank); |
| 311 | info = &flash_info[bank-1]; |
| 312 | rcode = flash_erase (info, 0, info->sector_count-1); |
| 313 | } |
| 314 | return rcode; |
| 315 | } |
| 316 | |
| 317 | if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) { |
| 318 | if (n < 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 319 | puts ("Bad sector specification\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 320 | return 1; |
| 321 | } |
| 322 | printf ("Erase Flash Sectors %d-%d in Bank # %d ", |
| 323 | sect_first, sect_last, (info-flash_info)+1); |
| 324 | rcode = flash_erase(info, sect_first, sect_last); |
| 325 | return rcode; |
| 326 | } |
| 327 | |
| 328 | if (argc != 3) { |
| 329 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | if (strcmp(argv[1], "bank") == 0) { |
| 334 | bank = simple_strtoul(argv[2], NULL, 16); |
| 335 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 336 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 337 | CFG_MAX_FLASH_BANKS); |
| 338 | return 1; |
| 339 | } |
| 340 | printf ("Erase Flash Bank # %ld ", bank); |
| 341 | info = &flash_info[bank-1]; |
| 342 | rcode = flash_erase (info, 0, info->sector_count-1); |
| 343 | return rcode; |
| 344 | } |
| 345 | |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 346 | if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){ |
| 347 | printf ("Bad address format\n"); |
| 348 | return 1; |
| 349 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 350 | |
| 351 | if (addr_first >= addr_last) { |
| 352 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 353 | return 1; |
| 354 | } |
| 355 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 356 | rcode = flash_sect_erase(addr_first, addr_last); |
| 357 | return rcode; |
| 358 | } |
| 359 | |
| 360 | int flash_sect_erase (ulong addr_first, ulong addr_last) |
| 361 | { |
| 362 | flash_info_t *info; |
| 363 | ulong bank; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 364 | int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS]; |
| 365 | int erased = 0; |
| 366 | int planned; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 367 | int rcode = 0; |
| 368 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 369 | rcode = flash_fill_sect_ranges (addr_first, addr_last, |
| 370 | s_first, s_last, &planned ); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 371 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 372 | if (planned && (rcode == 0)) { |
| 373 | for (bank=0,info=&flash_info[0]; |
| 374 | (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0); |
| 375 | ++bank, ++info) { |
| 376 | if (s_first[bank]>=0) { |
| 377 | erased += s_last[bank] - s_first[bank] + 1; |
wdenk | 013dc8d | 2003-08-07 14:52:18 +0000 | [diff] [blame] | 378 | debug ("Erase Flash from 0x%08lx to 0x%08lx " |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 379 | "in Bank # %ld ", |
| 380 | info->start[s_first[bank]], |
| 381 | (s_last[bank] == info->sector_count) ? |
| 382 | info->start[0] + info->size - 1: |
| 383 | info->start[s_last[bank]+1] - 1, |
| 384 | bank+1); |
| 385 | rcode = flash_erase (info, s_first[bank], s_last[bank]); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 388 | printf ("Erased %d sectors\n", erased); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 389 | } else if (rcode == 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 390 | puts ("Error: start and/or end address" |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 391 | " not on sector boundary\n"); |
| 392 | rcode = 1; |
| 393 | } |
| 394 | return rcode; |
| 395 | } |
| 396 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 397 | int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 398 | { |
| 399 | flash_info_t *info; |
| 400 | ulong bank, addr_first, addr_last; |
| 401 | int i, p, n, sect_first, sect_last; |
| 402 | int rcode = 0; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 403 | #ifdef CONFIG_HAS_DATAFLASH |
| 404 | int status; |
| 405 | #endif |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 406 | if (argc < 3) { |
| 407 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 408 | return 1; |
| 409 | } |
| 410 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 411 | if (strcmp(argv[1], "off") == 0) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 412 | p = 0; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 413 | } else if (strcmp(argv[1], "on") == 0) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 414 | p = 1; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 415 | } else { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 416 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 417 | return 1; |
| 418 | } |
| 419 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 420 | #ifdef CONFIG_HAS_DATAFLASH |
| 421 | if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) { |
| 422 | addr_first = simple_strtoul(argv[2], NULL, 16); |
| 423 | addr_last = simple_strtoul(argv[3], NULL, 16); |
| 424 | |
| 425 | if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) { |
| 426 | status = dataflash_real_protect(p,addr_first,addr_last); |
| 427 | if (status < 0){ |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 428 | puts ("Bad DataFlash sector specification\n"); |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 429 | return 1; |
| 430 | } |
| 431 | printf("%sProtect %d DataFlash Sectors\n", |
| 432 | p ? "" : "Un-", status); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | } |
| 436 | #endif |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 437 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 438 | if (strcmp(argv[2], "all") == 0) { |
| 439 | for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) { |
| 440 | info = &flash_info[bank-1]; |
| 441 | if (info->flash_id == FLASH_UNKNOWN) { |
| 442 | continue; |
| 443 | } |
| 444 | printf ("%sProtect Flash Bank # %ld\n", |
| 445 | p ? "" : "Un-", bank); |
| 446 | |
| 447 | for (i=0; i<info->sector_count; ++i) { |
| 448 | #if defined(CFG_FLASH_PROTECTION) |
| 449 | if (flash_real_protect(info, i, p)) |
| 450 | rcode = 1; |
| 451 | putc ('.'); |
| 452 | #else |
| 453 | info->protect[i] = p; |
| 454 | #endif /* CFG_FLASH_PROTECTION */ |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | #if defined(CFG_FLASH_PROTECTION) |
| 459 | if (!rcode) puts (" done\n"); |
| 460 | #endif /* CFG_FLASH_PROTECTION */ |
| 461 | |
| 462 | return rcode; |
| 463 | } |
| 464 | |
| 465 | if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) { |
| 466 | if (n < 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 467 | puts ("Bad sector specification\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 468 | return 1; |
| 469 | } |
| 470 | printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", |
| 471 | p ? "" : "Un-", sect_first, sect_last, |
| 472 | (info-flash_info)+1); |
| 473 | for (i = sect_first; i <= sect_last; i++) { |
| 474 | #if defined(CFG_FLASH_PROTECTION) |
| 475 | if (flash_real_protect(info, i, p)) |
| 476 | rcode = 1; |
| 477 | putc ('.'); |
| 478 | #else |
| 479 | info->protect[i] = p; |
| 480 | #endif /* CFG_FLASH_PROTECTION */ |
| 481 | } |
| 482 | |
| 483 | #if defined(CFG_FLASH_PROTECTION) |
| 484 | if (!rcode) puts (" done\n"); |
| 485 | #endif /* CFG_FLASH_PROTECTION */ |
| 486 | |
| 487 | return rcode; |
| 488 | } |
| 489 | |
| 490 | if (argc != 4) { |
| 491 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 492 | return 1; |
| 493 | } |
| 494 | |
| 495 | if (strcmp(argv[2], "bank") == 0) { |
| 496 | bank = simple_strtoul(argv[3], NULL, 16); |
| 497 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 498 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 499 | CFG_MAX_FLASH_BANKS); |
| 500 | return 1; |
| 501 | } |
| 502 | printf ("%sProtect Flash Bank # %ld\n", |
| 503 | p ? "" : "Un-", bank); |
| 504 | info = &flash_info[bank-1]; |
| 505 | |
| 506 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 507 | puts ("missing or unknown FLASH type\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 508 | return 1; |
| 509 | } |
| 510 | for (i=0; i<info->sector_count; ++i) { |
| 511 | #if defined(CFG_FLASH_PROTECTION) |
| 512 | if (flash_real_protect(info, i, p)) |
| 513 | rcode = 1; |
| 514 | putc ('.'); |
| 515 | #else |
| 516 | info->protect[i] = p; |
| 517 | #endif /* CFG_FLASH_PROTECTION */ |
| 518 | } |
| 519 | |
| 520 | #if defined(CFG_FLASH_PROTECTION) |
| 521 | if (!rcode) puts (" done\n"); |
| 522 | #endif /* CFG_FLASH_PROTECTION */ |
| 523 | |
| 524 | return rcode; |
| 525 | } |
| 526 | |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 527 | if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){ |
| 528 | printf("Bad address format\n"); |
| 529 | return 1; |
| 530 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 531 | |
| 532 | if (addr_first >= addr_last) { |
| 533 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 534 | return 1; |
| 535 | } |
| 536 | rcode = flash_sect_protect (p, addr_first, addr_last); |
| 537 | return rcode; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | int flash_sect_protect (int p, ulong addr_first, ulong addr_last) |
| 542 | { |
| 543 | flash_info_t *info; |
| 544 | ulong bank; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 545 | int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS]; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 546 | int protected, i; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 547 | int planned; |
| 548 | int rcode; |
| 549 | |
| 550 | 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] | 551 | |
| 552 | protected = 0; |
| 553 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 554 | if (planned && (rcode == 0)) { |
| 555 | for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) { |
| 556 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 557 | continue; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 558 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 559 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 560 | if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) { |
stroese | abcac87 | 2003-12-09 14:58:22 +0000 | [diff] [blame] | 561 | debug ("%sProtecting sectors %d..%d in bank %ld\n", |
| 562 | p ? "" : "Un-", |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 563 | s_first[bank], s_last[bank], bank+1); |
| 564 | protected += s_last[bank] - s_first[bank] + 1; |
| 565 | for (i=s_first[bank]; i<=s_last[bank]; ++i) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 566 | #if defined(CFG_FLASH_PROTECTION) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 567 | if (flash_real_protect(info, i, p)) |
| 568 | rcode = 1; |
| 569 | putc ('.'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 570 | #else |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 571 | info->protect[i] = p; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 572 | #endif /* CFG_FLASH_PROTECTION */ |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 573 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 574 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 575 | #if defined(CFG_FLASH_PROTECTION) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 576 | if (!rcode) putc ('\n'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 577 | #endif /* CFG_FLASH_PROTECTION */ |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 578 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 579 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 580 | printf ("%sProtected %d sectors\n", |
| 581 | p ? "" : "Un-", protected); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 582 | } else if (rcode == 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 583 | puts ("Error: start and/or end address" |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 584 | " not on sector boundary\n"); |
| 585 | rcode = 1; |
| 586 | } |
| 587 | return rcode; |
| 588 | } |
| 589 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 590 | |
| 591 | /**************************************************/ |
| 592 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 593 | U_BOOT_CMD( |
| 594 | flinfo, 2, 1, do_flinfo, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 595 | "flinfo - print FLASH memory information\n", |
| 596 | "\n - print information for all FLASH memory banks\n" |
| 597 | "flinfo N\n - print information for FLASH memory bank # N\n" |
| 598 | ); |
| 599 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 600 | U_BOOT_CMD( |
| 601 | erase, 3, 1, do_flerase, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 602 | "erase - erase FLASH memory\n", |
| 603 | "start end\n" |
| 604 | " - erase FLASH from addr 'start' to addr 'end'\n" |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 605 | "erase start +len\n" |
| 606 | " - erase FLASH from addr 'start' to the end of sect " |
| 607 | "w/addr 'start'+'len'-1\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 608 | "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n" |
| 609 | "erase bank N\n - erase FLASH bank # N\n" |
| 610 | "erase all\n - erase all FLASH banks\n" |
| 611 | ); |
| 612 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 613 | U_BOOT_CMD( |
| 614 | protect, 4, 1, do_protect, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 615 | "protect - enable or disable FLASH write protection\n", |
| 616 | "on start end\n" |
| 617 | " - protect FLASH from addr 'start' to addr 'end'\n" |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 618 | "protect on start +len\n" |
| 619 | " - protect FLASH from addr 'start' to end of sect " |
| 620 | "w/addr 'start'+'len'-1\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 621 | "protect on N:SF[-SL]\n" |
| 622 | " - protect sectors SF-SL in FLASH bank # N\n" |
| 623 | "protect on bank N\n - protect FLASH bank # N\n" |
| 624 | "protect on all\n - protect all FLASH banks\n" |
| 625 | "protect off start end\n" |
| 626 | " - make FLASH from addr 'start' to addr 'end' writable\n" |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 627 | "protect off start +len\n" |
| 628 | " - make FLASH from addr 'start' to end of sect " |
| 629 | "w/addr 'start'+'len'-1 wrtable\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 630 | "protect off N:SF[-SL]\n" |
| 631 | " - make sectors SF-SL writable in FLASH bank # N\n" |
| 632 | "protect off bank N\n - make FLASH bank # N writable\n" |
| 633 | "protect off all\n - make all FLASH banks writable\n" |
| 634 | ); |
| 635 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 636 | #endif /* CFG_CMD_FLASH */ |