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