2 * Copyright (c) 2009-2012 Semihalf
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/malloc.h>
38 #include <dev/nand/nand.h>
42 #define BBT_PRIMARY_PATTERN 0x01020304
43 #define BBT_SECONDARY_PATTERN 0x05060708
52 struct nand_chip *chip;
54 uint32_t secondary_map;
55 enum bbt_place active;
56 struct bbt_header *hdr;
66 static int nand_bbt_save(struct nand_bbt *);
67 static int nand_bbt_load_hdr(struct nand_bbt *, struct bbt_header *, int8_t);
68 static int nand_bbt_load_table(struct nand_bbt *);
69 static int nand_bbt_prescan(struct nand_bbt *);
72 nand_init_bbt(struct nand_chip *chip)
78 cg = &chip->chip_geom;
80 bbt = malloc(sizeof(struct nand_bbt), M_NAND, M_ZERO | M_WAITOK);
82 device_printf(chip->dev,
83 "Cannot allocate memory for bad block struct");
88 bbt->active = BBT_NONE;
89 bbt->primary_map = cg->chip_size - cg->block_size;
90 bbt->secondary_map = cg->chip_size - 2 * cg->block_size;
91 bbt->tab_len = cg->blks_per_chip * sizeof(uint32_t);
92 bbt->hdr = malloc(sizeof(struct bbt_header) + bbt->tab_len, M_NAND,
95 device_printf(chip->dev, "Cannot allocate %d bytes for BB "
96 "Table", bbt->tab_len);
100 bbt->hdr->seq_nr = 0;
101 bbt->table = (uint32_t *)((uint8_t *)bbt->hdr +
102 sizeof(struct bbt_header));
104 err = nand_bbt_load_table(bbt);
106 free(bbt->table, M_NAND);
112 if (bbt->active == BBT_NONE) {
113 bbt->active = BBT_PRIMARY;
114 memset(bbt->table, 0xff, bbt->tab_len);
115 nand_bbt_prescan(bbt);
118 device_printf(chip->dev, "Found BBT table for chip\n");
124 nand_destroy_bbt(struct nand_chip *chip)
128 nand_bbt_save(chip->bbt);
130 free(chip->bbt->hdr, M_NAND);
131 free(chip->bbt, M_NAND);
137 nand_update_bbt(struct nand_chip *chip)
140 nand_bbt_save(chip->bbt);
146 nand_bbt_save(struct nand_bbt *bbt)
152 if (bbt->active == BBT_PRIMARY) {
153 addr = bbt->secondary_map;
154 bbt->hdr->pattern = BBT_SECONDARY_PATTERN;
155 next = BBT_SECONDARY;
157 addr = bbt->primary_map;
158 bbt->hdr->pattern = BBT_PRIMARY_PATTERN;
162 err = nand_erase_blocks(bbt->chip, addr,
163 bbt->chip->chip_geom.block_size);
169 err = nand_prog_pages_raw(bbt->chip, addr, bbt->hdr,
170 bbt->tab_len + sizeof(struct bbt_header));
179 nand_bbt_load_hdr(struct nand_bbt *bbt, struct bbt_header *hdr, int8_t primary)
184 addr = bbt->primary_map;
186 addr = bbt->secondary_map;
188 return (nand_read_pages_raw(bbt->chip, addr, hdr,
189 sizeof(struct bbt_header)));
193 nand_bbt_load_table(struct nand_bbt *bbt)
195 struct bbt_header hdr1, hdr2;
196 uint32_t address = 0;
199 bzero(&hdr1, sizeof(hdr1));
200 bzero(&hdr2, sizeof(hdr2));
202 nand_bbt_load_hdr(bbt, &hdr1, 1);
203 if (hdr1.pattern == BBT_PRIMARY_PATTERN) {
204 bbt->active = BBT_PRIMARY;
205 address = bbt->primary_map;
207 bzero(&hdr1, sizeof(hdr1));
210 nand_bbt_load_hdr(bbt, &hdr2, 0);
211 if ((hdr2.pattern == BBT_SECONDARY_PATTERN) &&
212 (hdr2.seq_nr > hdr1.seq_nr)) {
213 bbt->active = BBT_SECONDARY;
214 address = bbt->secondary_map;
216 bzero(&hdr2, sizeof(hdr2));
218 if (bbt->active != BBT_NONE)
219 err = nand_read_pages_raw(bbt->chip, address, bbt->hdr,
220 bbt->tab_len + sizeof(struct bbt_header));
226 nand_bbt_prescan(struct nand_bbt *bbt)
230 bool printed_hash = 0;
232 device_printf(bbt->chip->dev, "No BBT found. Prescan chip...\n");
233 for (i = 0; i < bbt->chip->chip_geom.blks_per_chip; i++) {
234 if (NAND_IS_BLK_BAD(bbt->chip->dev, i, &bad))
238 device_printf(bbt->chip->dev, "Bad block(%d)\n", i);
239 bbt->table[i] = 0x0FFFFFFF;
254 nand_check_bad_block(struct nand_chip *chip, uint32_t block_number)
257 if (!chip || !chip->bbt)
260 if ((chip->bbt->table[block_number] & 0xF0000000) == 0)
267 nand_mark_bad_block(struct nand_chip *chip, uint32_t block_number)
270 chip->bbt->table[block_number] = 0x0FFFFFFF;