1 --- a/drivers/mtd/nand/Kconfig
2 +++ b/drivers/mtd/nand/Kconfig
3 @@ -15,6 +15,10 @@ config MTD_NAND_ECC
5 depends on MTD_NAND_CORE
7 +config MTD_NAND_MTK_BMT
8 + bool "Support MediaTek NAND Bad-block Management Table"
14 --- a/drivers/mtd/nand/Makefile
15 +++ b/drivers/mtd/nand/Makefile
18 nandcore-objs := core.o bbt.o
19 obj-$(CONFIG_MTD_NAND_CORE) += nandcore.o
20 +obj-$(CONFIG_MTD_NAND_MTK_BMT) += mtk_bmt.o
25 +++ b/drivers/mtd/nand/mtk_bmt.c
28 + * Copyright (c) 2017 MediaTek Inc.
29 + * Author: Xiangsheng Hou <xiangsheng.hou@mediatek.com>
30 + * Copyright (c) 2020 Felix Fietkau <nbd@nbd.name>
32 + * This program is free software; you can redistribute it and/or modify
33 + * it under the terms of the GNU General Public License version 2 as
34 + * published by the Free Software Foundation.
36 + * This program is distributed in the hope that it will be useful,
37 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 + * GNU General Public License for more details.
42 +#include <linux/slab.h>
43 +#include <linux/gfp.h>
44 +#include <linux/kernel.h>
45 +#include <linux/of.h>
46 +#include <linux/mtd/nand.h>
47 +#include <linux/mtd/partitions.h>
48 +#include <linux/mtd/mtk_bmt.h>
49 +#include <linux/module.h>
50 +#include <linux/debugfs.h>
52 +#define MAIN_SIGNATURE_OFFSET 0
53 +#define OOB_SIGNATURE_OFFSET 1
54 +#define BBPOOL_RATIO 2
56 +#define BBT_LOG(fmt, ...) pr_debug("[BBT][%s|%d] "fmt"\n", __func__, __LINE__, ##__VA_ARGS__)
58 +/* Maximum 8k blocks */
59 +#define BB_TABLE_MAX bmtd.table_size
60 +#define BMT_TABLE_MAX (BB_TABLE_MAX * BBPOOL_RATIO / 100)
61 +#define BMT_TBL_DEF_VAL 0x0
64 + * Burner Bad Block Table
65 + * --------- Only support SLC Nand Chips!!!!!!!!!!! ----------
70 + /* This version is used to distinguish the legacy and new algorithm */
71 +#define BBMT_VERSION 2
72 + unsigned char version;
73 + /* Below 2 tables will be written in SLC */
80 +#define NORMAL_MAPPED 1
85 +static struct bmt_desc {
86 + struct mtd_info *mtd;
88 + int (*_read_oob) (struct mtd_info *mtd, loff_t from,
89 + struct mtd_oob_ops *ops);
90 + int (*_write_oob) (struct mtd_info *mtd, loff_t to,
91 + struct mtd_oob_ops *ops);
92 + const struct nand_ops *nand_ops;
96 + struct dentry *debugfs_dir;
103 + /* bbt logical address */
105 + /* bbt physical address */
107 + /* Maximum count of bad blocks that the vendor guaranteed */
109 + /* Total blocks of the Nand Chip */
111 + /* The block(n) BMT is located at (bmt_tbl[n]) */
113 + /* How many pages needs to store 'struct bbbt' */
116 + /* to compensate for driver level remapping */
120 +static unsigned char *nand_bbt_buf;
121 +static unsigned char *nand_data_buf;
123 +/* -------- Unit conversions -------- */
124 +static inline u32 blk_pg(u16 block)
126 + return (u32)(block << (bmtd.blk_shift - bmtd.pg_shift));
129 +/* -------- Nand operations wrapper -------- */
131 +bbt_nand_read(u32 page, unsigned char *dat, int dat_len,
132 + unsigned char *fdm, int fdm_len)
134 + struct mtd_oob_ops ops = {
135 + .mode = MTD_OPS_PLACE_OOB,
136 + .ooboffs = bmtd.oob_offset,
143 + return bmtd._read_oob(bmtd.mtd, page << bmtd.pg_shift, &ops);
146 +static inline int bbt_nand_erase(u16 block)
148 + struct nand_device *nand = mtd_to_nanddev(bmtd.mtd);
149 + loff_t addr = (loff_t)block << bmtd.blk_shift;
150 + struct nand_pos pos;
152 + nanddev_offs_to_pos(nand, addr, &pos);
153 + return bmtd.nand_ops->erase(nand, &pos);
156 +/* -------- Bad Blocks Management -------- */
157 +static inline struct bbmt *bmt_tbl(struct bbbt *bbbt)
159 + return (struct bbmt *)&bbbt->bb_tbl[bmtd.table_size];
163 +read_bmt(u16 block, unsigned char *dat, unsigned char *fdm, int fdm_len)
165 + u32 len = bmtd.bmt_pgs << bmtd.pg_shift;
167 + return bbt_nand_read(blk_pg(block), dat, len, fdm, fdm_len);
170 +static int write_bmt(u16 block, unsigned char *dat)
172 + struct mtd_oob_ops ops = {
173 + .mode = MTD_OPS_PLACE_OOB,
174 + .ooboffs = OOB_SIGNATURE_OFFSET + bmtd.oob_offset,
178 + .len = bmtd.bmt_pgs << bmtd.pg_shift,
180 + loff_t addr = (loff_t)block << bmtd.blk_shift;
182 + return bmtd._write_oob(bmtd.mtd, addr, &ops);
185 +static u16 find_valid_block(u16 block)
192 + if (block >= bmtd.total_blks)
195 + ret = bbt_nand_read(blk_pg(block), nand_data_buf, bmtd.pg_size,
197 + /* Read the 1st byte of FDM to judge whether it's a bad
200 + if (ret || fdm[0] != 0xff) {
201 + pr_info("nand: found bad block 0x%x\n", block);
202 + if (loop >= bmtd.bb_max) {
203 + pr_info("nand: FATAL ERR: too many bad blocks!!\n");
215 +/* Find out all bad blocks, and fill in the mapping table */
216 +static int scan_bad_blocks(struct bbbt *bbt)
221 + /* First time download, the block0 MUST NOT be a bad block,
222 + * this is guaranteed by vendor
224 + bbt->bb_tbl[0] = 0;
227 + * Construct the mapping table of Normal data area(non-PMT/BMTPOOL)
228 + * G - Good block; B - Bad block
229 + * ---------------------------
230 + * physical |G|G|B|G|B|B|G|G|G|G|B|G|B|
231 + * ---------------------------
232 + * What bb_tbl[i] looks like:
233 + * physical block(i):
234 + * 0 1 2 3 4 5 6 7 8 9 a b c
235 + * mapped block(bb_tbl[i]):
236 + * 0 1 3 6 7 8 9 b ......
238 + * If new bad block ocurred(n), search bmt_tbl to find
239 + * a available block(x), and fill in the bb_tbl[n] = x;
241 + for (i = 1; i < bmtd.pool_lba; i++) {
242 + bbt->bb_tbl[i] = find_valid_block(bbt->bb_tbl[i - 1] + 1);
243 + BBT_LOG("bb_tbl[0x%x] = 0x%x", i, bbt->bb_tbl[i]);
244 + if (bbt->bb_tbl[i] == 0)
248 + /* Physical Block start Address of BMT pool */
249 + bmtd.pool_pba = bbt->bb_tbl[i - 1] + 1;
250 + if (bmtd.pool_pba >= bmtd.total_blks - 2) {
251 + pr_info("nand: FATAL ERR: Too many bad blocks!!\n");
255 + BBT_LOG("pool_pba=0x%x", bmtd.pool_pba);
257 + block = bmtd.pool_pba;
259 + * The bmt table is used for runtime bad block mapping
260 + * G - Good block; B - Bad block
261 + * ---------------------------
262 + * physical |G|G|B|G|B|B|G|G|G|G|B|G|B|
263 + * ---------------------------
264 + * block: 0 1 2 3 4 5 6 7 8 9 a b c
265 + * What bmt_tbl[i] looks like in initial state:
268 + * bmt_tbl[i].block:
270 + * bmt_tbl[i].mapped:
272 + * N - Not mapped(Available)
276 + * BMT always in the last valid block in pool
278 + while ((block = find_valid_block(block)) != 0) {
279 + bmt_tbl(bbt)[i].block = block;
280 + bmt_tbl(bbt)[i].mapped = NO_MAPPED;
281 + BBT_LOG("bmt_tbl[%d].block = 0x%x", i, block);
286 + /* i - How many available blocks in pool, which is the length of bmt_tbl[]
287 + * bmtd.bmt_blk_idx - bmt_tbl[bmtd.bmt_blk_idx].block => the BMT block
289 + bmtd.bmt_blk_idx = i - 1;
290 + bmt_tbl(bbt)[bmtd.bmt_blk_idx].mapped = BMT_MAPPED;
293 + pr_info("nand: FATAL ERR: no space to store BMT!!\n");
297 + pr_info("[BBT] %d available blocks in BMT pool\n", i);
302 +static bool is_valid_bmt(unsigned char *buf, unsigned char *fdm)
304 + struct bbbt *bbt = (struct bbbt *)buf;
305 + u8 *sig = (u8*)bbt->signature + MAIN_SIGNATURE_OFFSET;
308 + if (memcmp(bbt->signature + MAIN_SIGNATURE_OFFSET, "BMT", 3) == 0 &&
309 + memcmp(fdm + OOB_SIGNATURE_OFFSET, "bmt", 3) == 0) {
310 + if (bbt->version == BBMT_VERSION)
313 + BBT_LOG("[BBT] BMT Version not match,upgrage preloader and uboot please! sig=%02x%02x%02x, fdm=%02x%02x%02x",
314 + sig[0], sig[1], sig[2],
315 + fdm[1], fdm[2], fdm[3]);
319 +static u16 get_bmt_index(struct bbmt *bmt)
323 + while (bmt[i].block != BMT_TBL_DEF_VAL) {
324 + if (bmt[i].mapped == BMT_MAPPED)
331 +static struct bbbt *scan_bmt(u16 block)
335 + if (block < bmtd.pool_lba)
338 + if (read_bmt(block, nand_bbt_buf, fdm, sizeof(fdm)))
339 + return scan_bmt(block - 1);
341 + if (is_valid_bmt(nand_bbt_buf, fdm)) {
342 + bmtd.bmt_blk_idx = get_bmt_index(bmt_tbl((struct bbbt *)nand_bbt_buf));
343 + if (bmtd.bmt_blk_idx == 0) {
344 + pr_info("[BBT] FATAL ERR: bmt block index is wrong!\n");
347 + pr_info("[BBT] BMT.v2 is found at 0x%x\n", block);
348 + return (struct bbbt *)nand_bbt_buf;
350 + return scan_bmt(block - 1);
353 +/* Write the Burner Bad Block Table to Nand Flash
354 + * n - write BMT to bmt_tbl[n]
356 +static u16 upload_bmt(struct bbbt *bbt, int n)
361 + if (n < 0 || bmt_tbl(bbt)[n].mapped == NORMAL_MAPPED) {
362 + pr_info("nand: FATAL ERR: no space to store BMT!\n");
366 + block = bmt_tbl(bbt)[n].block;
367 + BBT_LOG("n = 0x%x, block = 0x%x", n, block);
368 + if (bbt_nand_erase(block)) {
369 + bmt_tbl(bbt)[n].block = 0;
370 + /* erase failed, try the previous block: bmt_tbl[n - 1].block */
375 + /* The signature offset is fixed set to 0,
376 + * oob signature offset is fixed set to 1
378 + memcpy(bbt->signature + MAIN_SIGNATURE_OFFSET, "BMT", 3);
379 + bbt->version = BBMT_VERSION;
381 + if (write_bmt(block, (unsigned char *)bbt)) {
382 + bmt_tbl(bbt)[n].block = 0;
384 + /* write failed, try the previous block in bmt_tbl[n - 1] */
389 + /* Return the current index(n) of BMT pool (bmt_tbl[n]) */
393 +static u16 find_valid_block_in_pool(struct bbbt *bbt)
397 + if (bmtd.bmt_blk_idx == 0)
400 + for (i = 0; i < bmtd.bmt_blk_idx; i++) {
401 + if (bmt_tbl(bbt)[i].block != 0 && bmt_tbl(bbt)[i].mapped == NO_MAPPED) {
402 + bmt_tbl(bbt)[i].mapped = NORMAL_MAPPED;
403 + return bmt_tbl(bbt)[i].block;
408 + pr_info("nand: FATAL ERR: BMT pool is run out!\n");
412 +/* We met a bad block, mark it as bad and map it to a valid block in pool,
413 + * if it's a write failure, we need to write the data to mapped block
415 +static bool update_bmt(u16 block)
421 + mapped_blk = find_valid_block_in_pool(bbt);
422 + if (mapped_blk == 0)
425 + /* Map new bad block to available block in pool */
426 + bbt->bb_tbl[block] = mapped_blk;
427 + bmtd.bmt_blk_idx = upload_bmt(bbt, bmtd.bmt_blk_idx);
432 +u16 get_mapping_block_index(int block)
436 + if (block < bmtd.pool_lba)
437 + mapping_block = bmtd.bbt->bb_tbl[block];
439 + mapping_block = block;
440 + BBT_LOG("0x%x mapped to 0x%x", block, mapping_block);
442 + return mapping_block;
446 +mtk_bmt_read(struct mtd_info *mtd, loff_t from,
447 + struct mtd_oob_ops *ops)
449 + struct mtd_oob_ops cur_ops = *ops;
450 + int retry_count = 0;
455 + ops->oobretlen = 0;
457 + while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
458 + u32 offset = from & (bmtd.blk_size - 1);
459 + u32 block = from >> bmtd.blk_shift;
462 + cur_block = get_mapping_block_index(block);
463 + cur_from = ((loff_t)cur_block << bmtd.blk_shift) + offset;
465 + cur_ops.oobretlen = 0;
466 + cur_ops.retlen = 0;
467 + cur_ops.len = min_t(u32, mtd->erasesize - offset,
468 + ops->len - ops->retlen);
469 + ret = bmtd._read_oob(mtd, cur_from, &cur_ops);
472 + if (retry_count++ < 10)
478 + ops->retlen += cur_ops.retlen;
479 + ops->oobretlen += cur_ops.oobretlen;
481 + cur_ops.ooboffs = 0;
482 + cur_ops.datbuf += cur_ops.retlen;
483 + cur_ops.oobbuf += cur_ops.oobretlen;
484 + cur_ops.ooblen -= cur_ops.oobretlen;
487 + cur_ops.len = mtd->erasesize - offset;
489 + from += cur_ops.len;
497 +mtk_bmt_write(struct mtd_info *mtd, loff_t to,
498 + struct mtd_oob_ops *ops)
500 + struct mtd_oob_ops cur_ops = *ops;
501 + int retry_count = 0;
506 + ops->oobretlen = 0;
508 + while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
509 + u32 offset = to & (bmtd.blk_size - 1);
510 + u32 block = to >> bmtd.blk_shift;
513 + cur_block = get_mapping_block_index(block);
514 + cur_to = ((loff_t)cur_block << bmtd.blk_shift) + offset;
516 + cur_ops.oobretlen = 0;
517 + cur_ops.retlen = 0;
518 + cur_ops.len = min_t(u32, bmtd.blk_size - offset,
519 + ops->len - ops->retlen);
520 + ret = bmtd._write_oob(mtd, cur_to, &cur_ops);
523 + if (retry_count++ < 10)
529 + ops->retlen += cur_ops.retlen;
530 + ops->oobretlen += cur_ops.oobretlen;
532 + cur_ops.ooboffs = 0;
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;
550 +mtk_bmt_erase(struct nand_device *nand, const struct nand_pos *pos)
552 + struct nand_pos new_pos = *pos;
553 + int retry_count = 0;
557 + new_pos.eraseblock = get_mapping_block_index(pos->eraseblock);
559 + ret = bmtd.nand_ops->erase(nand, &new_pos);
561 + update_bmt(pos->eraseblock);
562 + if (retry_count++ < 10)
570 +mtk_bmt_isbad(struct nand_device *nand, const struct nand_pos *pos)
572 + struct nand_pos new_pos = *pos;
573 + int retry_count = 0;
577 + new_pos.eraseblock = get_mapping_block_index(pos->eraseblock);
579 + ret = bmtd.nand_ops->isbad(nand, &new_pos);
581 + update_bmt(pos->eraseblock);
582 + if (retry_count++ < 10)
590 +mtk_bmt_markbad(struct nand_device *nand, const struct nand_pos *pos)
592 + struct nand_pos new_pos = *pos;
594 + new_pos.eraseblock = get_mapping_block_index(new_pos.eraseblock);
595 + update_bmt(pos->eraseblock);
597 + return bmtd.nand_ops->markbad(nand, &new_pos);
601 +mtk_bmt_replace_ops(struct mtd_info *mtd)
603 + static const struct nand_ops mtk_bmt_nand_ops = {
604 + .erase = mtk_bmt_erase,
605 + .isbad = mtk_bmt_isbad,
606 + .markbad = mtk_bmt_markbad,
608 + struct nand_device *nand = mtd_to_nanddev(mtd);
610 + bmtd.nand_ops = nand->ops;
611 + bmtd._read_oob = mtd->_read_oob;
612 + bmtd._write_oob = mtd->_write_oob;
614 + mtd->_read_oob = mtk_bmt_read;
615 + mtd->_write_oob = mtk_bmt_write;
616 + nand->ops = &mtk_bmt_nand_ops;
619 +static int mtk_bmt_debug_mark_good(void *data, u64 val)
621 + u32 block = val >> bmtd.blk_shift;
623 + bmtd.bbt->bb_tbl[block] = block;
624 + bmtd.bmt_blk_idx = upload_bmt(bmtd.bbt, bmtd.bmt_blk_idx);
629 +static int mtk_bmt_debug_mark_bad(void *data, u64 val)
631 + u32 block = val >> bmtd.blk_shift;
638 +DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_good, NULL, mtk_bmt_debug_mark_good, "%llu\n");
639 +DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_bad, NULL, mtk_bmt_debug_mark_bad, "%llu\n");
642 +mtk_bmt_add_debugfs(void)
644 + struct dentry *dir;
646 + dir = bmtd.debugfs_dir = debugfs_create_dir("mtk-bmt", NULL);
650 + debugfs_create_file_unsafe("mark_good", S_IWUSR, dir, NULL, &fops_mark_good);
651 + debugfs_create_file_unsafe("mark_bad", S_IWUSR, dir, NULL, &fops_mark_bad);
654 +void mtk_bmt_detach(struct mtd_info *mtd)
656 + struct nand_device *nand = mtd_to_nanddev(mtd);
658 + if (bmtd.mtd != mtd)
661 + if (bmtd.debugfs_dir)
662 + debugfs_remove_recursive(bmtd.debugfs_dir);
663 + bmtd.debugfs_dir = NULL;
665 + kfree(nand_bbt_buf);
666 + kfree(nand_data_buf);
668 + mtd->_read_oob = bmtd._read_oob;
669 + mtd->_write_oob = bmtd._write_oob;
670 + mtd->size = bmtd.total_blks << bmtd.blk_shift;
671 + nand->ops = bmtd.nand_ops;
673 + memset(&bmtd, 0, sizeof(bmtd));
676 +/* total_blocks - The total count of blocks that the Nand Chip has */
677 +int mtk_bmt_attach(struct mtd_info *mtd)
679 + struct device_node *np;
683 + u16 total_blocks, pmt_block;
685 + u32 bmt_pool_size, bmt_table_size;
690 + np = mtd_get_of_node(mtd);
694 + if (!of_property_read_bool(np, "mediatek,bmt-v2"))
697 + if (of_property_read_u32(np, "mediatek,bmt-pool-size",
698 + &bmt_pool_size) != 0)
699 + bmt_pool_size = 80;
701 + if (of_property_read_u8(np, "mediatek,bmt-oob-offset",
702 + &bmtd.oob_offset) != 0)
703 + bmtd.oob_offset = 0;
705 + if (of_property_read_u32(np, "mediatek,bmt-table-size",
706 + &bmt_table_size) != 0)
707 + bmt_table_size = 0x2000U;
710 + mtk_bmt_replace_ops(mtd);
712 + bmtd.table_size = bmt_table_size;
713 + bmtd.blk_size = mtd->erasesize;
714 + bmtd.blk_shift = ffs(bmtd.blk_size) - 1;
715 + bmtd.pg_size = mtd->writesize;
716 + bmtd.pg_shift = ffs(bmtd.pg_size) - 1;
717 + total_blocks = mtd->size >> bmtd.blk_shift;
718 + pmt_block = total_blocks - bmt_pool_size - 2;
720 + mtd->size = pmt_block << bmtd.blk_shift;
723 + * ---------------------------------------
724 + * | PMT(2blks) | BMT POOL(totalblks * 2%) |
725 + * ---------------------------------------
728 + * pmt_block pmt_block + 2blocks(pool_lba)
731 + * The blocks ahead of the boundary block are stored in bb_tbl
732 + * and blocks behind are stored in bmt_tbl
735 + bmtd.pool_lba = (u16)(pmt_block + 2);
736 + bmtd.total_blks = total_blocks;
737 + bmtd.bb_max = bmtd.total_blks * BBPOOL_RATIO / 100;
739 + /* 3 buffers we need */
740 + bufsz = round_up(sizeof(struct bbbt) +
741 + bmt_table_size * sizeof(struct bbmt), bmtd.pg_size);
742 + bmtd.bmt_pgs = bufsz >> bmtd.pg_shift;
744 + nand_bbt_buf = kzalloc(bufsz, GFP_KERNEL);
745 + nand_data_buf = kzalloc(bmtd.pg_size, GFP_KERNEL);
747 + if (!nand_bbt_buf || !nand_data_buf) {
748 + pr_info("nand: FATAL ERR: allocate buffer failed!\n");
753 + memset(nand_bbt_buf, 0xff, bufsz);
754 + memset(nand_data_buf, 0xff, bmtd.pg_size);
756 + BBT_LOG("bbtbuf=0x%p(0x%x) dat=0x%p(0x%x)",
757 + nand_bbt_buf, bufsz, nand_data_buf, bmtd.pg_size);
758 + BBT_LOG("pool_lba=0x%x total_blks=0x%x bb_max=0x%x",
759 + bmtd.pool_lba, bmtd.total_blks, bmtd.bb_max);
761 + /* Scanning start from the first page of the last block
764 + bbt = scan_bmt(bmtd.total_blks - 1);
766 + /* BMT not found */
767 + if (bmtd.total_blks > BB_TABLE_MAX + BMT_TABLE_MAX) {
768 + pr_info("nand: FATAL: Too many blocks, can not support!\n");
773 + bbt = (struct bbbt *)nand_bbt_buf;
774 + memset(bmt_tbl(bbt), BMT_TBL_DEF_VAL, bmtd.table_size * sizeof(struct bbmt));
776 + if (scan_bad_blocks(bbt)) {
781 + /* BMT always in the last valid block in pool */
782 + bmtd.bmt_blk_idx = upload_bmt(bbt, bmtd.bmt_blk_idx);
783 + block = bmt_tbl(bbt)[bmtd.bmt_blk_idx].block;
784 + pr_notice("[BBT] BMT.v2 is written into PBA:0x%x\n", block);
786 + if (bmtd.bmt_blk_idx == 0)
787 + pr_info("nand: Warning: no available block in BMT pool!\n");
788 + else if (bmtd.bmt_blk_idx == (u16)-1) {
793 + mtk_bmt_add_debugfs();
799 + mtk_bmt_detach(mtd);
804 +MODULE_LICENSE("GPL");
805 +MODULE_AUTHOR("Xiangsheng Hou <xiangsheng.hou@mediatek.com>, Felix Fietkau <nbd@nbd.name>");
806 +MODULE_DESCRIPTION("Bad Block mapping management v2 for MediaTek NAND Flash Driver");
809 +++ b/include/linux/mtd/mtk_bmt.h
814 +#ifdef CONFIG_MTD_NAND_MTK_BMT
815 +int mtk_bmt_attach(struct mtd_info *mtd);
816 +void mtk_bmt_detach(struct mtd_info *mtd);
818 +static inline int mtk_bmt_attach(struct mtd_info *mtd)
823 +static inline void mtk_bmt_detach(struct mtd_info *mtd)
829 --- a/drivers/mtd/mtk-snand/mtk-snand-mtd.c
830 +++ b/drivers/mtd/mtk-snand/mtk-snand-mtd.c
832 #include <linux/dma-mapping.h>
833 #include <linux/wait.h>
834 #include <linux/mtd/mtd.h>
835 +#include <linux/mtd/mtk_bmt.h>
836 #include <linux/mtd/partitions.h>
837 #include <linux/of_platform.h>
839 @@ -608,6 +609,8 @@ static int mtk_snand_probe(struct platfo
840 mtd->_block_isbad = mtk_snand_mtd_block_isbad;
841 mtd->_block_markbad = mtk_snand_mtd_block_markbad;
843 + mtk_bmt_attach(mtd);
845 ret = mtd_device_register(mtd, NULL, 0);
847 dev_err(msm->pdev.dev, "failed to register mtd partition\n");
848 @@ -619,6 +622,7 @@ static int mtk_snand_probe(struct platfo
852 + mtk_bmt_detach(mtd);
853 devm_kfree(msm->pdev.dev, msm->page_cache);
856 @@ -646,6 +650,8 @@ static int mtk_snand_remove(struct platf
860 + mtk_bmt_detach(mtd);
862 mtk_snand_cleanup(msm->snf);