14fbd246d7262fcf6008d9a66f570706408fb6b3
2 * Copyright (c) 2017 MediaTek Inc.
3 * Author: Xiangsheng Hou <xiangsheng.hou@mediatek.com>
4 * Copyright (c) 2020 Felix Fietkau <nbd@nbd.name>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/slab.h>
17 #include <linux/gfp.h>
18 #include <linux/kernel.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/mtd/mtk_bmt.h>
23 #include <linux/module.h>
24 #include <linux/debugfs.h>
25 #include <linux/bits.h>
27 #define MAIN_SIGNATURE_OFFSET 0
28 #define OOB_SIGNATURE_OFFSET 1
29 #define BBPOOL_RATIO 2
31 #define BBT_LOG(fmt, ...) pr_debug("[BBT][%s|%d] "fmt"\n", __func__, __LINE__, ##__VA_ARGS__)
33 /* Maximum 8k blocks */
34 #define BB_TABLE_MAX bmtd.table_size
35 #define BMT_TABLE_MAX (BB_TABLE_MAX * BBPOOL_RATIO / 100)
36 #define BMT_TBL_DEF_VAL 0x0
39 * Burner Bad Block Table
40 * --------- Only support SLC Nand Chips!!!!!!!!!!! ----------
45 /* This version is used to distinguish the legacy and new algorithm */
46 #define BBMT_VERSION 2
47 unsigned char version
;
48 /* Below 2 tables will be written in SLC */
55 #define NORMAL_MAPPED 1
60 static struct bmt_desc
{
63 int (*_read_oob
) (struct mtd_info
*mtd
, loff_t from
,
64 struct mtd_oob_ops
*ops
);
65 int (*_write_oob
) (struct mtd_info
*mtd
, loff_t to
,
66 struct mtd_oob_ops
*ops
);
67 int (*_erase
) (struct mtd_info
*mtd
, struct erase_info
*instr
);
68 int (*_block_isbad
) (struct mtd_info
*mtd
, loff_t ofs
);
69 int (*_block_markbad
) (struct mtd_info
*mtd
, loff_t ofs
);
73 struct dentry
*debugfs_dir
;
80 /* bbt logical address */
82 /* bbt physical address */
84 /* Maximum count of bad blocks that the vendor guaranteed */
86 /* Total blocks of the Nand Chip */
88 /* The block(n) BMT is located at (bmt_tbl[n]) */
90 /* How many pages needs to store 'struct bbbt' */
93 const __be32
*remap_range
;
96 /* to compensate for driver level remapping */
100 static unsigned char *nand_bbt_buf
;
101 static unsigned char *nand_data_buf
;
103 /* -------- Unit conversions -------- */
104 static inline u32
blk_pg(u16 block
)
106 return (u32
)(block
<< (bmtd
.blk_shift
- bmtd
.pg_shift
));
109 /* -------- Nand operations wrapper -------- */
111 bbt_nand_read(u32 page
, unsigned char *dat
, int dat_len
,
112 unsigned char *fdm
, int fdm_len
)
114 struct mtd_oob_ops ops
= {
115 .mode
= MTD_OPS_PLACE_OOB
,
116 .ooboffs
= bmtd
.oob_offset
,
123 return bmtd
._read_oob(bmtd
.mtd
, page
<< bmtd
.pg_shift
, &ops
);
126 static inline int bbt_nand_erase(u16 block
)
128 struct mtd_info
*mtd
= bmtd
.mtd
;
129 struct erase_info instr
= {
130 .addr
= (loff_t
)block
<< bmtd
.blk_shift
,
131 .len
= bmtd
.blk_size
,
134 return bmtd
._erase(mtd
, &instr
);
137 static inline int bbt_nand_copy(u16 dest_blk
, u16 src_blk
, loff_t max_offset
)
139 int pages
= bmtd
.blk_size
>> bmtd
.pg_shift
;
140 loff_t src
= (loff_t
)src_blk
<< bmtd
.blk_shift
;
141 loff_t dest
= (loff_t
)dest_blk
<< bmtd
.blk_shift
;
146 for (i
= 0; i
< pages
; i
++) {
147 struct mtd_oob_ops rd_ops
= {
148 .mode
= MTD_OPS_PLACE_OOB
,
150 .ooblen
= min_t(int, bmtd
.mtd
->oobsize
/ pages
, sizeof(oob
)),
151 .datbuf
= nand_data_buf
,
154 struct mtd_oob_ops wr_ops
= {
155 .mode
= MTD_OPS_PLACE_OOB
,
157 .datbuf
= nand_data_buf
,
161 if (offset
>= max_offset
)
164 ret
= bmtd
._read_oob(bmtd
.mtd
, src
+ offset
, &rd_ops
);
165 if (ret
< 0 && !mtd_is_bitflip(ret
))
171 ret
= bmtd
._write_oob(bmtd
.mtd
, dest
+ offset
, &wr_ops
);
175 wr_ops
.ooblen
= rd_ops
.oobretlen
;
176 offset
+= rd_ops
.retlen
;
182 /* -------- Bad Blocks Management -------- */
183 static inline struct bbmt
*bmt_tbl(struct bbbt
*bbbt
)
185 return (struct bbmt
*)&bbbt
->bb_tbl
[bmtd
.table_size
];
189 read_bmt(u16 block
, unsigned char *dat
, unsigned char *fdm
, int fdm_len
)
191 u32 len
= bmtd
.bmt_pgs
<< bmtd
.pg_shift
;
193 return bbt_nand_read(blk_pg(block
), dat
, len
, fdm
, fdm_len
);
196 static int write_bmt(u16 block
, unsigned char *dat
)
198 struct mtd_oob_ops ops
= {
199 .mode
= MTD_OPS_PLACE_OOB
,
200 .ooboffs
= OOB_SIGNATURE_OFFSET
+ bmtd
.oob_offset
,
204 .len
= bmtd
.bmt_pgs
<< bmtd
.pg_shift
,
206 loff_t addr
= (loff_t
)block
<< bmtd
.blk_shift
;
208 return bmtd
._write_oob(bmtd
.mtd
, addr
, &ops
);
211 static u16
find_valid_block(u16 block
)
218 if (block
>= bmtd
.total_blks
)
221 ret
= bbt_nand_read(blk_pg(block
), nand_data_buf
, bmtd
.pg_size
,
223 /* Read the 1st byte of FDM to judge whether it's a bad
226 if (ret
|| fdm
[0] != 0xff) {
227 pr_info("nand: found bad block 0x%x\n", block
);
228 if (loop
>= bmtd
.bb_max
) {
229 pr_info("nand: FATAL ERR: too many bad blocks!!\n");
241 /* Find out all bad blocks, and fill in the mapping table */
242 static int scan_bad_blocks(struct bbbt
*bbt
)
247 /* First time download, the block0 MUST NOT be a bad block,
248 * this is guaranteed by vendor
253 * Construct the mapping table of Normal data area(non-PMT/BMTPOOL)
254 * G - Good block; B - Bad block
255 * ---------------------------
256 * physical |G|G|B|G|B|B|G|G|G|G|B|G|B|
257 * ---------------------------
258 * What bb_tbl[i] looks like:
260 * 0 1 2 3 4 5 6 7 8 9 a b c
261 * mapped block(bb_tbl[i]):
262 * 0 1 3 6 7 8 9 b ......
264 * If new bad block ocurred(n), search bmt_tbl to find
265 * a available block(x), and fill in the bb_tbl[n] = x;
267 for (i
= 1; i
< bmtd
.pool_lba
; i
++) {
268 bbt
->bb_tbl
[i
] = find_valid_block(bbt
->bb_tbl
[i
- 1] + 1);
269 BBT_LOG("bb_tbl[0x%x] = 0x%x", i
, bbt
->bb_tbl
[i
]);
270 if (bbt
->bb_tbl
[i
] == 0)
274 /* Physical Block start Address of BMT pool */
275 bmtd
.pool_pba
= bbt
->bb_tbl
[i
- 1] + 1;
276 if (bmtd
.pool_pba
>= bmtd
.total_blks
- 2) {
277 pr_info("nand: FATAL ERR: Too many bad blocks!!\n");
281 BBT_LOG("pool_pba=0x%x", bmtd
.pool_pba
);
283 block
= bmtd
.pool_pba
;
285 * The bmt table is used for runtime bad block mapping
286 * G - Good block; B - Bad block
287 * ---------------------------
288 * physical |G|G|B|G|B|B|G|G|G|G|B|G|B|
289 * ---------------------------
290 * block: 0 1 2 3 4 5 6 7 8 9 a b c
291 * What bmt_tbl[i] looks like in initial state:
298 * N - Not mapped(Available)
302 * BMT always in the last valid block in pool
304 while ((block
= find_valid_block(block
)) != 0) {
305 bmt_tbl(bbt
)[i
].block
= block
;
306 bmt_tbl(bbt
)[i
].mapped
= NO_MAPPED
;
307 BBT_LOG("bmt_tbl[%d].block = 0x%x", i
, block
);
312 /* i - How many available blocks in pool, which is the length of bmt_tbl[]
313 * bmtd.bmt_blk_idx - bmt_tbl[bmtd.bmt_blk_idx].block => the BMT block
315 bmtd
.bmt_blk_idx
= i
- 1;
316 bmt_tbl(bbt
)[bmtd
.bmt_blk_idx
].mapped
= BMT_MAPPED
;
319 pr_info("nand: FATAL ERR: no space to store BMT!!\n");
323 pr_info("[BBT] %d available blocks in BMT pool\n", i
);
328 static bool is_valid_bmt(unsigned char *buf
, unsigned char *fdm
)
330 struct bbbt
*bbt
= (struct bbbt
*)buf
;
331 u8
*sig
= (u8
*)bbt
->signature
+ MAIN_SIGNATURE_OFFSET
;
334 if (memcmp(bbt
->signature
+ MAIN_SIGNATURE_OFFSET
, "BMT", 3) == 0 &&
335 memcmp(fdm
+ OOB_SIGNATURE_OFFSET
, "bmt", 3) == 0) {
336 if (bbt
->version
== BBMT_VERSION
)
339 BBT_LOG("[BBT] BMT Version not match,upgrage preloader and uboot please! sig=%02x%02x%02x, fdm=%02x%02x%02x",
340 sig
[0], sig
[1], sig
[2],
341 fdm
[1], fdm
[2], fdm
[3]);
345 static u16
get_bmt_index(struct bbmt
*bmt
)
349 while (bmt
[i
].block
!= BMT_TBL_DEF_VAL
) {
350 if (bmt
[i
].mapped
== BMT_MAPPED
)
357 static struct bbbt
*scan_bmt(u16 block
)
361 if (block
< bmtd
.pool_lba
)
364 if (read_bmt(block
, nand_bbt_buf
, fdm
, sizeof(fdm
)))
365 return scan_bmt(block
- 1);
367 if (is_valid_bmt(nand_bbt_buf
, fdm
)) {
368 bmtd
.bmt_blk_idx
= get_bmt_index(bmt_tbl((struct bbbt
*)nand_bbt_buf
));
369 if (bmtd
.bmt_blk_idx
== 0) {
370 pr_info("[BBT] FATAL ERR: bmt block index is wrong!\n");
373 pr_info("[BBT] BMT.v2 is found at 0x%x\n", block
);
374 return (struct bbbt
*)nand_bbt_buf
;
376 return scan_bmt(block
- 1);
379 /* Write the Burner Bad Block Table to Nand Flash
380 * n - write BMT to bmt_tbl[n]
382 static u16
upload_bmt(struct bbbt
*bbt
, int n
)
387 if (n
< 0 || bmt_tbl(bbt
)[n
].mapped
== NORMAL_MAPPED
) {
388 pr_info("nand: FATAL ERR: no space to store BMT!\n");
392 block
= bmt_tbl(bbt
)[n
].block
;
393 BBT_LOG("n = 0x%x, block = 0x%x", n
, block
);
394 if (bbt_nand_erase(block
)) {
395 bmt_tbl(bbt
)[n
].block
= 0;
396 /* erase failed, try the previous block: bmt_tbl[n - 1].block */
401 /* The signature offset is fixed set to 0,
402 * oob signature offset is fixed set to 1
404 memcpy(bbt
->signature
+ MAIN_SIGNATURE_OFFSET
, "BMT", 3);
405 bbt
->version
= BBMT_VERSION
;
407 if (write_bmt(block
, (unsigned char *)bbt
)) {
408 bmt_tbl(bbt
)[n
].block
= 0;
410 /* write failed, try the previous block in bmt_tbl[n - 1] */
415 /* Return the current index(n) of BMT pool (bmt_tbl[n]) */
419 static u16
find_valid_block_in_pool(struct bbbt
*bbt
)
423 if (bmtd
.bmt_blk_idx
== 0)
426 for (i
= 0; i
< bmtd
.bmt_blk_idx
; i
++) {
427 if (bmt_tbl(bbt
)[i
].block
!= 0 && bmt_tbl(bbt
)[i
].mapped
== NO_MAPPED
) {
428 bmt_tbl(bbt
)[i
].mapped
= NORMAL_MAPPED
;
429 return bmt_tbl(bbt
)[i
].block
;
434 pr_info("nand: FATAL ERR: BMT pool is run out!\n");
438 /* We met a bad block, mark it as bad and map it to a valid block in pool,
439 * if it's a write failure, we need to write the data to mapped block
441 static bool update_bmt(u16 block
, int copy_len
)
447 mapped_blk
= find_valid_block_in_pool(bbt
);
451 /* Map new bad block to available block in pool */
452 bbt
->bb_tbl
[block
] = mapped_blk
;
454 /* Erase new block */
455 bbt_nand_erase(mapped_blk
);
457 bbt_nand_copy(mapped_blk
, block
, copy_len
);
459 bmtd
.bmt_blk_idx
= upload_bmt(bbt
, bmtd
.bmt_blk_idx
);
465 mapping_block_in_range(int block
)
467 const __be32
*cur
= bmtd
.remap_range
;
468 u32 addr
= block
<< bmtd
.blk_shift
;
471 if (!cur
|| !bmtd
.remap_range_len
)
474 for (i
= 0; i
< bmtd
.remap_range_len
; i
++, cur
+= 2)
475 if (addr
>= be32_to_cpu(cur
[0]) && addr
< be32_to_cpu(cur
[1]))
481 u16
get_mapping_block_index(int block
)
483 if (block
>= bmtd
.pool_lba
)
486 if (!mapping_block_in_range(block
))
489 return bmtd
.bbt
->bb_tbl
[block
];
493 mtk_bmt_read(struct mtd_info
*mtd
, loff_t from
,
494 struct mtd_oob_ops
*ops
)
496 struct mtd_oob_ops cur_ops
= *ops
;
504 while (ops
->retlen
< ops
->len
|| ops
->oobretlen
< ops
->ooblen
) {
507 u32 offset
= from
& (bmtd
.blk_size
- 1);
508 u32 block
= from
>> bmtd
.blk_shift
;
511 cur_block
= get_mapping_block_index(block
);
512 cur_from
= ((loff_t
)cur_block
<< bmtd
.blk_shift
) + offset
;
514 cur_ops
.oobretlen
= 0;
516 cur_ops
.len
= min_t(u32
, mtd
->erasesize
- offset
,
517 ops
->len
- ops
->retlen
);
518 cur_ret
= bmtd
._read_oob(mtd
, cur_from
, &cur_ops
);
521 if (cur_ret
< 0 && !mtd_is_bitflip(cur_ret
)) {
522 update_bmt(block
, mtd
->erasesize
);
523 if (retry_count
++ < 10)
529 ops
->retlen
+= cur_ops
.retlen
;
530 ops
->oobretlen
+= cur_ops
.oobretlen
;
533 cur_ops
.datbuf
+= cur_ops
.retlen
;
534 cur_ops
.oobbuf
+= cur_ops
.oobretlen
;
535 cur_ops
.ooblen
-= cur_ops
.oobretlen
;
538 cur_ops
.len
= mtd
->erasesize
- offset
;
548 mtk_bmt_write(struct mtd_info
*mtd
, loff_t to
,
549 struct mtd_oob_ops
*ops
)
551 struct mtd_oob_ops cur_ops
= *ops
;
559 while (ops
->retlen
< ops
->len
|| ops
->oobretlen
< ops
->ooblen
) {
560 u32 offset
= to
& (bmtd
.blk_size
- 1);
561 u32 block
= to
>> bmtd
.blk_shift
;
564 cur_block
= get_mapping_block_index(block
);
565 cur_to
= ((loff_t
)cur_block
<< bmtd
.blk_shift
) + offset
;
567 cur_ops
.oobretlen
= 0;
569 cur_ops
.len
= min_t(u32
, bmtd
.blk_size
- offset
,
570 ops
->len
- ops
->retlen
);
571 ret
= bmtd
._write_oob(mtd
, cur_to
, &cur_ops
);
573 update_bmt(block
, offset
);
574 if (retry_count
++ < 10)
580 ops
->retlen
+= cur_ops
.retlen
;
581 ops
->oobretlen
+= cur_ops
.oobretlen
;
584 cur_ops
.datbuf
+= cur_ops
.retlen
;
585 cur_ops
.oobbuf
+= cur_ops
.oobretlen
;
586 cur_ops
.ooblen
-= cur_ops
.oobretlen
;
589 cur_ops
.len
= mtd
->erasesize
- offset
;
599 mtk_bmt_mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
601 struct erase_info mapped_instr
= {
602 .len
= bmtd
.blk_size
,
605 u64 start_addr
, end_addr
;
607 u16 orig_block
, block
;
609 start_addr
= instr
->addr
& (~mtd
->erasesize_mask
);
610 end_addr
= instr
->addr
+ instr
->len
;
612 while (start_addr
< end_addr
) {
613 orig_block
= start_addr
>> bmtd
.blk_shift
;
614 block
= get_mapping_block_index(orig_block
);
615 mapped_instr
.addr
= (loff_t
)block
<< bmtd
.blk_shift
;
616 ret
= bmtd
._erase(mtd
, &mapped_instr
);
618 update_bmt(orig_block
, 0);
619 if (retry_count
++ < 10)
621 instr
->fail_addr
= start_addr
;
624 start_addr
+= mtd
->erasesize
;
631 mtk_bmt_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
634 u16 orig_block
= ofs
>> bmtd
.blk_shift
;
639 block
= get_mapping_block_index(orig_block
);
640 ret
= bmtd
._block_isbad(mtd
, (loff_t
)block
<< bmtd
.blk_shift
);
642 update_bmt(orig_block
, bmtd
.blk_size
);
643 if (retry_count
++ < 10)
650 mtk_bmt_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
652 u16 orig_block
= ofs
>> bmtd
.blk_shift
;
653 u16 block
= get_mapping_block_index(orig_block
);
654 update_bmt(orig_block
, bmtd
.blk_size
);
655 return bmtd
._block_markbad(mtd
, (loff_t
)block
<< bmtd
.blk_shift
);
659 mtk_bmt_replace_ops(struct mtd_info
*mtd
)
661 bmtd
._read_oob
= mtd
->_read_oob
;
662 bmtd
._write_oob
= mtd
->_write_oob
;
663 bmtd
._erase
= mtd
->_erase
;
664 bmtd
._block_isbad
= mtd
->_block_isbad
;
665 bmtd
._block_markbad
= mtd
->_block_markbad
;
667 mtd
->_read_oob
= mtk_bmt_read
;
668 mtd
->_write_oob
= mtk_bmt_write
;
669 mtd
->_erase
= mtk_bmt_mtd_erase
;
670 mtd
->_block_isbad
= mtk_bmt_block_isbad
;
671 mtd
->_block_markbad
= mtk_bmt_block_markbad
;
674 static int mtk_bmt_debug_mark_good(void *data
, u64 val
)
676 u32 block
= val
>> bmtd
.blk_shift
;
678 bmtd
.bbt
->bb_tbl
[block
] = block
;
679 bmtd
.bmt_blk_idx
= upload_bmt(bmtd
.bbt
, bmtd
.bmt_blk_idx
);
684 static int mtk_bmt_debug_mark_bad(void *data
, u64 val
)
686 u32 block
= val
>> bmtd
.blk_shift
;
688 update_bmt(block
, bmtd
.blk_size
);
693 static unsigned long *
694 mtk_bmt_get_mapping_mask(void)
696 struct bbmt
*bbmt
= bmt_tbl(bmtd
.bbt
);
697 int main_blocks
= bmtd
.mtd
->size
>> bmtd
.blk_shift
;
701 used
= kcalloc(sizeof(unsigned long), BIT_WORD(bmtd
.bmt_blk_idx
) + 1, GFP_KERNEL
);
705 for (i
= 1; i
< main_blocks
; i
++) {
706 if (bmtd
.bbt
->bb_tbl
[i
] == i
)
709 for (k
= 0; k
< bmtd
.bmt_blk_idx
; k
++) {
710 if (bmtd
.bbt
->bb_tbl
[i
] != bbmt
[k
].block
)
721 static int mtk_bmt_debug(void *data
, u64 val
)
723 struct bbmt
*bbmt
= bmt_tbl(bmtd
.bbt
);
724 struct mtd_info
*mtd
= bmtd
.mtd
;
726 int main_blocks
= mtd
->size
>> bmtd
.blk_shift
;
730 used
= mtk_bmt_get_mapping_mask();
736 for (i
= 1; i
< main_blocks
; i
++) {
737 if (bmtd
.bbt
->bb_tbl
[i
] == i
)
740 printk("remap [%x->%x]\n", i
, bmtd
.bbt
->bb_tbl
[i
]);
743 for (i
= 0; i
<= bmtd
.bmt_blk_idx
; i
++) {
746 switch (bbmt
[i
].mapped
) {
751 if (test_bit(i
, used
))
761 printk("[%x:%c] = 0x%x\n", i
, c
, bbmt
[i
].block
);
765 for (i
= 0; i
<= bmtd
.bmt_blk_idx
; i
++) {
766 if (bbmt
[i
].mapped
!= NORMAL_MAPPED
)
769 if (test_bit(i
, used
))
773 bbmt
[i
].mapped
= NO_MAPPED
;
774 printk("free block [%d:%x]\n", i
, bbmt
[i
].block
);
777 bmtd
.bmt_blk_idx
= upload_bmt(bmtd
.bbt
, bmtd
.bmt_blk_idx
);
787 DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_good
, NULL
, mtk_bmt_debug_mark_good
, "%llu\n");
788 DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_bad
, NULL
, mtk_bmt_debug_mark_bad
, "%llu\n");
789 DEFINE_DEBUGFS_ATTRIBUTE(fops_debug
, NULL
, mtk_bmt_debug
, "%llu\n");
792 mtk_bmt_add_debugfs(void)
796 dir
= bmtd
.debugfs_dir
= debugfs_create_dir("mtk-bmt", NULL
);
800 debugfs_create_file_unsafe("mark_good", S_IWUSR
, dir
, NULL
, &fops_mark_good
);
801 debugfs_create_file_unsafe("mark_bad", S_IWUSR
, dir
, NULL
, &fops_mark_bad
);
802 debugfs_create_file_unsafe("debug", S_IWUSR
, dir
, NULL
, &fops_debug
);
805 void mtk_bmt_detach(struct mtd_info
*mtd
)
810 if (bmtd
.debugfs_dir
)
811 debugfs_remove_recursive(bmtd
.debugfs_dir
);
812 bmtd
.debugfs_dir
= NULL
;
815 kfree(nand_data_buf
);
817 mtd
->_read_oob
= bmtd
._read_oob
;
818 mtd
->_write_oob
= bmtd
._write_oob
;
819 mtd
->_erase
= bmtd
._erase
;
820 mtd
->_block_isbad
= bmtd
._block_isbad
;
821 mtd
->_block_markbad
= bmtd
._block_markbad
;
822 mtd
->size
= bmtd
.total_blks
<< bmtd
.blk_shift
;
824 memset(&bmtd
, 0, sizeof(bmtd
));
827 /* total_blocks - The total count of blocks that the Nand Chip has */
828 int mtk_bmt_attach(struct mtd_info
*mtd
)
830 struct device_node
*np
;
834 u16 total_blocks
, pmt_block
;
836 u32 bmt_pool_size
, bmt_table_size
;
841 np
= mtd_get_of_node(mtd
);
845 if (!of_property_read_bool(np
, "mediatek,bmt-v2"))
848 if (of_property_read_u32(np
, "mediatek,bmt-pool-size",
849 &bmt_pool_size
) != 0)
852 if (of_property_read_u8(np
, "mediatek,bmt-oob-offset",
853 &bmtd
.oob_offset
) != 0)
856 if (of_property_read_u32(np
, "mediatek,bmt-table-size",
857 &bmt_table_size
) != 0)
858 bmt_table_size
= 0x2000U
;
860 bmtd
.remap_range
= of_get_property(np
, "mediatek,bmt-remap-range",
861 &bmtd
.remap_range_len
);
862 bmtd
.remap_range_len
/= 8;
865 mtk_bmt_replace_ops(mtd
);
867 bmtd
.table_size
= bmt_table_size
;
868 bmtd
.blk_size
= mtd
->erasesize
;
869 bmtd
.blk_shift
= ffs(bmtd
.blk_size
) - 1;
870 bmtd
.pg_size
= mtd
->writesize
;
871 bmtd
.pg_shift
= ffs(bmtd
.pg_size
) - 1;
872 total_blocks
= mtd
->size
>> bmtd
.blk_shift
;
873 pmt_block
= total_blocks
- bmt_pool_size
- 2;
875 mtd
->size
= pmt_block
<< bmtd
.blk_shift
;
878 * ---------------------------------------
879 * | PMT(2blks) | BMT POOL(totalblks * 2%) |
880 * ---------------------------------------
883 * pmt_block pmt_block + 2blocks(pool_lba)
886 * The blocks ahead of the boundary block are stored in bb_tbl
887 * and blocks behind are stored in bmt_tbl
890 bmtd
.pool_lba
= (u16
)(pmt_block
+ 2);
891 bmtd
.total_blks
= total_blocks
;
892 bmtd
.bb_max
= bmtd
.total_blks
* BBPOOL_RATIO
/ 100;
894 /* 3 buffers we need */
895 bufsz
= round_up(sizeof(struct bbbt
) +
896 bmt_table_size
* sizeof(struct bbmt
), bmtd
.pg_size
);
897 bmtd
.bmt_pgs
= bufsz
>> bmtd
.pg_shift
;
899 nand_bbt_buf
= kzalloc(bufsz
, GFP_KERNEL
);
900 nand_data_buf
= kzalloc(bmtd
.pg_size
, GFP_KERNEL
);
902 if (!nand_bbt_buf
|| !nand_data_buf
) {
903 pr_info("nand: FATAL ERR: allocate buffer failed!\n");
908 memset(nand_bbt_buf
, 0xff, bufsz
);
909 memset(nand_data_buf
, 0xff, bmtd
.pg_size
);
911 BBT_LOG("bbtbuf=0x%p(0x%x) dat=0x%p(0x%x)",
912 nand_bbt_buf
, bufsz
, nand_data_buf
, bmtd
.pg_size
);
913 BBT_LOG("pool_lba=0x%x total_blks=0x%x bb_max=0x%x",
914 bmtd
.pool_lba
, bmtd
.total_blks
, bmtd
.bb_max
);
916 /* Scanning start from the first page of the last block
919 bbt
= scan_bmt(bmtd
.total_blks
- 1);
922 if (bmtd
.total_blks
> BB_TABLE_MAX
+ BMT_TABLE_MAX
) {
923 pr_info("nand: FATAL: Too many blocks, can not support!\n");
928 bbt
= (struct bbbt
*)nand_bbt_buf
;
929 memset(bmt_tbl(bbt
), BMT_TBL_DEF_VAL
, bmtd
.table_size
* sizeof(struct bbmt
));
931 if (scan_bad_blocks(bbt
)) {
936 /* BMT always in the last valid block in pool */
937 bmtd
.bmt_blk_idx
= upload_bmt(bbt
, bmtd
.bmt_blk_idx
);
938 block
= bmt_tbl(bbt
)[bmtd
.bmt_blk_idx
].block
;
939 pr_notice("[BBT] BMT.v2 is written into PBA:0x%x\n", block
);
941 if (bmtd
.bmt_blk_idx
== 0)
942 pr_info("nand: Warning: no available block in BMT pool!\n");
943 else if (bmtd
.bmt_blk_idx
== (u16
)-1) {
948 mtk_bmt_add_debugfs();
959 MODULE_LICENSE("GPL");
960 MODULE_AUTHOR("Xiangsheng Hou <xiangsheng.hou@mediatek.com>, Felix Fietkau <nbd@nbd.name>");
961 MODULE_DESCRIPTION("Bad Block mapping management v2 for MediaTek NAND Flash Driver");