diff options
| -rw-r--r-- | drivers/mtd/ubi/build.c | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index 9980441a8886..c17f8e03abc8 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c | |||
| @@ -565,6 +565,34 @@ void ubi_free_internal_volumes(struct ubi_device *ubi) | |||
| 565 | } | 565 | } |
| 566 | } | 566 | } |
| 567 | 567 | ||
| 568 | static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024) | ||
| 569 | { | ||
| 570 | int limit, device_pebs; | ||
| 571 | uint64_t device_size; | ||
| 572 | |||
| 573 | if (!max_beb_per1024) | ||
| 574 | return 0; | ||
| 575 | |||
| 576 | /* | ||
| 577 | * Here we are using size of the entire flash chip and | ||
| 578 | * not just the MTD partition size because the maximum | ||
| 579 | * number of bad eraseblocks is a percentage of the | ||
| 580 | * whole device and bad eraseblocks are not fairly | ||
| 581 | * distributed over the flash chip. So the worst case | ||
| 582 | * is that all the bad eraseblocks of the chip are in | ||
| 583 | * the MTD partition we are attaching (ubi->mtd). | ||
| 584 | */ | ||
| 585 | device_size = mtd_get_device_size(ubi->mtd); | ||
| 586 | device_pebs = mtd_div_by_eb(device_size, ubi->mtd); | ||
| 587 | limit = mult_frac(device_pebs, max_beb_per1024, 1024); | ||
| 588 | |||
| 589 | /* Round it up */ | ||
| 590 | if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs) | ||
| 591 | limit += 1; | ||
| 592 | |||
| 593 | return limit; | ||
| 594 | } | ||
| 595 | |||
| 568 | /** | 596 | /** |
| 569 | * io_init - initialize I/O sub-system for a given UBI device. | 597 | * io_init - initialize I/O sub-system for a given UBI device. |
| 570 | * @ubi: UBI device description object | 598 | * @ubi: UBI device description object |
| @@ -582,6 +610,8 @@ void ubi_free_internal_volumes(struct ubi_device *ubi) | |||
| 582 | */ | 610 | */ |
| 583 | static int io_init(struct ubi_device *ubi) | 611 | static int io_init(struct ubi_device *ubi) |
| 584 | { | 612 | { |
| 613 | const int max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT; | ||
| 614 | |||
| 585 | if (ubi->mtd->numeraseregions != 0) { | 615 | if (ubi->mtd->numeraseregions != 0) { |
| 586 | /* | 616 | /* |
| 587 | * Some flashes have several erase regions. Different regions | 617 | * Some flashes have several erase regions. Different regions |
| @@ -610,29 +640,7 @@ static int io_init(struct ubi_device *ubi) | |||
| 610 | 640 | ||
| 611 | if (mtd_can_have_bb(ubi->mtd)) { | 641 | if (mtd_can_have_bb(ubi->mtd)) { |
| 612 | ubi->bad_allowed = 1; | 642 | ubi->bad_allowed = 1; |
| 613 | if (CONFIG_MTD_UBI_BEB_LIMIT > 0) { | 643 | ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024); |
| 614 | int per1024 = CONFIG_MTD_UBI_BEB_LIMIT; | ||
| 615 | int limit, device_pebs; | ||
| 616 | uint64_t device_size; | ||
| 617 | |||
| 618 | /* | ||
| 619 | * Here we are using size of the entire flash chip and | ||
| 620 | * not just the MTD partition size because the maximum | ||
| 621 | * number of bad eraseblocks is a percentage of the | ||
| 622 | * whole device and bad eraseblocks are not fairly | ||
| 623 | * distributed over the flash chip. So the worst case | ||
| 624 | * is that all the bad eraseblocks of the chip are in | ||
| 625 | * the MTD partition we are attaching (ubi->mtd). | ||
| 626 | */ | ||
| 627 | device_size = mtd_get_device_size(ubi->mtd); | ||
| 628 | device_pebs = mtd_div_by_eb(device_size, ubi->mtd); | ||
| 629 | limit = mult_frac(device_pebs, per1024, 1024); | ||
| 630 | |||
| 631 | /* Round it up */ | ||
| 632 | if (mult_frac(limit, 1024, per1024) < device_pebs) | ||
| 633 | limit += 1; | ||
| 634 | ubi->bad_peb_limit = limit; | ||
| 635 | } | ||
| 636 | } | 644 | } |
| 637 | 645 | ||
| 638 | if (ubi->mtd->type == MTD_NORFLASH) { | 646 | if (ubi->mtd->type == MTD_NORFLASH) { |
