kernel: bump 5.4 to 5.4.135
[openwrt/openwrt.git] / target / linux / mediatek / patches-5.4 / 0310-mtk-bmt-support.patch
1 --- a/drivers/mtd/nand/Kconfig
2 +++ b/drivers/mtd/nand/Kconfig
3 @@ -5,3 +5,7 @@ config MTD_NAND_CORE
4 source "drivers/mtd/nand/onenand/Kconfig"
5 source "drivers/mtd/nand/raw/Kconfig"
6 source "drivers/mtd/nand/spi/Kconfig"
7 +
8 +config MTD_NAND_MTK_BMT
9 + bool "Support MediaTek NAND Bad-block Management Table"
10 + default n
11 --- a/drivers/mtd/nand/Makefile
12 +++ b/drivers/mtd/nand/Makefile
13 @@ -2,6 +2,7 @@
14
15 nandcore-objs := core.o bbt.o
16 obj-$(CONFIG_MTD_NAND_CORE) += nandcore.o
17 +obj-$(CONFIG_MTD_NAND_MTK_BMT) += mtk_bmt.o
18
19 obj-y += onenand/
20 obj-y += raw/
21 --- /dev/null
22 +++ b/drivers/mtd/nand/mtk_bmt.c
23 @@ -0,0 +1,766 @@
24 +/*
25 + * Copyright (c) 2017 MediaTek Inc.
26 + * Author: Xiangsheng Hou <xiangsheng.hou@mediatek.com>
27 + * Copyright (c) 2020 Felix Fietkau <nbd@nbd.name>
28 + *
29 + * This program is free software; you can redistribute it and/or modify
30 + * it under the terms of the GNU General Public License version 2 as
31 + * published by the Free Software Foundation.
32 + *
33 + * This program is distributed in the hope that it will be useful,
34 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 + * GNU General Public License for more details.
37 + */
38 +
39 +#include <linux/slab.h>
40 +#include <linux/gfp.h>
41 +#include <linux/kernel.h>
42 +#include <linux/of.h>
43 +#include <linux/mtd/nand.h>
44 +#include <linux/mtd/partitions.h>
45 +#include <linux/mtd/mtk_bmt.h>
46 +#include <linux/module.h>
47 +#include <linux/debugfs.h>
48 +
49 +#define MAIN_SIGNATURE_OFFSET 0
50 +#define OOB_SIGNATURE_OFFSET 1
51 +#define BBPOOL_RATIO 2
52 +
53 +#define BBT_LOG(fmt, ...) pr_debug("[BBT][%s|%d] "fmt"\n", __func__, __LINE__, ##__VA_ARGS__)
54 +
55 +/* Maximum 8k blocks */
56 +#define BB_TABLE_MAX 0x2000U
57 +#define BMT_TABLE_MAX (BB_TABLE_MAX * BBPOOL_RATIO / 100)
58 +#define BMT_TBL_DEF_VAL 0x0
59 +
60 +/*
61 + * Burner Bad Block Table
62 + * --------- Only support SLC Nand Chips!!!!!!!!!!! ----------
63 + */
64 +
65 +struct bbbt {
66 + char signature[3];
67 + /* This version is used to distinguish the legacy and new algorithm */
68 +#define BBMT_VERSION 2
69 + unsigned char version;
70 + /* Below 2 tables will be written in SLC */
71 + u16 bb_tbl[BB_TABLE_MAX];
72 + struct bbmt {
73 + u16 block;
74 +#define NO_MAPPED 0
75 +#define NORMAL_MAPPED 1
76 +#define BMT_MAPPED 2
77 + u16 mapped;
78 + } bmt_tbl[BMT_TABLE_MAX];
79 +};
80 +
81 +static struct bmt_desc {
82 + struct mtd_info *mtd;
83 +
84 + int (*_read_oob) (struct mtd_info *mtd, loff_t from,
85 + struct mtd_oob_ops *ops);
86 + int (*_write_oob) (struct mtd_info *mtd, loff_t to,
87 + struct mtd_oob_ops *ops);
88 + const struct nand_ops *nand_ops;
89 +
90 + struct bbbt *bbt;
91 +
92 + struct dentry *debugfs_dir;
93 +
94 + u32 pg_size;
95 + u32 blk_size;
96 + u16 pg_shift;
97 + u16 blk_shift;
98 + /* bbt logical address */
99 + u16 pool_lba;
100 + /* bbt physical address */
101 + u16 pool_pba;
102 + /* Maximum count of bad blocks that the vendor guaranteed */
103 + u16 bb_max;
104 + /* Total blocks of the Nand Chip */
105 + u16 total_blks;
106 + /* The block(n) BMT is located at (bmt_tbl[n]) */
107 + u16 bmt_blk_idx;
108 + /* How many pages needs to store 'struct bbbt' */
109 + u32 bmt_pgs;
110 +
111 + /* to compensate for driver level remapping */
112 + u8 oob_offset;
113 +} bmtd = {0};
114 +
115 +static unsigned char *nand_bbt_buf;
116 +static unsigned char *nand_data_buf;
117 +
118 +/* -------- Unit conversions -------- */
119 +static inline u32 blk_pg(u16 block)
120 +{
121 + return (u32)(block << (bmtd.blk_shift - bmtd.pg_shift));
122 +}
123 +
124 +/* -------- Nand operations wrapper -------- */
125 +static inline int
126 +bbt_nand_read(u32 page, unsigned char *dat, int dat_len,
127 + unsigned char *fdm, int fdm_len)
128 +{
129 + struct mtd_oob_ops ops = {
130 + .mode = MTD_OPS_PLACE_OOB,
131 + .ooboffs = bmtd.oob_offset,
132 + .oobbuf = fdm,
133 + .ooblen = fdm_len,
134 + .datbuf = dat,
135 + .len = dat_len,
136 + };
137 +
138 + return bmtd._read_oob(bmtd.mtd, page << bmtd.pg_shift, &ops);
139 +}
140 +
141 +static inline int bbt_nand_erase(u16 block)
142 +{
143 + struct nand_device *nand = mtd_to_nanddev(bmtd.mtd);
144 + loff_t addr = (loff_t)block << bmtd.blk_shift;
145 + struct nand_pos pos;
146 +
147 + nanddev_offs_to_pos(nand, addr, &pos);
148 + return bmtd.nand_ops->erase(nand, &pos);
149 +}
150 +
151 +/* -------- Bad Blocks Management -------- */
152 +static int
153 +read_bmt(u16 block, unsigned char *dat, unsigned char *fdm, int fdm_len)
154 +{
155 + u32 len = bmtd.bmt_pgs << bmtd.pg_shift;
156 +
157 + return bbt_nand_read(blk_pg(block), dat, len, fdm, fdm_len);
158 +}
159 +
160 +static int write_bmt(u16 block, unsigned char *dat)
161 +{
162 + struct mtd_oob_ops ops = {
163 + .mode = MTD_OPS_PLACE_OOB,
164 + .ooboffs = OOB_SIGNATURE_OFFSET + bmtd.oob_offset,
165 + .oobbuf = "bmt",
166 + .ooblen = 3,
167 + .datbuf = dat,
168 + .len = bmtd.bmt_pgs << bmtd.pg_shift,
169 + };
170 + loff_t addr = (loff_t)block << bmtd.blk_shift;
171 +
172 + return bmtd._write_oob(bmtd.mtd, addr, &ops);
173 +}
174 +
175 +static u16 find_valid_block(u16 block)
176 +{
177 + u8 fdm[4];
178 + int ret;
179 + int loop = 0;
180 +
181 +retry:
182 + if (block >= bmtd.total_blks)
183 + return 0;
184 +
185 + ret = bbt_nand_read(blk_pg(block), nand_data_buf, bmtd.pg_size,
186 + fdm, sizeof(fdm));
187 + /* Read the 1st byte of FDM to judge whether it's a bad
188 + * or not
189 + */
190 + if (ret || fdm[0] != 0xff) {
191 + pr_info("nand: found bad block 0x%x\n", block);
192 + if (loop >= bmtd.bb_max) {
193 + pr_info("nand: FATAL ERR: too many bad blocks!!\n");
194 + return 0;
195 + }
196 +
197 + loop++;
198 + block++;
199 + goto retry;
200 + }
201 +
202 + return block;
203 +}
204 +
205 +/* Find out all bad blocks, and fill in the mapping table */
206 +static int scan_bad_blocks(struct bbbt *bbt)
207 +{
208 + int i;
209 + u16 block = 0;
210 +
211 + /* First time download, the block0 MUST NOT be a bad block,
212 + * this is guaranteed by vendor
213 + */
214 + bbt->bb_tbl[0] = 0;
215 +
216 + /*
217 + * Construct the mapping table of Normal data area(non-PMT/BMTPOOL)
218 + * G - Good block; B - Bad block
219 + * ---------------------------
220 + * physical |G|G|B|G|B|B|G|G|G|G|B|G|B|
221 + * ---------------------------
222 + * What bb_tbl[i] looks like:
223 + * physical block(i):
224 + * 0 1 2 3 4 5 6 7 8 9 a b c
225 + * mapped block(bb_tbl[i]):
226 + * 0 1 3 6 7 8 9 b ......
227 + * ATTENTION:
228 + * If new bad block ocurred(n), search bmt_tbl to find
229 + * a available block(x), and fill in the bb_tbl[n] = x;
230 + */
231 + for (i = 1; i < bmtd.pool_lba; i++) {
232 + bbt->bb_tbl[i] = find_valid_block(bbt->bb_tbl[i - 1] + 1);
233 + BBT_LOG("bb_tbl[0x%x] = 0x%x", i, bbt->bb_tbl[i]);
234 + if (bbt->bb_tbl[i] == 0)
235 + return -1;
236 + }
237 +
238 + /* Physical Block start Address of BMT pool */
239 + bmtd.pool_pba = bbt->bb_tbl[i - 1] + 1;
240 + if (bmtd.pool_pba >= bmtd.total_blks - 2) {
241 + pr_info("nand: FATAL ERR: Too many bad blocks!!\n");
242 + return -1;
243 + }
244 +
245 + BBT_LOG("pool_pba=0x%x", bmtd.pool_pba);
246 + i = 0;
247 + block = bmtd.pool_pba;
248 + /*
249 + * The bmt table is used for runtime bad block mapping
250 + * G - Good block; B - Bad block
251 + * ---------------------------
252 + * physical |G|G|B|G|B|B|G|G|G|G|B|G|B|
253 + * ---------------------------
254 + * block: 0 1 2 3 4 5 6 7 8 9 a b c
255 + * What bmt_tbl[i] looks like in initial state:
256 + * i:
257 + * 0 1 2 3 4 5 6 7
258 + * bmt_tbl[i].block:
259 + * 0 1 3 6 7 8 9 b
260 + * bmt_tbl[i].mapped:
261 + * N N N N N N N B
262 + * N - Not mapped(Available)
263 + * M - Mapped
264 + * B - BMT
265 + * ATTENTION:
266 + * BMT always in the last valid block in pool
267 + */
268 + while ((block = find_valid_block(block)) != 0) {
269 + bbt->bmt_tbl[i].block = block;
270 + bbt->bmt_tbl[i].mapped = NO_MAPPED;
271 + BBT_LOG("bmt_tbl[%d].block = 0x%x", i, block);
272 + block++;
273 + i++;
274 + }
275 +
276 + /* i - How many available blocks in pool, which is the length of bmt_tbl[]
277 + * bmtd.bmt_blk_idx - bmt_tbl[bmtd.bmt_blk_idx].block => the BMT block
278 + */
279 + bmtd.bmt_blk_idx = i - 1;
280 + bbt->bmt_tbl[bmtd.bmt_blk_idx].mapped = BMT_MAPPED;
281 +
282 + if (i < 1) {
283 + pr_info("nand: FATAL ERR: no space to store BMT!!\n");
284 + return -1;
285 + }
286 +
287 + pr_info("[BBT] %d available blocks in BMT pool\n", i);
288 +
289 + return 0;
290 +}
291 +
292 +static bool is_valid_bmt(unsigned char *buf, unsigned char *fdm)
293 +{
294 + struct bbbt *bbt = (struct bbbt *)buf;
295 + u8 *sig = (u8*)bbt->signature + MAIN_SIGNATURE_OFFSET;
296 +
297 +
298 + if (memcmp(bbt->signature + MAIN_SIGNATURE_OFFSET, "BMT", 3) == 0 &&
299 + memcmp(fdm + OOB_SIGNATURE_OFFSET, "bmt", 3) == 0) {
300 + if (bbt->version == BBMT_VERSION)
301 + return true;
302 + }
303 + BBT_LOG("[BBT] BMT Version not match,upgrage preloader and uboot please! sig=%02x%02x%02x, fdm=%02x%02x%02x",
304 + sig[0], sig[1], sig[2],
305 + fdm[1], fdm[2], fdm[3]);
306 + return false;
307 +}
308 +
309 +static u16 get_bmt_index(struct bbmt *bmt)
310 +{
311 + int i = 0;
312 +
313 + while (bmt[i].block != BMT_TBL_DEF_VAL) {
314 + if (bmt[i].mapped == BMT_MAPPED)
315 + return i;
316 + i++;
317 + }
318 + return 0;
319 +}
320 +
321 +static struct bbbt *scan_bmt(u16 block)
322 +{
323 + u8 fdm[4];
324 +
325 + if (block < bmtd.pool_lba)
326 + return NULL;
327 +
328 + if (read_bmt(block, nand_bbt_buf, fdm, sizeof(fdm)))
329 + return scan_bmt(block - 1);
330 +
331 + if (is_valid_bmt(nand_bbt_buf, fdm)) {
332 + bmtd.bmt_blk_idx = get_bmt_index(((struct bbbt *)nand_bbt_buf)->bmt_tbl);
333 + if (bmtd.bmt_blk_idx == 0) {
334 + pr_info("[BBT] FATAL ERR: bmt block index is wrong!\n");
335 + return NULL;
336 + }
337 + pr_info("[BBT] BMT.v2 is found at 0x%x\n", block);
338 + return (struct bbbt *)nand_bbt_buf;
339 + } else
340 + return scan_bmt(block - 1);
341 +}
342 +
343 +/* Write the Burner Bad Block Table to Nand Flash
344 + * n - write BMT to bmt_tbl[n]
345 + */
346 +static u16 upload_bmt(struct bbbt *bbt, int n)
347 +{
348 + u16 block;
349 +
350 +retry:
351 + if (n < 0 || bbt->bmt_tbl[n].mapped == NORMAL_MAPPED) {
352 + pr_info("nand: FATAL ERR: no space to store BMT!\n");
353 + return (u16)-1;
354 + }
355 +
356 + block = bbt->bmt_tbl[n].block;
357 + BBT_LOG("n = 0x%x, block = 0x%x", n, block);
358 + if (bbt_nand_erase(block)) {
359 + bbt->bmt_tbl[n].block = 0;
360 + /* erase failed, try the previous block: bmt_tbl[n - 1].block */
361 + n--;
362 + goto retry;
363 + }
364 +
365 + /* The signature offset is fixed set to 0,
366 + * oob signature offset is fixed set to 1
367 + */
368 + memcpy(bbt->signature + MAIN_SIGNATURE_OFFSET, "BMT", 3);
369 + bbt->version = BBMT_VERSION;
370 +
371 + if (write_bmt(block, (unsigned char *)bbt)) {
372 + bbt->bmt_tbl[n].block = 0;
373 +
374 + /* write failed, try the previous block in bmt_tbl[n - 1] */
375 + n--;
376 + goto retry;
377 + }
378 +
379 + /* Return the current index(n) of BMT pool (bmt_tbl[n]) */
380 + return n;
381 +}
382 +
383 +static u16 find_valid_block_in_pool(struct bbbt *bbt)
384 +{
385 + int i;
386 +
387 + if (bmtd.bmt_blk_idx == 0)
388 + goto error;
389 +
390 + for (i = 0; i < bmtd.bmt_blk_idx; i++) {
391 + if (bbt->bmt_tbl[i].block != 0 && bbt->bmt_tbl[i].mapped == NO_MAPPED) {
392 + bbt->bmt_tbl[i].mapped = NORMAL_MAPPED;
393 + return bbt->bmt_tbl[i].block;
394 + }
395 + }
396 +
397 +error:
398 + pr_info("nand: FATAL ERR: BMT pool is run out!\n");
399 + return 0;
400 +}
401 +
402 +/* We met a bad block, mark it as bad and map it to a valid block in pool,
403 + * if it's a write failure, we need to write the data to mapped block
404 + */
405 +static bool update_bmt(u16 block)
406 +{
407 + u16 mapped_blk;
408 + struct bbbt *bbt;
409 +
410 + bbt = bmtd.bbt;
411 + mapped_blk = find_valid_block_in_pool(bbt);
412 + if (mapped_blk == 0)
413 + return false;
414 +
415 + /* Map new bad block to available block in pool */
416 + bbt->bb_tbl[block] = mapped_blk;
417 + bmtd.bmt_blk_idx = upload_bmt(bbt, bmtd.bmt_blk_idx);
418 +
419 + return true;
420 +}
421 +
422 +u16 get_mapping_block_index(int block)
423 +{
424 + int mapping_block;
425 +
426 + if (block < bmtd.pool_lba)
427 + mapping_block = bmtd.bbt->bb_tbl[block];
428 + else
429 + mapping_block = block;
430 + BBT_LOG("0x%x mapped to 0x%x", block, mapping_block);
431 +
432 + return mapping_block;
433 +}
434 +
435 +static int
436 +mtk_bmt_read(struct mtd_info *mtd, loff_t from,
437 + struct mtd_oob_ops *ops)
438 +{
439 + struct mtd_oob_ops cur_ops = *ops;
440 + int retry_count = 0;
441 + loff_t cur_from;
442 + int ret;
443 +
444 + ops->retlen = 0;
445 + ops->oobretlen = 0;
446 +
447 + while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
448 + u32 offset = from & (bmtd.blk_size - 1);
449 + u32 block = from >> bmtd.blk_shift;
450 + u32 cur_block;
451 +
452 + cur_block = get_mapping_block_index(block);
453 + cur_from = ((loff_t)cur_block << bmtd.blk_shift) + offset;
454 +
455 + cur_ops.oobretlen = 0;
456 + cur_ops.retlen = 0;
457 + cur_ops.len = min_t(u32, mtd->erasesize - offset,
458 + ops->len - ops->retlen);
459 + ret = bmtd._read_oob(mtd, cur_from, &cur_ops);
460 + if (ret < 0) {
461 + update_bmt(block);
462 + if (retry_count++ < 10)
463 + continue;
464 +
465 + return ret;
466 + }
467 +
468 + ops->retlen += cur_ops.retlen;
469 + ops->oobretlen += cur_ops.oobretlen;
470 +
471 + cur_ops.datbuf += cur_ops.retlen;
472 + cur_ops.oobbuf += cur_ops.oobretlen;
473 + cur_ops.ooblen -= cur_ops.oobretlen;
474 +
475 + if (!cur_ops.len)
476 + cur_ops.len = mtd->erasesize - offset;
477 +
478 + from += cur_ops.len;
479 + retry_count = 0;
480 + }
481 +
482 + return 0;
483 +}
484 +
485 +static int
486 +mtk_bmt_write(struct mtd_info *mtd, loff_t to,
487 + struct mtd_oob_ops *ops)
488 +{
489 + struct mtd_oob_ops cur_ops = *ops;
490 + int retry_count = 0;
491 + loff_t cur_to;
492 + int ret;
493 +
494 + ops->retlen = 0;
495 + ops->oobretlen = 0;
496 +
497 + while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
498 + u32 offset = to & (bmtd.blk_size - 1);
499 + u32 block = to >> bmtd.blk_shift;
500 + u32 cur_block;
501 +
502 + cur_block = get_mapping_block_index(block);
503 + cur_to = ((loff_t)cur_block << bmtd.blk_shift) + offset;
504 +
505 + cur_ops.oobretlen = 0;
506 + cur_ops.retlen = 0;
507 + cur_ops.len = min_t(u32, bmtd.blk_size - offset,
508 + ops->len - ops->retlen);
509 + ret = bmtd._write_oob(mtd, cur_to, &cur_ops);
510 + if (ret < 0) {
511 + update_bmt(block);
512 + if (retry_count++ < 10)
513 + continue;
514 +
515 + return ret;
516 + }
517 +
518 + ops->retlen += cur_ops.retlen;
519 + ops->oobretlen += cur_ops.oobretlen;
520 +
521 + cur_ops.datbuf += cur_ops.retlen;
522 + cur_ops.oobbuf += cur_ops.oobretlen;
523 + cur_ops.ooblen -= cur_ops.oobretlen;
524 +
525 + if (!cur_ops.len)
526 + cur_ops.len = mtd->erasesize - offset;
527 +
528 + to += cur_ops.len;
529 + retry_count = 0;
530 + }
531 +
532 + return 0;
533 +}
534 +
535 +
536 +
537 +static int
538 +mtk_bmt_erase(struct nand_device *nand, const struct nand_pos *pos)
539 +{
540 + struct nand_pos new_pos = *pos;
541 + int retry_count = 0;
542 + int ret;
543 +
544 +retry:
545 + new_pos.eraseblock = get_mapping_block_index(pos->eraseblock);
546 +
547 + ret = bmtd.nand_ops->erase(nand, &new_pos);
548 + if (ret) {
549 + update_bmt(pos->eraseblock);
550 + if (retry_count++ < 10)
551 + goto retry;
552 + }
553 +
554 + return ret;
555 +}
556 +
557 +static bool
558 +mtk_bmt_isbad(struct nand_device *nand, const struct nand_pos *pos)
559 +{
560 + struct nand_pos new_pos = *pos;
561 + int retry_count = 0;
562 + bool ret;
563 +
564 +retry:
565 + new_pos.eraseblock = get_mapping_block_index(pos->eraseblock);
566 +
567 + ret = bmtd.nand_ops->isbad(nand, &new_pos);
568 + if (ret) {
569 + update_bmt(pos->eraseblock);
570 + if (retry_count++ < 10)
571 + goto retry;
572 + }
573 +
574 + return ret;
575 +}
576 +
577 +static int
578 +mtk_bmt_markbad(struct nand_device *nand, const struct nand_pos *pos)
579 +{
580 + struct nand_pos new_pos = *pos;
581 +
582 + new_pos.eraseblock = get_mapping_block_index(new_pos.eraseblock);
583 + update_bmt(pos->eraseblock);
584 +
585 + return bmtd.nand_ops->markbad(nand, &new_pos);
586 +}
587 +
588 +static void
589 +mtk_bmt_replace_ops(struct mtd_info *mtd)
590 +{
591 + static const struct nand_ops mtk_bmt_nand_ops = {
592 + .erase = mtk_bmt_erase,
593 + .isbad = mtk_bmt_isbad,
594 + .markbad = mtk_bmt_markbad,
595 + };
596 + struct nand_device *nand = mtd_to_nanddev(mtd);
597 +
598 + bmtd.nand_ops = nand->ops;
599 + bmtd._read_oob = mtd->_read_oob;
600 + bmtd._write_oob = mtd->_write_oob;
601 +
602 + mtd->_read_oob = mtk_bmt_read;
603 + mtd->_write_oob = mtk_bmt_write;
604 + nand->ops = &mtk_bmt_nand_ops;
605 +}
606 +
607 +static int mtk_bmt_debug_mark_good(void *data, u64 val)
608 +{
609 + u32 block = val >> bmtd.blk_shift;
610 +
611 + bmtd.bbt->bb_tbl[block] = block;
612 + bmtd.bmt_blk_idx = upload_bmt(bmtd.bbt, bmtd.bmt_blk_idx);
613 +
614 + return 0;
615 +}
616 +
617 +static int mtk_bmt_debug_mark_bad(void *data, u64 val)
618 +{
619 + u32 block = val >> bmtd.blk_shift;
620 +
621 + update_bmt(block);
622 +
623 + return 0;
624 +}
625 +
626 +DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_good, NULL, mtk_bmt_debug_mark_good, "%llu\n");
627 +DEFINE_DEBUGFS_ATTRIBUTE(fops_mark_bad, NULL, mtk_bmt_debug_mark_bad, "%llu\n");
628 +
629 +static void
630 +mtk_bmt_add_debugfs(void)
631 +{
632 + struct dentry *dir;
633 +
634 + dir = bmtd.debugfs_dir = debugfs_create_dir("mtk-bmt", NULL);
635 + if (!dir)
636 + return;
637 +
638 + debugfs_create_file_unsafe("mark_good", S_IWUSR, dir, NULL, &fops_mark_good);
639 + debugfs_create_file_unsafe("mark_bad", S_IWUSR, dir, NULL, &fops_mark_bad);
640 +}
641 +
642 +void mtk_bmt_detach(struct mtd_info *mtd)
643 +{
644 + struct nand_device *nand = mtd_to_nanddev(mtd);
645 +
646 + if (bmtd.mtd != mtd)
647 + return;
648 +
649 + if (bmtd.debugfs_dir)
650 + debugfs_remove_recursive(bmtd.debugfs_dir);
651 + bmtd.debugfs_dir = NULL;
652 +
653 + kfree(nand_bbt_buf);
654 + kfree(nand_data_buf);
655 +
656 + mtd->_read_oob = bmtd._read_oob;
657 + mtd->_write_oob = bmtd._write_oob;
658 + mtd->size = bmtd.total_blks << bmtd.blk_shift;
659 + nand->ops = bmtd.nand_ops;
660 +
661 + memset(&bmtd, 0, sizeof(bmtd));
662 +}
663 +
664 +/* total_blocks - The total count of blocks that the Nand Chip has */
665 +int mtk_bmt_attach(struct mtd_info *mtd)
666 +{
667 + struct device_node *np;
668 + struct bbbt *bbt;
669 + u32 bufsz;
670 + u32 block;
671 + u16 total_blocks, pmt_block;
672 + int ret = 0;
673 + u32 bmt_pool_size;
674 +
675 + if (bmtd.mtd)
676 + return -ENOSPC;
677 +
678 + np = mtd_get_of_node(mtd);
679 + if (!np)
680 + return 0;
681 +
682 + if (!of_property_read_bool(np, "mediatek,bmt-v2"))
683 + return 0;
684 +
685 + if (of_property_read_u32(np, "mediatek,bmt-pool-size",
686 + &bmt_pool_size) != 0)
687 + bmt_pool_size = 80;
688 +
689 + if (of_property_read_u8(np, "mediatek,bmt-oob-offset",
690 + &bmtd.oob_offset) != 0)
691 + bmtd.oob_offset = 8;
692 +
693 + bmtd.mtd = mtd;
694 + mtk_bmt_replace_ops(mtd);
695 +
696 + bmtd.blk_size = mtd->erasesize;
697 + bmtd.blk_shift = ffs(bmtd.blk_size) - 1;
698 + bmtd.pg_size = mtd->writesize;
699 + bmtd.pg_shift = ffs(bmtd.pg_size) - 1;
700 + total_blocks = mtd->size >> bmtd.blk_shift;
701 + pmt_block = total_blocks - bmt_pool_size - 2;
702 +
703 + mtd->size = pmt_block << bmtd.blk_shift;
704 +
705 + /*
706 + * ---------------------------------------
707 + * | PMT(2blks) | BMT POOL(totalblks * 2%) |
708 + * ---------------------------------------
709 + * ^ ^
710 + * | |
711 + * pmt_block pmt_block + 2blocks(pool_lba)
712 + *
713 + * ATTETION!!!!!!
714 + * The blocks ahead of the boundary block are stored in bb_tbl
715 + * and blocks behind are stored in bmt_tbl
716 + */
717 +
718 + bmtd.pool_lba = (u16)(pmt_block + 2);
719 + bmtd.total_blks = total_blocks;
720 + bmtd.bb_max = bmtd.total_blks * BBPOOL_RATIO / 100;
721 +
722 + /* 3 buffers we need */
723 + bufsz = round_up(sizeof(struct bbbt), bmtd.pg_size);
724 + bmtd.bmt_pgs = bufsz >> bmtd.pg_shift;
725 +
726 + nand_bbt_buf = kzalloc(bufsz, GFP_KERNEL);
727 + nand_data_buf = kzalloc(bmtd.pg_size, GFP_KERNEL);
728 +
729 + if (!nand_bbt_buf || !nand_data_buf) {
730 + pr_info("nand: FATAL ERR: allocate buffer failed!\n");
731 + ret = -1;
732 + goto error;
733 + }
734 +
735 + memset(nand_bbt_buf, 0xff, bufsz);
736 + memset(nand_data_buf, 0xff, bmtd.pg_size);
737 +
738 + BBT_LOG("bbtbuf=0x%p(0x%x) dat=0x%p(0x%x)",
739 + nand_bbt_buf, bufsz, nand_data_buf, bmtd.pg_size);
740 + BBT_LOG("pool_lba=0x%x total_blks=0x%x bb_max=0x%x",
741 + bmtd.pool_lba, bmtd.total_blks, bmtd.bb_max);
742 +
743 + /* Scanning start from the first page of the last block
744 + * of whole flash
745 + */
746 + bbt = scan_bmt(bmtd.total_blks - 1);
747 + if (!bbt) {
748 + /* BMT not found */
749 + if (bmtd.total_blks > BB_TABLE_MAX + BMT_TABLE_MAX) {
750 + pr_info("nand: FATAL: Too many blocks, can not support!\n");
751 + ret = -1;
752 + goto error;
753 + }
754 +
755 + bbt = (struct bbbt *)nand_bbt_buf;
756 + memset(bbt->bmt_tbl, BMT_TBL_DEF_VAL, sizeof(bbt->bmt_tbl));
757 +
758 + if (scan_bad_blocks(bbt)) {
759 + ret = -1;
760 + goto error;
761 + }
762 +
763 + /* BMT always in the last valid block in pool */
764 + bmtd.bmt_blk_idx = upload_bmt(bbt, bmtd.bmt_blk_idx);
765 + block = bbt->bmt_tbl[bmtd.bmt_blk_idx].block;
766 + pr_notice("[BBT] BMT.v2 is written into PBA:0x%x\n", block);
767 +
768 + if (bmtd.bmt_blk_idx == 0)
769 + pr_info("nand: Warning: no available block in BMT pool!\n");
770 + else if (bmtd.bmt_blk_idx == (u16)-1) {
771 + ret = -1;
772 + goto error;
773 + }
774 + }
775 + mtk_bmt_add_debugfs();
776 +
777 + bmtd.bbt = bbt;
778 + return 0;
779 +
780 +error:
781 + mtk_bmt_detach(mtd);
782 + return ret;
783 +}
784 +
785 +
786 +MODULE_LICENSE("GPL");
787 +MODULE_AUTHOR("Xiangsheng Hou <xiangsheng.hou@mediatek.com>, Felix Fietkau <nbd@nbd.name>");
788 +MODULE_DESCRIPTION("Bad Block mapping management v2 for MediaTek NAND Flash Driver");
789 +
790 --- a/drivers/mtd/nand/spi/core.c
791 +++ b/drivers/mtd/nand/spi/core.c
792 @@ -18,6 +18,7 @@
793 #include <linux/slab.h>
794 #include <linux/spi/spi.h>
795 #include <linux/spi/spi-mem.h>
796 +#include <linux/mtd/mtk_bmt.h>
797
798 static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)
799 {
800 @@ -1100,6 +1101,8 @@ static int spinand_probe(struct spi_mem
801 if (ret)
802 return ret;
803
804 + mtk_bmt_attach(mtd);
805 +
806 ret = mtd_device_register(mtd, NULL, 0);
807 if (ret)
808 goto err_spinand_cleanup;
809 @@ -1125,6 +1128,7 @@ static int spinand_remove(struct spi_mem
810 if (ret)
811 return ret;
812
813 + mtk_bmt_detach(mtd);
814 spinand_cleanup(spinand);
815
816 return 0;
817 --- /dev/null
818 +++ b/include/linux/mtd/mtk_bmt.h
819 @@ -0,0 +1,18 @@
820 +#ifndef __MTK_BMT_H
821 +#define __MTK_BMT_H
822 +
823 +#ifdef CONFIG_MTD_NAND_MTK_BMT
824 +int mtk_bmt_attach(struct mtd_info *mtd);
825 +void mtk_bmt_detach(struct mtd_info *mtd);
826 +#else
827 +static inline int mtk_bmt_attach(struct mtd_info *mtd)
828 +{
829 + return 0;
830 +}
831 +
832 +static inline void mtk_bmt_detach(struct mtd_info *mtd)
833 +{
834 +}
835 +#endif
836 +
837 +#endif