1
2
3
4
5
6
7
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
17
18
19
20
21
22
23int 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 = kcalloc(nwords, sizeof(*nand->bbt.cache),
31 GFP_KERNEL);
32 if (!nand->bbt.cache)
33 return -ENOMEM;
34
35 return 0;
36}
37EXPORT_SYMBOL_GPL(nanddev_bbt_init);
38
39
40
41
42
43
44
45void nanddev_bbt_cleanup(struct nand_device *nand)
46{
47 kfree(nand->bbt.cache);
48}
49EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
50
51
52
53
54
55
56
57
58
59
60int nanddev_bbt_update(struct nand_device *nand)
61{
62 return 0;
63}
64EXPORT_SYMBOL_GPL(nanddev_bbt_update);
65
66
67
68
69
70
71
72
73
74int nanddev_bbt_get_block_status(const struct nand_device *nand,
75 unsigned int entry)
76{
77 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
78 unsigned long *pos = nand->bbt.cache +
79 ((entry * bits_per_block) / BITS_PER_LONG);
80 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
81 unsigned long status;
82
83 if (entry >= nanddev_neraseblocks(nand))
84 return -ERANGE;
85
86 status = pos[0] >> offs;
87 if (bits_per_block + offs > BITS_PER_LONG)
88 status |= pos[1] << (BITS_PER_LONG - offs);
89
90 return status & GENMASK(bits_per_block - 1, 0);
91}
92EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
108 enum nand_bbt_block_status status)
109{
110 unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
111 unsigned long *pos = nand->bbt.cache +
112 ((entry * bits_per_block) / BITS_PER_LONG);
113 unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
114 unsigned long val = status & GENMASK(bits_per_block - 1, 0);
115
116 if (entry >= nanddev_neraseblocks(nand))
117 return -ERANGE;
118
119 pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
120 pos[0] |= val << offs;
121
122 if (bits_per_block + offs > BITS_PER_LONG) {
123 unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
124
125 pos[1] &= ~GENMASK(rbits - 1, 0);
126 pos[1] |= val >> rbits;
127 }
128
129 return 0;
130}
131EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);
132