xref: /openbmc/linux/drivers/mtd/nand/bbt.c (revision 2359ccdd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017 Free Electrons
4  *
5  * Authors:
6  *	Boris Brezillon <boris.brezillon@free-electrons.com>
7  *	Peter Pan <peterpandong@micron.com>
8  */
9 
10 #define pr_fmt(fmt)	"nand-bbt: " fmt
11 
12 #include <linux/mtd/nand.h>
13 #include <linux/slab.h>
14 
15 /**
16  * nanddev_bbt_init() - Initialize the BBT (Bad Block Table)
17  * @nand: NAND device
18  *
19  * Initialize the in-memory BBT.
20  *
21  * Return: 0 in case of success, a negative error code otherwise.
22  */
23 int nanddev_bbt_init(struct nand_device *nand)
24 {
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,
28 					   BITS_PER_LONG);
29 
30 	nand->bbt.cache = kzalloc(nwords, GFP_KERNEL);
31 	if (!nand->bbt.cache)
32 		return -ENOMEM;
33 
34 	return 0;
35 }
36 EXPORT_SYMBOL_GPL(nanddev_bbt_init);
37 
38 /**
39  * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
40  * @nand: NAND device
41  *
42  * Undoes what has been done in nanddev_bbt_init()
43  */
44 void nanddev_bbt_cleanup(struct nand_device *nand)
45 {
46 	kfree(nand->bbt.cache);
47 }
48 EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
49 
50 /**
51  * nanddev_bbt_update() - Update a BBT
52  * @nand: nand device
53  *
54  * Update the BBT. Currently a NOP function since on-flash bbt is not yet
55  * supported.
56  *
57  * Return: 0 in case of success, a negative error code otherwise.
58  */
59 int nanddev_bbt_update(struct nand_device *nand)
60 {
61 	return 0;
62 }
63 EXPORT_SYMBOL_GPL(nanddev_bbt_update);
64 
65 /**
66  * nanddev_bbt_get_block_status() - Return the status of an eraseblock
67  * @nand: nand device
68  * @entry: the BBT entry
69  *
70  * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
71  *	   is bigger than the BBT size.
72  */
73 int nanddev_bbt_get_block_status(const struct nand_device *nand,
74 				 unsigned int entry)
75 {
76 	unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
77 	unsigned long *pos = nand->bbt.cache +
78 			     ((entry * bits_per_block) / BITS_PER_LONG);
79 	unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
80 	unsigned long status;
81 
82 	if (entry >= nanddev_neraseblocks(nand))
83 		return -ERANGE;
84 
85 	status = pos[0] >> offs;
86 	if (bits_per_block + offs > BITS_PER_LONG)
87 		status |= pos[1] << (BITS_PER_LONG - offs);
88 
89 	return status & GENMASK(bits_per_block - 1, 0);
90 }
91 EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
92 
93 /**
94  * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
95  *				    in-memory BBT
96  * @nand: nand device
97  * @entry: the BBT entry to update
98  * @status: the new status
99  *
100  * Update an entry of the in-memory BBT. If you want to push the updated BBT
101  * the NAND you should call nanddev_bbt_update().
102  *
103  * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
104  *	   size.
105  */
106 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
107 				 enum nand_bbt_block_status status)
108 {
109 	unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
110 	unsigned long *pos = nand->bbt.cache +
111 			     ((entry * bits_per_block) / BITS_PER_LONG);
112 	unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
113 	unsigned long val = status & GENMASK(bits_per_block - 1, 0);
114 
115 	if (entry >= nanddev_neraseblocks(nand))
116 		return -ERANGE;
117 
118 	pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
119 	pos[0] |= val << offs;
120 
121 	if (bits_per_block + offs > BITS_PER_LONG) {
122 		unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
123 
124 		pos[1] &= ~GENMASK(rbits - 1, 0);
125 		pos[1] |= val >> rbits;
126 	}
127 
128 	return 0;
129 }
130 EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);
131