2140c619da9624643031106c44a6d24096616748
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
;
500 int max_bitflips
= 0;
505 while (ops
->retlen
< ops
->len
|| ops
->oobretlen
< ops
->ooblen
) {
508 u32 offset
= from
& (bmtd
.blk_size
- 1);
509 u32 block
= from
>> bmtd
.blk_shift
;
512 cur_block
= get_mapping_block_index(block
);
513 cur_from
= ((loff_t
)cur_block
<< bmtd
.blk_shift
) + offset
;
515 cur_ops
.oobretlen
= 0;
517 cur_ops
.len
= min_t(u32
, mtd
->erasesize
- offset
,
518 ops
->len
- ops
->retlen
);
519 cur_ret
= bmtd
._read_oob(mtd
, cur_from
, &cur_ops
);
523 max_bitflips
= max_t(int, max_bitflips
, cur_ret
);
524 if (cur_ret
< 0 && !mtd_is_bitflip(cur_ret
)) {
525 update_bmt(block
, mtd
->erasesize
);
526 if (retry_count
++ < 10)
532 if (cur_ret
>= mtd
->bitflip_threshold
&&
533 mapping_block_in_range(block
))
534 update_bmt(block
, mtd
->erasesize
);
536 ops
->retlen
+= cur_ops
.retlen
;
537 ops
->oobretlen
+= cur_ops
.oobretlen
;
540 cur_ops
.datbuf
+= cur_ops
.retlen
;
541 cur_ops
.oobbuf
+= cur_ops
.oobretlen
;
542 cur_ops
.ooblen
-= cur_ops
.oobretlen
;
545 cur_ops
.len
= mtd
->erasesize
- offset
;
559 mtk_bmt_write(struct mtd_info
*mtd
, loff_t to
,
560 struct mtd_oob_ops
*ops
)
562 struct mtd_oob_ops cur_ops
= *ops
;
570 while (ops
->retlen
< ops
->len
|| ops
->oobretlen
< ops
->ooblen
) {
571 u32 offset
= to
& (bmtd
.blk_size
- 1);
572 u32 block
= to
>> bmtd
.blk_shift
;
575 cur_block
= get_mapping_block_index(block
);
576 cur_to
= ((loff_t
)cur_block
<< bmtd
.blk_shift
) + offset
;
578 cur_ops
.oobretlen
= 0;
580 cur_ops
.len
= min_t(u32
, bmtd
.blk_size
- offset
,
581 ops
->len
- ops
->retlen
);
582 ret
= bmtd
._write_oob(mtd
, cur_to
, &cur_ops
);
584 update_bmt(block
, offset
);
585 if (retry_count
++ < 10)
591 ops
->retlen
+= cur_ops
.retlen
;
592 ops
->oobretlen
+= cur_ops
.oobretlen
;
595 cur_ops
.datbuf
+= cur_ops
.retlen
;
596 cur_ops
.oobbuf
+= cur_ops
.oobretlen
;
597 cur_ops
.ooblen
-= cur_ops
.oobretlen
;
600 cur_ops
.len
= mtd
->erasesize
- offset
;
610 mtk_bmt_mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
612 struct erase_info mapped_instr
= {
613 .len
= bmtd
.blk_size
,
616 u64 start_addr
, end_addr
;
618 u16 orig_block
, block
;
620 start_addr
= instr
->addr
& (~mtd
->erasesize_mask
);
621 end_addr
= instr
->addr
+ instr
->len
;
623 while (start_addr
< end_addr
) {
624 orig_block
= start_addr
>> bmtd
.blk_shift
;
625 block
= get_mapping_block_index(orig_block
);
626 mapped_instr
.addr
= (loff_t
)block
<< bmtd
.blk_shift
;
627 ret
= bmtd
._erase(mtd
, &mapped_instr
);
629 update_bmt(orig_block
, 0);
630 if (retry_count
++ < 10)
632 instr
->fail_addr
= start_addr
;
635 start_addr
+= mtd
->erasesize
;
642 mtk_bmt_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
645 u16 orig_block
= ofs
>> bmtd
.blk_shift
;
650 block
= get_mapping_block_index(orig_block
);
651 ret
= bmtd
._block_isbad(mtd
, (loff_t
)block
<< bmtd
.blk_shift
);
653 update_bmt(orig_block
, bmtd
.blk_size
);
654 if (retry_count
++ < 10)
661 mtk_bmt_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
663 u16 orig_block
= ofs
>> bmtd
.blk_shift
;
664 u16 block
= get_mapping_block_index(orig_block
);
665 update_bmt(orig_block
, bmtd
.blk_size
);
666 return bmtd
._block_markbad(mtd
, (loff_t
)block
<< bmtd
.blk_shift
);
670 mtk_bmt_replace_ops(struct mtd_info
*mtd
)
672 bmtd
._read_oob
= mtd
->_read_oob
;
673 bmtd
._write_oob
= mtd
->_write_oob
;
674 bmtd
._erase
= mtd
->_erase
;
675 bmtd
._block_isbad
= mtd
->_block_isbad
;
676 bmtd
._block_markbad
= mtd
->_block_markbad
;
678 mtd
->_read_oob
= mtk_bmt_read
;
679 mtd
->_write_oob
= mtk_bmt_write
;
680 mtd
->_erase
= mtk_bmt_mtd_erase
;
681 mtd
->_block_isbad
= mtk_bmt_block_isbad
;
682 mtd
->_block_markbad
= mtk_bmt_block_markbad
;
685 static int mtk_bmt_debug_mark_good(void *data
, u64 val
)
687 u32 block
= val
>> bmtd
.blk_shift
;
689 bmtd
.bbt
->bb_tbl
[block
] = block
;
690 bmtd
.bmt_blk_idx
= upload_bmt(bmtd
.bbt
, bmtd
.bmt_blk_idx
);
695 static int mtk_bmt_debug_mark_bad(void *data
, u64 val
)
697 u32 block
= val
>> bmtd
.blk_shift
;
699 update_bmt(block
, bmtd
.blk_size
);
704 static unsigned long *
705 mtk_bmt_get_mapping_mask(void)
707 struct bbmt
*bbmt
= bmt_tbl(bmtd
.bbt
);
708 int main_blocks
= bmtd
.mtd
->size
>> bmtd
.blk_shift
;
712 used
= kcalloc(sizeof(unsigned long), BIT_WORD(bmtd
.bmt_blk_idx
) + 1, GFP_KERNEL
);
716 for (i
= 1; i
< main_blocks
; i
++) {
717 if (bmtd
.bbt
->bb_tbl
[i
] == i
)
720 for (k
= 0; k
< bmtd
.bmt_blk_idx
; k
++) {
721 if (bmtd
.bbt
->bb_tbl
[i
] != bbmt
[k
].block
)
732 static int mtk_bmt_debug(void *data
, u64 val
)
734 struct bbmt
*bbmt
= bmt_tbl(bmtd
.bbt
);
735 struct mtd_info
*mtd
= bmtd
.mtd
;
737 int main_blocks
= mtd
->size
>> bmtd
.blk_shift
;
741 used
= mtk_bmt_get_mapping_mask();
747 for (i
= 1; i
< main_blocks
; i
++) {
748 if (bmtd
.bbt
->bb_tbl
[i
] == i
)
751 printk("remap [%x->%x]\n", i
, bmtd
.bbt
->bb_tbl
[i
]);
754 for (i
= 0; i
<= bmtd
.bmt_blk_idx
; i
++) {
757 switch (bbmt
[i
].mapped
) {
762 if (test_bit(i
, used
))
772 printk("[%x:%c] = 0x%x\n", i
, c
, bbmt
[i
].block
);
776 for (i
= 0; i
<= bmtd
.bmt_blk_idx
; i
++) {
777 if (bbmt
[i
].mapped
!= NORMAL_MAPPED
)
780 if (test_bit(i
, used
))
784 bbmt
[i
].mapped
= NO_MAPPED
;
785 printk("free block [%d:%x]\n", i
, bbmt
[i
].block
);
788 bmtd
.bmt_blk_idx
= upload_bmt(bmtd
.bbt
, bmtd
.bmt_blk_idx
);
798 DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_good
, NULL
, mtk_bmt_debug_mark_good
, "%llu\n");
799 DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_bad
, NULL
, mtk_bmt_debug_mark_bad
, "%llu\n");
800 DEFINE_DEBUGFS_ATTRIBUTE(fops_debug
, NULL
, mtk_bmt_debug
, "%llu\n");
803 mtk_bmt_add_debugfs(void)
807 dir
= bmtd
.debugfs_dir
= debugfs_create_dir("mtk-bmt", NULL
);
811 debugfs_create_file_unsafe("mark_good", S_IWUSR
, dir
, NULL
, &fops_mark_good
);
812 debugfs_create_file_unsafe("mark_bad", S_IWUSR
, dir
, NULL
, &fops_mark_bad
);
813 debugfs_create_file_unsafe("debug", S_IWUSR
, dir
, NULL
, &fops_debug
);
816 void mtk_bmt_detach(struct mtd_info
*mtd
)
821 if (bmtd
.debugfs_dir
)
822 debugfs_remove_recursive(bmtd
.debugfs_dir
);
823 bmtd
.debugfs_dir
= NULL
;
826 kfree(nand_data_buf
);
828 mtd
->_read_oob
= bmtd
._read_oob
;
829 mtd
->_write_oob
= bmtd
._write_oob
;
830 mtd
->_erase
= bmtd
._erase
;
831 mtd
->_block_isbad
= bmtd
._block_isbad
;
832 mtd
->_block_markbad
= bmtd
._block_markbad
;
833 mtd
->size
= bmtd
.total_blks
<< bmtd
.blk_shift
;
835 memset(&bmtd
, 0, sizeof(bmtd
));
838 /* total_blocks - The total count of blocks that the Nand Chip has */
839 int mtk_bmt_attach(struct mtd_info
*mtd
)
841 struct device_node
*np
;
845 u16 total_blocks
, pmt_block
;
847 u32 bmt_pool_size
, bmt_table_size
;
852 np
= mtd_get_of_node(mtd
);
856 if (!of_property_read_bool(np
, "mediatek,bmt-v2"))
859 if (of_property_read_u32(np
, "mediatek,bmt-pool-size",
860 &bmt_pool_size
) != 0)
863 if (of_property_read_u8(np
, "mediatek,bmt-oob-offset",
864 &bmtd
.oob_offset
) != 0)
867 if (of_property_read_u32(np
, "mediatek,bmt-table-size",
868 &bmt_table_size
) != 0)
869 bmt_table_size
= 0x2000U
;
871 bmtd
.remap_range
= of_get_property(np
, "mediatek,bmt-remap-range",
872 &bmtd
.remap_range_len
);
873 bmtd
.remap_range_len
/= 8;
876 mtk_bmt_replace_ops(mtd
);
878 bmtd
.table_size
= bmt_table_size
;
879 bmtd
.blk_size
= mtd
->erasesize
;
880 bmtd
.blk_shift
= ffs(bmtd
.blk_size
) - 1;
881 bmtd
.pg_size
= mtd
->writesize
;
882 bmtd
.pg_shift
= ffs(bmtd
.pg_size
) - 1;
883 total_blocks
= mtd
->size
>> bmtd
.blk_shift
;
884 pmt_block
= total_blocks
- bmt_pool_size
- 2;
886 mtd
->size
= pmt_block
<< bmtd
.blk_shift
;
889 * ---------------------------------------
890 * | PMT(2blks) | BMT POOL(totalblks * 2%) |
891 * ---------------------------------------
894 * pmt_block pmt_block + 2blocks(pool_lba)
897 * The blocks ahead of the boundary block are stored in bb_tbl
898 * and blocks behind are stored in bmt_tbl
901 bmtd
.pool_lba
= (u16
)(pmt_block
+ 2);
902 bmtd
.total_blks
= total_blocks
;
903 bmtd
.bb_max
= bmtd
.total_blks
* BBPOOL_RATIO
/ 100;
905 /* 3 buffers we need */
906 bufsz
= round_up(sizeof(struct bbbt
) +
907 bmt_table_size
* sizeof(struct bbmt
), bmtd
.pg_size
);
908 bmtd
.bmt_pgs
= bufsz
>> bmtd
.pg_shift
;
910 nand_bbt_buf
= kzalloc(bufsz
, GFP_KERNEL
);
911 nand_data_buf
= kzalloc(bmtd
.pg_size
, GFP_KERNEL
);
913 if (!nand_bbt_buf
|| !nand_data_buf
) {
914 pr_info("nand: FATAL ERR: allocate buffer failed!\n");
919 memset(nand_bbt_buf
, 0xff, bufsz
);
920 memset(nand_data_buf
, 0xff, bmtd
.pg_size
);
922 BBT_LOG("bbtbuf=0x%p(0x%x) dat=0x%p(0x%x)",
923 nand_bbt_buf
, bufsz
, nand_data_buf
, bmtd
.pg_size
);
924 BBT_LOG("pool_lba=0x%x total_blks=0x%x bb_max=0x%x",
925 bmtd
.pool_lba
, bmtd
.total_blks
, bmtd
.bb_max
);
927 /* Scanning start from the first page of the last block
930 bbt
= scan_bmt(bmtd
.total_blks
- 1);
933 if (bmtd
.total_blks
> BB_TABLE_MAX
+ BMT_TABLE_MAX
) {
934 pr_info("nand: FATAL: Too many blocks, can not support!\n");
939 bbt
= (struct bbbt
*)nand_bbt_buf
;
940 memset(bmt_tbl(bbt
), BMT_TBL_DEF_VAL
, bmtd
.table_size
* sizeof(struct bbmt
));
942 if (scan_bad_blocks(bbt
)) {
947 /* BMT always in the last valid block in pool */
948 bmtd
.bmt_blk_idx
= upload_bmt(bbt
, bmtd
.bmt_blk_idx
);
949 block
= bmt_tbl(bbt
)[bmtd
.bmt_blk_idx
].block
;
950 pr_notice("[BBT] BMT.v2 is written into PBA:0x%x\n", block
);
952 if (bmtd
.bmt_blk_idx
== 0)
953 pr_info("nand: Warning: no available block in BMT pool!\n");
954 else if (bmtd
.bmt_blk_idx
== (u16
)-1) {
959 mtk_bmt_add_debugfs();
970 MODULE_LICENSE("GPL");
971 MODULE_AUTHOR("Xiangsheng Hou <xiangsheng.hou@mediatek.com>, Felix Fietkau <nbd@nbd.name>");
972 MODULE_DESCRIPTION("Bad Block mapping management v2 for MediaTek NAND Flash Driver");