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