1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017 Free Electrons
6 * Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Peter Pan <peterpandong@micron.com>
10 #define pr_fmt(fmt) "nand-bbt: " fmt
12 #include <linux/mtd/nand.h>
13 #include <linux/slab.h>
16 * nanddev_bbt_init() - Initialize the BBT (Bad Block Table)
19 * Initialize the in-memory BBT.
21 * Return: 0 in case of success, a negative error code otherwise.
23 int nanddev_bbt_init(struct nand_device
*nand
)
25 unsigned int bits_per_block
= fls(NAND_BBT_BLOCK_NUM_STATUS
);
26 unsigned int nblocks
= nanddev_neraseblocks(nand
);
28 nand
->bbt
.cache
= bitmap_zalloc(nblocks
* bits_per_block
, GFP_KERNEL
);
34 EXPORT_SYMBOL_GPL(nanddev_bbt_init
);
37 * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
40 * Undoes what has been done in nanddev_bbt_init()
42 void nanddev_bbt_cleanup(struct nand_device
*nand
)
44 bitmap_free(nand
->bbt
.cache
);
46 EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup
);
49 * nanddev_bbt_update() - Update a BBT
52 * Update the BBT. Currently a NOP function since on-flash bbt is not yet
55 * Return: 0 in case of success, a negative error code otherwise.
57 int nanddev_bbt_update(struct nand_device
*nand
)
61 EXPORT_SYMBOL_GPL(nanddev_bbt_update
);
64 * nanddev_bbt_get_block_status() - Return the status of an eraseblock
66 * @entry: the BBT entry
68 * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
69 * is bigger than the BBT size.
71 int nanddev_bbt_get_block_status(const struct nand_device
*nand
,
74 unsigned int bits_per_block
= fls(NAND_BBT_BLOCK_NUM_STATUS
);
75 unsigned long *pos
= nand
->bbt
.cache
+
76 ((entry
* bits_per_block
) / BITS_PER_LONG
);
77 unsigned int offs
= (entry
* bits_per_block
) % BITS_PER_LONG
;
80 if (entry
>= nanddev_neraseblocks(nand
))
83 status
= pos
[0] >> offs
;
84 if (bits_per_block
+ offs
> BITS_PER_LONG
)
85 status
|= pos
[1] << (BITS_PER_LONG
- offs
);
87 return status
& GENMASK(bits_per_block
- 1, 0);
89 EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status
);
92 * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
95 * @entry: the BBT entry to update
96 * @status: the new status
98 * Update an entry of the in-memory BBT. If you want to push the updated BBT
99 * the NAND you should call nanddev_bbt_update().
101 * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
104 int nanddev_bbt_set_block_status(struct nand_device
*nand
, unsigned int entry
,
105 enum nand_bbt_block_status status
)
107 unsigned int bits_per_block
= fls(NAND_BBT_BLOCK_NUM_STATUS
);
108 unsigned long *pos
= nand
->bbt
.cache
+
109 ((entry
* bits_per_block
) / BITS_PER_LONG
);
110 unsigned int offs
= (entry
* bits_per_block
) % BITS_PER_LONG
;
111 unsigned long val
= status
& GENMASK(bits_per_block
- 1, 0);
113 if (entry
>= nanddev_neraseblocks(nand
))
116 pos
[0] &= ~GENMASK(offs
+ bits_per_block
- 1, offs
);
117 pos
[0] |= val
<< offs
;
119 if (bits_per_block
+ offs
> BITS_PER_LONG
) {
120 unsigned int rbits
= bits_per_block
+ offs
- BITS_PER_LONG
;
122 pos
[1] &= ~GENMASK(rbits
- 1, 0);
123 pos
[1] |= val
>> (bits_per_block
- rbits
);
128 EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status
);