Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/nand/nand_util.c |
| 3 | * |
| 4 | * Copyright (C) 2006 by Weiss-Electronic GmbH. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * @author: Guido Classen <clagix@gmail.com> |
| 8 | * @descr: NAND Flash support |
| 9 | * @references: borrowed heavily from Linux mtd-utils code: |
| 10 | * flash_eraseall.c by Arcom Control System Ltd |
| 11 | * nandwrite.c by Steven J. Hill (sjhill@realitydiluted.com) |
| 12 | * and Thomas Gleixner (tglx@linutronix.de) |
| 13 | * |
| 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License version |
| 19 | * 2 as published by the Free Software Foundation. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 29 | * MA 02111-1307 USA |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include <common.h> |
| 34 | |
Jon Loeliger | cb51c0b | 2007-07-09 17:39:42 -0500 | [diff] [blame] | 35 | #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY) |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 36 | |
| 37 | #include <command.h> |
| 38 | #include <watchdog.h> |
| 39 | #include <malloc.h> |
Dirk Behme | 3a6d56c | 2007-08-02 17:42:08 +0200 | [diff] [blame] | 40 | #include <div64.h> |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <nand.h> |
| 43 | #include <jffs2/jffs2.h> |
| 44 | |
| 45 | typedef struct erase_info erase_info_t; |
| 46 | typedef struct mtd_info mtd_info_t; |
| 47 | |
| 48 | /* support only for native endian JFFS2 */ |
| 49 | #define cpu_to_je16(x) (x) |
| 50 | #define cpu_to_je32(x) (x) |
| 51 | |
| 52 | /*****************************************************************************/ |
| 53 | static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip) |
| 54 | { |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * nand_erase_opts: - erase NAND flash with support for various options |
| 60 | * (jffs2 formating) |
| 61 | * |
| 62 | * @param meminfo NAND device to erase |
| 63 | * @param opts options, @see struct nand_erase_options |
| 64 | * @return 0 in case of success |
| 65 | * |
| 66 | * This code is ported from flash_eraseall.c from Linux mtd utils by |
| 67 | * Arcom Control System Ltd. |
| 68 | */ |
| 69 | int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts) |
| 70 | { |
| 71 | struct jffs2_unknown_node cleanmarker; |
| 72 | int clmpos = 0; |
| 73 | int clmlen = 8; |
| 74 | erase_info_t erase; |
| 75 | ulong erase_length; |
| 76 | int isNAND; |
| 77 | int bbtest = 1; |
| 78 | int result; |
| 79 | int percent_complete = -1; |
| 80 | int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL; |
| 81 | const char *mtd_device = meminfo->name; |
| 82 | |
| 83 | memset(&erase, 0, sizeof(erase)); |
| 84 | |
| 85 | erase.mtd = meminfo; |
| 86 | erase.len = meminfo->erasesize; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 87 | erase.addr = opts->offset; |
| 88 | erase_length = opts->length; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 89 | |
| 90 | isNAND = meminfo->type == MTD_NANDFLASH ? 1 : 0; |
| 91 | |
| 92 | if (opts->jffs2) { |
| 93 | cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); |
| 94 | cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); |
| 95 | if (isNAND) { |
| 96 | struct nand_oobinfo *oobinfo = &meminfo->oobinfo; |
| 97 | |
| 98 | /* check for autoplacement */ |
| 99 | if (oobinfo->useecc == MTD_NANDECC_AUTOPLACE) { |
| 100 | /* get the position of the free bytes */ |
| 101 | if (!oobinfo->oobfree[0][1]) { |
| 102 | printf(" Eeep. Autoplacement selected " |
| 103 | "and no empty space in oob\n"); |
| 104 | return -1; |
| 105 | } |
| 106 | clmpos = oobinfo->oobfree[0][0]; |
| 107 | clmlen = oobinfo->oobfree[0][1]; |
| 108 | if (clmlen > 8) |
| 109 | clmlen = 8; |
| 110 | } else { |
| 111 | /* legacy mode */ |
| 112 | switch (meminfo->oobsize) { |
| 113 | case 8: |
| 114 | clmpos = 6; |
| 115 | clmlen = 2; |
| 116 | break; |
| 117 | case 16: |
| 118 | clmpos = 8; |
| 119 | clmlen = 8; |
| 120 | break; |
| 121 | case 64: |
| 122 | clmpos = 16; |
| 123 | clmlen = 8; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | cleanmarker.totlen = cpu_to_je32(8); |
| 129 | } else { |
| 130 | cleanmarker.totlen = |
| 131 | cpu_to_je32(sizeof(struct jffs2_unknown_node)); |
| 132 | } |
| 133 | cleanmarker.hdr_crc = cpu_to_je32( |
| 134 | crc32_no_comp(0, (unsigned char *) &cleanmarker, |
| 135 | sizeof(struct jffs2_unknown_node) - 4)); |
| 136 | } |
| 137 | |
| 138 | /* scrub option allows to erase badblock. To prevent internal |
| 139 | * check from erase() method, set block check method to dummy |
| 140 | * and disable bad block table while erasing. |
| 141 | */ |
| 142 | if (opts->scrub) { |
| 143 | struct nand_chip *priv_nand = meminfo->priv; |
| 144 | |
| 145 | nand_block_bad_old = priv_nand->block_bad; |
| 146 | priv_nand->block_bad = nand_block_bad_scrub; |
| 147 | /* we don't need the bad block table anymore... |
| 148 | * after scrub, there are no bad blocks left! |
| 149 | */ |
| 150 | if (priv_nand->bbt) { |
| 151 | kfree(priv_nand->bbt); |
| 152 | } |
| 153 | priv_nand->bbt = NULL; |
| 154 | } |
| 155 | |
| 156 | for (; |
| 157 | erase.addr < opts->offset + erase_length; |
| 158 | erase.addr += meminfo->erasesize) { |
| 159 | |
| 160 | WATCHDOG_RESET (); |
| 161 | |
| 162 | if (!opts->scrub && bbtest) { |
| 163 | int ret = meminfo->block_isbad(meminfo, erase.addr); |
| 164 | if (ret > 0) { |
| 165 | if (!opts->quiet) |
| 166 | printf("\rSkipping bad block at " |
Wolfgang Denk | 87621bc | 2006-10-12 11:43:47 +0200 | [diff] [blame] | 167 | "0x%08x " |
| 168 | " \n", |
| 169 | erase.addr); |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 170 | continue; |
| 171 | |
| 172 | } else if (ret < 0) { |
| 173 | printf("\n%s: MTD get bad block failed: %d\n", |
| 174 | mtd_device, |
| 175 | ret); |
| 176 | return -1; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | result = meminfo->erase(meminfo, &erase); |
| 181 | if (result != 0) { |
| 182 | printf("\n%s: MTD Erase failure: %d\n", |
| 183 | mtd_device, result); |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | /* format for JFFS2 ? */ |
| 188 | if (opts->jffs2) { |
| 189 | |
| 190 | /* write cleanmarker */ |
| 191 | if (isNAND) { |
| 192 | size_t written; |
| 193 | result = meminfo->write_oob(meminfo, |
| 194 | erase.addr + clmpos, |
| 195 | clmlen, |
| 196 | &written, |
| 197 | (unsigned char *) |
| 198 | &cleanmarker); |
| 199 | if (result != 0) { |
| 200 | printf("\n%s: MTD writeoob failure: %d\n", |
| 201 | mtd_device, result); |
| 202 | continue; |
| 203 | } |
| 204 | } else { |
| 205 | printf("\n%s: this erase routine only supports" |
| 206 | " NAND devices!\n", |
| 207 | mtd_device); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (!opts->quiet) { |
Wolfgang Denk | be5d72d | 2007-08-13 21:57:53 +0200 | [diff] [blame^] | 212 | unsigned long long n =(unsigned long long) |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 213 | (erase.addr+meminfo->erasesize-opts->offset) |
Dirk Behme | 3a6d56c | 2007-08-02 17:42:08 +0200 | [diff] [blame] | 214 | * 100; |
| 215 | int percent = (int)do_div(n, erase_length); |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 216 | |
| 217 | /* output progress message only at whole percent |
| 218 | * steps to reduce the number of messages printed |
| 219 | * on (slow) serial consoles |
| 220 | */ |
| 221 | if (percent != percent_complete) { |
| 222 | percent_complete = percent; |
| 223 | |
| 224 | printf("\rErasing at 0x%x -- %3d%% complete.", |
| 225 | erase.addr, percent); |
| 226 | |
| 227 | if (opts->jffs2 && result == 0) |
| 228 | printf(" Cleanmarker written at 0x%x.", |
| 229 | erase.addr); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | if (!opts->quiet) |
| 234 | printf("\n"); |
| 235 | |
| 236 | if (nand_block_bad_old) { |
| 237 | struct nand_chip *priv_nand = meminfo->priv; |
| 238 | |
| 239 | priv_nand->block_bad = nand_block_bad_old; |
| 240 | priv_nand->scan_bbt(meminfo); |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | #define MAX_PAGE_SIZE 2048 |
| 247 | #define MAX_OOB_SIZE 64 |
| 248 | |
| 249 | /* |
| 250 | * buffer array used for writing data |
| 251 | */ |
| 252 | static unsigned char data_buf[MAX_PAGE_SIZE]; |
| 253 | static unsigned char oob_buf[MAX_OOB_SIZE]; |
| 254 | |
| 255 | /* OOB layouts to pass into the kernel as default */ |
| 256 | static struct nand_oobinfo none_oobinfo = { |
| 257 | .useecc = MTD_NANDECC_OFF, |
| 258 | }; |
| 259 | |
| 260 | static struct nand_oobinfo jffs2_oobinfo = { |
| 261 | .useecc = MTD_NANDECC_PLACE, |
| 262 | .eccbytes = 6, |
| 263 | .eccpos = { 0, 1, 2, 3, 6, 7 } |
| 264 | }; |
| 265 | |
| 266 | static struct nand_oobinfo yaffs_oobinfo = { |
| 267 | .useecc = MTD_NANDECC_PLACE, |
| 268 | .eccbytes = 6, |
| 269 | .eccpos = { 8, 9, 10, 13, 14, 15} |
| 270 | }; |
| 271 | |
| 272 | static struct nand_oobinfo autoplace_oobinfo = { |
| 273 | .useecc = MTD_NANDECC_AUTOPLACE |
| 274 | }; |
| 275 | |
| 276 | /** |
| 277 | * nand_write_opts: - write image to NAND flash with support for various options |
| 278 | * |
| 279 | * @param meminfo NAND device to erase |
| 280 | * @param opts write options (@see nand_write_options) |
| 281 | * @return 0 in case of success |
| 282 | * |
| 283 | * This code is ported from nandwrite.c from Linux mtd utils by |
| 284 | * Steven J. Hill and Thomas Gleixner. |
| 285 | */ |
| 286 | int nand_write_opts(nand_info_t *meminfo, const nand_write_options_t *opts) |
| 287 | { |
| 288 | int imglen = 0; |
| 289 | int pagelen; |
| 290 | int baderaseblock; |
| 291 | int blockstart = -1; |
| 292 | loff_t offs; |
| 293 | int readlen; |
| 294 | int oobinfochanged = 0; |
| 295 | int percent_complete = -1; |
| 296 | struct nand_oobinfo old_oobinfo; |
| 297 | ulong mtdoffset = opts->offset; |
| 298 | ulong erasesize_blockalign; |
| 299 | u_char *buffer = opts->buffer; |
| 300 | size_t written; |
| 301 | int result; |
| 302 | |
| 303 | if (opts->pad && opts->writeoob) { |
| 304 | printf("Can't pad when oob data is present.\n"); |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | /* set erasesize to specified number of blocks - to match |
| 309 | * jffs2 (virtual) block size */ |
| 310 | if (opts->blockalign == 0) { |
| 311 | erasesize_blockalign = meminfo->erasesize; |
| 312 | } else { |
| 313 | erasesize_blockalign = meminfo->erasesize * opts->blockalign; |
| 314 | } |
| 315 | |
| 316 | /* make sure device page sizes are valid */ |
| 317 | if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512) |
| 318 | && !(meminfo->oobsize == 8 && meminfo->oobblock == 256) |
| 319 | && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) { |
| 320 | printf("Unknown flash (not normal NAND)\n"); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | /* read the current oob info */ |
| 325 | memcpy(&old_oobinfo, &meminfo->oobinfo, sizeof(old_oobinfo)); |
| 326 | |
| 327 | /* write without ecc? */ |
| 328 | if (opts->noecc) { |
| 329 | memcpy(&meminfo->oobinfo, &none_oobinfo, |
| 330 | sizeof(meminfo->oobinfo)); |
| 331 | oobinfochanged = 1; |
| 332 | } |
| 333 | |
| 334 | /* autoplace ECC? */ |
| 335 | if (opts->autoplace && (old_oobinfo.useecc != MTD_NANDECC_AUTOPLACE)) { |
| 336 | |
| 337 | memcpy(&meminfo->oobinfo, &autoplace_oobinfo, |
| 338 | sizeof(meminfo->oobinfo)); |
| 339 | oobinfochanged = 1; |
| 340 | } |
| 341 | |
| 342 | /* force OOB layout for jffs2 or yaffs? */ |
| 343 | if (opts->forcejffs2 || opts->forceyaffs) { |
| 344 | struct nand_oobinfo *oobsel = |
| 345 | opts->forcejffs2 ? &jffs2_oobinfo : &yaffs_oobinfo; |
| 346 | |
| 347 | if (meminfo->oobsize == 8) { |
| 348 | if (opts->forceyaffs) { |
| 349 | printf("YAFSS cannot operate on " |
| 350 | "256 Byte page size\n"); |
| 351 | goto restoreoob; |
| 352 | } |
| 353 | /* Adjust number of ecc bytes */ |
| 354 | jffs2_oobinfo.eccbytes = 3; |
| 355 | } |
| 356 | |
| 357 | memcpy(&meminfo->oobinfo, oobsel, sizeof(meminfo->oobinfo)); |
| 358 | } |
| 359 | |
| 360 | /* get image length */ |
| 361 | imglen = opts->length; |
| 362 | pagelen = meminfo->oobblock |
| 363 | + ((opts->writeoob != 0) ? meminfo->oobsize : 0); |
| 364 | |
| 365 | /* check, if file is pagealigned */ |
| 366 | if ((!opts->pad) && ((imglen % pagelen) != 0)) { |
| 367 | printf("Input block length is not page aligned\n"); |
| 368 | goto restoreoob; |
| 369 | } |
| 370 | |
| 371 | /* check, if length fits into device */ |
| 372 | if (((imglen / pagelen) * meminfo->oobblock) |
| 373 | > (meminfo->size - opts->offset)) { |
| 374 | printf("Image %d bytes, NAND page %d bytes, " |
| 375 | "OOB area %u bytes, device size %u bytes\n", |
| 376 | imglen, pagelen, meminfo->oobblock, meminfo->size); |
| 377 | printf("Input block does not fit into device\n"); |
| 378 | goto restoreoob; |
| 379 | } |
| 380 | |
| 381 | if (!opts->quiet) |
| 382 | printf("\n"); |
| 383 | |
| 384 | /* get data from input and write to the device */ |
| 385 | while (imglen && (mtdoffset < meminfo->size)) { |
| 386 | |
| 387 | WATCHDOG_RESET (); |
| 388 | |
| 389 | /* |
| 390 | * new eraseblock, check for bad block(s). Stay in the |
| 391 | * loop to be sure if the offset changes because of |
| 392 | * a bad block, that the next block that will be |
| 393 | * written to is also checked. Thus avoiding errors if |
| 394 | * the block(s) after the skipped block(s) is also bad |
| 395 | * (number of blocks depending on the blockalign |
| 396 | */ |
| 397 | while (blockstart != (mtdoffset & (~erasesize_blockalign+1))) { |
| 398 | blockstart = mtdoffset & (~erasesize_blockalign+1); |
| 399 | offs = blockstart; |
| 400 | baderaseblock = 0; |
| 401 | |
| 402 | /* check all the blocks in an erase block for |
| 403 | * bad blocks */ |
| 404 | do { |
| 405 | int ret = meminfo->block_isbad(meminfo, offs); |
| 406 | |
| 407 | if (ret < 0) { |
| 408 | printf("Bad block check failed\n"); |
| 409 | goto restoreoob; |
| 410 | } |
| 411 | if (ret == 1) { |
| 412 | baderaseblock = 1; |
| 413 | if (!opts->quiet) |
| 414 | printf("\rBad block at 0x%lx " |
| 415 | "in erase block from " |
| 416 | "0x%x will be skipped\n", |
| 417 | (long) offs, |
| 418 | blockstart); |
| 419 | } |
| 420 | |
| 421 | if (baderaseblock) { |
| 422 | mtdoffset = blockstart |
| 423 | + erasesize_blockalign; |
| 424 | } |
| 425 | offs += erasesize_blockalign |
| 426 | / opts->blockalign; |
| 427 | } while (offs < blockstart + erasesize_blockalign); |
| 428 | } |
| 429 | |
| 430 | readlen = meminfo->oobblock; |
| 431 | if (opts->pad && (imglen < readlen)) { |
| 432 | readlen = imglen; |
| 433 | memset(data_buf + readlen, 0xff, |
| 434 | meminfo->oobblock - readlen); |
| 435 | } |
| 436 | |
| 437 | /* read page data from input memory buffer */ |
| 438 | memcpy(data_buf, buffer, readlen); |
| 439 | buffer += readlen; |
| 440 | |
| 441 | if (opts->writeoob) { |
| 442 | /* read OOB data from input memory block, exit |
| 443 | * on failure */ |
| 444 | memcpy(oob_buf, buffer, meminfo->oobsize); |
| 445 | buffer += meminfo->oobsize; |
| 446 | |
| 447 | /* write OOB data first, as ecc will be placed |
| 448 | * in there*/ |
| 449 | result = meminfo->write_oob(meminfo, |
| 450 | mtdoffset, |
| 451 | meminfo->oobsize, |
| 452 | &written, |
| 453 | (unsigned char *) |
| 454 | &oob_buf); |
| 455 | |
| 456 | if (result != 0) { |
| 457 | printf("\nMTD writeoob failure: %d\n", |
| 458 | result); |
| 459 | goto restoreoob; |
| 460 | } |
| 461 | imglen -= meminfo->oobsize; |
| 462 | } |
| 463 | |
| 464 | /* write out the page data */ |
| 465 | result = meminfo->write(meminfo, |
| 466 | mtdoffset, |
| 467 | meminfo->oobblock, |
| 468 | &written, |
| 469 | (unsigned char *) &data_buf); |
| 470 | |
| 471 | if (result != 0) { |
| 472 | printf("writing NAND page at offset 0x%lx failed\n", |
| 473 | mtdoffset); |
| 474 | goto restoreoob; |
| 475 | } |
| 476 | imglen -= readlen; |
| 477 | |
| 478 | if (!opts->quiet) { |
Wolfgang Denk | be5d72d | 2007-08-13 21:57:53 +0200 | [diff] [blame^] | 479 | unsigned long long n = (unsigned long long) |
| 480 | (opts->length-imglen) * 100; |
Dirk Behme | 3a6d56c | 2007-08-02 17:42:08 +0200 | [diff] [blame] | 481 | int percent = (int)do_div(n, opts->length); |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 482 | /* output progress message only at whole percent |
| 483 | * steps to reduce the number of messages printed |
| 484 | * on (slow) serial consoles |
| 485 | */ |
| 486 | if (percent != percent_complete) { |
| 487 | printf("\rWriting data at 0x%x " |
| 488 | "-- %3d%% complete.", |
| 489 | mtdoffset, percent); |
| 490 | percent_complete = percent; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | mtdoffset += meminfo->oobblock; |
| 495 | } |
| 496 | |
| 497 | if (!opts->quiet) |
| 498 | printf("\n"); |
| 499 | |
| 500 | restoreoob: |
| 501 | if (oobinfochanged) { |
| 502 | memcpy(&meminfo->oobinfo, &old_oobinfo, |
| 503 | sizeof(meminfo->oobinfo)); |
| 504 | } |
| 505 | |
| 506 | if (imglen > 0) { |
| 507 | printf("Data did not fit into device, due to bad blocks\n"); |
| 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | /* return happy */ |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * nand_read_opts: - read image from NAND flash with support for various options |
| 517 | * |
| 518 | * @param meminfo NAND device to erase |
| 519 | * @param opts read options (@see struct nand_read_options) |
| 520 | * @return 0 in case of success |
| 521 | * |
| 522 | */ |
| 523 | int nand_read_opts(nand_info_t *meminfo, const nand_read_options_t *opts) |
| 524 | { |
| 525 | int imglen = opts->length; |
| 526 | int pagelen; |
| 527 | int baderaseblock; |
| 528 | int blockstart = -1; |
| 529 | int percent_complete = -1; |
| 530 | loff_t offs; |
| 531 | size_t readlen; |
| 532 | ulong mtdoffset = opts->offset; |
| 533 | u_char *buffer = opts->buffer; |
| 534 | int result; |
| 535 | |
| 536 | /* make sure device page sizes are valid */ |
| 537 | if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512) |
| 538 | && !(meminfo->oobsize == 8 && meminfo->oobblock == 256) |
| 539 | && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) { |
| 540 | printf("Unknown flash (not normal NAND)\n"); |
| 541 | return -1; |
| 542 | } |
| 543 | |
| 544 | pagelen = meminfo->oobblock |
| 545 | + ((opts->readoob != 0) ? meminfo->oobsize : 0); |
| 546 | |
| 547 | /* check, if length is not larger than device */ |
| 548 | if (((imglen / pagelen) * meminfo->oobblock) |
| 549 | > (meminfo->size - opts->offset)) { |
| 550 | printf("Image %d bytes, NAND page %d bytes, " |
| 551 | "OOB area %u bytes, device size %u bytes\n", |
| 552 | imglen, pagelen, meminfo->oobblock, meminfo->size); |
| 553 | printf("Input block is larger than device\n"); |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | if (!opts->quiet) |
| 558 | printf("\n"); |
| 559 | |
| 560 | /* get data from input and write to the device */ |
| 561 | while (imglen && (mtdoffset < meminfo->size)) { |
| 562 | |
| 563 | WATCHDOG_RESET (); |
| 564 | |
| 565 | /* |
| 566 | * new eraseblock, check for bad block(s). Stay in the |
| 567 | * loop to be sure if the offset changes because of |
| 568 | * a bad block, that the next block that will be |
| 569 | * written to is also checked. Thus avoiding errors if |
| 570 | * the block(s) after the skipped block(s) is also bad |
| 571 | * (number of blocks depending on the blockalign |
| 572 | */ |
| 573 | while (blockstart != (mtdoffset & (~meminfo->erasesize+1))) { |
| 574 | blockstart = mtdoffset & (~meminfo->erasesize+1); |
| 575 | offs = blockstart; |
| 576 | baderaseblock = 0; |
| 577 | |
| 578 | /* check all the blocks in an erase block for |
| 579 | * bad blocks */ |
| 580 | do { |
| 581 | int ret = meminfo->block_isbad(meminfo, offs); |
| 582 | |
| 583 | if (ret < 0) { |
| 584 | printf("Bad block check failed\n"); |
| 585 | return -1; |
| 586 | } |
| 587 | if (ret == 1) { |
| 588 | baderaseblock = 1; |
| 589 | if (!opts->quiet) |
| 590 | printf("\rBad block at 0x%lx " |
| 591 | "in erase block from " |
| 592 | "0x%x will be skipped\n", |
| 593 | (long) offs, |
| 594 | blockstart); |
| 595 | } |
| 596 | |
| 597 | if (baderaseblock) { |
| 598 | mtdoffset = blockstart |
| 599 | + meminfo->erasesize; |
| 600 | } |
| 601 | offs += meminfo->erasesize; |
| 602 | |
| 603 | } while (offs < blockstart + meminfo->erasesize); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | /* read page data to memory buffer */ |
| 608 | result = meminfo->read(meminfo, |
| 609 | mtdoffset, |
| 610 | meminfo->oobblock, |
| 611 | &readlen, |
| 612 | (unsigned char *) &data_buf); |
| 613 | |
| 614 | if (result != 0) { |
| 615 | printf("reading NAND page at offset 0x%lx failed\n", |
| 616 | mtdoffset); |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | if (imglen < readlen) { |
| 621 | readlen = imglen; |
| 622 | } |
| 623 | |
| 624 | memcpy(buffer, data_buf, readlen); |
| 625 | buffer += readlen; |
| 626 | imglen -= readlen; |
| 627 | |
| 628 | if (opts->readoob) { |
| 629 | result = meminfo->read_oob(meminfo, |
| 630 | mtdoffset, |
| 631 | meminfo->oobsize, |
| 632 | &readlen, |
| 633 | (unsigned char *) |
| 634 | &oob_buf); |
| 635 | |
| 636 | if (result != 0) { |
| 637 | printf("\nMTD readoob failure: %d\n", |
| 638 | result); |
| 639 | return -1; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | if (imglen < readlen) { |
| 644 | readlen = imglen; |
| 645 | } |
| 646 | |
| 647 | memcpy(buffer, oob_buf, readlen); |
| 648 | |
| 649 | buffer += readlen; |
| 650 | imglen -= readlen; |
| 651 | } |
| 652 | |
| 653 | if (!opts->quiet) { |
Wolfgang Denk | be5d72d | 2007-08-13 21:57:53 +0200 | [diff] [blame^] | 654 | unsigned long long n = (unsigned long long) |
| 655 | (opts->length-imglen) * 100; |
Dirk Behme | 3a6d56c | 2007-08-02 17:42:08 +0200 | [diff] [blame] | 656 | int percent = (int)do_div(n ,opts->length); |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 657 | /* output progress message only at whole percent |
| 658 | * steps to reduce the number of messages printed |
| 659 | * on (slow) serial consoles |
| 660 | */ |
| 661 | if (percent != percent_complete) { |
| 662 | if (!opts->quiet) |
| 663 | printf("\rReading data from 0x%x " |
| 664 | "-- %3d%% complete.", |
| 665 | mtdoffset, percent); |
| 666 | percent_complete = percent; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | mtdoffset += meminfo->oobblock; |
| 671 | } |
| 672 | |
| 673 | if (!opts->quiet) |
| 674 | printf("\n"); |
| 675 | |
| 676 | if (imglen > 0) { |
| 677 | printf("Could not read entire image due to bad blocks\n"); |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | /* return happy */ |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /****************************************************************************** |
| 686 | * Support for locking / unlocking operations of some NAND devices |
| 687 | *****************************************************************************/ |
| 688 | |
| 689 | #define NAND_CMD_LOCK 0x2a |
| 690 | #define NAND_CMD_LOCK_TIGHT 0x2c |
| 691 | #define NAND_CMD_UNLOCK1 0x23 |
| 692 | #define NAND_CMD_UNLOCK2 0x24 |
| 693 | #define NAND_CMD_LOCK_STATUS 0x7a |
| 694 | |
| 695 | /** |
| 696 | * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT |
| 697 | * state |
| 698 | * |
| 699 | * @param meminfo nand mtd instance |
| 700 | * @param tight bring device in lock tight mode |
| 701 | * |
| 702 | * @return 0 on success, -1 in case of error |
| 703 | * |
| 704 | * The lock / lock-tight command only applies to the whole chip. To get some |
| 705 | * parts of the chip lock and others unlocked use the following sequence: |
| 706 | * |
| 707 | * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin) |
| 708 | * - Call nand_unlock() once for each consecutive area to be unlocked |
| 709 | * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1) |
| 710 | * |
| 711 | * If the device is in lock-tight state software can't change the |
| 712 | * current active lock/unlock state of all pages. nand_lock() / nand_unlock() |
| 713 | * calls will fail. It is only posible to leave lock-tight state by |
| 714 | * an hardware signal (low pulse on _WP pin) or by power down. |
| 715 | */ |
| 716 | int nand_lock(nand_info_t *meminfo, int tight) |
| 717 | { |
| 718 | int ret = 0; |
| 719 | int status; |
| 720 | struct nand_chip *this = meminfo->priv; |
| 721 | |
| 722 | /* select the NAND device */ |
| 723 | this->select_chip(meminfo, 0); |
| 724 | |
| 725 | this->cmdfunc(meminfo, |
| 726 | (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK), |
| 727 | -1, -1); |
| 728 | |
| 729 | /* call wait ready function */ |
| 730 | status = this->waitfunc(meminfo, this, FL_WRITING); |
| 731 | |
| 732 | /* see if device thinks it succeeded */ |
| 733 | if (status & 0x01) { |
| 734 | ret = -1; |
| 735 | } |
| 736 | |
| 737 | /* de-select the NAND device */ |
| 738 | this->select_chip(meminfo, -1); |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * nand_get_lock_status: - query current lock state from one page of NAND |
| 744 | * flash |
| 745 | * |
| 746 | * @param meminfo nand mtd instance |
| 747 | * @param offset page address to query (muss be page aligned!) |
| 748 | * |
| 749 | * @return -1 in case of error |
| 750 | * >0 lock status: |
| 751 | * bitfield with the following combinations: |
| 752 | * NAND_LOCK_STATUS_TIGHT: page in tight state |
| 753 | * NAND_LOCK_STATUS_LOCK: page locked |
| 754 | * NAND_LOCK_STATUS_UNLOCK: page unlocked |
| 755 | * |
| 756 | */ |
| 757 | int nand_get_lock_status(nand_info_t *meminfo, ulong offset) |
| 758 | { |
| 759 | int ret = 0; |
| 760 | int chipnr; |
| 761 | int page; |
| 762 | struct nand_chip *this = meminfo->priv; |
| 763 | |
| 764 | /* select the NAND device */ |
| 765 | chipnr = (int)(offset >> this->chip_shift); |
| 766 | this->select_chip(meminfo, chipnr); |
| 767 | |
| 768 | |
| 769 | if ((offset & (meminfo->oobblock - 1)) != 0) { |
| 770 | printf ("nand_get_lock_status: " |
| 771 | "Start address must be beginning of " |
| 772 | "nand page!\n"); |
| 773 | ret = -1; |
| 774 | goto out; |
| 775 | } |
| 776 | |
| 777 | /* check the Lock Status */ |
| 778 | page = (int)(offset >> this->page_shift); |
| 779 | this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask); |
| 780 | |
| 781 | ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT |
| 782 | | NAND_LOCK_STATUS_LOCK |
| 783 | | NAND_LOCK_STATUS_UNLOCK); |
| 784 | |
| 785 | out: |
| 786 | /* de-select the NAND device */ |
| 787 | this->select_chip(meminfo, -1); |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * nand_unlock: - Unlock area of NAND pages |
| 793 | * only one consecutive area can be unlocked at one time! |
| 794 | * |
| 795 | * @param meminfo nand mtd instance |
| 796 | * @param start start byte address |
| 797 | * @param length number of bytes to unlock (must be a multiple of |
| 798 | * page size nand->oobblock) |
| 799 | * |
| 800 | * @return 0 on success, -1 in case of error |
| 801 | */ |
| 802 | int nand_unlock(nand_info_t *meminfo, ulong start, ulong length) |
| 803 | { |
| 804 | int ret = 0; |
| 805 | int chipnr; |
| 806 | int status; |
| 807 | int page; |
| 808 | struct nand_chip *this = meminfo->priv; |
| 809 | printf ("nand_unlock: start: %08x, length: %d!\n", |
| 810 | (int)start, (int)length); |
| 811 | |
| 812 | /* select the NAND device */ |
| 813 | chipnr = (int)(start >> this->chip_shift); |
| 814 | this->select_chip(meminfo, chipnr); |
| 815 | |
| 816 | /* check the WP bit */ |
| 817 | this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1); |
| 818 | if ((this->read_byte(meminfo) & 0x80) == 0) { |
| 819 | printf ("nand_unlock: Device is write protected!\n"); |
| 820 | ret = -1; |
| 821 | goto out; |
| 822 | } |
| 823 | |
| 824 | if ((start & (meminfo->oobblock - 1)) != 0) { |
| 825 | printf ("nand_unlock: Start address must be beginning of " |
| 826 | "nand page!\n"); |
| 827 | ret = -1; |
| 828 | goto out; |
| 829 | } |
| 830 | |
| 831 | if (length == 0 || (length & (meminfo->oobblock - 1)) != 0) { |
| 832 | printf ("nand_unlock: Length must be a multiple of nand page " |
| 833 | "size!\n"); |
| 834 | ret = -1; |
| 835 | goto out; |
| 836 | } |
| 837 | |
| 838 | /* submit address of first page to unlock */ |
| 839 | page = (int)(start >> this->page_shift); |
| 840 | this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask); |
| 841 | |
| 842 | /* submit ADDRESS of LAST page to unlock */ |
| 843 | page += (int)(length >> this->page_shift) - 1; |
| 844 | this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask); |
| 845 | |
| 846 | /* call wait ready function */ |
| 847 | status = this->waitfunc(meminfo, this, FL_WRITING); |
| 848 | /* see if device thinks it succeeded */ |
| 849 | if (status & 0x01) { |
| 850 | /* there was an error */ |
| 851 | ret = -1; |
| 852 | goto out; |
| 853 | } |
| 854 | |
| 855 | out: |
| 856 | /* de-select the NAND device */ |
| 857 | this->select_chip(meminfo, -1); |
| 858 | return ret; |
| 859 | } |
| 860 | |
Jon Loeliger | cb51c0b | 2007-07-09 17:39:42 -0500 | [diff] [blame] | 861 | #endif |