xref: /openbmc/linux/drivers/mtd/nand/bbt.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
19c3736a3SBoris Brezillon // SPDX-License-Identifier: GPL-2.0
29c3736a3SBoris Brezillon /*
39c3736a3SBoris Brezillon  * Copyright (c) 2017 Free Electrons
49c3736a3SBoris Brezillon  *
59c3736a3SBoris Brezillon  * Authors:
69c3736a3SBoris Brezillon  *	Boris Brezillon <boris.brezillon@free-electrons.com>
79c3736a3SBoris Brezillon  *	Peter Pan <peterpandong@micron.com>
89c3736a3SBoris Brezillon  */
99c3736a3SBoris Brezillon 
109c3736a3SBoris Brezillon #define pr_fmt(fmt)	"nand-bbt: " fmt
119c3736a3SBoris Brezillon 
129c3736a3SBoris Brezillon #include <linux/mtd/nand.h>
139c3736a3SBoris Brezillon #include <linux/slab.h>
149c3736a3SBoris Brezillon 
159c3736a3SBoris Brezillon /**
169c3736a3SBoris Brezillon  * nanddev_bbt_init() - Initialize the BBT (Bad Block Table)
179c3736a3SBoris Brezillon  * @nand: NAND device
189c3736a3SBoris Brezillon  *
199c3736a3SBoris Brezillon  * Initialize the in-memory BBT.
209c3736a3SBoris Brezillon  *
219c3736a3SBoris Brezillon  * Return: 0 in case of success, a negative error code otherwise.
229c3736a3SBoris Brezillon  */
nanddev_bbt_init(struct nand_device * nand)239c3736a3SBoris Brezillon int nanddev_bbt_init(struct nand_device *nand)
249c3736a3SBoris Brezillon {
259c3736a3SBoris Brezillon 	unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
269c3736a3SBoris Brezillon 	unsigned int nblocks = nanddev_neraseblocks(nand);
279c3736a3SBoris Brezillon 
28*054c6b58SChristophe JAILLET 	nand->bbt.cache = bitmap_zalloc(nblocks * bits_per_block, GFP_KERNEL);
299c3736a3SBoris Brezillon 	if (!nand->bbt.cache)
309c3736a3SBoris Brezillon 		return -ENOMEM;
319c3736a3SBoris Brezillon 
329c3736a3SBoris Brezillon 	return 0;
339c3736a3SBoris Brezillon }
349c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_init);
359c3736a3SBoris Brezillon 
369c3736a3SBoris Brezillon /**
379c3736a3SBoris Brezillon  * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
389c3736a3SBoris Brezillon  * @nand: NAND device
399c3736a3SBoris Brezillon  *
409c3736a3SBoris Brezillon  * Undoes what has been done in nanddev_bbt_init()
419c3736a3SBoris Brezillon  */
nanddev_bbt_cleanup(struct nand_device * nand)429c3736a3SBoris Brezillon void nanddev_bbt_cleanup(struct nand_device *nand)
439c3736a3SBoris Brezillon {
44*054c6b58SChristophe JAILLET 	bitmap_free(nand->bbt.cache);
459c3736a3SBoris Brezillon }
469c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
479c3736a3SBoris Brezillon 
489c3736a3SBoris Brezillon /**
499c3736a3SBoris Brezillon  * nanddev_bbt_update() - Update a BBT
509c3736a3SBoris Brezillon  * @nand: nand device
519c3736a3SBoris Brezillon  *
529c3736a3SBoris Brezillon  * Update the BBT. Currently a NOP function since on-flash bbt is not yet
539c3736a3SBoris Brezillon  * supported.
549c3736a3SBoris Brezillon  *
559c3736a3SBoris Brezillon  * Return: 0 in case of success, a negative error code otherwise.
569c3736a3SBoris Brezillon  */
nanddev_bbt_update(struct nand_device * nand)579c3736a3SBoris Brezillon int nanddev_bbt_update(struct nand_device *nand)
589c3736a3SBoris Brezillon {
599c3736a3SBoris Brezillon 	return 0;
609c3736a3SBoris Brezillon }
619c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_update);
629c3736a3SBoris Brezillon 
639c3736a3SBoris Brezillon /**
649c3736a3SBoris Brezillon  * nanddev_bbt_get_block_status() - Return the status of an eraseblock
659c3736a3SBoris Brezillon  * @nand: nand device
669c3736a3SBoris Brezillon  * @entry: the BBT entry
679c3736a3SBoris Brezillon  *
689c3736a3SBoris Brezillon  * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
699c3736a3SBoris Brezillon  *	   is bigger than the BBT size.
709c3736a3SBoris Brezillon  */
nanddev_bbt_get_block_status(const struct nand_device * nand,unsigned int entry)719c3736a3SBoris Brezillon int nanddev_bbt_get_block_status(const struct nand_device *nand,
729c3736a3SBoris Brezillon 				 unsigned int entry)
739c3736a3SBoris Brezillon {
749c3736a3SBoris Brezillon 	unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
759c3736a3SBoris Brezillon 	unsigned long *pos = nand->bbt.cache +
769c3736a3SBoris Brezillon 			     ((entry * bits_per_block) / BITS_PER_LONG);
779c3736a3SBoris Brezillon 	unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
789c3736a3SBoris Brezillon 	unsigned long status;
799c3736a3SBoris Brezillon 
809c3736a3SBoris Brezillon 	if (entry >= nanddev_neraseblocks(nand))
819c3736a3SBoris Brezillon 		return -ERANGE;
829c3736a3SBoris Brezillon 
839c3736a3SBoris Brezillon 	status = pos[0] >> offs;
849c3736a3SBoris Brezillon 	if (bits_per_block + offs > BITS_PER_LONG)
859c3736a3SBoris Brezillon 		status |= pos[1] << (BITS_PER_LONG - offs);
869c3736a3SBoris Brezillon 
879c3736a3SBoris Brezillon 	return status & GENMASK(bits_per_block - 1, 0);
889c3736a3SBoris Brezillon }
899c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
909c3736a3SBoris Brezillon 
919c3736a3SBoris Brezillon /**
929c3736a3SBoris Brezillon  * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
939c3736a3SBoris Brezillon  *				    in-memory BBT
949c3736a3SBoris Brezillon  * @nand: nand device
959c3736a3SBoris Brezillon  * @entry: the BBT entry to update
969c3736a3SBoris Brezillon  * @status: the new status
979c3736a3SBoris Brezillon  *
989c3736a3SBoris Brezillon  * Update an entry of the in-memory BBT. If you want to push the updated BBT
999c3736a3SBoris Brezillon  * the NAND you should call nanddev_bbt_update().
1009c3736a3SBoris Brezillon  *
1019c3736a3SBoris Brezillon  * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
1029c3736a3SBoris Brezillon  *	   size.
1039c3736a3SBoris Brezillon  */
nanddev_bbt_set_block_status(struct nand_device * nand,unsigned int entry,enum nand_bbt_block_status status)1049c3736a3SBoris Brezillon int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
1059c3736a3SBoris Brezillon 				 enum nand_bbt_block_status status)
1069c3736a3SBoris Brezillon {
1079c3736a3SBoris Brezillon 	unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
1089c3736a3SBoris Brezillon 	unsigned long *pos = nand->bbt.cache +
1099c3736a3SBoris Brezillon 			     ((entry * bits_per_block) / BITS_PER_LONG);
1109c3736a3SBoris Brezillon 	unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
1119c3736a3SBoris Brezillon 	unsigned long val = status & GENMASK(bits_per_block - 1, 0);
1129c3736a3SBoris Brezillon 
1139c3736a3SBoris Brezillon 	if (entry >= nanddev_neraseblocks(nand))
1149c3736a3SBoris Brezillon 		return -ERANGE;
1159c3736a3SBoris Brezillon 
1169c3736a3SBoris Brezillon 	pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
1179c3736a3SBoris Brezillon 	pos[0] |= val << offs;
1189c3736a3SBoris Brezillon 
1199c3736a3SBoris Brezillon 	if (bits_per_block + offs > BITS_PER_LONG) {
1209c3736a3SBoris Brezillon 		unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
1219c3736a3SBoris Brezillon 
1229c3736a3SBoris Brezillon 		pos[1] &= ~GENMASK(rbits - 1, 0);
123fd0d8d85SDoyle, Patrick 		pos[1] |= val >> (bits_per_block - rbits);
1249c3736a3SBoris Brezillon 	}
1259c3736a3SBoris Brezillon 
1269c3736a3SBoris Brezillon 	return 0;
1279c3736a3SBoris Brezillon }
1289c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);
129