xref: /openbmc/linux/drivers/mtd/nand/bbt.c (revision fd0d8d85f7230052e638a56d1bfea170c488e6bc)
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  */
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 	unsigned int nwords = DIV_ROUND_UP(nblocks * bits_per_block,
289c3736a3SBoris Brezillon 					   BITS_PER_LONG);
299c3736a3SBoris Brezillon 
3040b41289SFrieder Schrempf 	nand->bbt.cache = kcalloc(nwords, sizeof(*nand->bbt.cache),
3140b41289SFrieder Schrempf 				  GFP_KERNEL);
329c3736a3SBoris Brezillon 	if (!nand->bbt.cache)
339c3736a3SBoris Brezillon 		return -ENOMEM;
349c3736a3SBoris Brezillon 
359c3736a3SBoris Brezillon 	return 0;
369c3736a3SBoris Brezillon }
379c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_init);
389c3736a3SBoris Brezillon 
399c3736a3SBoris Brezillon /**
409c3736a3SBoris Brezillon  * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
419c3736a3SBoris Brezillon  * @nand: NAND device
429c3736a3SBoris Brezillon  *
439c3736a3SBoris Brezillon  * Undoes what has been done in nanddev_bbt_init()
449c3736a3SBoris Brezillon  */
459c3736a3SBoris Brezillon void nanddev_bbt_cleanup(struct nand_device *nand)
469c3736a3SBoris Brezillon {
479c3736a3SBoris Brezillon 	kfree(nand->bbt.cache);
489c3736a3SBoris Brezillon }
499c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
509c3736a3SBoris Brezillon 
519c3736a3SBoris Brezillon /**
529c3736a3SBoris Brezillon  * nanddev_bbt_update() - Update a BBT
539c3736a3SBoris Brezillon  * @nand: nand device
549c3736a3SBoris Brezillon  *
559c3736a3SBoris Brezillon  * Update the BBT. Currently a NOP function since on-flash bbt is not yet
569c3736a3SBoris Brezillon  * supported.
579c3736a3SBoris Brezillon  *
589c3736a3SBoris Brezillon  * Return: 0 in case of success, a negative error code otherwise.
599c3736a3SBoris Brezillon  */
609c3736a3SBoris Brezillon int nanddev_bbt_update(struct nand_device *nand)
619c3736a3SBoris Brezillon {
629c3736a3SBoris Brezillon 	return 0;
639c3736a3SBoris Brezillon }
649c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_update);
659c3736a3SBoris Brezillon 
669c3736a3SBoris Brezillon /**
679c3736a3SBoris Brezillon  * nanddev_bbt_get_block_status() - Return the status of an eraseblock
689c3736a3SBoris Brezillon  * @nand: nand device
699c3736a3SBoris Brezillon  * @entry: the BBT entry
709c3736a3SBoris Brezillon  *
719c3736a3SBoris Brezillon  * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
729c3736a3SBoris Brezillon  *	   is bigger than the BBT size.
739c3736a3SBoris Brezillon  */
749c3736a3SBoris Brezillon int nanddev_bbt_get_block_status(const struct nand_device *nand,
759c3736a3SBoris Brezillon 				 unsigned int entry)
769c3736a3SBoris Brezillon {
779c3736a3SBoris Brezillon 	unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
789c3736a3SBoris Brezillon 	unsigned long *pos = nand->bbt.cache +
799c3736a3SBoris Brezillon 			     ((entry * bits_per_block) / BITS_PER_LONG);
809c3736a3SBoris Brezillon 	unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
819c3736a3SBoris Brezillon 	unsigned long status;
829c3736a3SBoris Brezillon 
839c3736a3SBoris Brezillon 	if (entry >= nanddev_neraseblocks(nand))
849c3736a3SBoris Brezillon 		return -ERANGE;
859c3736a3SBoris Brezillon 
869c3736a3SBoris Brezillon 	status = pos[0] >> offs;
879c3736a3SBoris Brezillon 	if (bits_per_block + offs > BITS_PER_LONG)
889c3736a3SBoris Brezillon 		status |= pos[1] << (BITS_PER_LONG - offs);
899c3736a3SBoris Brezillon 
909c3736a3SBoris Brezillon 	return status & GENMASK(bits_per_block - 1, 0);
919c3736a3SBoris Brezillon }
929c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
939c3736a3SBoris Brezillon 
949c3736a3SBoris Brezillon /**
959c3736a3SBoris Brezillon  * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
969c3736a3SBoris Brezillon  *				    in-memory BBT
979c3736a3SBoris Brezillon  * @nand: nand device
989c3736a3SBoris Brezillon  * @entry: the BBT entry to update
999c3736a3SBoris Brezillon  * @status: the new status
1009c3736a3SBoris Brezillon  *
1019c3736a3SBoris Brezillon  * Update an entry of the in-memory BBT. If you want to push the updated BBT
1029c3736a3SBoris Brezillon  * the NAND you should call nanddev_bbt_update().
1039c3736a3SBoris Brezillon  *
1049c3736a3SBoris Brezillon  * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
1059c3736a3SBoris Brezillon  *	   size.
1069c3736a3SBoris Brezillon  */
1079c3736a3SBoris Brezillon int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
1089c3736a3SBoris Brezillon 				 enum nand_bbt_block_status status)
1099c3736a3SBoris Brezillon {
1109c3736a3SBoris Brezillon 	unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
1119c3736a3SBoris Brezillon 	unsigned long *pos = nand->bbt.cache +
1129c3736a3SBoris Brezillon 			     ((entry * bits_per_block) / BITS_PER_LONG);
1139c3736a3SBoris Brezillon 	unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
1149c3736a3SBoris Brezillon 	unsigned long val = status & GENMASK(bits_per_block - 1, 0);
1159c3736a3SBoris Brezillon 
1169c3736a3SBoris Brezillon 	if (entry >= nanddev_neraseblocks(nand))
1179c3736a3SBoris Brezillon 		return -ERANGE;
1189c3736a3SBoris Brezillon 
1199c3736a3SBoris Brezillon 	pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
1209c3736a3SBoris Brezillon 	pos[0] |= val << offs;
1219c3736a3SBoris Brezillon 
1229c3736a3SBoris Brezillon 	if (bits_per_block + offs > BITS_PER_LONG) {
1239c3736a3SBoris Brezillon 		unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
1249c3736a3SBoris Brezillon 
1259c3736a3SBoris Brezillon 		pos[1] &= ~GENMASK(rbits - 1, 0);
126*fd0d8d85SDoyle, Patrick 		pos[1] |= val >> (bits_per_block - rbits);
1279c3736a3SBoris Brezillon 	}
1289c3736a3SBoris Brezillon 
1299c3736a3SBoris Brezillon 	return 0;
1309c3736a3SBoris Brezillon }
1319c3736a3SBoris Brezillon EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);
132