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
13 #include <dm/devres.h>
14 #include <linux/mtd/nand.h>
16 #include <linux/slab.h>
20 * nanddev_bbt_init() - Initialize the BBT (Bad Block Table)
23 * Initialize the in-memory BBT.
25 * Return: 0 in case of success, a negative error code otherwise.
27 int nanddev_bbt_init(struct nand_device *nand)
29 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
30 unsigned int nblocks = nanddev_neraseblocks(nand);
31 unsigned int nwords = DIV_ROUND_UP(nblocks * bits_per_block,
34 nand->bbt.cache = kzalloc(nwords, GFP_KERNEL);
40 EXPORT_SYMBOL_GPL(nanddev_bbt_init);
43 * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
46 * Undoes what has been done in nanddev_bbt_init()
48 void nanddev_bbt_cleanup(struct nand_device *nand)
50 kfree(nand->bbt.cache);
52 EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
55 * nanddev_bbt_update() - Update a BBT
58 * Update the BBT. Currently a NOP function since on-flash bbt is not yet
61 * Return: 0 in case of success, a negative error code otherwise.
63 int nanddev_bbt_update(struct nand_device *nand)
67 EXPORT_SYMBOL_GPL(nanddev_bbt_update);
70 * nanddev_bbt_get_block_status() - Return the status of an eraseblock
72 * @entry: the BBT entry
74 * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
75 * is bigger than the BBT size.
77 int nanddev_bbt_get_block_status(const struct nand_device *nand,
80 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
81 unsigned long *pos = nand->bbt.cache +
82 ((entry * bits_per_block) / BITS_PER_LONG);
83 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
86 if (entry >= nanddev_neraseblocks(nand))
89 status = pos[0] >> offs;
90 if (bits_per_block + offs > BITS_PER_LONG)
91 status |= pos[1] << (BITS_PER_LONG - offs);
93 return status & GENMASK(bits_per_block - 1, 0);
95 EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
98 * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
101 * @entry: the BBT entry to update
102 * @status: the new status
104 * Update an entry of the in-memory BBT. If you want to push the updated BBT
105 * the NAND you should call nanddev_bbt_update().
107 * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
110 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
111 enum nand_bbt_block_status status)
113 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
114 unsigned long *pos = nand->bbt.cache +
115 ((entry * bits_per_block) / BITS_PER_LONG);
116 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
117 unsigned long val = status & GENMASK(bits_per_block - 1, 0);
119 if (entry >= nanddev_neraseblocks(nand))
122 pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
123 pos[0] |= val << offs;
125 if (bits_per_block + offs > BITS_PER_LONG) {
126 unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
128 pos[1] &= ~GENMASK(rbits - 1, 0);
129 pos[1] |= val >> rbits;
134 EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);