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/bitops.h>
15 #include <linux/mtd/nand.h>
17 #include <linux/slab.h>
21 * nanddev_bbt_init() - Initialize the BBT (Bad Block Table)
24 * Initialize the in-memory BBT.
26 * Return: 0 in case of success, a negative error code otherwise.
28 int nanddev_bbt_init(struct nand_device *nand)
30 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
31 unsigned int nblocks = nanddev_neraseblocks(nand);
32 unsigned int nwords = DIV_ROUND_UP(nblocks * bits_per_block,
35 nand->bbt.cache = kzalloc(nwords, GFP_KERNEL);
41 EXPORT_SYMBOL_GPL(nanddev_bbt_init);
44 * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
47 * Undoes what has been done in nanddev_bbt_init()
49 void nanddev_bbt_cleanup(struct nand_device *nand)
51 kfree(nand->bbt.cache);
53 EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
56 * nanddev_bbt_update() - Update a BBT
59 * Update the BBT. Currently a NOP function since on-flash bbt is not yet
62 * Return: 0 in case of success, a negative error code otherwise.
64 int nanddev_bbt_update(struct nand_device *nand)
68 EXPORT_SYMBOL_GPL(nanddev_bbt_update);
71 * nanddev_bbt_get_block_status() - Return the status of an eraseblock
73 * @entry: the BBT entry
75 * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
76 * is bigger than the BBT size.
78 int nanddev_bbt_get_block_status(const struct nand_device *nand,
81 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
82 unsigned long *pos = nand->bbt.cache +
83 ((entry * bits_per_block) / BITS_PER_LONG);
84 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
87 if (entry >= nanddev_neraseblocks(nand))
90 status = pos[0] >> offs;
91 if (bits_per_block + offs > BITS_PER_LONG)
92 status |= pos[1] << (BITS_PER_LONG - offs);
94 return status & GENMASK(bits_per_block - 1, 0);
96 EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
99 * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
102 * @entry: the BBT entry to update
103 * @status: the new status
105 * Update an entry of the in-memory BBT. If you want to push the updated BBT
106 * the NAND you should call nanddev_bbt_update().
108 * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
111 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
112 enum nand_bbt_block_status status)
114 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
115 unsigned long *pos = nand->bbt.cache +
116 ((entry * bits_per_block) / BITS_PER_LONG);
117 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
118 unsigned long val = status & GENMASK(bits_per_block - 1, 0);
120 if (entry >= nanddev_neraseblocks(nand))
123 pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
124 pos[0] |= val << offs;
126 if (bits_per_block + offs > BITS_PER_LONG) {
127 unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
129 pos[1] &= ~GENMASK(rbits - 1, 0);
130 pos[1] |= val >> rbits;
135 EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);