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 = kcalloc(nwords, sizeof(*nand->bbt.cache),
37 EXPORT_SYMBOL_GPL(nanddev_bbt_init);
40 * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
43 * Undoes what has been done in nanddev_bbt_init()
45 void nanddev_bbt_cleanup(struct nand_device *nand)
47 kfree(nand->bbt.cache);
49 EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
52 * nanddev_bbt_update() - Update a BBT
55 * Update the BBT. Currently a NOP function since on-flash bbt is not yet
58 * Return: 0 in case of success, a negative error code otherwise.
60 int nanddev_bbt_update(struct nand_device *nand)
64 EXPORT_SYMBOL_GPL(nanddev_bbt_update);
67 * nanddev_bbt_get_block_status() - Return the status of an eraseblock
69 * @entry: the BBT entry
71 * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
72 * is bigger than the BBT size.
74 int nanddev_bbt_get_block_status(const struct nand_device *nand,
77 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
78 unsigned long *pos = nand->bbt.cache +
79 ((entry * bits_per_block) / BITS_PER_LONG);
80 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
83 if (entry >= nanddev_neraseblocks(nand))
86 status = pos[0] >> offs;
87 if (bits_per_block + offs > BITS_PER_LONG)
88 status |= pos[1] << (BITS_PER_LONG - offs);
90 return status & GENMASK(bits_per_block - 1, 0);
92 EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
95 * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
98 * @entry: the BBT entry to update
99 * @status: the new status
101 * Update an entry of the in-memory BBT. If you want to push the updated BBT
102 * the NAND you should call nanddev_bbt_update().
104 * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
107 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
108 enum nand_bbt_block_status status)
110 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
111 unsigned long *pos = nand->bbt.cache +
112 ((entry * bits_per_block) / BITS_PER_LONG);
113 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
114 unsigned long val = status & GENMASK(bits_per_block - 1, 0);
116 if (entry >= nanddev_neraseblocks(nand))
119 pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
120 pos[0] |= val << offs;
122 if (bits_per_block + offs > BITS_PER_LONG) {
123 unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
125 pos[1] &= ~GENMASK(rbits - 1, 0);
126 pos[1] |= val >> rbits;
131 EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);