1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017 Free Electrons
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);
27 unsigned int nwords = DIV_ROUND_UP(nblocks * bits_per_block,
30 nand->bbt.cache = kzalloc(nwords, GFP_KERNEL);
36 EXPORT_SYMBOL_GPL(nanddev_bbt_init);
39 * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
42 * Undoes what has been done in nanddev_bbt_init()
44 void nanddev_bbt_cleanup(struct nand_device *nand)
46 kfree(nand->bbt.cache);
48 EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
51 * nanddev_bbt_update() - Update a BBT
54 * Update the BBT. Currently a NOP function since on-flash bbt is not yet
57 * Return: 0 in case of success, a negative error code otherwise.
59 int nanddev_bbt_update(struct nand_device *nand)
63 EXPORT_SYMBOL_GPL(nanddev_bbt_update);
66 * nanddev_bbt_get_block_status() - Return the status of an eraseblock
68 * @entry: the BBT entry
70 * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
71 * is bigger than the BBT size.
73 int nanddev_bbt_get_block_status(const struct nand_device *nand,
76 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
77 unsigned long *pos = nand->bbt.cache +
78 ((entry * bits_per_block) / BITS_PER_LONG);
79 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
82 if (entry >= nanddev_neraseblocks(nand))
85 status = pos[0] >> offs;
86 if (bits_per_block + offs > BITS_PER_LONG)
87 status |= pos[1] << (BITS_PER_LONG - offs);
89 return status & GENMASK(bits_per_block - 1, 0);
91 EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
94 * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
97 * @entry: the BBT entry to update
98 * @status: the new status
100 * Update an entry of the in-memory BBT. If you want to push the updated BBT
101 * the NAND you should call nanddev_bbt_update().
103 * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
106 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
107 enum nand_bbt_block_status status)
109 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
110 unsigned long *pos = nand->bbt.cache +
111 ((entry * bits_per_block) / BITS_PER_LONG);
112 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
113 unsigned long val = status & GENMASK(bits_per_block - 1, 0);
115 if (entry >= nanddev_neraseblocks(nand))
118 pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
119 pos[0] |= val << offs;
121 if (bits_per_block + offs > BITS_PER_LONG) {
122 unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
124 pos[1] &= ~GENMASK(rbits - 1, 0);
125 pos[1] |= val >> rbits;
130 EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);