Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mtd: rawnand: gpmi: Add large oob bch setting support

The code change proposes a new way to set bch geometry for large oob
NAND (oobsize > 1KB). In this case, previous implementation can NOT
guarantee the bad block mark always locates in data chunk, so we need a
new way to do it. The general idea is,

1.Try all ECC strength from the maximum ecc that controller can support
to minimum value required by NAND chip, any ECC strength makes the
BBM locate in data chunk can be eligible.

2.If none of them works, using separate ECC for meta, which will add
one extra ecc with the same ECC strength as other data chunks. This
extra ECC can guarantee BBM located in data chunk, also we need to
check if oob can afford it.

Signed-off-by: Han Xu <han.xu@nxp.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20220412025246.24269-6-han.xu@nxp.com

authored by

Han Xu and committed by
Miquel Raynal
d9edc4bc 2fb038ea

+197 -5
+194 -5
drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c
··· 266 266 return true; 267 267 } 268 268 269 + /* check if bbm locates in data chunk rather than ecc chunk */ 270 + static bool bbm_in_data_chunk(struct gpmi_nand_data *this, 271 + unsigned int *chunk_num) 272 + { 273 + struct bch_geometry *geo = &this->bch_geometry; 274 + struct nand_chip *chip = &this->nand; 275 + struct mtd_info *mtd = nand_to_mtd(chip); 276 + unsigned int i, j; 277 + 278 + if (geo->ecc0_chunk_size != geo->eccn_chunk_size) { 279 + dev_err(this->dev, 280 + "The size of ecc0_chunk must equal to eccn_chunk\n"); 281 + return false; 282 + } 283 + 284 + i = (mtd->writesize * 8 - geo->metadata_size * 8) / 285 + (geo->gf_len * geo->ecc_strength + 286 + geo->eccn_chunk_size * 8); 287 + 288 + j = (mtd->writesize * 8 - geo->metadata_size * 8) - 289 + (geo->gf_len * geo->ecc_strength + 290 + geo->eccn_chunk_size * 8) * i; 291 + 292 + if (j < geo->eccn_chunk_size * 8) { 293 + *chunk_num = i+1; 294 + dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n", 295 + geo->ecc_strength, *chunk_num); 296 + return true; 297 + } 298 + 299 + return false; 300 + } 301 + 269 302 /* 270 303 * If we can get the ECC information from the nand chip, we do not 271 304 * need to calculate them ourselves. ··· 448 415 return round_down(ecc_strength, 2); 449 416 } 450 417 418 + static int set_geometry_for_large_oob(struct gpmi_nand_data *this) 419 + { 420 + struct bch_geometry *geo = &this->bch_geometry; 421 + struct nand_chip *chip = &this->nand; 422 + struct mtd_info *mtd = nand_to_mtd(chip); 423 + const struct nand_ecc_props *requirements = 424 + nanddev_get_ecc_requirements(&chip->base); 425 + unsigned int block_mark_bit_offset; 426 + unsigned int max_ecc; 427 + unsigned int bbm_chunk; 428 + unsigned int i; 429 + 430 + /* sanity check for the minimum ecc nand required */ 431 + if (!(requirements->strength > 0 && 432 + requirements->step_size > 0)) 433 + return -EINVAL; 434 + geo->ecc_strength = requirements->strength; 435 + 436 + /* check if platform can support this nand */ 437 + if (!gpmi_check_ecc(this)) { 438 + dev_err(this->dev, 439 + "unsupported NAND chip, minimum ecc required %d\n", 440 + geo->ecc_strength); 441 + return -EINVAL; 442 + } 443 + 444 + /* calculate the maximum ecc platform can support*/ 445 + geo->metadata_size = 10; 446 + geo->gf_len = 14; 447 + geo->ecc0_chunk_size = 1024; 448 + geo->eccn_chunk_size = 1024; 449 + geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size; 450 + max_ecc = min(get_ecc_strength(this), 451 + this->devdata->bch_max_ecc_strength); 452 + 453 + /* 454 + * search a supported ecc strength that makes bbm 455 + * located in data chunk 456 + */ 457 + geo->ecc_strength = max_ecc; 458 + while (!(geo->ecc_strength < requirements->strength)) { 459 + if (bbm_in_data_chunk(this, &bbm_chunk)) 460 + goto geo_setting; 461 + geo->ecc_strength -= 2; 462 + } 463 + 464 + /* if none of them works, keep using the minimum ecc */ 465 + /* nand required but changing ecc page layout */ 466 + geo->ecc_strength = requirements->strength; 467 + /* add extra ecc for meta data */ 468 + geo->ecc0_chunk_size = 0; 469 + geo->ecc_chunk_count = (mtd->writesize / geo->eccn_chunk_size) + 1; 470 + geo->ecc_for_meta = 1; 471 + /* check if oob can afford this extra ecc chunk */ 472 + if (mtd->oobsize * 8 < geo->metadata_size * 8 + 473 + geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) { 474 + dev_err(this->dev, "unsupported NAND chip with new layout\n"); 475 + return -EINVAL; 476 + } 477 + 478 + /* calculate in which chunk bbm located */ 479 + bbm_chunk = (mtd->writesize * 8 - geo->metadata_size * 8 - 480 + geo->gf_len * geo->ecc_strength) / 481 + (geo->gf_len * geo->ecc_strength + 482 + geo->eccn_chunk_size * 8) + 1; 483 + 484 + geo_setting: 485 + 486 + geo->page_size = mtd->writesize + geo->metadata_size + 487 + (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8; 488 + geo->payload_size = mtd->writesize; 489 + 490 + /* 491 + * The auxiliary buffer contains the metadata and the ECC status. The 492 + * metadata is padded to the nearest 32-bit boundary. The ECC status 493 + * contains one byte for every ECC chunk, and is also padded to the 494 + * nearest 32-bit boundary. 495 + */ 496 + geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4); 497 + geo->auxiliary_size = ALIGN(geo->metadata_size, 4) 498 + + ALIGN(geo->ecc_chunk_count, 4); 499 + 500 + if (!this->swap_block_mark) 501 + return 0; 502 + 503 + /* calculate the number of ecc chunk behind the bbm */ 504 + i = (mtd->writesize / geo->eccn_chunk_size) - bbm_chunk + 1; 505 + 506 + block_mark_bit_offset = mtd->writesize * 8 - 507 + (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i) 508 + + geo->metadata_size * 8); 509 + 510 + geo->block_mark_byte_offset = block_mark_bit_offset / 8; 511 + geo->block_mark_bit_offset = block_mark_bit_offset % 8; 512 + 513 + dev_dbg(this->dev, "BCH Geometry :\n" 514 + "GF length : %u\n" 515 + "ECC Strength : %u\n" 516 + "Page Size in Bytes : %u\n" 517 + "Metadata Size in Bytes : %u\n" 518 + "ECC0 Chunk Size in Bytes: %u\n" 519 + "ECCn Chunk Size in Bytes: %u\n" 520 + "ECC Chunk Count : %u\n" 521 + "Payload Size in Bytes : %u\n" 522 + "Auxiliary Size in Bytes: %u\n" 523 + "Auxiliary Status Offset: %u\n" 524 + "Block Mark Byte Offset : %u\n" 525 + "Block Mark Bit Offset : %u\n" 526 + "Block Mark in chunk : %u\n" 527 + "Ecc for Meta data : %u\n", 528 + geo->gf_len, 529 + geo->ecc_strength, 530 + geo->page_size, 531 + geo->metadata_size, 532 + geo->ecc0_chunk_size, 533 + geo->eccn_chunk_size, 534 + geo->ecc_chunk_count, 535 + geo->payload_size, 536 + geo->auxiliary_size, 537 + geo->auxiliary_status_offset, 538 + geo->block_mark_byte_offset, 539 + geo->block_mark_bit_offset, 540 + bbm_chunk, 541 + geo->ecc_for_meta); 542 + 543 + return 0; 544 + } 545 + 451 546 static int legacy_set_geometry(struct gpmi_nand_data *this) 452 547 { 453 548 struct bch_geometry *geo = &this->bch_geometry; ··· 707 546 !(requirements->strength > 0 && requirements->step_size > 0)) { 708 547 dev_dbg(this->dev, "use legacy bch geometry\n"); 709 548 err = legacy_set_geometry(this); 549 + if (!err) 550 + return 0; 551 + } 552 + 553 + /* for large oob nand */ 554 + if (mtd->oobsize > 1024) { 555 + dev_dbg(this->dev, "use large oob bch geometry\n"); 556 + err = set_geometry_for_large_oob(this); 710 557 if (!err) 711 558 return 0; 712 559 } ··· 1602 1433 } 1603 1434 } 1604 1435 1436 + /* 1437 + * if there is an ECC dedicate for meta: 1438 + * - need to add an extra ECC size when calculating col and page_size, 1439 + * if the meta size is NOT zero. 1440 + * - ecc0_chunk size need to set to the same size as other chunks, 1441 + * if the meta size is zero. 1442 + */ 1443 + 1605 1444 meta = geo->metadata_size; 1606 1445 if (first) { 1607 - col = meta + (size + ecc_parity_size) * first; 1446 + if (geo->ecc_for_meta) 1447 + col = meta + ecc_parity_size 1448 + + (size + ecc_parity_size) * first; 1449 + else 1450 + col = meta + (size + ecc_parity_size) * first; 1451 + 1608 1452 meta = 0; 1609 1453 buf = buf + first * size; 1610 1454 } 1611 1455 1612 1456 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8; 1613 - 1614 1457 n = last - first + 1; 1615 - page_size = meta + (size + ecc_parity_size) * n; 1458 + 1459 + if (geo->ecc_for_meta && meta) 1460 + page_size = meta + ecc_parity_size 1461 + + (size + ecc_parity_size) * n; 1462 + else 1463 + page_size = meta + (size + ecc_parity_size) * n; 1464 + 1616 1465 ecc_strength = geo->ecc_strength >> 1; 1617 1466 1618 - this->bch_flashlayout0 = BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1) | 1467 + this->bch_flashlayout0 = BF_BCH_FLASH0LAYOUT0_NBLOCKS( 1468 + (geo->ecc_for_meta ? n : n - 1)) | 1619 1469 BF_BCH_FLASH0LAYOUT0_META_SIZE(meta) | 1620 1470 BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) | 1621 1471 BF_BCH_FLASH0LAYOUT0_GF(geo->gf_len, this) | 1622 - BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(geo->eccn_chunk_size, this); 1472 + BF_BCH_FLASH0LAYOUT0_DATA0_SIZE((geo->ecc_for_meta ? 1473 + 0 : geo->ecc0_chunk_size), this); 1623 1474 1624 1475 this->bch_flashlayout1 = BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) | 1625 1476 BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
+3
drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.h
··· 42 42 * which the underlying physical block mark appears. 43 43 * @block_mark_bit_offset: The bit offset into the ECC-based page view at 44 44 * which the underlying physical block mark appears. 45 + * @ecc_for_meta: The flag to indicate if there is a dedicate ecc 46 + * for meta. 45 47 */ 46 48 struct bch_geometry { 47 49 unsigned int gf_len; ··· 58 56 unsigned int auxiliary_status_offset; 59 57 unsigned int block_mark_byte_offset; 60 58 unsigned int block_mark_bit_offset; 59 + unsigned int ecc_for_meta; /* ECC for meta data */ 61 60 }; 62 61 63 62 /**