1 From bec69bb8e85151729014d859106dcc3fe652b1d4 Mon Sep 17 00:00:00 2001
2 From: Boris BREZILLON <boris.brezillon@free-electrons.com>
3 Date: Mon, 28 Jul 2014 14:45:40 +0200
4 Subject: [PATCH] mtd: nand: Add page status table (pst)
6 Page status table is an byte array storing pages status.
8 - unknown: the page has not been read yet and we do not know its current
10 - empty: the page contains only FFs
11 - filled: the page has been filled with data
13 Care must be taken: an empty page does not mean it can be written, because
14 it might have already been written with only FFs.
16 These page status are useful to check wether the controller should try to
17 correct errors (using ECC) or a derandomize data (using a randomizer
20 Signed-off-by: Boris BREZILLON <boris.brezillon@free-electrons.com>
21 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
23 drivers/mtd/nand/nand_base.c | 154 +++++++++++++++++++++++++++++++++++++++++++
24 include/linux/mtd/nand.h | 21 ++++++
25 2 files changed, 175 insertions(+)
27 --- a/drivers/mtd/nand/nand_base.c
28 +++ b/drivers/mtd/nand/nand_base.c
29 @@ -1102,6 +1102,138 @@ out:
30 EXPORT_SYMBOL(nand_lock);
33 + * nand_page_is_empty - check wether a NAND page contains only FFs
35 + * @data: data buffer
38 + * Reads the data stored in the databuf buffer and check if it contains only
41 + * Return true if it does else return false.
43 +bool nand_page_is_empty(struct mtd_info *mtd, void *data, void *oob)
47 + u32 pattern = 0xffffffff;
52 + length = mtd->writesize;
54 + cnt = length < sizeof(pattern) ? length : sizeof(pattern);
55 + if (memcmp(&pattern, buf, cnt)) {
57 + for (i = 0; i < cnt * BITS_PER_BYTE; i++) {
58 + if (!(buf[i / BITS_PER_BYTE] &
59 + (1 << (i % BITS_PER_BYTE)))) {
61 + if (bitflips > mtd->ecc_strength)
67 + buf += sizeof(pattern);
68 + length -= sizeof(pattern);
72 + length = mtd->oobsize;
74 + cnt = length < sizeof(pattern) ? length : sizeof(pattern);
75 + if (memcmp(&pattern, buf, cnt)) {
77 + for (i = 0; i < cnt * BITS_PER_BYTE; i++) {
78 + if (!(buf[i / BITS_PER_BYTE] &
79 + (1 << (i % BITS_PER_BYTE)))) {
81 + if (bitflips > mtd->ecc_strength)
87 + buf += sizeof(pattern);
88 + length -= sizeof(pattern);
93 +EXPORT_SYMBOL(nand_page_is_empty);
96 + * nand_page_get_status - retrieve page status from the page status table (pst)
98 + * @page: page you want to get status on
100 + * Return the page status.
102 +int nand_page_get_status(struct mtd_info *mtd, int page)
104 + struct nand_chip *chip = mtd->priv;
105 + u8 shift = (page % 4) * 2;
106 + uint64_t offset = page / 4;
107 + int ret = NAND_PAGE_STATUS_UNKNOWN;
110 + ret = (chip->pst[offset] >> shift) & 0x3;
114 +EXPORT_SYMBOL(nand_page_get_status);
117 + * nand_page_set_status - assign page status from in the page status table
119 + * @page: page you want to get status on
120 + * @status: new status to assign
122 +void nand_page_set_status(struct mtd_info *mtd, int page,
123 + enum nand_page_status status)
125 + struct nand_chip *chip = mtd->priv;
132 + shift = (page % 4) * 2;
134 + chip->pst[offset] &= ~(0x3 << shift);
135 + chip->pst[offset] |= (status & 0x3) << shift;
137 +EXPORT_SYMBOL(nand_page_set_status);
140 + * nand_pst_create - create a page status table
143 + * Allocate a page status table and assign it to the mtd device.
145 + * Returns 0 in case of success or -ERRNO in case of error.
147 +int nand_pst_create(struct mtd_info *mtd)
149 + struct nand_chip *chip = mtd->priv;
154 + chip->pst = kzalloc(mtd->size >>
155 + (chip->page_shift + mtd->subpage_sft + 2),
162 +EXPORT_SYMBOL(nand_pst_create);
165 * nand_read_page_raw - [INTERN] read raw page data without ecc
166 * @mtd: mtd info structure
167 * @chip: nand chip info structure
168 @@ -2539,6 +2671,7 @@ static int nand_do_write_ops(struct mtd_
171 int part_pagewr = (column || writelen < (mtd->writesize - 1));
176 @@ -2574,6 +2707,14 @@ static int nand_do_write_ops(struct mtd_
180 + for (subpage = column / chip->subpagesize;
181 + subpage < (column + writelen) / chip->subpagesize;
183 + nand_page_set_status(mtd,
184 + (page << mtd->subpage_sft) +
191 @@ -2979,6 +3120,7 @@ int nand_erase_nand(struct mtd_info *mtd
192 int page, status, pages_per_block, ret, chipnr;
193 struct nand_chip *chip = mtd->priv;
197 pr_debug("%s: start = 0x%012llx, len = %llu\n",
198 __func__, (unsigned long long)instr->addr,
199 @@ -3051,6 +3193,18 @@ int nand_erase_nand(struct mtd_info *mtd
203 + for (i = 0; i < pages_per_block; i++) {
206 + subpage < 1 << mtd->subpage_sft;
208 + nand_page_set_status(mtd,
209 + ((page + i) << mtd->subpage_sft) +
215 /* Increment page address and decrement length */
216 len -= (1ULL << chip->phys_erase_shift);
217 page += pages_per_block;
218 --- a/include/linux/mtd/nand.h
219 +++ b/include/linux/mtd/nand.h
220 @@ -521,6 +521,24 @@ struct nand_ecc_ctrl {
225 + * Constants for page status
227 +enum nand_page_status {
228 + NAND_PAGE_STATUS_UNKNOWN,
233 +bool nand_page_is_empty(struct mtd_info *mtd, void *data, void *oob);
235 +int nand_page_get_status(struct mtd_info *mtd, int page);
237 +void nand_page_set_status(struct mtd_info *mtd, int page,
238 + enum nand_page_status status);
240 +int nand_pst_create(struct mtd_info *mtd);
243 * struct nand_buffers - buffer structure for read/write
244 * @ecccalc: buffer pointer for calculated ECC, size is oobsize.
245 @@ -630,6 +648,7 @@ struct nand_buffers {
246 * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
247 * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
249 + * @pst: [INTERN] page status table
250 * @controller: [REPLACEABLE] a pointer to a hardware controller
251 * structure which is shared among multiple independent
253 @@ -718,6 +737,8 @@ struct nand_chip {
255 struct nand_bbt_descr *badblock_pattern;
259 struct list_head partitions;
260 struct mutex part_lock;