2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2012 Semihalf
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/malloc.h>
40 #include <dev/nand/nand.h>
44 #define BBT_PRIMARY_PATTERN 0x01020304
45 #define BBT_SECONDARY_PATTERN 0x05060708
54 struct nand_chip *chip;
56 uint32_t secondary_map;
57 enum bbt_place active;
58 struct bbt_header *hdr;
68 static int nand_bbt_save(struct nand_bbt *);
69 static int nand_bbt_load_hdr(struct nand_bbt *, struct bbt_header *, int8_t);
70 static int nand_bbt_load_table(struct nand_bbt *);
71 static int nand_bbt_prescan(struct nand_bbt *);
74 nand_init_bbt(struct nand_chip *chip)
80 cg = &chip->chip_geom;
82 bbt = malloc(sizeof(struct nand_bbt), M_NAND, M_ZERO | M_WAITOK);
84 device_printf(chip->dev,
85 "Cannot allocate memory for bad block struct");
90 bbt->active = BBT_NONE;
91 bbt->primary_map = cg->chip_size - cg->block_size;
92 bbt->secondary_map = cg->chip_size - 2 * cg->block_size;
93 bbt->tab_len = cg->blks_per_chip * sizeof(uint32_t);
94 bbt->hdr = malloc(sizeof(struct bbt_header) + bbt->tab_len, M_NAND,
97 device_printf(chip->dev, "Cannot allocate %d bytes for BB "
98 "Table", bbt->tab_len);
102 bbt->hdr->seq_nr = 0;
103 bbt->table = (uint32_t *)((uint8_t *)bbt->hdr +
104 sizeof(struct bbt_header));
106 err = nand_bbt_load_table(bbt);
108 free(bbt->table, M_NAND);
114 if (bbt->active == BBT_NONE) {
115 bbt->active = BBT_PRIMARY;
116 memset(bbt->table, 0xff, bbt->tab_len);
117 nand_bbt_prescan(bbt);
120 device_printf(chip->dev, "Found BBT table for chip\n");
126 nand_destroy_bbt(struct nand_chip *chip)
130 nand_bbt_save(chip->bbt);
132 free(chip->bbt->hdr, M_NAND);
133 free(chip->bbt, M_NAND);
139 nand_update_bbt(struct nand_chip *chip)
142 nand_bbt_save(chip->bbt);
148 nand_bbt_save(struct nand_bbt *bbt)
154 if (bbt->active == BBT_PRIMARY) {
155 addr = bbt->secondary_map;
156 bbt->hdr->pattern = BBT_SECONDARY_PATTERN;
157 next = BBT_SECONDARY;
159 addr = bbt->primary_map;
160 bbt->hdr->pattern = BBT_PRIMARY_PATTERN;
164 err = nand_erase_blocks(bbt->chip, addr,
165 bbt->chip->chip_geom.block_size);
171 err = nand_prog_pages_raw(bbt->chip, addr, bbt->hdr,
172 bbt->tab_len + sizeof(struct bbt_header));
181 nand_bbt_load_hdr(struct nand_bbt *bbt, struct bbt_header *hdr, int8_t primary)
186 addr = bbt->primary_map;
188 addr = bbt->secondary_map;
190 return (nand_read_pages_raw(bbt->chip, addr, hdr,
191 sizeof(struct bbt_header)));
195 nand_bbt_load_table(struct nand_bbt *bbt)
197 struct bbt_header hdr1, hdr2;
198 uint32_t address = 0;
201 bzero(&hdr1, sizeof(hdr1));
202 bzero(&hdr2, sizeof(hdr2));
204 nand_bbt_load_hdr(bbt, &hdr1, 1);
205 if (hdr1.pattern == BBT_PRIMARY_PATTERN) {
206 bbt->active = BBT_PRIMARY;
207 address = bbt->primary_map;
209 bzero(&hdr1, sizeof(hdr1));
212 nand_bbt_load_hdr(bbt, &hdr2, 0);
213 if ((hdr2.pattern == BBT_SECONDARY_PATTERN) &&
214 (hdr2.seq_nr > hdr1.seq_nr)) {
215 bbt->active = BBT_SECONDARY;
216 address = bbt->secondary_map;
218 bzero(&hdr2, sizeof(hdr2));
220 if (bbt->active != BBT_NONE)
221 err = nand_read_pages_raw(bbt->chip, address, bbt->hdr,
222 bbt->tab_len + sizeof(struct bbt_header));
228 nand_bbt_prescan(struct nand_bbt *bbt)
232 bool printed_hash = 0;
234 device_printf(bbt->chip->dev, "No BBT found. Prescan chip...\n");
235 for (i = 0; i < bbt->chip->chip_geom.blks_per_chip; i++) {
236 if (NAND_IS_BLK_BAD(bbt->chip->dev, i, &bad))
240 device_printf(bbt->chip->dev, "Bad block(%d)\n", i);
241 bbt->table[i] = 0x0FFFFFFF;
256 nand_check_bad_block(struct nand_chip *chip, uint32_t block_number)
259 if (!chip || !chip->bbt)
262 if ((chip->bbt->table[block_number] & 0xF0000000) == 0)
269 nand_mark_bad_block(struct nand_chip *chip, uint32_t block_number)
272 chip->bbt->table[block_number] = 0x0FFFFFFF;