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