b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * drivers/mtd/nand.c |
| 3 | * |
| 4 | * Overview: |
| 5 | * This is the generic MTD driver for NAND flash devices. It should be |
| 6 | * capable of working with almost all NAND chips currently available. |
| 7 | * Basic support for AG-AND chips is provided. |
| 8 | * |
| 9 | * Additional technical information is available on |
| 10 | * http://www.linux-mtd.infradead.org/doc/nand.html |
| 11 | * |
| 12 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
| 13 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
| 14 | * |
| 15 | * Credits: |
| 16 | * David Woodhouse for adding multichip support |
| 17 | * |
| 18 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 19 | * rework for 2K page size chips |
| 20 | * |
| 21 | * TODO: |
| 22 | * Enable cached programming for 2k page size chips |
| 23 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
| 24 | * if we have HW ECC support. |
| 25 | * The AG-AND chips have nice features for speed improvement, |
| 26 | * which are not supported yet. Read / program 4 pages in one go. |
| 27 | * BBT table is not serialized, has to be fixed |
| 28 | * |
| 29 | * This program is free software; you can redistribute it and/or modify |
| 30 | * it under the terms of the GNU General Public License version 2 as |
| 31 | * published by the Free Software Foundation. |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | #include <common.h> |
| 36 | |
| 37 | #define ENOTSUPP 524 /* Operation is not supported */ |
| 38 | |
| 39 | #include <malloc.h> |
| 40 | #include <watchdog.h> |
| 41 | #include <linux/err.h> |
| 42 | #include <linux/compat.h> |
| 43 | #include <linux/mtd/mtd.h> |
| 44 | #include <linux/mtd/nand.h> |
| 45 | #include <linux/mtd/nand_ecc.h> |
| 46 | #include <linux/mtd/nand_bch.h> |
| 47 | |
| 48 | #ifdef CONFIG_MTD_PARTITIONS |
| 49 | #include <linux/mtd/partitions.h> |
| 50 | #endif |
| 51 | |
| 52 | #include <asm/io.h> |
| 53 | #include <asm/errno.h> |
| 54 | |
| 55 | /* |
| 56 | * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting |
| 57 | * a flash. NAND flash is initialized prior to interrupts so standard timers |
| 58 | * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value |
| 59 | * which is greater than (max NAND reset time / NAND status read time). |
| 60 | * A conservative default of 200000 (500 us / 25 ns) is used as a default. |
| 61 | */ |
| 62 | #ifndef CONFIG_SYS_NAND_RESET_CNT |
| 63 | #define CONFIG_SYS_NAND_RESET_CNT 200000 |
| 64 | #endif |
| 65 | |
| 66 | /* Define default oob placement schemes for large and small page devices */ |
| 67 | static struct nand_ecclayout nand_oob_8 = { |
| 68 | .eccbytes = 3, |
| 69 | .eccpos = {0, 1, 2}, |
| 70 | .oobfree = { |
| 71 | {.offset = 3, |
| 72 | .length = 2}, |
| 73 | {.offset = 6, |
| 74 | .length = 2} } |
| 75 | }; |
| 76 | |
| 77 | static struct nand_ecclayout nand_oob_16 = { |
| 78 | .eccbytes = 6, |
| 79 | .eccpos = {0, 1, 2, 3, 6, 7}, |
| 80 | .oobfree = { |
| 81 | {.offset = 8, |
| 82 | . length = 8} } |
| 83 | }; |
| 84 | |
| 85 | static struct nand_ecclayout nand_oob_64 = { |
| 86 | .eccbytes = 24, |
| 87 | .eccpos = { |
| 88 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 89 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 90 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 91 | .oobfree = { |
| 92 | {.offset = 2, |
| 93 | .length = 38} } |
| 94 | }; |
| 95 | |
| 96 | static struct nand_ecclayout nand_oob_128 = { |
| 97 | .eccbytes = 48, |
| 98 | .eccpos = { |
| 99 | 80, 81, 82, 83, 84, 85, 86, 87, |
| 100 | 88, 89, 90, 91, 92, 93, 94, 95, |
| 101 | 96, 97, 98, 99, 100, 101, 102, 103, |
| 102 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 103 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 104 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 105 | .oobfree = { |
| 106 | {.offset = 2, |
| 107 | .length = 78} } |
| 108 | }; |
| 109 | |
| 110 | static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, |
| 111 | int new_state); |
| 112 | |
| 113 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 114 | struct mtd_oob_ops *ops); |
| 115 | |
| 116 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *this); |
| 117 | |
| 118 | static int check_offs_len(struct mtd_info *mtd, |
| 119 | loff_t ofs, uint64_t len) |
| 120 | { |
| 121 | struct nand_chip *chip = mtd->priv; |
| 122 | int ret = 0; |
| 123 | |
| 124 | /* Start address must align on block boundary */ |
| 125 | if (ofs & ((1 << chip->phys_erase_shift) - 1)) { |
| 126 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__); |
| 127 | ret = -EINVAL; |
| 128 | } |
| 129 | |
| 130 | /* Length must align on block boundary */ |
| 131 | if (len & ((1 << chip->phys_erase_shift) - 1)) { |
| 132 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n", |
| 133 | __func__); |
| 134 | ret = -EINVAL; |
| 135 | } |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * nand_release_device - [GENERIC] release chip |
| 142 | * @mtd: MTD device structure |
| 143 | * |
| 144 | * Deselect, release chip lock and wake up anyone waiting on the device. |
| 145 | */ |
| 146 | static void nand_release_device(struct mtd_info *mtd) |
| 147 | { |
| 148 | struct nand_chip *chip = mtd->priv; |
| 149 | |
| 150 | /* De-select the NAND device */ |
| 151 | chip->select_chip(mtd, -1); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * nand_read_byte - [DEFAULT] read one byte from the chip |
| 156 | * @mtd: MTD device structure |
| 157 | * |
| 158 | * Default read function for 8bit buswidth. |
| 159 | */ |
| 160 | uint8_t nand_read_byte(struct mtd_info *mtd) |
| 161 | { |
| 162 | struct nand_chip *chip = mtd->priv; |
| 163 | return readb(chip->IO_ADDR_R); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip |
| 168 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
| 169 | * @mtd: MTD device structure |
| 170 | * |
| 171 | * Default read function for 16bit buswidth with endianness conversion. |
| 172 | * |
| 173 | */ |
| 174 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
| 175 | { |
| 176 | struct nand_chip *chip = mtd->priv; |
| 177 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * nand_read_word - [DEFAULT] read one word from the chip |
| 182 | * @mtd: MTD device structure |
| 183 | * |
| 184 | * Default read function for 16bit buswidth without endianness conversion. |
| 185 | */ |
| 186 | static u16 nand_read_word(struct mtd_info *mtd) |
| 187 | { |
| 188 | struct nand_chip *chip = mtd->priv; |
| 189 | return readw(chip->IO_ADDR_R); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * nand_select_chip - [DEFAULT] control CE line |
| 194 | * @mtd: MTD device structure |
| 195 | * @chipnr: chipnumber to select, -1 for deselect |
| 196 | * |
| 197 | * Default select function for 1 chip devices. |
| 198 | */ |
| 199 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
| 200 | { |
| 201 | struct nand_chip *chip = mtd->priv; |
| 202 | |
| 203 | switch (chipnr) { |
| 204 | case -1: |
| 205 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
| 206 | break; |
| 207 | case 0: |
| 208 | break; |
| 209 | |
| 210 | default: |
| 211 | BUG(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * nand_write_buf - [DEFAULT] write buffer to chip |
| 217 | * @mtd: MTD device structure |
| 218 | * @buf: data buffer |
| 219 | * @len: number of bytes to write |
| 220 | * |
| 221 | * Default write function for 8bit buswidth. |
| 222 | */ |
| 223 | void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
| 224 | { |
| 225 | int i; |
| 226 | struct nand_chip *chip = mtd->priv; |
| 227 | |
| 228 | for (i = 0; i < len; i++) |
| 229 | writeb(buf[i], chip->IO_ADDR_W); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * nand_read_buf - [DEFAULT] read chip data into buffer |
| 234 | * @mtd: MTD device structure |
| 235 | * @buf: buffer to store date |
| 236 | * @len: number of bytes to read |
| 237 | * |
| 238 | * Default read function for 8bit buswidth. |
| 239 | */ |
| 240 | void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 241 | { |
| 242 | int i; |
| 243 | struct nand_chip *chip = mtd->priv; |
| 244 | |
| 245 | for (i = 0; i < len; i++) |
| 246 | buf[i] = readb(chip->IO_ADDR_R); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * nand_verify_buf - [DEFAULT] Verify chip data against buffer |
| 251 | * @mtd: MTD device structure |
| 252 | * @buf: buffer containing the data to compare |
| 253 | * @len: number of bytes to compare |
| 254 | * |
| 255 | * Default verify function for 8bit buswidth. |
| 256 | */ |
| 257 | static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
| 258 | { |
| 259 | int i; |
| 260 | struct nand_chip *chip = mtd->priv; |
| 261 | |
| 262 | for (i = 0; i < len; i++) |
| 263 | if (buf[i] != readb(chip->IO_ADDR_R)) |
| 264 | return -EFAULT; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
| 270 | * @mtd: MTD device structure |
| 271 | * @buf: data buffer |
| 272 | * @len: number of bytes to write |
| 273 | * |
| 274 | * Default write function for 16bit buswidth. |
| 275 | */ |
| 276 | void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
| 277 | { |
| 278 | int i; |
| 279 | struct nand_chip *chip = mtd->priv; |
| 280 | u16 *p = (u16 *) buf; |
| 281 | len >>= 1; |
| 282 | |
| 283 | for (i = 0; i < len; i++) |
| 284 | writew(p[i], chip->IO_ADDR_W); |
| 285 | |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
| 290 | * @mtd: MTD device structure |
| 291 | * @buf: buffer to store date |
| 292 | * @len: number of bytes to read |
| 293 | * |
| 294 | * Default read function for 16bit buswidth. |
| 295 | */ |
| 296 | void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
| 297 | { |
| 298 | int i; |
| 299 | struct nand_chip *chip = mtd->priv; |
| 300 | u16 *p = (u16 *) buf; |
| 301 | len >>= 1; |
| 302 | |
| 303 | for (i = 0; i < len; i++) |
| 304 | p[i] = readw(chip->IO_ADDR_R); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer |
| 309 | * @mtd: MTD device structure |
| 310 | * @buf: buffer containing the data to compare |
| 311 | * @len: number of bytes to compare |
| 312 | * |
| 313 | * Default verify function for 16bit buswidth. |
| 314 | */ |
| 315 | static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
| 316 | { |
| 317 | int i; |
| 318 | struct nand_chip *chip = mtd->priv; |
| 319 | u16 *p = (u16 *) buf; |
| 320 | len >>= 1; |
| 321 | |
| 322 | for (i = 0; i < len; i++) |
| 323 | if (p[i] != readw(chip->IO_ADDR_R)) |
| 324 | return -EFAULT; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
| 331 | * @mtd: MTD device structure |
| 332 | * @ofs: offset from device start |
| 333 | * @getchip: 0, if the chip is already selected |
| 334 | * |
| 335 | * Check, if the block is bad. |
| 336 | */ |
| 337 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) |
| 338 | { |
| 339 | int page, chipnr, res = 0, i = 0; |
| 340 | struct nand_chip *chip = mtd->priv; |
| 341 | u16 bad; |
| 342 | |
| 343 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 344 | ofs += mtd->erasesize - mtd->writesize; |
| 345 | |
| 346 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 347 | |
| 348 | if (getchip) { |
| 349 | chipnr = (int)(ofs >> chip->chip_shift); |
| 350 | |
| 351 | nand_get_device(chip, mtd, FL_READING); |
| 352 | |
| 353 | /* Select the NAND device */ |
| 354 | chip->select_chip(mtd, chipnr); |
| 355 | } |
| 356 | |
| 357 | do { |
| 358 | if (chip->options & NAND_BUSWIDTH_16) { |
| 359 | chip->cmdfunc(mtd, NAND_CMD_READOOB, |
| 360 | chip->badblockpos & 0xFE, page); |
| 361 | bad = cpu_to_le16(chip->read_word(mtd)); |
| 362 | if (chip->badblockpos & 0x1) |
| 363 | bad >>= 8; |
| 364 | else |
| 365 | bad &= 0xFF; |
| 366 | } else { |
| 367 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, |
| 368 | page); |
| 369 | bad = chip->read_byte(mtd); |
| 370 | } |
| 371 | |
| 372 | if (likely(chip->badblockbits == 8)) |
| 373 | res = bad != 0xFF; |
| 374 | else |
| 375 | res = hweight8(bad) < chip->badblockbits; |
| 376 | ofs += mtd->writesize; |
| 377 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 378 | i++; |
| 379 | } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); |
| 380 | |
| 381 | if (getchip) |
| 382 | nand_release_device(mtd); |
| 383 | |
| 384 | return res; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * nand_default_block_markbad - [DEFAULT] mark a block bad |
| 389 | * @mtd: MTD device structure |
| 390 | * @ofs: offset from device start |
| 391 | * |
| 392 | * This is the default implementation, which can be overridden by a hardware |
| 393 | * specific driver. We try operations in the following order, according to our |
| 394 | * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH): |
| 395 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
| 396 | * (2) update in-memory BBT |
| 397 | * (3) write bad block marker to OOB area of affected block |
| 398 | * (4) update flash-based BBT |
| 399 | * Note that we retain the first error encountered in (3) or (4), finish the |
| 400 | * procedures, and dump the error in the end. |
| 401 | */ |
| 402 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 403 | { |
| 404 | struct nand_chip *chip = mtd->priv; |
| 405 | uint8_t buf[2] = { 0, 0 }; |
| 406 | int block, res, ret = 0, i = 0; |
| 407 | int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM); |
| 408 | |
| 409 | if (write_oob) { |
| 410 | struct erase_info einfo; |
| 411 | |
| 412 | /* Attempt erase before marking OOB */ |
| 413 | memset(&einfo, 0, sizeof(einfo)); |
| 414 | einfo.mtd = mtd; |
| 415 | einfo.addr = ofs; |
| 416 | einfo.len = 1 << chip->phys_erase_shift; |
| 417 | nand_erase_nand(mtd, &einfo, 0); |
| 418 | } |
| 419 | |
| 420 | /* Get block number */ |
| 421 | block = (int)(ofs >> chip->bbt_erase_shift); |
| 422 | /* Mark block bad in memory-based BBT */ |
| 423 | if (chip->bbt) |
| 424 | chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); |
| 425 | |
| 426 | /* Write bad block marker to OOB */ |
| 427 | if (write_oob) { |
| 428 | struct mtd_oob_ops ops; |
| 429 | loff_t wr_ofs = ofs; |
| 430 | |
| 431 | nand_get_device(chip, mtd, FL_WRITING); |
| 432 | |
| 433 | ops.datbuf = NULL; |
| 434 | ops.oobbuf = buf; |
| 435 | ops.ooboffs = chip->badblockpos; |
| 436 | if (chip->options & NAND_BUSWIDTH_16) { |
| 437 | ops.ooboffs &= ~0x01; |
| 438 | ops.len = ops.ooblen = 2; |
| 439 | } else { |
| 440 | ops.len = ops.ooblen = 1; |
| 441 | } |
| 442 | ops.mode = MTD_OPS_PLACE_OOB; |
| 443 | |
| 444 | /* Write to first/last page(s) if necessary */ |
| 445 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 446 | wr_ofs += mtd->erasesize - mtd->writesize; |
| 447 | do { |
| 448 | res = nand_do_write_oob(mtd, wr_ofs, &ops); |
| 449 | if (!ret) |
| 450 | ret = res; |
| 451 | |
| 452 | i++; |
| 453 | wr_ofs += mtd->writesize; |
| 454 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 455 | |
| 456 | nand_release_device(mtd); |
| 457 | } |
| 458 | |
| 459 | /* Update flash-based bad block table */ |
| 460 | if (chip->bbt_options & NAND_BBT_USE_FLASH) { |
| 461 | res = nand_update_bbt(mtd, ofs); |
| 462 | if (!ret) |
| 463 | ret = res; |
| 464 | } |
| 465 | |
| 466 | if (!ret) |
| 467 | mtd->ecc_stats.badblocks++; |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * nand_check_wp - [GENERIC] check if the chip is write protected |
| 474 | * @mtd: MTD device structure |
| 475 | * |
| 476 | * Check, if the device is write protected. The function expects, that the |
| 477 | * device is already selected. |
| 478 | */ |
| 479 | static int nand_check_wp(struct mtd_info *mtd) |
| 480 | { |
| 481 | struct nand_chip *chip = mtd->priv; |
| 482 | |
| 483 | /* Broken xD cards report WP despite being writable */ |
| 484 | if (chip->options & NAND_BROKEN_XD) |
| 485 | return 0; |
| 486 | |
| 487 | /* Check the WP bit */ |
| 488 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 489 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 494 | * @mtd: MTD device structure |
| 495 | * @ofs: offset from device start |
| 496 | * @getchip: 0, if the chip is already selected |
| 497 | * @allowbbt: 1, if its allowed to access the bbt area |
| 498 | * |
| 499 | * Check, if the block is bad. Either by reading the bad block table or |
| 500 | * calling of the scan function. |
| 501 | */ |
| 502 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, |
| 503 | int allowbbt) |
| 504 | { |
| 505 | struct nand_chip *chip = mtd->priv; |
| 506 | |
| 507 | if (!(chip->options & NAND_BBT_SCANNED)) { |
| 508 | chip->options |= NAND_BBT_SCANNED; |
| 509 | chip->scan_bbt(mtd); |
| 510 | } |
| 511 | |
| 512 | if (!chip->bbt) |
| 513 | return chip->block_bad(mtd, ofs, getchip); |
| 514 | |
| 515 | /* Return info from the table */ |
| 516 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
| 517 | } |
| 518 | |
| 519 | /* Wait for the ready pin, after a command. The timeout is caught later. */ |
| 520 | void nand_wait_ready(struct mtd_info *mtd) |
| 521 | { |
| 522 | struct nand_chip *chip = mtd->priv; |
| 523 | u32 timeo = (CONFIG_SYS_HZ * 20) / 1000; |
| 524 | u32 time_start; |
| 525 | |
| 526 | time_start = get_timer(0); |
| 527 | |
| 528 | /* Wait until command is processed or timeout occurs */ |
| 529 | while (get_timer(time_start) < timeo) { |
| 530 | if (chip->dev_ready) |
| 531 | if (chip->dev_ready(mtd)) |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * nand_command - [DEFAULT] Send command to NAND device |
| 538 | * @mtd: MTD device structure |
| 539 | * @command: the command to be sent |
| 540 | * @column: the column address for this command, -1 if none |
| 541 | * @page_addr: the page address for this command, -1 if none |
| 542 | * |
| 543 | * Send command to NAND device. This function is used for small page devices |
| 544 | * (256/512 Bytes per page). |
| 545 | */ |
| 546 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 547 | int column, int page_addr) |
| 548 | { |
| 549 | register struct nand_chip *chip = mtd->priv; |
| 550 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
| 551 | uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT; |
| 552 | |
| 553 | /* Write out the command to the device */ |
| 554 | if (command == NAND_CMD_SEQIN) { |
| 555 | int readcmd; |
| 556 | |
| 557 | if (column >= mtd->writesize) { |
| 558 | /* OOB area */ |
| 559 | column -= mtd->writesize; |
| 560 | readcmd = NAND_CMD_READOOB; |
| 561 | } else if (column < 256) { |
| 562 | /* First 256 bytes --> READ0 */ |
| 563 | readcmd = NAND_CMD_READ0; |
| 564 | } else { |
| 565 | column -= 256; |
| 566 | readcmd = NAND_CMD_READ1; |
| 567 | } |
| 568 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
| 569 | ctrl &= ~NAND_CTRL_CHANGE; |
| 570 | } |
| 571 | chip->cmd_ctrl(mtd, command, ctrl); |
| 572 | |
| 573 | /* Address cycle, when necessary */ |
| 574 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 575 | /* Serially input address */ |
| 576 | if (column != -1) { |
| 577 | /* Adjust columns for 16 bit buswidth */ |
| 578 | if (chip->options & NAND_BUSWIDTH_16) |
| 579 | column >>= 1; |
| 580 | chip->cmd_ctrl(mtd, column, ctrl); |
| 581 | ctrl &= ~NAND_CTRL_CHANGE; |
| 582 | } |
| 583 | if (page_addr != -1) { |
| 584 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 585 | ctrl &= ~NAND_CTRL_CHANGE; |
| 586 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
| 587 | /* One more address cycle for devices > 32MiB */ |
| 588 | if (chip->chipsize > (32 << 20)) |
| 589 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
| 590 | } |
| 591 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
| 592 | |
| 593 | /* |
| 594 | * Program and erase have their own busy handlers status and sequential |
| 595 | * in needs no delay |
| 596 | */ |
| 597 | switch (command) { |
| 598 | |
| 599 | case NAND_CMD_PAGEPROG: |
| 600 | case NAND_CMD_ERASE1: |
| 601 | case NAND_CMD_ERASE2: |
| 602 | case NAND_CMD_SEQIN: |
| 603 | case NAND_CMD_STATUS: |
| 604 | return; |
| 605 | |
| 606 | case NAND_CMD_RESET: |
| 607 | if (chip->dev_ready) |
| 608 | break; |
| 609 | udelay(chip->chip_delay); |
| 610 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 611 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 612 | chip->cmd_ctrl(mtd, |
| 613 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
| 614 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY) && |
| 615 | (rst_sts_cnt--)); |
| 616 | return; |
| 617 | |
| 618 | /* This applies to read commands */ |
| 619 | default: |
| 620 | /* |
| 621 | * If we don't have access to the busy pin, we apply the given |
| 622 | * command delay |
| 623 | */ |
| 624 | if (!chip->dev_ready) { |
| 625 | udelay(chip->chip_delay); |
| 626 | return; |
| 627 | } |
| 628 | } |
| 629 | /* |
| 630 | * Apply this short delay always to ensure that we do wait tWB in |
| 631 | * any case on any machine. |
| 632 | */ |
| 633 | ndelay(100); |
| 634 | |
| 635 | nand_wait_ready(mtd); |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
| 640 | * @mtd: MTD device structure |
| 641 | * @command: the command to be sent |
| 642 | * @column: the column address for this command, -1 if none |
| 643 | * @page_addr: the page address for this command, -1 if none |
| 644 | * |
| 645 | * Send command to NAND device. This is the version for the new large page |
| 646 | * devices. We don't have the separate regions as we have in the small page |
| 647 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
| 648 | */ |
| 649 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 650 | int column, int page_addr) |
| 651 | { |
| 652 | register struct nand_chip *chip = mtd->priv; |
| 653 | uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT; |
| 654 | |
| 655 | /* Emulate NAND_CMD_READOOB */ |
| 656 | if (command == NAND_CMD_READOOB) { |
| 657 | column += mtd->writesize; |
| 658 | command = NAND_CMD_READ0; |
| 659 | } |
| 660 | |
| 661 | /* Command latch cycle */ |
| 662 | chip->cmd_ctrl(mtd, command & 0xff, |
| 663 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 664 | |
| 665 | if (column != -1 || page_addr != -1) { |
| 666 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
| 667 | |
| 668 | /* Serially input address */ |
| 669 | if (column != -1) { |
| 670 | /* Adjust columns for 16 bit buswidth */ |
| 671 | if (chip->options & NAND_BUSWIDTH_16) |
| 672 | column >>= 1; |
| 673 | chip->cmd_ctrl(mtd, column, ctrl); |
| 674 | ctrl &= ~NAND_CTRL_CHANGE; |
| 675 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
| 676 | } |
| 677 | if (page_addr != -1) { |
| 678 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 679 | chip->cmd_ctrl(mtd, page_addr >> 8, |
| 680 | NAND_NCE | NAND_ALE); |
| 681 | /* One more address cycle for devices > 128MiB */ |
| 682 | if (chip->chipsize > (128 << 20)) |
| 683 | chip->cmd_ctrl(mtd, page_addr >> 16, |
| 684 | NAND_NCE | NAND_ALE); |
| 685 | } |
| 686 | } |
| 687 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
| 688 | |
| 689 | /* |
| 690 | * Program and erase have their own busy handlers status, sequential |
| 691 | * in, and deplete1 need no delay. |
| 692 | */ |
| 693 | switch (command) { |
| 694 | |
| 695 | case NAND_CMD_CACHEDPROG: |
| 696 | case NAND_CMD_PAGEPROG: |
| 697 | case NAND_CMD_ERASE1: |
| 698 | case NAND_CMD_ERASE2: |
| 699 | case NAND_CMD_SEQIN: |
| 700 | case NAND_CMD_RNDIN: |
| 701 | case NAND_CMD_STATUS: |
| 702 | case NAND_CMD_DEPLETE1: |
| 703 | return; |
| 704 | |
| 705 | case NAND_CMD_STATUS_ERROR: |
| 706 | case NAND_CMD_STATUS_ERROR0: |
| 707 | case NAND_CMD_STATUS_ERROR1: |
| 708 | case NAND_CMD_STATUS_ERROR2: |
| 709 | case NAND_CMD_STATUS_ERROR3: |
| 710 | /* Read error status commands require only a short delay */ |
| 711 | udelay(chip->chip_delay); |
| 712 | return; |
| 713 | |
| 714 | case NAND_CMD_RESET: |
| 715 | if (chip->dev_ready) |
| 716 | break; |
| 717 | udelay(chip->chip_delay); |
| 718 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 719 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 720 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 721 | NAND_NCE | NAND_CTRL_CHANGE); |
| 722 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY) && |
| 723 | (rst_sts_cnt--)); |
| 724 | return; |
| 725 | |
| 726 | case NAND_CMD_RNDOUT: |
| 727 | /* No ready / busy check necessary */ |
| 728 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 729 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 730 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 731 | NAND_NCE | NAND_CTRL_CHANGE); |
| 732 | return; |
| 733 | |
| 734 | case NAND_CMD_READ0: |
| 735 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 736 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 737 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 738 | NAND_NCE | NAND_CTRL_CHANGE); |
| 739 | |
| 740 | /* This applies to read commands */ |
| 741 | default: |
| 742 | /* |
| 743 | * If we don't have access to the busy pin, we apply the given |
| 744 | * command delay. |
| 745 | */ |
| 746 | if (!chip->dev_ready) { |
| 747 | udelay(chip->chip_delay); |
| 748 | return; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Apply this short delay always to ensure that we do wait tWB in |
| 754 | * any case on any machine. |
| 755 | */ |
| 756 | ndelay(100); |
| 757 | |
| 758 | nand_wait_ready(mtd); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * nand_get_device - [GENERIC] Get chip for selected access |
| 763 | * @chip: the nand chip descriptor |
| 764 | * @mtd: MTD device structure |
| 765 | * @new_state: the state which is requested |
| 766 | * |
| 767 | * Get the device and lock it for exclusive access |
| 768 | */ |
| 769 | static int |
| 770 | nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state) |
| 771 | { |
| 772 | chip->state = new_state; |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * nand_wait - [DEFAULT] wait until the command is done |
| 778 | * @mtd: MTD device structure |
| 779 | * @chip: NAND chip structure |
| 780 | * |
| 781 | * Wait for command done. This applies to erase and program only. Erase can |
| 782 | * take up to 400ms and program up to 20ms according to general NAND and |
| 783 | * SmartMedia specs. |
| 784 | */ |
| 785 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
| 786 | { |
| 787 | unsigned long timeo; |
| 788 | int state = chip->state; |
| 789 | u32 time_start; |
| 790 | |
| 791 | if (state == FL_ERASING) |
| 792 | timeo = (CONFIG_SYS_HZ * 400) / 1000; |
| 793 | else |
| 794 | timeo = (CONFIG_SYS_HZ * 20) / 1000; |
| 795 | |
| 796 | if ((state == FL_ERASING) && (chip->options & NAND_IS_AND)) |
| 797 | chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1); |
| 798 | else |
| 799 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 800 | |
| 801 | time_start = get_timer(0); |
| 802 | |
| 803 | while (1) { |
| 804 | if (get_timer(time_start) > timeo) { |
| 805 | printf("Timeout!"); |
| 806 | return 0x01; |
| 807 | } |
| 808 | |
| 809 | if (chip->dev_ready) { |
| 810 | if (chip->dev_ready(mtd)) |
| 811 | break; |
| 812 | } else { |
| 813 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 814 | break; |
| 815 | } |
| 816 | } |
| 817 | #ifdef PPCHAMELON_NAND_TIMER_HACK |
| 818 | time_start = get_timer(0); |
| 819 | while (get_timer(time_start) < 10) |
| 820 | ; |
| 821 | #endif /* PPCHAMELON_NAND_TIMER_HACK */ |
| 822 | |
| 823 | return (int)chip->read_byte(mtd); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
| 828 | * @mtd: mtd info structure |
| 829 | * @chip: nand chip info structure |
| 830 | * @buf: buffer to store read data |
| 831 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 832 | * @page: page number to read |
| 833 | * |
| 834 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
| 835 | */ |
| 836 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 837 | uint8_t *buf, int oob_required, int page) |
| 838 | { |
| 839 | chip->read_buf(mtd, buf, mtd->writesize); |
| 840 | if (oob_required) |
| 841 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
| 847 | * @mtd: mtd info structure |
| 848 | * @chip: nand chip info structure |
| 849 | * @buf: buffer to store read data |
| 850 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 851 | * @page: page number to read |
| 852 | * |
| 853 | * We need a special oob layout and handling even when OOB isn't used. |
| 854 | */ |
| 855 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
| 856 | struct nand_chip *chip, uint8_t *buf, |
| 857 | int oob_required, int page) |
| 858 | { |
| 859 | int eccsize = chip->ecc.size; |
| 860 | int eccbytes = chip->ecc.bytes; |
| 861 | uint8_t *oob = chip->oob_poi; |
| 862 | int steps, size; |
| 863 | |
| 864 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 865 | chip->read_buf(mtd, buf, eccsize); |
| 866 | buf += eccsize; |
| 867 | |
| 868 | if (chip->ecc.prepad) { |
| 869 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 870 | oob += chip->ecc.prepad; |
| 871 | } |
| 872 | |
| 873 | chip->read_buf(mtd, oob, eccbytes); |
| 874 | oob += eccbytes; |
| 875 | |
| 876 | if (chip->ecc.postpad) { |
| 877 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 878 | oob += chip->ecc.postpad; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 883 | if (size) |
| 884 | chip->read_buf(mtd, oob, size); |
| 885 | |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
| 891 | * @mtd: mtd info structure |
| 892 | * @chip: nand chip info structure |
| 893 | * @buf: buffer to store read data |
| 894 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 895 | * @page: page number to read |
| 896 | */ |
| 897 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 898 | uint8_t *buf, int oob_required, int page) |
| 899 | { |
| 900 | int i, eccsize = chip->ecc.size; |
| 901 | int eccbytes = chip->ecc.bytes; |
| 902 | int eccsteps = chip->ecc.steps; |
| 903 | uint8_t *p = buf; |
| 904 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 905 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 906 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 907 | |
| 908 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
| 909 | |
| 910 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 911 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 912 | |
| 913 | for (i = 0; i < chip->ecc.total; i++) |
| 914 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 915 | |
| 916 | eccsteps = chip->ecc.steps; |
| 917 | p = buf; |
| 918 | |
| 919 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 920 | int stat; |
| 921 | |
| 922 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
| 923 | if (stat < 0) |
| 924 | mtd->ecc_stats.failed++; |
| 925 | else |
| 926 | mtd->ecc_stats.corrected += stat; |
| 927 | } |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function |
| 933 | * @mtd: mtd info structure |
| 934 | * @chip: nand chip info structure |
| 935 | * @data_offs: offset of requested data within the page |
| 936 | * @readlen: data length |
| 937 | * @bufpoi: buffer to store read data |
| 938 | */ |
| 939 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
| 940 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi) |
| 941 | { |
| 942 | int start_step, end_step, num_steps; |
| 943 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 944 | uint8_t *p; |
| 945 | int data_col_addr, i, gaps = 0; |
| 946 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 947 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
| 948 | int index = 0; |
| 949 | |
| 950 | /* Column address within the page aligned to ECC size (256bytes) */ |
| 951 | start_step = data_offs / chip->ecc.size; |
| 952 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 953 | num_steps = end_step - start_step + 1; |
| 954 | |
| 955 | /* Data size aligned to ECC ecc.size */ |
| 956 | datafrag_len = num_steps * chip->ecc.size; |
| 957 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 958 | |
| 959 | data_col_addr = start_step * chip->ecc.size; |
| 960 | /* If we read not a page aligned data */ |
| 961 | if (data_col_addr != 0) |
| 962 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 963 | |
| 964 | p = bufpoi + data_col_addr; |
| 965 | chip->read_buf(mtd, p, datafrag_len); |
| 966 | |
| 967 | /* Calculate ECC */ |
| 968 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 969 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 970 | |
| 971 | /* |
| 972 | * The performance is faster if we position offsets according to |
| 973 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
| 974 | */ |
| 975 | for (i = 0; i < eccfrag_len - 1; i++) { |
| 976 | if (eccpos[i + start_step * chip->ecc.bytes] + 1 != |
| 977 | eccpos[i + start_step * chip->ecc.bytes + 1]) { |
| 978 | gaps = 1; |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | if (gaps) { |
| 983 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 984 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 985 | } else { |
| 986 | /* |
| 987 | * Send the command to read the particular ECC bytes take care |
| 988 | * about buswidth alignment in read_buf. |
| 989 | */ |
| 990 | index = start_step * chip->ecc.bytes; |
| 991 | |
| 992 | aligned_pos = eccpos[index] & ~(busw - 1); |
| 993 | aligned_len = eccfrag_len; |
| 994 | if (eccpos[index] & (busw - 1)) |
| 995 | aligned_len++; |
| 996 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
| 997 | aligned_len++; |
| 998 | |
| 999 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 1000 | mtd->writesize + aligned_pos, -1); |
| 1001 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
| 1002 | } |
| 1003 | |
| 1004 | for (i = 0; i < eccfrag_len; i++) |
| 1005 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
| 1006 | |
| 1007 | p = bufpoi + data_col_addr; |
| 1008 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1009 | int stat; |
| 1010 | |
| 1011 | stat = chip->ecc.correct(mtd, p, |
| 1012 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
| 1013 | if (stat < 0) |
| 1014 | mtd->ecc_stats.failed++; |
| 1015 | else |
| 1016 | mtd->ecc_stats.corrected += stat; |
| 1017 | } |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
| 1023 | * @mtd: mtd info structure |
| 1024 | * @chip: nand chip info structure |
| 1025 | * @buf: buffer to store read data |
| 1026 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1027 | * @page: page number to read |
| 1028 | * |
| 1029 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
| 1030 | */ |
| 1031 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 1032 | uint8_t *buf, int oob_required, int page) |
| 1033 | { |
| 1034 | int i, eccsize = chip->ecc.size; |
| 1035 | int eccbytes = chip->ecc.bytes; |
| 1036 | int eccsteps = chip->ecc.steps; |
| 1037 | uint8_t *p = buf; |
| 1038 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1039 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1040 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1041 | |
| 1042 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1043 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1044 | chip->read_buf(mtd, p, eccsize); |
| 1045 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1046 | } |
| 1047 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1048 | |
| 1049 | for (i = 0; i < chip->ecc.total; i++) |
| 1050 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 1051 | |
| 1052 | eccsteps = chip->ecc.steps; |
| 1053 | p = buf; |
| 1054 | |
| 1055 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1056 | int stat; |
| 1057 | |
| 1058 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
| 1059 | if (stat < 0) |
| 1060 | mtd->ecc_stats.failed++; |
| 1061 | else |
| 1062 | mtd->ecc_stats.corrected += stat; |
| 1063 | } |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
| 1069 | * @mtd: mtd info structure |
| 1070 | * @chip: nand chip info structure |
| 1071 | * @buf: buffer to store read data |
| 1072 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1073 | * @page: page number to read |
| 1074 | * |
| 1075 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 1076 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 1077 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 1078 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 1079 | * the data area, by overwriting the NAND manufacturer bad block markings. |
| 1080 | */ |
| 1081 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
| 1082 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
| 1083 | { |
| 1084 | int i, eccsize = chip->ecc.size; |
| 1085 | int eccbytes = chip->ecc.bytes; |
| 1086 | int eccsteps = chip->ecc.steps; |
| 1087 | uint8_t *p = buf; |
| 1088 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1089 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1090 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1091 | |
| 1092 | /* Read the OOB area first */ |
| 1093 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1094 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1095 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1096 | |
| 1097 | for (i = 0; i < chip->ecc.total; i++) |
| 1098 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 1099 | |
| 1100 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1101 | int stat; |
| 1102 | |
| 1103 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1104 | chip->read_buf(mtd, p, eccsize); |
| 1105 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1106 | |
| 1107 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
| 1108 | if (stat < 0) |
| 1109 | mtd->ecc_stats.failed++; |
| 1110 | else |
| 1111 | mtd->ecc_stats.corrected += stat; |
| 1112 | } |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
| 1118 | * @mtd: mtd info structure |
| 1119 | * @chip: nand chip info structure |
| 1120 | * @buf: buffer to store read data |
| 1121 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1122 | * @page: page number to read |
| 1123 | * |
| 1124 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 1125 | * need a special oob layout and handling. |
| 1126 | */ |
| 1127 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 1128 | uint8_t *buf, int oob_required, int page) |
| 1129 | { |
| 1130 | int i, eccsize = chip->ecc.size; |
| 1131 | int eccbytes = chip->ecc.bytes; |
| 1132 | int eccsteps = chip->ecc.steps; |
| 1133 | uint8_t *p = buf; |
| 1134 | uint8_t *oob = chip->oob_poi; |
| 1135 | |
| 1136 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1137 | int stat; |
| 1138 | |
| 1139 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1140 | chip->read_buf(mtd, p, eccsize); |
| 1141 | |
| 1142 | if (chip->ecc.prepad) { |
| 1143 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1144 | oob += chip->ecc.prepad; |
| 1145 | } |
| 1146 | |
| 1147 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
| 1148 | chip->read_buf(mtd, oob, eccbytes); |
| 1149 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1150 | |
| 1151 | if (stat < 0) |
| 1152 | mtd->ecc_stats.failed++; |
| 1153 | else |
| 1154 | mtd->ecc_stats.corrected += stat; |
| 1155 | |
| 1156 | oob += eccbytes; |
| 1157 | |
| 1158 | if (chip->ecc.postpad) { |
| 1159 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1160 | oob += chip->ecc.postpad; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /* Calculate remaining oob bytes */ |
| 1165 | i = mtd->oobsize - (oob - chip->oob_poi); |
| 1166 | if (i) |
| 1167 | chip->read_buf(mtd, oob, i); |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
| 1174 | * @chip: nand chip structure |
| 1175 | * @oob: oob destination address |
| 1176 | * @ops: oob ops structure |
| 1177 | * @len: size of oob to transfer |
| 1178 | */ |
| 1179 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
| 1180 | struct mtd_oob_ops *ops, size_t len) |
| 1181 | { |
| 1182 | switch (ops->mode) { |
| 1183 | |
| 1184 | case MTD_OPS_PLACE_OOB: |
| 1185 | case MTD_OPS_RAW: |
| 1186 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 1187 | return oob + len; |
| 1188 | |
| 1189 | case MTD_OPS_AUTO_OOB: { |
| 1190 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 1191 | uint32_t boffs = 0, roffs = ops->ooboffs; |
| 1192 | size_t bytes = 0; |
| 1193 | |
| 1194 | for (; free->length && len; free++, len -= bytes) { |
| 1195 | /* Read request not from offset 0? */ |
| 1196 | if (unlikely(roffs)) { |
| 1197 | if (roffs >= free->length) { |
| 1198 | roffs -= free->length; |
| 1199 | continue; |
| 1200 | } |
| 1201 | boffs = free->offset + roffs; |
| 1202 | bytes = min_t(size_t, len, |
| 1203 | (free->length - roffs)); |
| 1204 | roffs = 0; |
| 1205 | } else { |
| 1206 | bytes = min_t(size_t, len, free->length); |
| 1207 | boffs = free->offset; |
| 1208 | } |
| 1209 | memcpy(oob, chip->oob_poi + boffs, bytes); |
| 1210 | oob += bytes; |
| 1211 | } |
| 1212 | return oob; |
| 1213 | } |
| 1214 | default: |
| 1215 | BUG(); |
| 1216 | } |
| 1217 | return NULL; |
| 1218 | } |
| 1219 | |
| 1220 | /** |
| 1221 | * nand_do_read_ops - [INTERN] Read data with ECC |
| 1222 | * @mtd: MTD device structure |
| 1223 | * @from: offset to read from |
| 1224 | * @ops: oob ops structure |
| 1225 | * |
| 1226 | * Internal function. Called with chip held. |
| 1227 | */ |
| 1228 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 1229 | struct mtd_oob_ops *ops) |
| 1230 | { |
| 1231 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
| 1232 | struct nand_chip *chip = mtd->priv; |
| 1233 | struct mtd_ecc_stats stats; |
| 1234 | int ret = 0; |
| 1235 | uint32_t readlen = ops->len; |
| 1236 | uint32_t oobreadlen = ops->ooblen; |
| 1237 | uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ? |
| 1238 | mtd->oobavail : mtd->oobsize; |
| 1239 | |
| 1240 | uint8_t *bufpoi, *oob, *buf; |
| 1241 | unsigned int max_bitflips = 0; |
| 1242 | int ecc_mode = chip->ecc.mode; |
| 1243 | |
| 1244 | if (ops->mode == MTD_OPS_RAW) |
| 1245 | chip->ecc.mode = NAND_ECC_NONE; |
| 1246 | |
| 1247 | stats = mtd->ecc_stats; |
| 1248 | |
| 1249 | chipnr = (int)(from >> chip->chip_shift); |
| 1250 | chip->select_chip(mtd, chipnr); |
| 1251 | |
| 1252 | realpage = (int)(from >> chip->page_shift); |
| 1253 | page = realpage & chip->pagemask; |
| 1254 | |
| 1255 | col = (int)(from & (mtd->writesize - 1)); |
| 1256 | |
| 1257 | buf = ops->datbuf; |
| 1258 | oob = ops->oobbuf; |
| 1259 | oob_required = oob ? 1 : 0; |
| 1260 | |
| 1261 | while (1) { |
| 1262 | WATCHDOG_RESET(); |
| 1263 | |
| 1264 | bytes = min(mtd->writesize - col, readlen); |
| 1265 | aligned = (bytes == mtd->writesize); |
| 1266 | |
| 1267 | /* Is the current page in the buffer? */ |
| 1268 | if (realpage != chip->pagebuf || oob) { |
| 1269 | bufpoi = aligned ? buf : chip->buffers->databuf; |
| 1270 | |
| 1271 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
| 1272 | |
| 1273 | /* |
| 1274 | * Now read the page into the buffer. Absent an error, |
| 1275 | * the read methods return max bitflips per ecc step. |
| 1276 | */ |
| 1277 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
| 1278 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
| 1279 | oob_required, |
| 1280 | page); |
| 1281 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 1282 | !oob) |
| 1283 | ret = chip->ecc.read_subpage(mtd, chip, |
| 1284 | col, bytes, bufpoi); |
| 1285 | else |
| 1286 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
| 1287 | oob_required, page); |
| 1288 | if (ret < 0) { |
| 1289 | if (!aligned) |
| 1290 | /* Invalidate page cache */ |
| 1291 | chip->pagebuf = -1; |
| 1292 | break; |
| 1293 | } |
| 1294 | |
| 1295 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 1296 | |
| 1297 | /* Transfer not aligned data */ |
| 1298 | if (!aligned) { |
| 1299 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
| 1300 | !(mtd->ecc_stats.failed - stats.failed) && |
| 1301 | (ops->mode != MTD_OPS_RAW)) { |
| 1302 | chip->pagebuf = realpage; |
| 1303 | chip->pagebuf_bitflips = ret; |
| 1304 | } else { |
| 1305 | /* Invalidate page cache */ |
| 1306 | chip->pagebuf = -1; |
| 1307 | } |
| 1308 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 1309 | } |
| 1310 | |
| 1311 | buf += bytes; |
| 1312 | |
| 1313 | if (unlikely(oob)) { |
| 1314 | int toread = min(oobreadlen, max_oobsize); |
| 1315 | |
| 1316 | if (toread) { |
| 1317 | oob = nand_transfer_oob(chip, |
| 1318 | oob, ops, toread); |
| 1319 | oobreadlen -= toread; |
| 1320 | } |
| 1321 | } |
| 1322 | } else { |
| 1323 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 1324 | buf += bytes; |
| 1325 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 1326 | chip->pagebuf_bitflips); |
| 1327 | } |
| 1328 | |
| 1329 | readlen -= bytes; |
| 1330 | |
| 1331 | if (!readlen) |
| 1332 | break; |
| 1333 | |
| 1334 | /* For subsequent reads align to page boundary */ |
| 1335 | col = 0; |
| 1336 | /* Increment page address */ |
| 1337 | realpage++; |
| 1338 | |
| 1339 | page = realpage & chip->pagemask; |
| 1340 | /* Check, if we cross a chip boundary */ |
| 1341 | if (!page) { |
| 1342 | chipnr++; |
| 1343 | chip->select_chip(mtd, -1); |
| 1344 | chip->select_chip(mtd, chipnr); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | ops->retlen = ops->len - (size_t) readlen; |
| 1349 | if (oob) |
| 1350 | ops->oobretlen = ops->ooblen - oobreadlen; |
| 1351 | |
| 1352 | if (ops->mode == MTD_OPS_RAW) |
| 1353 | chip->ecc.mode = ecc_mode; |
| 1354 | |
| 1355 | if (ret) |
| 1356 | return ret; |
| 1357 | |
| 1358 | if (mtd->ecc_stats.failed - stats.failed) |
| 1359 | return -EBADMSG; |
| 1360 | |
| 1361 | return max_bitflips; |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
| 1366 | * @mtd: MTD device structure |
| 1367 | * @from: offset to read from |
| 1368 | * @len: number of bytes to read |
| 1369 | * @retlen: pointer to variable to store the number of read bytes |
| 1370 | * @buf: the databuffer to put data |
| 1371 | * |
| 1372 | * Get hold of the chip and call nand_do_read. |
| 1373 | */ |
| 1374 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 1375 | size_t *retlen, uint8_t *buf) |
| 1376 | { |
| 1377 | struct nand_chip *chip = mtd->priv; |
| 1378 | struct mtd_oob_ops ops; |
| 1379 | int ret; |
| 1380 | |
| 1381 | nand_get_device(chip, mtd, FL_READING); |
| 1382 | ops.len = len; |
| 1383 | ops.datbuf = buf; |
| 1384 | ops.oobbuf = NULL; |
| 1385 | ops.mode = MTD_OPS_PLACE_OOB; |
| 1386 | ret = nand_do_read_ops(mtd, from, &ops); |
| 1387 | *retlen = ops.retlen; |
| 1388 | nand_release_device(mtd); |
| 1389 | return ret; |
| 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
| 1394 | * @mtd: mtd info structure |
| 1395 | * @chip: nand chip info structure |
| 1396 | * @page: page number to read |
| 1397 | */ |
| 1398 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 1399 | int page) |
| 1400 | { |
| 1401 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1402 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | /** |
| 1407 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
| 1408 | * with syndromes |
| 1409 | * @mtd: mtd info structure |
| 1410 | * @chip: nand chip info structure |
| 1411 | * @page: page number to read |
| 1412 | */ |
| 1413 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 1414 | int page) |
| 1415 | { |
| 1416 | uint8_t *buf = chip->oob_poi; |
| 1417 | int length = mtd->oobsize; |
| 1418 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 1419 | int eccsize = chip->ecc.size; |
| 1420 | uint8_t *bufpoi = buf; |
| 1421 | int i, toread, sndrnd = 0, pos; |
| 1422 | |
| 1423 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); |
| 1424 | for (i = 0; i < chip->ecc.steps; i++) { |
| 1425 | if (sndrnd) { |
| 1426 | pos = eccsize + i * (eccsize + chunk); |
| 1427 | if (mtd->writesize > 512) |
| 1428 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); |
| 1429 | else |
| 1430 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); |
| 1431 | } else |
| 1432 | sndrnd = 1; |
| 1433 | toread = min_t(int, length, chunk); |
| 1434 | chip->read_buf(mtd, bufpoi, toread); |
| 1435 | bufpoi += toread; |
| 1436 | length -= toread; |
| 1437 | } |
| 1438 | if (length > 0) |
| 1439 | chip->read_buf(mtd, bufpoi, length); |
| 1440 | |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | /** |
| 1445 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
| 1446 | * @mtd: mtd info structure |
| 1447 | * @chip: nand chip info structure |
| 1448 | * @page: page number to write |
| 1449 | */ |
| 1450 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 1451 | int page) |
| 1452 | { |
| 1453 | int status = 0; |
| 1454 | const uint8_t *buf = chip->oob_poi; |
| 1455 | int length = mtd->oobsize; |
| 1456 | |
| 1457 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 1458 | chip->write_buf(mtd, buf, length); |
| 1459 | /* Send command to program the OOB data */ |
| 1460 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1461 | |
| 1462 | status = chip->waitfunc(mtd, chip); |
| 1463 | |
| 1464 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 1465 | } |
| 1466 | |
| 1467 | /** |
| 1468 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
| 1469 | * with syndrome - only for large page flash |
| 1470 | * @mtd: mtd info structure |
| 1471 | * @chip: nand chip info structure |
| 1472 | * @page: page number to write |
| 1473 | */ |
| 1474 | static int nand_write_oob_syndrome(struct mtd_info *mtd, |
| 1475 | struct nand_chip *chip, int page) |
| 1476 | { |
| 1477 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 1478 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
| 1479 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; |
| 1480 | const uint8_t *bufpoi = chip->oob_poi; |
| 1481 | |
| 1482 | /* |
| 1483 | * data-ecc-data-ecc ... ecc-oob |
| 1484 | * or |
| 1485 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 1486 | */ |
| 1487 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 1488 | pos = steps * (eccsize + chunk); |
| 1489 | steps = 0; |
| 1490 | } else |
| 1491 | pos = eccsize; |
| 1492 | |
| 1493 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); |
| 1494 | for (i = 0; i < steps; i++) { |
| 1495 | if (sndcmd) { |
| 1496 | if (mtd->writesize <= 512) { |
| 1497 | uint32_t fill = 0xFFFFFFFF; |
| 1498 | |
| 1499 | len = eccsize; |
| 1500 | while (len > 0) { |
| 1501 | int num = min_t(int, len, 4); |
| 1502 | chip->write_buf(mtd, (uint8_t *)&fill, |
| 1503 | num); |
| 1504 | len -= num; |
| 1505 | } |
| 1506 | } else { |
| 1507 | pos = eccsize + i * (eccsize + chunk); |
| 1508 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); |
| 1509 | } |
| 1510 | } else |
| 1511 | sndcmd = 1; |
| 1512 | len = min_t(int, length, chunk); |
| 1513 | chip->write_buf(mtd, bufpoi, len); |
| 1514 | bufpoi += len; |
| 1515 | length -= len; |
| 1516 | } |
| 1517 | if (length > 0) |
| 1518 | chip->write_buf(mtd, bufpoi, length); |
| 1519 | |
| 1520 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1521 | status = chip->waitfunc(mtd, chip); |
| 1522 | |
| 1523 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 1524 | } |
| 1525 | |
| 1526 | /** |
| 1527 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
| 1528 | * @mtd: MTD device structure |
| 1529 | * @from: offset to read from |
| 1530 | * @ops: oob operations description structure |
| 1531 | * |
| 1532 | * NAND read out-of-band data from the spare area. |
| 1533 | */ |
| 1534 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 1535 | struct mtd_oob_ops *ops) |
| 1536 | { |
| 1537 | unsigned int max_bitflips = 0; |
| 1538 | int page, realpage, chipnr; |
| 1539 | struct nand_chip *chip = mtd->priv; |
| 1540 | struct mtd_ecc_stats stats; |
| 1541 | int readlen = ops->ooblen; |
| 1542 | int len; |
| 1543 | uint8_t *buf = ops->oobbuf; |
| 1544 | int ret = 0; |
| 1545 | |
| 1546 | MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n", |
| 1547 | __func__, (unsigned long long)from, readlen); |
| 1548 | |
| 1549 | stats = mtd->ecc_stats; |
| 1550 | |
| 1551 | if (ops->mode == MTD_OPS_AUTO_OOB) |
| 1552 | len = chip->ecc.layout->oobavail; |
| 1553 | else |
| 1554 | len = mtd->oobsize; |
| 1555 | |
| 1556 | if (unlikely(ops->ooboffs >= len)) { |
| 1557 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read " |
| 1558 | "outside oob\n", __func__); |
| 1559 | return -EINVAL; |
| 1560 | } |
| 1561 | |
| 1562 | /* Do not allow reads past end of device */ |
| 1563 | if (unlikely(from >= mtd->size || |
| 1564 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 1565 | (from >> chip->page_shift)) * len)) { |
| 1566 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end " |
| 1567 | "of device\n", __func__); |
| 1568 | return -EINVAL; |
| 1569 | } |
| 1570 | |
| 1571 | chipnr = (int)(from >> chip->chip_shift); |
| 1572 | chip->select_chip(mtd, chipnr); |
| 1573 | |
| 1574 | /* Shift to get page */ |
| 1575 | realpage = (int)(from >> chip->page_shift); |
| 1576 | page = realpage & chip->pagemask; |
| 1577 | |
| 1578 | while (1) { |
| 1579 | WATCHDOG_RESET(); |
| 1580 | if (ops->mode == MTD_OPS_RAW) |
| 1581 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
| 1582 | else |
| 1583 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 1584 | |
| 1585 | if (ret < 0) |
| 1586 | break; |
| 1587 | |
| 1588 | len = min(len, readlen); |
| 1589 | buf = nand_transfer_oob(chip, buf, ops, len); |
| 1590 | |
| 1591 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 1592 | |
| 1593 | readlen -= len; |
| 1594 | if (!readlen) |
| 1595 | break; |
| 1596 | |
| 1597 | /* Increment page address */ |
| 1598 | realpage++; |
| 1599 | |
| 1600 | page = realpage & chip->pagemask; |
| 1601 | /* Check, if we cross a chip boundary */ |
| 1602 | if (!page) { |
| 1603 | chipnr++; |
| 1604 | chip->select_chip(mtd, -1); |
| 1605 | chip->select_chip(mtd, chipnr); |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | ops->oobretlen = ops->ooblen - readlen; |
| 1610 | |
| 1611 | if (ret < 0) |
| 1612 | return ret; |
| 1613 | |
| 1614 | if (mtd->ecc_stats.failed - stats.failed) |
| 1615 | return -EBADMSG; |
| 1616 | |
| 1617 | return max_bitflips; |
| 1618 | } |
| 1619 | |
| 1620 | /** |
| 1621 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
| 1622 | * @mtd: MTD device structure |
| 1623 | * @from: offset to read from |
| 1624 | * @ops: oob operation description structure |
| 1625 | * |
| 1626 | * NAND read data and/or out-of-band data. |
| 1627 | */ |
| 1628 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 1629 | struct mtd_oob_ops *ops) |
| 1630 | { |
| 1631 | struct nand_chip *chip = mtd->priv; |
| 1632 | int ret = -ENOTSUPP; |
| 1633 | |
| 1634 | ops->retlen = 0; |
| 1635 | |
| 1636 | /* Do not allow reads past end of device */ |
| 1637 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
| 1638 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read " |
| 1639 | "beyond end of device\n", __func__); |
| 1640 | return -EINVAL; |
| 1641 | } |
| 1642 | |
| 1643 | nand_get_device(chip, mtd, FL_READING); |
| 1644 | |
| 1645 | switch (ops->mode) { |
| 1646 | case MTD_OPS_PLACE_OOB: |
| 1647 | case MTD_OPS_AUTO_OOB: |
| 1648 | case MTD_OPS_RAW: |
| 1649 | break; |
| 1650 | |
| 1651 | default: |
| 1652 | goto out; |
| 1653 | } |
| 1654 | |
| 1655 | if (!ops->datbuf) |
| 1656 | ret = nand_do_read_oob(mtd, from, ops); |
| 1657 | else |
| 1658 | ret = nand_do_read_ops(mtd, from, ops); |
| 1659 | |
| 1660 | out: |
| 1661 | nand_release_device(mtd); |
| 1662 | return ret; |
| 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | /** |
| 1667 | * nand_write_page_raw - [INTERN] raw page write function |
| 1668 | * @mtd: mtd info structure |
| 1669 | * @chip: nand chip info structure |
| 1670 | * @buf: data buffer |
| 1671 | * @oob_required: must write chip->oob_poi to OOB |
| 1672 | * |
| 1673 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
| 1674 | */ |
| 1675 | static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1676 | const uint8_t *buf, int oob_required) |
| 1677 | { |
| 1678 | chip->write_buf(mtd, buf, mtd->writesize); |
| 1679 | if (oob_required) |
| 1680 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1681 | |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
| 1687 | * @mtd: mtd info structure |
| 1688 | * @chip: nand chip info structure |
| 1689 | * @buf: data buffer |
| 1690 | * @oob_required: must write chip->oob_poi to OOB |
| 1691 | * |
| 1692 | * We need a special oob layout and handling even when ECC isn't checked. |
| 1693 | */ |
| 1694 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
| 1695 | struct nand_chip *chip, |
| 1696 | const uint8_t *buf, int oob_required) |
| 1697 | { |
| 1698 | int eccsize = chip->ecc.size; |
| 1699 | int eccbytes = chip->ecc.bytes; |
| 1700 | uint8_t *oob = chip->oob_poi; |
| 1701 | int steps, size; |
| 1702 | |
| 1703 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 1704 | chip->write_buf(mtd, buf, eccsize); |
| 1705 | buf += eccsize; |
| 1706 | |
| 1707 | if (chip->ecc.prepad) { |
| 1708 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 1709 | oob += chip->ecc.prepad; |
| 1710 | } |
| 1711 | |
| 1712 | chip->read_buf(mtd, oob, eccbytes); |
| 1713 | oob += eccbytes; |
| 1714 | |
| 1715 | if (chip->ecc.postpad) { |
| 1716 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 1717 | oob += chip->ecc.postpad; |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 1722 | if (size) |
| 1723 | chip->write_buf(mtd, oob, size); |
| 1724 | |
| 1725 | return 0; |
| 1726 | } |
| 1727 | /** |
| 1728 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
| 1729 | * @mtd: mtd info structure |
| 1730 | * @chip: nand chip info structure |
| 1731 | * @buf: data buffer |
| 1732 | * @oob_required: must write chip->oob_poi to OOB |
| 1733 | */ |
| 1734 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 1735 | const uint8_t *buf, int oob_required) |
| 1736 | { |
| 1737 | int i, eccsize = chip->ecc.size; |
| 1738 | int eccbytes = chip->ecc.bytes; |
| 1739 | int eccsteps = chip->ecc.steps; |
| 1740 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1741 | const uint8_t *p = buf; |
| 1742 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1743 | |
| 1744 | /* Software ECC calculation */ |
| 1745 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1746 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1747 | |
| 1748 | for (i = 0; i < chip->ecc.total; i++) |
| 1749 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 1750 | |
| 1751 | return chip->ecc.write_page_raw(mtd, chip, buf, 1); |
| 1752 | } |
| 1753 | |
| 1754 | /** |
| 1755 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
| 1756 | * @mtd: mtd info structure |
| 1757 | * @chip: nand chip info structure |
| 1758 | * @buf: data buffer |
| 1759 | * @oob_required: must write chip->oob_poi to OOB |
| 1760 | */ |
| 1761 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 1762 | const uint8_t *buf, int oob_required) |
| 1763 | { |
| 1764 | int i, eccsize = chip->ecc.size; |
| 1765 | int eccbytes = chip->ecc.bytes; |
| 1766 | int eccsteps = chip->ecc.steps; |
| 1767 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1768 | const uint8_t *p = buf; |
| 1769 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1770 | |
| 1771 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1772 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 1773 | chip->write_buf(mtd, p, eccsize); |
| 1774 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1775 | } |
| 1776 | |
| 1777 | for (i = 0; i < chip->ecc.total; i++) |
| 1778 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 1779 | |
| 1780 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1781 | |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | /** |
| 1786 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
| 1787 | * @mtd: mtd info structure |
| 1788 | * @chip: nand chip info structure |
| 1789 | * @buf: data buffer |
| 1790 | * @oob_required: must write chip->oob_poi to OOB |
| 1791 | * |
| 1792 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 1793 | * need a special oob layout and handling. |
| 1794 | */ |
| 1795 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
| 1796 | struct nand_chip *chip, |
| 1797 | const uint8_t *buf, int oob_required) |
| 1798 | { |
| 1799 | int i, eccsize = chip->ecc.size; |
| 1800 | int eccbytes = chip->ecc.bytes; |
| 1801 | int eccsteps = chip->ecc.steps; |
| 1802 | const uint8_t *p = buf; |
| 1803 | uint8_t *oob = chip->oob_poi; |
| 1804 | |
| 1805 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1806 | |
| 1807 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 1808 | chip->write_buf(mtd, p, eccsize); |
| 1809 | |
| 1810 | if (chip->ecc.prepad) { |
| 1811 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 1812 | oob += chip->ecc.prepad; |
| 1813 | } |
| 1814 | |
| 1815 | chip->ecc.calculate(mtd, p, oob); |
| 1816 | chip->write_buf(mtd, oob, eccbytes); |
| 1817 | oob += eccbytes; |
| 1818 | |
| 1819 | if (chip->ecc.postpad) { |
| 1820 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 1821 | oob += chip->ecc.postpad; |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | /* Calculate remaining oob bytes */ |
| 1826 | i = mtd->oobsize - (oob - chip->oob_poi); |
| 1827 | if (i) |
| 1828 | chip->write_buf(mtd, oob, i); |
| 1829 | |
| 1830 | return 0; |
| 1831 | } |
| 1832 | |
| 1833 | /** |
| 1834 | * nand_write_page - [REPLACEABLE] write one page |
| 1835 | * @mtd: MTD device structure |
| 1836 | * @chip: NAND chip descriptor |
| 1837 | * @buf: the data to write |
| 1838 | * @oob_required: must write chip->oob_poi to OOB |
| 1839 | * @page: page number to write |
| 1840 | * @cached: cached programming |
| 1841 | * @raw: use _raw version of write_page |
| 1842 | */ |
| 1843 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 1844 | const uint8_t *buf, int oob_required, int page, |
| 1845 | int cached, int raw) |
| 1846 | { |
| 1847 | int status; |
| 1848 | |
| 1849 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
| 1850 | |
| 1851 | if (unlikely(raw)) |
| 1852 | status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required); |
| 1853 | else |
| 1854 | status = chip->ecc.write_page(mtd, chip, buf, oob_required); |
| 1855 | |
| 1856 | if (status < 0) |
| 1857 | return status; |
| 1858 | |
| 1859 | /* |
| 1860 | * Cached progamming disabled for now. Not sure if it's worth the |
| 1861 | * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s). |
| 1862 | */ |
| 1863 | cached = 0; |
| 1864 | |
| 1865 | if (!cached || !(chip->options & NAND_CACHEPRG)) { |
| 1866 | |
| 1867 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, page); |
| 1868 | status = chip->waitfunc(mtd, chip); |
| 1869 | /* |
| 1870 | * See if operation failed and additional status checks are |
| 1871 | * available. |
| 1872 | */ |
| 1873 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 1874 | status = chip->errstat(mtd, chip, FL_WRITING, status, |
| 1875 | page); |
| 1876 | |
| 1877 | if (status & NAND_STATUS_FAIL) |
| 1878 | return -EIO; |
| 1879 | } else { |
| 1880 | chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1); |
| 1881 | status = chip->waitfunc(mtd, chip); |
| 1882 | } |
| 1883 | |
| 1884 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE |
| 1885 | /* Send command to read back the data */ |
| 1886 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1887 | |
| 1888 | if (chip->verify_buf(mtd, buf, mtd->writesize)) |
| 1889 | return -EIO; |
| 1890 | |
| 1891 | /* Make sure the next page prog is preceded by a status read */ |
| 1892 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 1893 | #endif |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
| 1899 | * @mtd: MTD device structure |
| 1900 | * @oob: oob data buffer |
| 1901 | * @len: oob data write length |
| 1902 | * @ops: oob ops structure |
| 1903 | */ |
| 1904 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 1905 | struct mtd_oob_ops *ops) |
| 1906 | { |
| 1907 | struct nand_chip *chip = mtd->priv; |
| 1908 | |
| 1909 | /* |
| 1910 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 1911 | * data from a previous OOB read. |
| 1912 | */ |
| 1913 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 1914 | |
| 1915 | switch (ops->mode) { |
| 1916 | |
| 1917 | case MTD_OPS_PLACE_OOB: |
| 1918 | case MTD_OPS_RAW: |
| 1919 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 1920 | return oob + len; |
| 1921 | |
| 1922 | case MTD_OPS_AUTO_OOB: { |
| 1923 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 1924 | uint32_t boffs = 0, woffs = ops->ooboffs; |
| 1925 | size_t bytes = 0; |
| 1926 | |
| 1927 | for (; free->length && len; free++, len -= bytes) { |
| 1928 | /* Write request not from offset 0? */ |
| 1929 | if (unlikely(woffs)) { |
| 1930 | if (woffs >= free->length) { |
| 1931 | woffs -= free->length; |
| 1932 | continue; |
| 1933 | } |
| 1934 | boffs = free->offset + woffs; |
| 1935 | bytes = min_t(size_t, len, |
| 1936 | (free->length - woffs)); |
| 1937 | woffs = 0; |
| 1938 | } else { |
| 1939 | bytes = min_t(size_t, len, free->length); |
| 1940 | boffs = free->offset; |
| 1941 | } |
| 1942 | memcpy(chip->oob_poi + boffs, oob, bytes); |
| 1943 | oob += bytes; |
| 1944 | } |
| 1945 | return oob; |
| 1946 | } |
| 1947 | default: |
| 1948 | BUG(); |
| 1949 | } |
| 1950 | return NULL; |
| 1951 | } |
| 1952 | |
| 1953 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
| 1954 | |
| 1955 | /** |
| 1956 | * nand_do_write_ops - [INTERN] NAND write with ECC |
| 1957 | * @mtd: MTD device structure |
| 1958 | * @to: offset to write to |
| 1959 | * @ops: oob operations description structure |
| 1960 | * |
| 1961 | * NAND write with ECC. |
| 1962 | */ |
| 1963 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 1964 | struct mtd_oob_ops *ops) |
| 1965 | { |
| 1966 | int chipnr, realpage, page, blockmask, column; |
| 1967 | struct nand_chip *chip = mtd->priv; |
| 1968 | uint32_t writelen = ops->len; |
| 1969 | |
| 1970 | uint32_t oobwritelen = ops->ooblen; |
| 1971 | uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ? |
| 1972 | mtd->oobavail : mtd->oobsize; |
| 1973 | |
| 1974 | uint8_t *oob = ops->oobbuf; |
| 1975 | uint8_t *buf = ops->datbuf; |
| 1976 | int ret, subpage; |
| 1977 | int oob_required = oob ? 1 : 0; |
| 1978 | int ecc_mode = chip->ecc.mode; |
| 1979 | |
| 1980 | ops->retlen = 0; |
| 1981 | if (!writelen) |
| 1982 | return 0; |
| 1983 | |
| 1984 | column = to & (mtd->writesize - 1); |
| 1985 | subpage = column || (writelen & (mtd->writesize - 1)); |
| 1986 | |
| 1987 | if (subpage && oob) |
| 1988 | return -EINVAL; |
| 1989 | |
| 1990 | chipnr = (int)(to >> chip->chip_shift); |
| 1991 | chip->select_chip(mtd, chipnr); |
| 1992 | |
| 1993 | /* Check, if it is write protected */ |
| 1994 | if (nand_check_wp(mtd)) { |
| 1995 | printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n"); |
| 1996 | return -EIO; |
| 1997 | } |
| 1998 | |
| 1999 | realpage = (int)(to >> chip->page_shift); |
| 2000 | page = realpage & chip->pagemask; |
| 2001 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 2002 | |
| 2003 | /* Invalidate the page cache, when we write to the cached page */ |
| 2004 | if (to <= (chip->pagebuf << chip->page_shift) && |
| 2005 | (chip->pagebuf << chip->page_shift) < (to + ops->len)) |
| 2006 | chip->pagebuf = -1; |
| 2007 | |
| 2008 | /* Don't allow multipage oob writes with offset */ |
| 2009 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) |
| 2010 | return -EINVAL; |
| 2011 | |
| 2012 | if (ops->mode == MTD_OPS_RAW) |
| 2013 | chip->ecc.mode = NAND_ECC_NONE; |
| 2014 | |
| 2015 | while (1) { |
| 2016 | WATCHDOG_RESET(); |
| 2017 | |
| 2018 | int bytes = mtd->writesize; |
| 2019 | int cached = writelen > bytes && page != blockmask; |
| 2020 | uint8_t *wbuf = buf; |
| 2021 | |
| 2022 | /* Partial page write? */ |
| 2023 | if (unlikely(column || writelen < mtd->writesize)) { |
| 2024 | cached = 0; |
| 2025 | bytes = min_t(int, bytes - column, (int) writelen); |
| 2026 | chip->pagebuf = -1; |
| 2027 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 2028 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 2029 | wbuf = chip->buffers->databuf; |
| 2030 | } |
| 2031 | |
| 2032 | if (unlikely(oob)) { |
| 2033 | size_t len = min(oobwritelen, oobmaxlen); |
| 2034 | oob = nand_fill_oob(mtd, oob, len, ops); |
| 2035 | oobwritelen -= len; |
| 2036 | } else { |
| 2037 | /* We still need to erase leftover OOB data */ |
| 2038 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 2039 | } |
| 2040 | |
| 2041 | ret = chip->write_page(mtd, chip, wbuf, oob_required, page, |
| 2042 | cached, ops->mode == MTD_OPS_RAW); |
| 2043 | if (ret) |
| 2044 | break; |
| 2045 | |
| 2046 | writelen -= bytes; |
| 2047 | if (!writelen) |
| 2048 | break; |
| 2049 | |
| 2050 | column = 0; |
| 2051 | buf += bytes; |
| 2052 | realpage++; |
| 2053 | |
| 2054 | page = realpage & chip->pagemask; |
| 2055 | /* Check, if we cross a chip boundary */ |
| 2056 | if (!page) { |
| 2057 | chipnr++; |
| 2058 | chip->select_chip(mtd, -1); |
| 2059 | chip->select_chip(mtd, chipnr); |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | ops->retlen = ops->len - writelen; |
| 2064 | if (unlikely(oob)) |
| 2065 | ops->oobretlen = ops->ooblen; |
| 2066 | |
| 2067 | if (ops->mode == MTD_OPS_RAW) |
| 2068 | chip->ecc.mode = ecc_mode; |
| 2069 | return ret; |
| 2070 | } |
| 2071 | |
| 2072 | /** |
| 2073 | * nand_write - [MTD Interface] NAND write with ECC |
| 2074 | * @mtd: MTD device structure |
| 2075 | * @to: offset to write to |
| 2076 | * @len: number of bytes to write |
| 2077 | * @retlen: pointer to variable to store the number of written bytes |
| 2078 | * @buf: the data to write |
| 2079 | * |
| 2080 | * NAND write with ECC. |
| 2081 | */ |
| 2082 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2083 | size_t *retlen, const uint8_t *buf) |
| 2084 | { |
| 2085 | struct nand_chip *chip = mtd->priv; |
| 2086 | struct mtd_oob_ops ops; |
| 2087 | int ret; |
| 2088 | |
| 2089 | nand_get_device(chip, mtd, FL_WRITING); |
| 2090 | ops.len = len; |
| 2091 | ops.datbuf = (uint8_t *)buf; |
| 2092 | ops.oobbuf = NULL; |
| 2093 | ops.mode = MTD_OPS_PLACE_OOB; |
| 2094 | ret = nand_do_write_ops(mtd, to, &ops); |
| 2095 | *retlen = ops.retlen; |
| 2096 | nand_release_device(mtd); |
| 2097 | return ret; |
| 2098 | } |
| 2099 | |
| 2100 | /** |
| 2101 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
| 2102 | * @mtd: MTD device structure |
| 2103 | * @to: offset to write to |
| 2104 | * @ops: oob operation description structure |
| 2105 | * |
| 2106 | * NAND write out-of-band. |
| 2107 | */ |
| 2108 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 2109 | struct mtd_oob_ops *ops) |
| 2110 | { |
| 2111 | int chipnr, page, status, len; |
| 2112 | struct nand_chip *chip = mtd->priv; |
| 2113 | |
| 2114 | MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n", |
| 2115 | __func__, (unsigned int)to, (int)ops->ooblen); |
| 2116 | |
| 2117 | if (ops->mode == MTD_OPS_AUTO_OOB) |
| 2118 | len = chip->ecc.layout->oobavail; |
| 2119 | else |
| 2120 | len = mtd->oobsize; |
| 2121 | |
| 2122 | /* Do not allow write past end of page */ |
| 2123 | if ((ops->ooboffs + ops->ooblen) > len) { |
| 2124 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write " |
| 2125 | "past end of page\n", __func__); |
| 2126 | return -EINVAL; |
| 2127 | } |
| 2128 | |
| 2129 | if (unlikely(ops->ooboffs >= len)) { |
| 2130 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start " |
| 2131 | "write outside oob\n", __func__); |
| 2132 | return -EINVAL; |
| 2133 | } |
| 2134 | |
| 2135 | /* Do not allow write past end of device */ |
| 2136 | if (unlikely(to >= mtd->size || |
| 2137 | ops->ooboffs + ops->ooblen > |
| 2138 | ((mtd->size >> chip->page_shift) - |
| 2139 | (to >> chip->page_shift)) * len)) { |
| 2140 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond " |
| 2141 | "end of device\n", __func__); |
| 2142 | return -EINVAL; |
| 2143 | } |
| 2144 | |
| 2145 | chipnr = (int)(to >> chip->chip_shift); |
| 2146 | chip->select_chip(mtd, chipnr); |
| 2147 | |
| 2148 | /* Shift to get page */ |
| 2149 | page = (int)(to >> chip->page_shift); |
| 2150 | |
| 2151 | /* |
| 2152 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 2153 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 2154 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 2155 | * it in the doc2000 driver in August 1999. dwmw2. |
| 2156 | */ |
| 2157 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 2158 | |
| 2159 | /* Check, if it is write protected */ |
| 2160 | if (nand_check_wp(mtd)) |
| 2161 | return -EROFS; |
| 2162 | |
| 2163 | /* Invalidate the page cache, if we write to the cached page */ |
| 2164 | if (page == chip->pagebuf) |
| 2165 | chip->pagebuf = -1; |
| 2166 | |
| 2167 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
| 2168 | |
| 2169 | if (ops->mode == MTD_OPS_RAW) |
| 2170 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 2171 | else |
| 2172 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
| 2173 | |
| 2174 | if (status) |
| 2175 | return status; |
| 2176 | |
| 2177 | ops->oobretlen = ops->ooblen; |
| 2178 | |
| 2179 | return 0; |
| 2180 | } |
| 2181 | |
| 2182 | /** |
| 2183 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
| 2184 | * @mtd: MTD device structure |
| 2185 | * @to: offset to write to |
| 2186 | * @ops: oob operation description structure |
| 2187 | */ |
| 2188 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 2189 | struct mtd_oob_ops *ops) |
| 2190 | { |
| 2191 | struct nand_chip *chip = mtd->priv; |
| 2192 | int ret = -ENOTSUPP; |
| 2193 | |
| 2194 | ops->retlen = 0; |
| 2195 | |
| 2196 | /* Do not allow writes past end of device */ |
| 2197 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
| 2198 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond " |
| 2199 | "end of device\n", __func__); |
| 2200 | return -EINVAL; |
| 2201 | } |
| 2202 | |
| 2203 | nand_get_device(chip, mtd, FL_WRITING); |
| 2204 | |
| 2205 | switch (ops->mode) { |
| 2206 | case MTD_OPS_PLACE_OOB: |
| 2207 | case MTD_OPS_AUTO_OOB: |
| 2208 | case MTD_OPS_RAW: |
| 2209 | break; |
| 2210 | |
| 2211 | default: |
| 2212 | goto out; |
| 2213 | } |
| 2214 | |
| 2215 | if (!ops->datbuf) |
| 2216 | ret = nand_do_write_oob(mtd, to, ops); |
| 2217 | else |
| 2218 | ret = nand_do_write_ops(mtd, to, ops); |
| 2219 | |
| 2220 | out: |
| 2221 | nand_release_device(mtd); |
| 2222 | return ret; |
| 2223 | } |
| 2224 | |
| 2225 | /** |
| 2226 | * single_erase_cmd - [GENERIC] NAND standard block erase command function |
| 2227 | * @mtd: MTD device structure |
| 2228 | * @page: the page address of the block which will be erased |
| 2229 | * |
| 2230 | * Standard erase command for NAND chips. |
| 2231 | */ |
| 2232 | static void single_erase_cmd(struct mtd_info *mtd, int page) |
| 2233 | { |
| 2234 | struct nand_chip *chip = mtd->priv; |
| 2235 | /* Send commands to erase a block */ |
| 2236 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2237 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 2238 | } |
| 2239 | |
| 2240 | /** |
| 2241 | * multi_erase_cmd - [GENERIC] AND specific block erase command function |
| 2242 | * @mtd: MTD device structure |
| 2243 | * @page: the page address of the block which will be erased |
| 2244 | * |
| 2245 | * AND multi block erase command function. Erase 4 consecutive blocks. |
| 2246 | */ |
| 2247 | static void multi_erase_cmd(struct mtd_info *mtd, int page) |
| 2248 | { |
| 2249 | struct nand_chip *chip = mtd->priv; |
| 2250 | /* Send commands to erase a block */ |
| 2251 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2252 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2253 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2254 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2255 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 2256 | } |
| 2257 | |
| 2258 | /** |
| 2259 | * nand_erase - [MTD Interface] erase block(s) |
| 2260 | * @mtd: MTD device structure |
| 2261 | * @instr: erase instruction |
| 2262 | * |
| 2263 | * Erase one ore more blocks. |
| 2264 | */ |
| 2265 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 2266 | { |
| 2267 | struct nand_chip *chip = mtd->priv; |
| 2268 | int ret, page, retries = 0; |
| 2269 | page = (int)(instr->addr >> chip->page_shift); |
| 2270 | |
| 2271 | retry: |
| 2272 | ret = nand_erase_nand(mtd, instr, 0); |
| 2273 | if (ret) { |
| 2274 | if (chip->options & BBT_RELOCATION_IFBAD) { |
| 2275 | if (retries++ < 3) |
| 2276 | goto retry; |
| 2277 | |
| 2278 | ret = chip->block_markbad(mtd, |
| 2279 | (loff_t)(page & chip->pagemask) |
| 2280 | << chip->page_shift); |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | return ret; |
| 2285 | } |
| 2286 | |
| 2287 | #define BBT_PAGE_MASK 0xffffff3f |
| 2288 | /** |
| 2289 | * nand_erase_nand - [INTERN] erase block(s) |
| 2290 | * @mtd: MTD device structure |
| 2291 | * @instr: erase instruction |
| 2292 | * @allowbbt: allow erasing the bbt area |
| 2293 | * |
| 2294 | * Erase one ore more blocks. |
| 2295 | */ |
| 2296 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 2297 | int allowbbt) |
| 2298 | { |
| 2299 | int page, status, pages_per_block, ret, chipnr; |
| 2300 | struct nand_chip *chip = mtd->priv; |
| 2301 | loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0}; |
| 2302 | unsigned int bbt_masked_page = 0xffffffff; |
| 2303 | loff_t len; |
| 2304 | |
| 2305 | MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n", |
| 2306 | __func__, (unsigned long long)instr->addr, |
| 2307 | (unsigned long long)instr->len); |
| 2308 | |
| 2309 | if (check_offs_len(mtd, instr->addr, instr->len)) |
| 2310 | return -EINVAL; |
| 2311 | |
| 2312 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
| 2313 | |
| 2314 | /* Grab the lock and see if the device is available */ |
| 2315 | nand_get_device(chip, mtd, FL_ERASING); |
| 2316 | |
| 2317 | /* Shift to get first page */ |
| 2318 | page = (int)(instr->addr >> chip->page_shift); |
| 2319 | chipnr = (int)(instr->addr >> chip->chip_shift); |
| 2320 | |
| 2321 | /* Calculate pages in each block */ |
| 2322 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
| 2323 | |
| 2324 | /* Select the NAND device */ |
| 2325 | chip->select_chip(mtd, chipnr); |
| 2326 | |
| 2327 | /* Check, if it is write protected */ |
| 2328 | if (nand_check_wp(mtd)) { |
| 2329 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n", |
| 2330 | __func__); |
| 2331 | instr->state = MTD_ERASE_FAILED; |
| 2332 | goto erase_exit; |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * If BBT requires refresh, set the BBT page mask to see if the BBT |
| 2337 | * should be rewritten. Otherwise the mask is set to 0xffffffff which |
| 2338 | * can not be matched. This is also done when the bbt is actually |
| 2339 | * erased to avoid recursive updates. |
| 2340 | */ |
| 2341 | if (chip->options & BBT_AUTO_REFRESH && !allowbbt) |
| 2342 | bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK; |
| 2343 | |
| 2344 | /* Loop through the pages */ |
| 2345 | len = instr->len; |
| 2346 | |
| 2347 | instr->state = MTD_ERASING; |
| 2348 | |
| 2349 | while (len) { |
| 2350 | WATCHDOG_RESET(); |
| 2351 | /* Check if we have a bad block, we do not erase bad blocks! */ |
| 2352 | if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) << |
| 2353 | chip->page_shift, 0, allowbbt)) { |
| 2354 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 2355 | __func__, page); |
| 2356 | instr->state = MTD_ERASE_FAILED; |
| 2357 | goto erase_exit; |
| 2358 | } |
| 2359 | |
| 2360 | /* |
| 2361 | * Invalidate the page cache, if we erase the block which |
| 2362 | * contains the current cached page. |
| 2363 | */ |
| 2364 | if (page <= chip->pagebuf && chip->pagebuf < |
| 2365 | (page + pages_per_block)) |
| 2366 | chip->pagebuf = -1; |
| 2367 | |
| 2368 | chip->erase_cmd(mtd, page & chip->pagemask); |
| 2369 | |
| 2370 | status = chip->waitfunc(mtd, chip); |
| 2371 | |
| 2372 | /* |
| 2373 | * See if operation failed and additional status checks are |
| 2374 | * available |
| 2375 | */ |
| 2376 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 2377 | status = chip->errstat(mtd, chip, FL_ERASING, |
| 2378 | status, page); |
| 2379 | |
| 2380 | /* See if block erase succeeded */ |
| 2381 | if (status & NAND_STATUS_FAIL) { |
| 2382 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, " |
| 2383 | "page 0x%08x\n", __func__, page); |
| 2384 | instr->state = MTD_ERASE_FAILED; |
| 2385 | instr->fail_addr = |
| 2386 | ((loff_t)page << chip->page_shift); |
| 2387 | goto erase_exit; |
| 2388 | } |
| 2389 | |
| 2390 | /* |
| 2391 | * If BBT requires refresh, set the BBT rewrite flag to the |
| 2392 | * page being erased. |
| 2393 | */ |
| 2394 | if (bbt_masked_page != 0xffffffff && |
| 2395 | (page & BBT_PAGE_MASK) == bbt_masked_page) |
| 2396 | rewrite_bbt[chipnr] = |
| 2397 | ((loff_t)page << chip->page_shift); |
| 2398 | |
| 2399 | /* Increment page address and decrement length */ |
| 2400 | len -= (1 << chip->phys_erase_shift); |
| 2401 | page += pages_per_block; |
| 2402 | |
| 2403 | /* Check, if we cross a chip boundary */ |
| 2404 | if (len && !(page & chip->pagemask)) { |
| 2405 | chipnr++; |
| 2406 | chip->select_chip(mtd, -1); |
| 2407 | chip->select_chip(mtd, chipnr); |
| 2408 | |
| 2409 | /* |
| 2410 | * If BBT requires refresh and BBT-PERCHIP, set the BBT |
| 2411 | * page mask to see if this BBT should be rewritten. |
| 2412 | */ |
| 2413 | if (bbt_masked_page != 0xffffffff && |
| 2414 | (chip->bbt_td->options & NAND_BBT_PERCHIP)) |
| 2415 | bbt_masked_page = chip->bbt_td->pages[chipnr] & |
| 2416 | BBT_PAGE_MASK; |
| 2417 | } |
| 2418 | } |
| 2419 | instr->state = MTD_ERASE_DONE; |
| 2420 | |
| 2421 | erase_exit: |
| 2422 | |
| 2423 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
| 2424 | |
| 2425 | /* Deselect and wake up anyone waiting on the device */ |
| 2426 | nand_release_device(mtd); |
| 2427 | |
| 2428 | /* Do call back function */ |
| 2429 | if (!ret) |
| 2430 | mtd_erase_callback(instr); |
| 2431 | |
| 2432 | /* |
| 2433 | * If BBT requires refresh and erase was successful, rewrite any |
| 2434 | * selected bad block tables. |
| 2435 | */ |
| 2436 | if (bbt_masked_page == 0xffffffff || ret) |
| 2437 | return ret; |
| 2438 | |
| 2439 | for (chipnr = 0; chipnr < chip->numchips; chipnr++) { |
| 2440 | if (!rewrite_bbt[chipnr]) |
| 2441 | continue; |
| 2442 | /* Update the BBT for chip */ |
| 2443 | MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt " |
| 2444 | "(%d:0x%0llx 0x%0x)\n", __func__, chipnr, |
| 2445 | rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]); |
| 2446 | nand_update_bbt(mtd, rewrite_bbt[chipnr]); |
| 2447 | } |
| 2448 | |
| 2449 | /* Return more or less happy */ |
| 2450 | return ret; |
| 2451 | } |
| 2452 | |
| 2453 | /** |
| 2454 | * nand_sync - [MTD Interface] sync |
| 2455 | * @mtd: MTD device structure |
| 2456 | * |
| 2457 | * Sync is actually a wait for chip ready function. |
| 2458 | */ |
| 2459 | static void nand_sync(struct mtd_info *mtd) |
| 2460 | { |
| 2461 | struct nand_chip *chip = mtd->priv; |
| 2462 | |
| 2463 | MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__); |
| 2464 | |
| 2465 | /* Grab the lock and see if the device is available */ |
| 2466 | nand_get_device(chip, mtd, FL_SYNCING); |
| 2467 | /* Release it and go back */ |
| 2468 | nand_release_device(mtd); |
| 2469 | } |
| 2470 | |
| 2471 | /** |
| 2472 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
| 2473 | * @mtd: MTD device structure |
| 2474 | * @offs: offset relative to mtd start |
| 2475 | */ |
| 2476 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
| 2477 | { |
| 2478 | return nand_block_checkbad(mtd, offs, 1, 0); |
| 2479 | } |
| 2480 | |
| 2481 | /** |
| 2482 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
| 2483 | * @mtd: MTD device structure |
| 2484 | * @ofs: offset relative to mtd start |
| 2485 | */ |
| 2486 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 2487 | { |
| 2488 | struct nand_chip *chip = mtd->priv; |
| 2489 | int ret; |
| 2490 | |
| 2491 | ret = nand_block_isbad(mtd, ofs); |
| 2492 | if (ret) { |
| 2493 | /* If it was bad already, return success and do nothing */ |
| 2494 | if (ret > 0) |
| 2495 | return 0; |
| 2496 | return ret; |
| 2497 | } |
| 2498 | |
| 2499 | return chip->block_markbad(mtd, ofs); |
| 2500 | } |
| 2501 | |
| 2502 | /** |
| 2503 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 2504 | * @mtd: MTD device structure |
| 2505 | * @chip: nand chip info structure |
| 2506 | * @addr: feature address. |
| 2507 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 2508 | */ |
| 2509 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 2510 | int addr, uint8_t *subfeature_param) |
| 2511 | { |
| 2512 | int status; |
| 2513 | |
| 2514 | if (!chip->onfi_version) |
| 2515 | return -EINVAL; |
| 2516 | |
| 2517 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); |
| 2518 | chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN); |
| 2519 | status = chip->waitfunc(mtd, chip); |
| 2520 | if (status & NAND_STATUS_FAIL) |
| 2521 | return -EIO; |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | /** |
| 2526 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 2527 | * @mtd: MTD device structure |
| 2528 | * @chip: nand chip info structure |
| 2529 | * @addr: feature address. |
| 2530 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 2531 | */ |
| 2532 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 2533 | int addr, uint8_t *subfeature_param) |
| 2534 | { |
| 2535 | if (!chip->onfi_version) |
| 2536 | return -EINVAL; |
| 2537 | |
| 2538 | /* clear the sub feature parameters */ |
| 2539 | memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN); |
| 2540 | |
| 2541 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); |
| 2542 | chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN); |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
| 2546 | /* Set default functions */ |
| 2547 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
| 2548 | { |
| 2549 | /* check for proper chip_delay setup, set 20us if not */ |
| 2550 | if (!chip->chip_delay) |
| 2551 | chip->chip_delay = 20; |
| 2552 | |
| 2553 | /* check, if a user supplied command function given */ |
| 2554 | if (chip->cmdfunc == NULL) |
| 2555 | chip->cmdfunc = nand_command; |
| 2556 | |
| 2557 | /* check, if a user supplied wait function given */ |
| 2558 | if (chip->waitfunc == NULL) |
| 2559 | chip->waitfunc = nand_wait; |
| 2560 | |
| 2561 | if (!chip->select_chip) |
| 2562 | chip->select_chip = nand_select_chip; |
| 2563 | if (!chip->read_byte) |
| 2564 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 2565 | if (!chip->read_word) |
| 2566 | chip->read_word = nand_read_word; |
| 2567 | if (!chip->block_bad) |
| 2568 | chip->block_bad = nand_block_bad; |
| 2569 | if (!chip->block_markbad) |
| 2570 | chip->block_markbad = nand_default_block_markbad; |
| 2571 | if (!chip->write_buf) |
| 2572 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
| 2573 | if (!chip->read_buf) |
| 2574 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
| 2575 | if (!chip->verify_buf) |
| 2576 | chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf; |
| 2577 | if (!chip->scan_bbt) |
| 2578 | chip->scan_bbt = nand_default_bbt; |
| 2579 | if (!chip->controller) |
| 2580 | chip->controller = &chip->hwcontrol; |
| 2581 | } |
| 2582 | |
| 2583 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 2584 | /* Sanitize ONFI strings so we can safely print them */ |
| 2585 | static void sanitize_string(char *s, size_t len) |
| 2586 | { |
| 2587 | ssize_t i; |
| 2588 | |
| 2589 | /* Null terminate */ |
| 2590 | s[len - 1] = 0; |
| 2591 | |
| 2592 | /* Remove non printable chars */ |
| 2593 | for (i = 0; i < len - 1; i++) { |
| 2594 | if (s[i] < ' ' || s[i] > 127) |
| 2595 | s[i] = '?'; |
| 2596 | } |
| 2597 | |
| 2598 | /* Remove trailing spaces */ |
| 2599 | strim(s); |
| 2600 | } |
| 2601 | |
| 2602 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 2603 | { |
| 2604 | int i; |
| 2605 | while (len--) { |
| 2606 | crc ^= *p++ << 8; |
| 2607 | for (i = 0; i < 8; i++) |
| 2608 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 2609 | } |
| 2610 | |
| 2611 | return crc; |
| 2612 | } |
| 2613 | |
| 2614 | /* |
| 2615 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
| 2616 | */ |
| 2617 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, |
| 2618 | int *busw) |
| 2619 | { |
| 2620 | struct nand_onfi_params *p = &chip->onfi_params; |
| 2621 | int i; |
| 2622 | int val; |
| 2623 | |
| 2624 | /* Try ONFI for unknown chip or LP */ |
| 2625 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 2626 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 2627 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') |
| 2628 | return 0; |
| 2629 | |
| 2630 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 2631 | for (i = 0; i < 3; i++) { |
| 2632 | chip->read_buf(mtd, (uint8_t *)p, sizeof(*p)); |
| 2633 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 2634 | le16_to_cpu(p->crc)) { |
| 2635 | pr_info("ONFI param page %d valid\n", i); |
| 2636 | break; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | if (i == 3) |
| 2641 | return 0; |
| 2642 | |
| 2643 | /* Check version */ |
| 2644 | val = le16_to_cpu(p->revision); |
| 2645 | if (val & (1 << 5)) |
| 2646 | chip->onfi_version = 23; |
| 2647 | else if (val & (1 << 4)) |
| 2648 | chip->onfi_version = 22; |
| 2649 | else if (val & (1 << 3)) |
| 2650 | chip->onfi_version = 21; |
| 2651 | else if (val & (1 << 2)) |
| 2652 | chip->onfi_version = 20; |
| 2653 | else if (val & (1 << 1)) |
| 2654 | chip->onfi_version = 10; |
| 2655 | else |
| 2656 | chip->onfi_version = 0; |
| 2657 | |
| 2658 | if (!chip->onfi_version) { |
| 2659 | pr_info("%s: unsupported ONFI version: %d\n", __func__, val); |
| 2660 | return 0; |
| 2661 | } |
| 2662 | |
| 2663 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 2664 | sanitize_string(p->model, sizeof(p->model)); |
| 2665 | if (!mtd->name) |
| 2666 | mtd->name = p->model; |
| 2667 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 2668 | mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize; |
| 2669 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 2670 | chip->chipsize = le32_to_cpu(p->blocks_per_lun); |
| 2671 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 2672 | *busw = 0; |
| 2673 | if (le16_to_cpu(p->features) & 1) |
| 2674 | *busw = NAND_BUSWIDTH_16; |
| 2675 | |
| 2676 | pr_info("ONFI flash detected\n"); |
| 2677 | return 1; |
| 2678 | } |
| 2679 | #else |
| 2680 | static inline int nand_flash_detect_onfi(struct mtd_info *mtd, |
| 2681 | struct nand_chip *chip, |
| 2682 | int *busw) |
| 2683 | { |
| 2684 | return 0; |
| 2685 | } |
| 2686 | #endif |
| 2687 | |
| 2688 | /* |
| 2689 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 2690 | * @id_data: the ID string |
| 2691 | * @arrlen: the length of the @id_data array |
| 2692 | * @period: the period of repitition |
| 2693 | * |
| 2694 | * Check if an ID string is repeated within a given sequence of bytes at |
| 2695 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
| 2696 | * period of 2). This is a helper function for nand_id_len(). Returns non-zero |
| 2697 | * if the repetition has a period of @period; otherwise, returns zero. |
| 2698 | */ |
| 2699 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 2700 | { |
| 2701 | int i, j; |
| 2702 | for (i = 0; i < period; i++) |
| 2703 | for (j = i + period; j < arrlen; j += period) |
| 2704 | if (id_data[i] != id_data[j]) |
| 2705 | return 0; |
| 2706 | return 1; |
| 2707 | } |
| 2708 | |
| 2709 | /* |
| 2710 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 2711 | * @id_data: the ID string |
| 2712 | * @arrlen: the length of the @id_data array |
| 2713 | |
| 2714 | * Returns the length of the ID string, according to known wraparound/trailing |
| 2715 | * zero patterns. If no pattern exists, returns the length of the array. |
| 2716 | */ |
| 2717 | static int nand_id_len(u8 *id_data, int arrlen) |
| 2718 | { |
| 2719 | int last_nonzero, period; |
| 2720 | |
| 2721 | /* Find last non-zero byte */ |
| 2722 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 2723 | if (id_data[last_nonzero]) |
| 2724 | break; |
| 2725 | |
| 2726 | /* All zeros */ |
| 2727 | if (last_nonzero < 0) |
| 2728 | return 0; |
| 2729 | |
| 2730 | /* Calculate wraparound period */ |
| 2731 | for (period = 1; period < arrlen; period++) |
| 2732 | if (nand_id_has_period(id_data, arrlen, period)) |
| 2733 | break; |
| 2734 | |
| 2735 | /* There's a repeated pattern */ |
| 2736 | if (period < arrlen) |
| 2737 | return period; |
| 2738 | |
| 2739 | /* There are trailing zeros */ |
| 2740 | if (last_nonzero < arrlen - 1) |
| 2741 | return last_nonzero + 1; |
| 2742 | |
| 2743 | /* No pattern detected */ |
| 2744 | return arrlen; |
| 2745 | } |
| 2746 | |
| 2747 | /* |
| 2748 | * Many new NAND share similar device ID codes, which represent the size of the |
| 2749 | * chip. The rest of the parameters must be decoded according to generic or |
| 2750 | * manufacturer-specific "extended ID" decoding patterns. |
| 2751 | */ |
| 2752 | static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip, |
| 2753 | u8 id_data[8], int *busw) |
| 2754 | { |
| 2755 | int extid, id_len; |
| 2756 | /* The 3rd id byte holds MLC / multichip data */ |
| 2757 | chip->cellinfo = id_data[2]; |
| 2758 | /* The 4th id byte is the important one */ |
| 2759 | extid = id_data[3]; |
| 2760 | |
| 2761 | id_len = nand_id_len(id_data, 8); |
| 2762 | |
| 2763 | /* |
| 2764 | * Field definitions are in the following datasheets: |
| 2765 | * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32) |
| 2766 | * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44) |
| 2767 | * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22) |
| 2768 | * |
| 2769 | * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung |
| 2770 | * ID to decide what to do. |
| 2771 | */ |
| 2772 | if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG && |
| 2773 | (chip->cellinfo & NAND_CI_CELLTYPE_MSK) && |
| 2774 | id_data[5] != 0x00) { |
| 2775 | /* Calc pagesize */ |
| 2776 | mtd->writesize = 2048 << (extid & 0x03); |
| 2777 | extid >>= 2; |
| 2778 | /* Calc oobsize */ |
| 2779 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
| 2780 | case 1: |
| 2781 | mtd->oobsize = 128; |
| 2782 | break; |
| 2783 | case 2: |
| 2784 | mtd->oobsize = 218; |
| 2785 | break; |
| 2786 | case 3: |
| 2787 | mtd->oobsize = 400; |
| 2788 | break; |
| 2789 | case 4: |
| 2790 | mtd->oobsize = 436; |
| 2791 | break; |
| 2792 | case 5: |
| 2793 | mtd->oobsize = 512; |
| 2794 | break; |
| 2795 | case 6: |
| 2796 | default: /* Other cases are "reserved" (unknown) */ |
| 2797 | mtd->oobsize = 640; |
| 2798 | break; |
| 2799 | } |
| 2800 | extid >>= 2; |
| 2801 | /* Calc blocksize */ |
| 2802 | mtd->erasesize = (128 * 1024) << |
| 2803 | (((extid >> 1) & 0x04) | (extid & 0x03)); |
| 2804 | *busw = 0; |
| 2805 | } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX && |
| 2806 | (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) { |
| 2807 | unsigned int tmp; |
| 2808 | |
| 2809 | /* Calc pagesize */ |
| 2810 | mtd->writesize = 2048 << (extid & 0x03); |
| 2811 | extid >>= 2; |
| 2812 | /* Calc oobsize */ |
| 2813 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
| 2814 | case 0: |
| 2815 | mtd->oobsize = 128; |
| 2816 | break; |
| 2817 | case 1: |
| 2818 | mtd->oobsize = 224; |
| 2819 | break; |
| 2820 | case 2: |
| 2821 | mtd->oobsize = 448; |
| 2822 | break; |
| 2823 | case 3: |
| 2824 | mtd->oobsize = 64; |
| 2825 | break; |
| 2826 | case 4: |
| 2827 | mtd->oobsize = 32; |
| 2828 | break; |
| 2829 | case 5: |
| 2830 | mtd->oobsize = 16; |
| 2831 | break; |
| 2832 | default: |
| 2833 | mtd->oobsize = 640; |
| 2834 | break; |
| 2835 | } |
| 2836 | extid >>= 2; |
| 2837 | /* Calc blocksize */ |
| 2838 | tmp = ((extid >> 1) & 0x04) | (extid & 0x03); |
| 2839 | if (tmp < 0x03) |
| 2840 | mtd->erasesize = (128 * 1024) << tmp; |
| 2841 | else if (tmp == 0x03) |
| 2842 | mtd->erasesize = 768 * 1024; |
| 2843 | else |
| 2844 | mtd->erasesize = (64 * 1024) << tmp; |
| 2845 | *busw = 0; |
| 2846 | } else { |
| 2847 | /* Calc pagesize */ |
| 2848 | mtd->writesize = 1024 << (extid & 0x03); |
| 2849 | extid >>= 2; |
| 2850 | /* Calc oobsize */ |
| 2851 | mtd->oobsize = (8 << (extid & 0x01)) * |
| 2852 | (mtd->writesize >> 9); |
| 2853 | extid >>= 2; |
| 2854 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 2855 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 2856 | extid >>= 2; |
| 2857 | /* Get buswidth information */ |
| 2858 | *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | /* |
| 2863 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 2864 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 2865 | * the chip. |
| 2866 | */ |
| 2867 | static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip, |
| 2868 | const struct nand_flash_dev *type, u8 id_data[8], |
| 2869 | int *busw) |
| 2870 | { |
| 2871 | int maf_id = id_data[0]; |
| 2872 | |
| 2873 | mtd->erasesize = type->erasesize; |
| 2874 | mtd->writesize = type->pagesize; |
| 2875 | mtd->oobsize = mtd->writesize / 32; |
| 2876 | *busw = type->options & NAND_BUSWIDTH_16; |
| 2877 | |
| 2878 | /* |
| 2879 | * Check for Spansion/AMD ID + repeating 5th, 6th byte since |
| 2880 | * some Spansion chips have erasesize that conflicts with size |
| 2881 | * listed in nand_ids table. |
| 2882 | * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) |
| 2883 | */ |
| 2884 | if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00 |
| 2885 | && id_data[6] == 0x00 && id_data[7] == 0x00 |
| 2886 | && mtd->writesize == 512) { |
| 2887 | mtd->erasesize = 128 * 1024; |
| 2888 | mtd->erasesize <<= ((id_data[3] & 0x03) << 1); |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | /* |
| 2893 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 2894 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 2895 | * page size, cell-type information). |
| 2896 | */ |
| 2897 | static void nand_decode_bbm_options(struct mtd_info *mtd, |
| 2898 | struct nand_chip *chip, u8 id_data[8]) |
| 2899 | { |
| 2900 | int maf_id = id_data[0]; |
| 2901 | |
| 2902 | /* Set the bad block position */ |
| 2903 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 2904 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 2905 | else |
| 2906 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
| 2907 | |
| 2908 | /* |
| 2909 | * Bad block marker is stored in the last page of each block on Samsung |
| 2910 | * and Hynix MLC devices; stored in first two pages of each block on |
| 2911 | * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba, |
| 2912 | * AMD/Spansion, and Macronix. All others scan only the first page. |
| 2913 | */ |
| 2914 | if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) && |
| 2915 | (maf_id == NAND_MFR_SAMSUNG || |
| 2916 | maf_id == NAND_MFR_HYNIX)) |
| 2917 | chip->bbt_options |= NAND_BBT_SCANLASTPAGE; |
| 2918 | else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) && |
| 2919 | (maf_id == NAND_MFR_SAMSUNG || |
| 2920 | maf_id == NAND_MFR_HYNIX || |
| 2921 | maf_id == NAND_MFR_TOSHIBA || |
| 2922 | maf_id == NAND_MFR_AMD || |
| 2923 | maf_id == NAND_MFR_MACRONIX)) || |
| 2924 | (mtd->writesize == 2048 && |
| 2925 | maf_id == NAND_MFR_MICRON)) |
| 2926 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; |
| 2927 | } |
| 2928 | |
| 2929 | /* |
| 2930 | * Get the flash and manufacturer id and lookup if the type is supported. |
| 2931 | */ |
| 2932 | static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, |
| 2933 | struct nand_chip *chip, |
| 2934 | int busw, |
| 2935 | int *maf_id, int *dev_id, |
| 2936 | const struct nand_flash_dev *type) |
| 2937 | { |
| 2938 | const char *name; |
| 2939 | int i, maf_idx; |
| 2940 | u8 id_data[8]; |
| 2941 | |
| 2942 | /* Select the device */ |
| 2943 | chip->select_chip(mtd, 0); |
| 2944 | |
| 2945 | /* |
| 2946 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
| 2947 | * after power-up. |
| 2948 | */ |
| 2949 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 2950 | |
| 2951 | /* Send the command for reading device ID */ |
| 2952 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 2953 | |
| 2954 | /* Read manufacturer and device IDs */ |
| 2955 | *maf_id = chip->read_byte(mtd); |
| 2956 | *dev_id = chip->read_byte(mtd); |
| 2957 | |
| 2958 | /* |
| 2959 | * Try again to make sure, as some systems the bus-hold or other |
| 2960 | * interface concerns can cause random data which looks like a |
| 2961 | * possibly credible NAND flash to appear. If the two results do |
| 2962 | * not match, ignore the device completely. |
| 2963 | */ |
| 2964 | |
| 2965 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 2966 | |
| 2967 | /* Read entire ID string */ |
| 2968 | for (i = 0; i < 8; i++) |
| 2969 | id_data[i] = chip->read_byte(mtd); |
| 2970 | |
| 2971 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
| 2972 | pr_info("%s: second ID read did not match " |
| 2973 | "%02x,%02x against %02x,%02x\n", __func__, |
| 2974 | *maf_id, *dev_id, id_data[0], id_data[1]); |
| 2975 | return ERR_PTR(-ENODEV); |
| 2976 | } |
| 2977 | |
| 2978 | if (!type) |
| 2979 | type = nand_flash_ids; |
| 2980 | |
| 2981 | for (; type->name != NULL; type++) |
| 2982 | if (*dev_id == type->id) |
| 2983 | break; |
| 2984 | |
| 2985 | chip->onfi_version = 0; |
| 2986 | if (!type->name || !type->pagesize) { |
| 2987 | /* Check is chip is ONFI compliant */ |
| 2988 | if (nand_flash_detect_onfi(mtd, chip, &busw)) |
| 2989 | goto ident_done; |
| 2990 | } |
| 2991 | |
| 2992 | if (!type->name) |
| 2993 | return ERR_PTR(-ENODEV); |
| 2994 | |
| 2995 | if (!mtd->name) |
| 2996 | mtd->name = type->name; |
| 2997 | |
| 2998 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 2999 | |
| 3000 | if (!type->pagesize && chip->init_size) { |
| 3001 | /* Set the pagesize, oobsize, erasesize by the driver */ |
| 3002 | busw = chip->init_size(mtd, chip, id_data); |
| 3003 | } else if (!type->pagesize) { |
| 3004 | /* Decode parameters from extended ID */ |
| 3005 | nand_decode_ext_id(mtd, chip, id_data, &busw); |
| 3006 | } else { |
| 3007 | nand_decode_id(mtd, chip, type, id_data, &busw); |
| 3008 | } |
| 3009 | /* Get chip options, preserve non chip based options */ |
| 3010 | chip->options |= type->options; |
| 3011 | |
| 3012 | /* |
| 3013 | * Check if chip is not a Samsung device. Do not clear the |
| 3014 | * options for chips which do not have an extended id. |
| 3015 | */ |
| 3016 | if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) |
| 3017 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; |
| 3018 | ident_done: |
| 3019 | |
| 3020 | /* Try to identify manufacturer */ |
| 3021 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
| 3022 | if (nand_manuf_ids[maf_idx].id == *maf_id) |
| 3023 | break; |
| 3024 | } |
| 3025 | |
| 3026 | /* |
| 3027 | * Check, if buswidth is correct. Hardware drivers should set |
| 3028 | * chip correct! |
| 3029 | */ |
| 3030 | if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 3031 | pr_info("NAND device: Manufacturer ID:" |
| 3032 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, |
| 3033 | *dev_id, nand_manuf_ids[maf_idx].name, mtd->name); |
| 3034 | pr_warn("NAND bus width %d instead %d bit\n", |
| 3035 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
| 3036 | busw ? 16 : 8); |
| 3037 | return ERR_PTR(-EINVAL); |
| 3038 | } |
| 3039 | |
| 3040 | nand_decode_bbm_options(mtd, chip, id_data); |
| 3041 | |
| 3042 | /* Calculate the address shift from the page size */ |
| 3043 | chip->page_shift = ffs(mtd->writesize) - 1; |
| 3044 | /* Convert chipsize to number of pages per chip -1 */ |
| 3045 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
| 3046 | |
| 3047 | chip->bbt_erase_shift = chip->phys_erase_shift = |
| 3048 | ffs(mtd->erasesize) - 1; |
| 3049 | if (chip->chipsize & 0xffffffff) |
| 3050 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
| 3051 | else { |
| 3052 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 3053 | chip->chip_shift += 32 - 1; |
| 3054 | } |
| 3055 | |
| 3056 | chip->badblockbits = 8; |
| 3057 | |
| 3058 | /* Check for AND chips with 4 page planes */ |
| 3059 | if (chip->options & NAND_4PAGE_ARRAY) |
| 3060 | chip->erase_cmd = multi_erase_cmd; |
| 3061 | else |
| 3062 | chip->erase_cmd = single_erase_cmd; |
| 3063 | |
| 3064 | /* Do not replace user supplied command function! */ |
| 3065 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3066 | chip->cmdfunc = nand_command_lp; |
| 3067 | |
| 3068 | name = type->name; |
| 3069 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3070 | if (chip->onfi_version) |
| 3071 | name = chip->onfi_params.model; |
| 3072 | #endif |
| 3073 | pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s)," |
| 3074 | " page size: %d, OOB size: %d\n", |
| 3075 | *maf_id, *dev_id, nand_manuf_ids[maf_idx].name, |
| 3076 | name, |
| 3077 | mtd->writesize, mtd->oobsize); |
| 3078 | |
| 3079 | #ifdef CONFIG_SPINAND_GIGADEVICE |
| 3080 | chip->cmdfunc(mtd, NAND_CMD_LOCK, 0x00, -1); |
| 3081 | #endif |
| 3082 | return type; |
| 3083 | } |
| 3084 | |
| 3085 | /** |
| 3086 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
| 3087 | * @mtd: MTD device structure |
| 3088 | * @maxchips: number of chips to scan for |
| 3089 | * @table: alternative NAND ID table |
| 3090 | * |
| 3091 | * This is the first phase of the normal nand_scan() function. It reads the |
| 3092 | * flash ID and sets up MTD fields accordingly. |
| 3093 | * |
| 3094 | * The mtd->owner field must be set to the module of the caller. |
| 3095 | */ |
| 3096 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
| 3097 | const struct nand_flash_dev *table) |
| 3098 | { |
| 3099 | int i, busw, nand_maf_id, nand_dev_id; |
| 3100 | struct nand_chip *chip = mtd->priv; |
| 3101 | const struct nand_flash_dev *type; |
| 3102 | |
| 3103 | /* Get buswidth to select the correct functions */ |
| 3104 | busw = chip->options & NAND_BUSWIDTH_16; |
| 3105 | /* Set the default functions */ |
| 3106 | nand_set_defaults(chip, busw); |
| 3107 | |
| 3108 | /* Read the flash type */ |
| 3109 | type = nand_get_flash_type(mtd, chip, busw, |
| 3110 | &nand_maf_id, &nand_dev_id, table); |
| 3111 | |
| 3112 | if (IS_ERR(type)) { |
| 3113 | #ifndef CONFIG_SYS_NAND_QUIET_TEST |
| 3114 | pr_warn("No NAND device found\n"); |
| 3115 | #endif |
| 3116 | chip->select_chip(mtd, -1); |
| 3117 | return PTR_ERR(type); |
| 3118 | } |
| 3119 | |
| 3120 | /* Check for a chip array */ |
| 3121 | for (i = 1; i < maxchips; i++) { |
| 3122 | chip->select_chip(mtd, i); |
| 3123 | /* See comment in nand_get_flash_type for reset */ |
| 3124 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 3125 | /* Send the command for reading device ID */ |
| 3126 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 3127 | /* Read manufacturer and device IDs */ |
| 3128 | if (nand_maf_id != chip->read_byte(mtd) || |
| 3129 | nand_dev_id != chip->read_byte(mtd)) |
| 3130 | break; |
| 3131 | } |
| 3132 | #ifdef DEBUG |
| 3133 | if (i > 1) |
| 3134 | pr_info("%d NAND chips detected\n", i); |
| 3135 | #endif |
| 3136 | |
| 3137 | /* Store the number of chips and calc total size for mtd */ |
| 3138 | chip->numchips = i; |
| 3139 | mtd->size = i * chip->chipsize; |
| 3140 | /* Gigadevice SPI NAND 1020-1023 blocks are reserved */ |
| 3141 | #ifdef CONFIG_SPINAND_GIGADEVICE |
| 3142 | if((nand_maf_id == NAND_MFR_GIGADEVICE) && |
| 3143 | nand_dev_id == 0xe1) |
| 3144 | mtd->size -= 4 * mtd->erasesize; |
| 3145 | #endif |
| 3146 | return 0; |
| 3147 | } |
| 3148 | |
| 3149 | |
| 3150 | /** |
| 3151 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
| 3152 | * @mtd: MTD device structure |
| 3153 | * |
| 3154 | * This is the second phase of the normal nand_scan() function. It fills out |
| 3155 | * all the uninitialized function pointers with the defaults and scans for a |
| 3156 | * bad block table if appropriate. |
| 3157 | */ |
| 3158 | int nand_scan_tail(struct mtd_info *mtd) |
| 3159 | { |
| 3160 | int i; |
| 3161 | struct nand_chip *chip = mtd->priv; |
| 3162 | |
| 3163 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
| 3164 | BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
| 3165 | !(chip->bbt_options & NAND_BBT_USE_FLASH)); |
| 3166 | |
| 3167 | if (!(chip->options & NAND_OWN_BUFFERS)) |
| 3168 | chip->buffers = memalign(ARCH_DMA_MINALIGN, |
| 3169 | sizeof(*chip->buffers)); |
| 3170 | if (!chip->buffers) |
| 3171 | return -ENOMEM; |
| 3172 | |
| 3173 | /* Set the internal oob buffer location, just after the page data */ |
| 3174 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
| 3175 | |
| 3176 | /* |
| 3177 | * If no default placement scheme is given, select an appropriate one. |
| 3178 | */ |
| 3179 | if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) { |
| 3180 | switch (mtd->oobsize) { |
| 3181 | case 8: |
| 3182 | chip->ecc.layout = &nand_oob_8; |
| 3183 | break; |
| 3184 | case 16: |
| 3185 | chip->ecc.layout = &nand_oob_16; |
| 3186 | break; |
| 3187 | case 64: |
| 3188 | chip->ecc.layout = &nand_oob_64; |
| 3189 | break; |
| 3190 | case 128: |
| 3191 | chip->ecc.layout = &nand_oob_128; |
| 3192 | break; |
| 3193 | default: |
| 3194 | pr_warn("No oob scheme defined for oobsize %d\n", |
| 3195 | mtd->oobsize); |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | if (!chip->write_page) |
| 3200 | chip->write_page = nand_write_page; |
| 3201 | |
| 3202 | /* set for ONFI nand */ |
| 3203 | if (!chip->onfi_set_features) |
| 3204 | chip->onfi_set_features = nand_onfi_set_features; |
| 3205 | if (!chip->onfi_get_features) |
| 3206 | chip->onfi_get_features = nand_onfi_get_features; |
| 3207 | |
| 3208 | /* |
| 3209 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
| 3210 | * selected and we have 256 byte pagesize fallback to software ECC |
| 3211 | */ |
| 3212 | |
| 3213 | switch (chip->ecc.mode) { |
| 3214 | case NAND_ECC_HW_OOB_FIRST: |
| 3215 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
| 3216 | if (!chip->ecc.calculate || !chip->ecc.correct || |
| 3217 | !chip->ecc.hwctl) { |
| 3218 | pr_warn("No ECC functions supplied; " |
| 3219 | "hardware ECC not possible\n"); |
| 3220 | BUG(); |
| 3221 | } |
| 3222 | if (!chip->ecc.read_page) |
| 3223 | chip->ecc.read_page = nand_read_page_hwecc_oob_first; |
| 3224 | |
| 3225 | case NAND_ECC_HW: |
| 3226 | /* Use standard hwecc read page function? */ |
| 3227 | if (!chip->ecc.read_page) |
| 3228 | chip->ecc.read_page = nand_read_page_hwecc; |
| 3229 | if (!chip->ecc.write_page) |
| 3230 | chip->ecc.write_page = nand_write_page_hwecc; |
| 3231 | if (!chip->ecc.read_page_raw) |
| 3232 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 3233 | if (!chip->ecc.write_page_raw) |
| 3234 | chip->ecc.write_page_raw = nand_write_page_raw; |
| 3235 | if (!chip->ecc.read_oob) |
| 3236 | chip->ecc.read_oob = nand_read_oob_std; |
| 3237 | if (!chip->ecc.write_oob) |
| 3238 | chip->ecc.write_oob = nand_write_oob_std; |
| 3239 | |
| 3240 | case NAND_ECC_HW_SYNDROME: |
| 3241 | if ((!chip->ecc.calculate || !chip->ecc.correct || |
| 3242 | !chip->ecc.hwctl) && |
| 3243 | (!chip->ecc.read_page || |
| 3244 | chip->ecc.read_page == nand_read_page_hwecc || |
| 3245 | !chip->ecc.write_page || |
| 3246 | chip->ecc.write_page == nand_write_page_hwecc)) { |
| 3247 | pr_warn("No ECC functions supplied; " |
| 3248 | "hardware ECC not possible\n"); |
| 3249 | BUG(); |
| 3250 | } |
| 3251 | /* Use standard syndrome read/write page function? */ |
| 3252 | if (!chip->ecc.read_page) |
| 3253 | chip->ecc.read_page = nand_read_page_syndrome; |
| 3254 | if (!chip->ecc.write_page) |
| 3255 | chip->ecc.write_page = nand_write_page_syndrome; |
| 3256 | if (!chip->ecc.read_page_raw) |
| 3257 | chip->ecc.read_page_raw = nand_read_page_raw_syndrome; |
| 3258 | if (!chip->ecc.write_page_raw) |
| 3259 | chip->ecc.write_page_raw = nand_write_page_raw_syndrome; |
| 3260 | if (!chip->ecc.read_oob) |
| 3261 | chip->ecc.read_oob = nand_read_oob_syndrome; |
| 3262 | if (!chip->ecc.write_oob) |
| 3263 | chip->ecc.write_oob = nand_write_oob_syndrome; |
| 3264 | |
| 3265 | if (mtd->writesize >= chip->ecc.size) { |
| 3266 | #if 0 |
| 3267 | if (!chip->ecc.strength) { |
| 3268 | pr_warn("Driver must set ecc.strength when using hardware ECC\n"); |
| 3269 | BUG(); |
| 3270 | } |
| 3271 | #endif |
| 3272 | break; |
| 3273 | } |
| 3274 | |
| 3275 | pr_warn("%d byte HW ECC not possible on " |
| 3276 | "%d byte page size, fallback to SW ECC\n", |
| 3277 | chip->ecc.size, mtd->writesize); |
| 3278 | chip->ecc.mode = NAND_ECC_SOFT; |
| 3279 | |
| 3280 | case NAND_ECC_SOFT: |
| 3281 | chip->ecc.calculate = nand_calculate_ecc; |
| 3282 | chip->ecc.correct = nand_correct_data; |
| 3283 | chip->ecc.read_page = nand_read_page_swecc; |
| 3284 | chip->ecc.read_subpage = nand_read_subpage; |
| 3285 | chip->ecc.write_page = nand_write_page_swecc; |
| 3286 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 3287 | chip->ecc.write_page_raw = nand_write_page_raw; |
| 3288 | chip->ecc.read_oob = nand_read_oob_std; |
| 3289 | chip->ecc.write_oob = nand_write_oob_std; |
| 3290 | if (!chip->ecc.size) |
| 3291 | chip->ecc.size = 256; |
| 3292 | chip->ecc.bytes = 3; |
| 3293 | chip->ecc.strength = 1; |
| 3294 | break; |
| 3295 | |
| 3296 | case NAND_ECC_SOFT_BCH: |
| 3297 | if (!mtd_nand_has_bch()) { |
| 3298 | pr_warn("CONFIG_MTD_ECC_BCH not enabled\n"); |
| 3299 | return -EINVAL; |
| 3300 | } |
| 3301 | chip->ecc.calculate = nand_bch_calculate_ecc; |
| 3302 | chip->ecc.correct = nand_bch_correct_data; |
| 3303 | chip->ecc.read_page = nand_read_page_swecc; |
| 3304 | chip->ecc.read_subpage = nand_read_subpage; |
| 3305 | chip->ecc.write_page = nand_write_page_swecc; |
| 3306 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 3307 | chip->ecc.write_page_raw = nand_write_page_raw; |
| 3308 | chip->ecc.read_oob = nand_read_oob_std; |
| 3309 | chip->ecc.write_oob = nand_write_oob_std; |
| 3310 | /* |
| 3311 | * Board driver should supply ecc.size and ecc.bytes values to |
| 3312 | * select how many bits are correctable; see nand_bch_init() |
| 3313 | * for details. Otherwise, default to 4 bits for large page |
| 3314 | * devices. |
| 3315 | */ |
| 3316 | if (!chip->ecc.size && (mtd->oobsize >= 64)) { |
| 3317 | chip->ecc.size = 512; |
| 3318 | chip->ecc.bytes = 7; |
| 3319 | } |
| 3320 | chip->ecc.priv = nand_bch_init(mtd, |
| 3321 | chip->ecc.size, |
| 3322 | chip->ecc.bytes, |
| 3323 | &chip->ecc.layout); |
| 3324 | if (!chip->ecc.priv) |
| 3325 | pr_warn("BCH ECC initialization failed!\n"); |
| 3326 | chip->ecc.strength = |
| 3327 | chip->ecc.bytes * 8 / fls(8 * chip->ecc.size); |
| 3328 | break; |
| 3329 | |
| 3330 | case NAND_ECC_NONE: |
| 3331 | pr_warn("NAND_ECC_NONE selected by board driver. " |
| 3332 | "This is not recommended !!\n"); |
| 3333 | chip->ecc.read_page = nand_read_page_raw; |
| 3334 | chip->ecc.write_page = nand_write_page_raw; |
| 3335 | chip->ecc.read_oob = nand_read_oob_std; |
| 3336 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 3337 | chip->ecc.write_page_raw = nand_write_page_raw; |
| 3338 | chip->ecc.write_oob = nand_write_oob_std; |
| 3339 | chip->ecc.size = mtd->writesize; |
| 3340 | chip->ecc.bytes = 0; |
| 3341 | break; |
| 3342 | |
| 3343 | default: |
| 3344 | pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode); |
| 3345 | BUG(); |
| 3346 | } |
| 3347 | |
| 3348 | /* For many systems, the standard OOB write also works for raw */ |
| 3349 | if (!chip->ecc.read_oob_raw) |
| 3350 | chip->ecc.read_oob_raw = chip->ecc.read_oob; |
| 3351 | if (!chip->ecc.write_oob_raw) |
| 3352 | chip->ecc.write_oob_raw = chip->ecc.write_oob; |
| 3353 | |
| 3354 | /* |
| 3355 | * The number of bytes available for a client to place data into |
| 3356 | * the out of band area. |
| 3357 | */ |
| 3358 | chip->ecc.layout->oobavail = 0; |
| 3359 | for (i = 0; chip->ecc.layout->oobfree[i].length |
| 3360 | && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++) |
| 3361 | chip->ecc.layout->oobavail += |
| 3362 | chip->ecc.layout->oobfree[i].length; |
| 3363 | mtd->oobavail = chip->ecc.layout->oobavail; |
| 3364 | |
| 3365 | /* |
| 3366 | * Set the number of read / write steps for one page depending on ECC |
| 3367 | * mode. |
| 3368 | */ |
| 3369 | chip->ecc.steps = mtd->writesize / chip->ecc.size; |
| 3370 | if (chip->ecc.steps * chip->ecc.size != mtd->writesize) { |
| 3371 | pr_warn("Invalid ECC parameters\n"); |
| 3372 | BUG(); |
| 3373 | } |
| 3374 | chip->ecc.total = chip->ecc.steps * chip->ecc.bytes; |
| 3375 | |
| 3376 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
| 3377 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3378 | !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) { |
| 3379 | switch (chip->ecc.steps) { |
| 3380 | case 2: |
| 3381 | mtd->subpage_sft = 1; |
| 3382 | break; |
| 3383 | case 4: |
| 3384 | case 8: |
| 3385 | case 16: |
| 3386 | mtd->subpage_sft = 2; |
| 3387 | break; |
| 3388 | } |
| 3389 | } |
| 3390 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 3391 | |
| 3392 | /* Initialize state */ |
| 3393 | chip->state = FL_READY; |
| 3394 | |
| 3395 | /* De-select the device */ |
| 3396 | chip->select_chip(mtd, -1); |
| 3397 | |
| 3398 | /* Invalidate the pagebuffer reference */ |
| 3399 | chip->pagebuf = -1; |
| 3400 | |
| 3401 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
| 3402 | if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9)) |
| 3403 | chip->options |= NAND_SUBPAGE_READ; |
| 3404 | |
| 3405 | /* Fill in remaining MTD driver data */ |
| 3406 | mtd->type = MTD_NANDFLASH; |
| 3407 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 3408 | MTD_CAP_NANDFLASH; |
| 3409 | mtd->_erase = nand_erase; |
| 3410 | mtd->_point = NULL; |
| 3411 | mtd->_unpoint = NULL; |
| 3412 | mtd->_read = nand_read; |
| 3413 | mtd->_write = nand_write; |
| 3414 | mtd->_read_oob = nand_read_oob; |
| 3415 | mtd->_write_oob = nand_write_oob; |
| 3416 | mtd->_sync = nand_sync; |
| 3417 | mtd->_lock = NULL; |
| 3418 | mtd->_unlock = NULL; |
| 3419 | mtd->_block_isbad = nand_block_isbad; |
| 3420 | mtd->_block_markbad = nand_block_markbad; |
| 3421 | |
| 3422 | /* propagate ecc info to mtd_info */ |
| 3423 | mtd->ecclayout = chip->ecc.layout; |
| 3424 | mtd->ecc_strength = chip->ecc.strength; |
| 3425 | /* |
| 3426 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 3427 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 3428 | * properly set. |
| 3429 | */ |
| 3430 | if (!mtd->bitflip_threshold) |
| 3431 | mtd->bitflip_threshold = mtd->ecc_strength; |
| 3432 | |
| 3433 | /* Check, if we should skip the bad block table scan */ |
| 3434 | if (chip->options & NAND_SKIP_BBTSCAN) |
| 3435 | chip->options |= NAND_BBT_SCANNED; |
| 3436 | |
| 3437 | return 0; |
| 3438 | } |
| 3439 | |
| 3440 | /** |
| 3441 | * nand_scan - [NAND Interface] Scan for the NAND device |
| 3442 | * @mtd: MTD device structure |
| 3443 | * @maxchips: number of chips to scan for |
| 3444 | * |
| 3445 | * This fills out all the uninitialized function pointers with the defaults. |
| 3446 | * The flash ID is read and the mtd/chip structures are filled with the |
| 3447 | * appropriate values. The mtd->owner field must be set to the module of the |
| 3448 | * caller. |
| 3449 | */ |
| 3450 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 3451 | { |
| 3452 | int ret; |
| 3453 | |
| 3454 | ret = nand_scan_ident(mtd, maxchips, NULL); |
| 3455 | if (!ret) |
| 3456 | ret = nand_scan_tail(mtd); |
| 3457 | return ret; |
| 3458 | } |
| 3459 | |
| 3460 | /** |
| 3461 | * nand_release - [NAND Interface] Free resources held by the NAND device |
| 3462 | * @mtd: MTD device structure |
| 3463 | */ |
| 3464 | void nand_release(struct mtd_info *mtd) |
| 3465 | { |
| 3466 | struct nand_chip *chip = mtd->priv; |
| 3467 | |
| 3468 | if (chip->ecc.mode == NAND_ECC_SOFT_BCH) |
| 3469 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 3470 | |
| 3471 | #ifdef CONFIG_MTD_PARTITIONS |
| 3472 | /* Deregister partitions */ |
| 3473 | del_mtd_partitions(mtd); |
| 3474 | #endif |
| 3475 | |
| 3476 | /* Free bad block table memory */ |
| 3477 | kfree(chip->bbt); |
| 3478 | if (!(chip->options & NAND_OWN_BUFFERS)) |
| 3479 | kfree(chip->buffers); |
| 3480 | |
| 3481 | /* Free bad block descriptor memory */ |
| 3482 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 3483 | & NAND_BBT_DYNAMICSTRUCT) |
| 3484 | kfree(chip->badblock_pattern); |
| 3485 | } |