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
41 int (*init
)(struct device_node
*np
);
42 bool (*remap_block
)(u16 block
, u16 mapped_block
, int copy_len
);
43 void (*unmap_block
)(u16 block
);
44 u16 (*get_mapping_block
)(int block
);
45 int (*debug
)(void *data
, u64 val
);
50 /* This version is used to distinguish the legacy and new algorithm */
51 #define BBMT_VERSION 2
52 unsigned char version
;
53 /* Below 2 tables will be written in SLC */
60 #define NORMAL_MAPPED 1
65 static struct bmt_desc
{
68 int (*_read_oob
) (struct mtd_info
*mtd
, loff_t from
,
69 struct mtd_oob_ops
*ops
);
70 int (*_write_oob
) (struct mtd_info
*mtd
, loff_t to
,
71 struct mtd_oob_ops
*ops
);
72 int (*_erase
) (struct mtd_info
*mtd
, struct erase_info
*instr
);
73 int (*_block_isbad
) (struct mtd_info
*mtd
, loff_t ofs
);
74 int (*_block_markbad
) (struct mtd_info
*mtd
, loff_t ofs
);
76 const struct mtk_bmt_ops
*ops
;
80 struct dentry
*debugfs_dir
;
87 /* bbt logical address */
89 /* bbt physical address */
91 /* Maximum count of bad blocks that the vendor guaranteed */
93 /* Total blocks of the Nand Chip */
95 /* The block(n) BMT is located at (bmt_tbl[n]) */
97 /* How many pages needs to store 'struct bbbt' */
100 const __be32
*remap_range
;
103 /* to compensate for driver level remapping */
107 static unsigned char *nand_bbt_buf
;
108 static unsigned char *nand_data_buf
;
110 /* -------- Unit conversions -------- */
111 static inline u32
blk_pg(u16 block
)
113 return (u32
)(block
<< (bmtd
.blk_shift
- bmtd
.pg_shift
));
116 /* -------- Nand operations wrapper -------- */
118 bbt_nand_read(u32 page
, unsigned char *dat
, int dat_len
,
119 unsigned char *fdm
, int fdm_len
)
121 struct mtd_oob_ops ops
= {
122 .mode
= MTD_OPS_PLACE_OOB
,
123 .ooboffs
= bmtd
.oob_offset
,
130 return bmtd
._read_oob(bmtd
.mtd
, page
<< bmtd
.pg_shift
, &ops
);
133 static inline int bbt_nand_erase(u16 block
)
135 struct mtd_info
*mtd
= bmtd
.mtd
;
136 struct erase_info instr
= {
137 .addr
= (loff_t
)block
<< bmtd
.blk_shift
,
138 .len
= bmtd
.blk_size
,
141 return bmtd
._erase(mtd
, &instr
);
144 static inline int bbt_nand_copy(u16 dest_blk
, u16 src_blk
, loff_t max_offset
)
146 int pages
= bmtd
.blk_size
>> bmtd
.pg_shift
;
147 loff_t src
= (loff_t
)src_blk
<< bmtd
.blk_shift
;
148 loff_t dest
= (loff_t
)dest_blk
<< bmtd
.blk_shift
;
153 for (i
= 0; i
< pages
; i
++) {
154 struct mtd_oob_ops rd_ops
= {
155 .mode
= MTD_OPS_PLACE_OOB
,
157 .ooblen
= min_t(int, bmtd
.mtd
->oobsize
/ pages
, sizeof(oob
)),
158 .datbuf
= nand_data_buf
,
161 struct mtd_oob_ops wr_ops
= {
162 .mode
= MTD_OPS_PLACE_OOB
,
164 .datbuf
= nand_data_buf
,
168 if (offset
>= max_offset
)
171 ret
= bmtd
._read_oob(bmtd
.mtd
, src
+ offset
, &rd_ops
);
172 if (ret
< 0 && !mtd_is_bitflip(ret
))
178 ret
= bmtd
._write_oob(bmtd
.mtd
, dest
+ offset
, &wr_ops
);
182 wr_ops
.ooblen
= rd_ops
.oobretlen
;
183 offset
+= rd_ops
.retlen
;
189 /* -------- Bad Blocks Management -------- */
190 static inline struct bbmt
*bmt_tbl(struct bbbt
*bbbt
)
192 return (struct bbmt
*)&bbbt
->bb_tbl
[bmtd
.table_size
];
196 read_bmt(u16 block
, unsigned char *dat
, unsigned char *fdm
, int fdm_len
)
198 u32 len
= bmtd
.bmt_pgs
<< bmtd
.pg_shift
;
200 return bbt_nand_read(blk_pg(block
), dat
, len
, fdm
, fdm_len
);
203 static int write_bmt(u16 block
, unsigned char *dat
)
205 struct mtd_oob_ops ops
= {
206 .mode
= MTD_OPS_PLACE_OOB
,
207 .ooboffs
= OOB_SIGNATURE_OFFSET
+ bmtd
.oob_offset
,
208 .oobbuf
= bmtd
.ops
->sig
,
209 .ooblen
= bmtd
.ops
->sig_len
,
211 .len
= bmtd
.bmt_pgs
<< bmtd
.pg_shift
,
213 loff_t addr
= (loff_t
)block
<< bmtd
.blk_shift
;
215 return bmtd
._write_oob(bmtd
.mtd
, addr
, &ops
);
218 static u16
find_valid_block(u16 block
)
225 if (block
>= bmtd
.total_blks
)
228 ret
= bbt_nand_read(blk_pg(block
), nand_data_buf
, bmtd
.pg_size
,
230 /* Read the 1st byte of FDM to judge whether it's a bad
233 if (ret
|| fdm
[0] != 0xff) {
234 pr_info("nand: found bad block 0x%x\n", block
);
235 if (loop
>= bmtd
.bb_max
) {
236 pr_info("nand: FATAL ERR: too many bad blocks!!\n");
248 /* Find out all bad blocks, and fill in the mapping table */
249 static int scan_bad_blocks(struct bbbt
*bbt
)
254 /* First time download, the block0 MUST NOT be a bad block,
255 * this is guaranteed by vendor
260 * Construct the mapping table of Normal data area(non-PMT/BMTPOOL)
261 * G - Good block; B - Bad block
262 * ---------------------------
263 * physical |G|G|B|G|B|B|G|G|G|G|B|G|B|
264 * ---------------------------
265 * What bb_tbl[i] looks like:
267 * 0 1 2 3 4 5 6 7 8 9 a b c
268 * mapped block(bb_tbl[i]):
269 * 0 1 3 6 7 8 9 b ......
271 * If new bad block ocurred(n), search bmt_tbl to find
272 * a available block(x), and fill in the bb_tbl[n] = x;
274 for (i
= 1; i
< bmtd
.pool_lba
; i
++) {
275 bbt
->bb_tbl
[i
] = find_valid_block(bbt
->bb_tbl
[i
- 1] + 1);
276 BBT_LOG("bb_tbl[0x%x] = 0x%x", i
, bbt
->bb_tbl
[i
]);
277 if (bbt
->bb_tbl
[i
] == 0)
281 /* Physical Block start Address of BMT pool */
282 bmtd
.pool_pba
= bbt
->bb_tbl
[i
- 1] + 1;
283 if (bmtd
.pool_pba
>= bmtd
.total_blks
- 2) {
284 pr_info("nand: FATAL ERR: Too many bad blocks!!\n");
288 BBT_LOG("pool_pba=0x%x", bmtd
.pool_pba
);
290 block
= bmtd
.pool_pba
;
292 * The bmt table is used for runtime bad block mapping
293 * G - Good block; B - Bad block
294 * ---------------------------
295 * physical |G|G|B|G|B|B|G|G|G|G|B|G|B|
296 * ---------------------------
297 * block: 0 1 2 3 4 5 6 7 8 9 a b c
298 * What bmt_tbl[i] looks like in initial state:
305 * N - Not mapped(Available)
309 * BMT always in the last valid block in pool
311 while ((block
= find_valid_block(block
)) != 0) {
312 bmt_tbl(bbt
)[i
].block
= block
;
313 bmt_tbl(bbt
)[i
].mapped
= NO_MAPPED
;
314 BBT_LOG("bmt_tbl[%d].block = 0x%x", i
, block
);
319 /* i - How many available blocks in pool, which is the length of bmt_tbl[]
320 * bmtd.bmt_blk_idx - bmt_tbl[bmtd.bmt_blk_idx].block => the BMT block
322 bmtd
.bmt_blk_idx
= i
- 1;
323 bmt_tbl(bbt
)[bmtd
.bmt_blk_idx
].mapped
= BMT_MAPPED
;
326 pr_info("nand: FATAL ERR: no space to store BMT!!\n");
330 pr_info("[BBT] %d available blocks in BMT pool\n", i
);
335 static bool is_valid_bmt(unsigned char *buf
, unsigned char *fdm
)
337 struct bbbt
*bbt
= (struct bbbt
*)buf
;
338 u8
*sig
= (u8
*)bbt
->signature
+ MAIN_SIGNATURE_OFFSET
;
341 if (memcmp(bbt
->signature
+ MAIN_SIGNATURE_OFFSET
, "BMT", 3) == 0 &&
342 memcmp(fdm
+ OOB_SIGNATURE_OFFSET
, "bmt", 3) == 0) {
343 if (bbt
->version
== BBMT_VERSION
)
346 BBT_LOG("[BBT] BMT Version not match,upgrage preloader and uboot please! sig=%02x%02x%02x, fdm=%02x%02x%02x",
347 sig
[0], sig
[1], sig
[2],
348 fdm
[1], fdm
[2], fdm
[3]);
352 static u16
get_bmt_index(struct bbmt
*bmt
)
356 while (bmt
[i
].block
!= BMT_TBL_DEF_VAL
) {
357 if (bmt
[i
].mapped
== BMT_MAPPED
)
364 static struct bbbt
*scan_bmt(u16 block
)
368 if (block
< bmtd
.pool_lba
)
371 if (read_bmt(block
, nand_bbt_buf
, fdm
, sizeof(fdm
)))
372 return scan_bmt(block
- 1);
374 if (is_valid_bmt(nand_bbt_buf
, fdm
)) {
375 bmtd
.bmt_blk_idx
= get_bmt_index(bmt_tbl((struct bbbt
*)nand_bbt_buf
));
376 if (bmtd
.bmt_blk_idx
== 0) {
377 pr_info("[BBT] FATAL ERR: bmt block index is wrong!\n");
380 pr_info("[BBT] BMT.v2 is found at 0x%x\n", block
);
381 return (struct bbbt
*)nand_bbt_buf
;
383 return scan_bmt(block
- 1);
386 /* Write the Burner Bad Block Table to Nand Flash
387 * n - write BMT to bmt_tbl[n]
389 static u16
upload_bmt(struct bbbt
*bbt
, int n
)
394 if (n
< 0 || bmt_tbl(bbt
)[n
].mapped
== NORMAL_MAPPED
) {
395 pr_info("nand: FATAL ERR: no space to store BMT!\n");
399 block
= bmt_tbl(bbt
)[n
].block
;
400 BBT_LOG("n = 0x%x, block = 0x%x", n
, block
);
401 if (bbt_nand_erase(block
)) {
402 bmt_tbl(bbt
)[n
].block
= 0;
403 /* erase failed, try the previous block: bmt_tbl[n - 1].block */
408 /* The signature offset is fixed set to 0,
409 * oob signature offset is fixed set to 1
411 memcpy(bbt
->signature
+ MAIN_SIGNATURE_OFFSET
, "BMT", 3);
412 bbt
->version
= BBMT_VERSION
;
414 if (write_bmt(block
, (unsigned char *)bbt
)) {
415 bmt_tbl(bbt
)[n
].block
= 0;
417 /* write failed, try the previous block in bmt_tbl[n - 1] */
422 /* Return the current index(n) of BMT pool (bmt_tbl[n]) */
426 static u16
find_valid_block_in_pool(struct bbbt
*bbt
)
430 if (bmtd
.bmt_blk_idx
== 0)
433 for (i
= 0; i
< bmtd
.bmt_blk_idx
; i
++) {
434 if (bmt_tbl(bbt
)[i
].block
!= 0 && bmt_tbl(bbt
)[i
].mapped
== NO_MAPPED
) {
435 bmt_tbl(bbt
)[i
].mapped
= NORMAL_MAPPED
;
436 return bmt_tbl(bbt
)[i
].block
;
441 pr_info("nand: FATAL ERR: BMT pool is run out!\n");
445 /* We met a bad block, mark it as bad and map it to a valid block in pool,
446 * if it's a write failure, we need to write the data to mapped block
448 static bool remap_block_v2(u16 block
, u16 mapped_block
, int copy_len
)
454 mapped_blk
= find_valid_block_in_pool(bbt
);
458 /* Map new bad block to available block in pool */
459 bbt
->bb_tbl
[block
] = mapped_blk
;
461 /* Erase new block */
462 bbt_nand_erase(mapped_blk
);
464 bbt_nand_copy(mapped_blk
, block
, copy_len
);
466 bmtd
.bmt_blk_idx
= upload_bmt(bbt
, bmtd
.bmt_blk_idx
);
472 mapping_block_in_range(int block
)
474 const __be32
*cur
= bmtd
.remap_range
;
475 u32 addr
= block
<< bmtd
.blk_shift
;
478 if (!cur
|| !bmtd
.remap_range_len
)
481 for (i
= 0; i
< bmtd
.remap_range_len
; i
++, cur
+= 2)
482 if (addr
>= be32_to_cpu(cur
[0]) && addr
< be32_to_cpu(cur
[1]))
489 get_mapping_block_index_v2(int block
)
491 if (block
>= bmtd
.pool_lba
)
494 if (!mapping_block_in_range(block
))
497 return bmtd
.bbt
->bb_tbl
[block
];
501 mtk_bmt_read(struct mtd_info
*mtd
, loff_t from
,
502 struct mtd_oob_ops
*ops
)
504 struct mtd_oob_ops cur_ops
= *ops
;
508 int max_bitflips
= 0;
514 while (ops
->retlen
< ops
->len
|| ops
->oobretlen
< ops
->ooblen
) {
517 u32 offset
= from
& (bmtd
.blk_size
- 1);
518 u32 block
= from
>> bmtd
.blk_shift
;
521 cur_block
= bmtd
.ops
->get_mapping_block(block
);
522 cur_from
= ((loff_t
)cur_block
<< bmtd
.blk_shift
) + offset
;
524 cur_ops
.oobretlen
= 0;
526 cur_ops
.len
= min_t(u32
, mtd
->erasesize
- offset
,
527 ops
->len
- ops
->retlen
);
528 cur_ret
= bmtd
._read_oob(mtd
, cur_from
, &cur_ops
);
532 max_bitflips
= max_t(int, max_bitflips
, cur_ret
);
533 if (cur_ret
< 0 && !mtd_is_bitflip(cur_ret
)) {
534 bmtd
.ops
->remap_block(block
, cur_block
, mtd
->erasesize
);
535 if (retry_count
++ < 10)
541 if (cur_ret
>= mtd
->bitflip_threshold
&&
542 mapping_block_in_range(block
, &start
, &end
))
543 bmtd
.ops
->remap_block(block
, cur_block
, mtd
->erasesize
);
545 ops
->retlen
+= cur_ops
.retlen
;
546 ops
->oobretlen
+= cur_ops
.oobretlen
;
549 cur_ops
.datbuf
+= cur_ops
.retlen
;
550 cur_ops
.oobbuf
+= cur_ops
.oobretlen
;
551 cur_ops
.ooblen
-= cur_ops
.oobretlen
;
554 cur_ops
.len
= mtd
->erasesize
- offset
;
568 mtk_bmt_write(struct mtd_info
*mtd
, loff_t to
,
569 struct mtd_oob_ops
*ops
)
571 struct mtd_oob_ops cur_ops
= *ops
;
579 while (ops
->retlen
< ops
->len
|| ops
->oobretlen
< ops
->ooblen
) {
580 u32 offset
= to
& (bmtd
.blk_size
- 1);
581 u32 block
= to
>> bmtd
.blk_shift
;
584 cur_block
= bmtd
.ops
->get_mapping_block(block
);
585 cur_to
= ((loff_t
)cur_block
<< bmtd
.blk_shift
) + offset
;
587 cur_ops
.oobretlen
= 0;
589 cur_ops
.len
= min_t(u32
, bmtd
.blk_size
- offset
,
590 ops
->len
- ops
->retlen
);
591 ret
= bmtd
._write_oob(mtd
, cur_to
, &cur_ops
);
593 bmtd
.ops
->remap_block(block
, cur_block
, offset
);
594 if (retry_count
++ < 10)
600 ops
->retlen
+= cur_ops
.retlen
;
601 ops
->oobretlen
+= cur_ops
.oobretlen
;
604 cur_ops
.datbuf
+= cur_ops
.retlen
;
605 cur_ops
.oobbuf
+= cur_ops
.oobretlen
;
606 cur_ops
.ooblen
-= cur_ops
.oobretlen
;
609 cur_ops
.len
= mtd
->erasesize
- offset
;
619 mtk_bmt_mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
621 struct erase_info mapped_instr
= {
622 .len
= bmtd
.blk_size
,
625 u64 start_addr
, end_addr
;
627 u16 orig_block
, block
;
629 start_addr
= instr
->addr
& (~mtd
->erasesize_mask
);
630 end_addr
= instr
->addr
+ instr
->len
;
632 while (start_addr
< end_addr
) {
633 orig_block
= start_addr
>> bmtd
.blk_shift
;
634 block
= bmtd
.ops
->get_mapping_block(orig_block
);
635 mapped_instr
.addr
= (loff_t
)block
<< bmtd
.blk_shift
;
636 ret
= bmtd
._erase(mtd
, &mapped_instr
);
638 bmtd
.ops
->remap_block(orig_block
, block
, 0);
639 if (retry_count
++ < 10)
641 instr
->fail_addr
= start_addr
;
644 start_addr
+= mtd
->erasesize
;
651 mtk_bmt_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
654 u16 orig_block
= ofs
>> bmtd
.blk_shift
;
659 block
= bmtd
.ops
->get_mapping_block(orig_block
);
660 ret
= bmtd
._block_isbad(mtd
, (loff_t
)block
<< bmtd
.blk_shift
);
662 bmtd
.ops
->remap_block(orig_block
, block
, bmtd
.blk_size
);
663 if (retry_count
++ < 10)
670 mtk_bmt_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
672 u16 orig_block
= ofs
>> bmtd
.blk_shift
;
673 u16 block
= bmtd
.ops
->get_mapping_block(orig_block
);
675 bmtd
.ops
->remap_block(orig_block
, block
, bmtd
.blk_size
);
677 return bmtd
._block_markbad(mtd
, (loff_t
)block
<< bmtd
.blk_shift
);
681 mtk_bmt_replace_ops(struct mtd_info
*mtd
)
683 bmtd
._read_oob
= mtd
->_read_oob
;
684 bmtd
._write_oob
= mtd
->_write_oob
;
685 bmtd
._erase
= mtd
->_erase
;
686 bmtd
._block_isbad
= mtd
->_block_isbad
;
687 bmtd
._block_markbad
= mtd
->_block_markbad
;
689 mtd
->_read_oob
= mtk_bmt_read
;
690 mtd
->_write_oob
= mtk_bmt_write
;
691 mtd
->_erase
= mtk_bmt_mtd_erase
;
692 mtd
->_block_isbad
= mtk_bmt_block_isbad
;
693 mtd
->_block_markbad
= mtk_bmt_block_markbad
;
697 unmap_block_v2(u16 block
)
699 bmtd
.bbt
->bb_tbl
[block
] = block
;
700 bmtd
.bmt_blk_idx
= upload_bmt(bmtd
.bbt
, bmtd
.bmt_blk_idx
);
703 static int mtk_bmt_debug_mark_good(void *data
, u64 val
)
705 bmtd
.ops
->unmap_block(val
>> bmtd
.blk_shift
);
710 static int mtk_bmt_debug_mark_bad(void *data
, u64 val
)
712 u32 block
= val
>> bmtd
.blk_shift
;
713 u16 cur_block
= bmtd
.ops
->get_mapping_block(block
);
715 bmtd
.ops
->remap_block(block
, cur_block
, bmtd
.blk_size
);
720 static unsigned long *
721 mtk_bmt_get_mapping_mask(void)
723 struct bbmt
*bbmt
= bmt_tbl(bmtd
.bbt
);
724 int main_blocks
= bmtd
.mtd
->size
>> bmtd
.blk_shift
;
728 used
= kcalloc(sizeof(unsigned long), BIT_WORD(bmtd
.bmt_blk_idx
) + 1, GFP_KERNEL
);
732 for (i
= 1; i
< main_blocks
; i
++) {
733 if (bmtd
.bbt
->bb_tbl
[i
] == i
)
736 for (k
= 0; k
< bmtd
.bmt_blk_idx
; k
++) {
737 if (bmtd
.bbt
->bb_tbl
[i
] != bbmt
[k
].block
)
748 static int mtk_bmt_debug_v2(void *data
, u64 val
)
750 struct bbmt
*bbmt
= bmt_tbl(bmtd
.bbt
);
751 struct mtd_info
*mtd
= bmtd
.mtd
;
753 int main_blocks
= mtd
->size
>> bmtd
.blk_shift
;
757 used
= mtk_bmt_get_mapping_mask();
763 for (i
= 1; i
< main_blocks
; i
++) {
764 if (bmtd
.bbt
->bb_tbl
[i
] == i
)
767 printk("remap [%x->%x]\n", i
, bmtd
.bbt
->bb_tbl
[i
]);
770 for (i
= 0; i
<= bmtd
.bmt_blk_idx
; i
++) {
773 switch (bbmt
[i
].mapped
) {
778 if (test_bit(i
, used
))
788 printk("[%x:%c] = 0x%x\n", i
, c
, bbmt
[i
].block
);
792 for (i
= 0; i
<= bmtd
.bmt_blk_idx
; i
++) {
793 if (bbmt
[i
].mapped
!= NORMAL_MAPPED
)
796 if (test_bit(i
, used
))
800 bbmt
[i
].mapped
= NO_MAPPED
;
801 printk("free block [%d:%x]\n", i
, bbmt
[i
].block
);
804 bmtd
.bmt_blk_idx
= upload_bmt(bmtd
.bbt
, bmtd
.bmt_blk_idx
);
813 static int mtk_bmt_debug(void *data
, u64 val
)
815 return bmtd
.ops
->debug(data
, val
);
819 DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_good
, NULL
, mtk_bmt_debug_mark_good
, "%llu\n");
820 DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_bad
, NULL
, mtk_bmt_debug_mark_bad
, "%llu\n");
821 DEFINE_DEBUGFS_ATTRIBUTE(fops_debug
, NULL
, mtk_bmt_debug
, "%llu\n");
824 mtk_bmt_add_debugfs(void)
828 dir
= bmtd
.debugfs_dir
= debugfs_create_dir("mtk-bmt", NULL
);
832 debugfs_create_file_unsafe("mark_good", S_IWUSR
, dir
, NULL
, &fops_mark_good
);
833 debugfs_create_file_unsafe("mark_bad", S_IWUSR
, dir
, NULL
, &fops_mark_bad
);
834 debugfs_create_file_unsafe("debug", S_IWUSR
, dir
, NULL
, &fops_debug
);
837 void mtk_bmt_detach(struct mtd_info
*mtd
)
842 if (bmtd
.debugfs_dir
)
843 debugfs_remove_recursive(bmtd
.debugfs_dir
);
844 bmtd
.debugfs_dir
= NULL
;
847 kfree(nand_data_buf
);
849 mtd
->_read_oob
= bmtd
._read_oob
;
850 mtd
->_write_oob
= bmtd
._write_oob
;
851 mtd
->_erase
= bmtd
._erase
;
852 mtd
->_block_isbad
= bmtd
._block_isbad
;
853 mtd
->_block_markbad
= bmtd
._block_markbad
;
854 mtd
->size
= bmtd
.total_blks
<< bmtd
.blk_shift
;
856 memset(&bmtd
, 0, sizeof(bmtd
));
859 static int mtk_bmt_init_v2(struct device_node
*np
)
861 u32 bmt_pool_size
, bmt_table_size
;
865 if (of_property_read_u32(np
, "mediatek,bmt-pool-size",
866 &bmt_pool_size
) != 0)
869 if (of_property_read_u8(np
, "mediatek,bmt-oob-offset",
870 &bmtd
.oob_offset
) != 0)
873 if (of_property_read_u32(np
, "mediatek,bmt-table-size",
874 &bmt_table_size
) != 0)
875 bmt_table_size
= 0x2000U
;
877 bmtd
.table_size
= bmt_table_size
;
879 pmt_block
= bmtd
.total_blks
- bmt_pool_size
- 2;
881 bmtd
.mtd
->size
= pmt_block
<< bmtd
.blk_shift
;
884 * ---------------------------------------
885 * | PMT(2blks) | BMT POOL(totalblks * 2%) |
886 * ---------------------------------------
889 * pmt_block pmt_block + 2blocks(pool_lba)
892 * The blocks ahead of the boundary block are stored in bb_tbl
893 * and blocks behind are stored in bmt_tbl
896 bmtd
.pool_lba
= (u16
)(pmt_block
+ 2);
897 bmtd
.bb_max
= bmtd
.total_blks
* BBPOOL_RATIO
/ 100;
899 bufsz
= round_up(sizeof(struct bbbt
) +
900 bmt_table_size
* sizeof(struct bbmt
), bmtd
.pg_size
);
901 bmtd
.bmt_pgs
= bufsz
>> bmtd
.pg_shift
;
903 nand_bbt_buf
= kzalloc(bufsz
, GFP_KERNEL
);
907 memset(nand_bbt_buf
, 0xff, bufsz
);
909 /* Scanning start from the first page of the last block
912 bmtd
.bbt
= scan_bmt(bmtd
.total_blks
- 1);
915 if (bmtd
.total_blks
> BB_TABLE_MAX
+ BMT_TABLE_MAX
) {
916 pr_info("nand: FATAL: Too many blocks, can not support!\n");
920 bmtd
.bbt
= (struct bbbt
*)nand_bbt_buf
;
921 memset(bmt_tbl(bmtd
.bbt
), BMT_TBL_DEF_VAL
,
922 bmtd
.table_size
* sizeof(struct bbmt
));
924 if (scan_bad_blocks(bmtd
.bbt
))
927 /* BMT always in the last valid block in pool */
928 bmtd
.bmt_blk_idx
= upload_bmt(bmtd
.bbt
, bmtd
.bmt_blk_idx
);
929 block
= bmt_tbl(bmtd
.bbt
)[bmtd
.bmt_blk_idx
].block
;
930 pr_notice("[BBT] BMT.v2 is written into PBA:0x%x\n", block
);
932 if (bmtd
.bmt_blk_idx
== 0)
933 pr_info("nand: Warning: no available block in BMT pool!\n");
934 else if (bmtd
.bmt_blk_idx
== (u16
)-1)
941 int mtk_bmt_attach(struct mtd_info
*mtd
)
943 static const struct mtk_bmt_ops v2_ops
= {
946 .init
= mtk_bmt_init_v2
,
947 .remap_block
= remap_block_v2
,
948 .unmap_block
= unmap_block_v2
,
949 .get_mapping_block
= get_mapping_block_index_v2
,
950 .debug
= mtk_bmt_debug_v2
,
952 struct device_node
*np
;
958 np
= mtd_get_of_node(mtd
);
962 if (of_property_read_bool(np
, "mediatek,bmt-v2"))
967 bmtd
.remap_range
= of_get_property(np
, "mediatek,bmt-remap-range",
968 &bmtd
.remap_range_len
);
969 bmtd
.remap_range_len
/= 8;
972 mtk_bmt_replace_ops(mtd
);
974 bmtd
.blk_size
= mtd
->erasesize
;
975 bmtd
.blk_shift
= ffs(bmtd
.blk_size
) - 1;
976 bmtd
.pg_size
= mtd
->writesize
;
977 bmtd
.pg_shift
= ffs(bmtd
.pg_size
) - 1;
978 bmtd
.total_blks
= mtd
->size
>> bmtd
.blk_shift
;
980 nand_data_buf
= kzalloc(bmtd
.pg_size
, GFP_KERNEL
);
981 if (!nand_data_buf
) {
982 pr_info("nand: FATAL ERR: allocate buffer failed!\n");
987 memset(nand_data_buf
, 0xff, bmtd
.pg_size
);
989 ret
= bmtd
.ops
->init(np
);
993 mtk_bmt_add_debugfs();
1002 MODULE_LICENSE("GPL");
1003 MODULE_AUTHOR("Xiangsheng Hou <xiangsheng.hou@mediatek.com>, Felix Fietkau <nbd@nbd.name>");
1004 MODULE_DESCRIPTION("Bad Block mapping management v2 for MediaTek NAND Flash Driver");