summaryrefslogtreecommitdiffstats
path: root/sys/dev/nand/nand_bbt.c
blob: d3f163af3b1716499455eb4732990de06e8a3ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*-
 * Copyright (c) 2009-2012 Semihalf
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/cdefs.h>

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/bus.h>

#include <dev/nand/nand.h>

#include "nand_if.h"

#define BBT_PRIMARY_PATTERN	0x01020304
#define BBT_SECONDARY_PATTERN	0x05060708

enum bbt_place {
	BBT_NONE,
	BBT_PRIMARY,
	BBT_SECONDARY
};

struct nand_bbt {
	struct nand_chip	*chip;
	uint32_t		primary_map;
	uint32_t		secondary_map;
	enum bbt_place		active;
	struct bbt_header	*hdr;
	uint32_t		tab_len;
	uint32_t		*table;
};

struct bbt_header {
	uint32_t pattern;
	int32_t seq_nr;
};

static int nand_bbt_save(struct nand_bbt *);
static int nand_bbt_load_hdr(struct nand_bbt *, struct bbt_header *, int8_t);
static int nand_bbt_load_table(struct nand_bbt *);
static int nand_bbt_prescan(struct nand_bbt *);

int
nand_init_bbt(struct nand_chip *chip)
{
	struct chip_geom *cg;
	struct nand_bbt *bbt;
	int err;

	cg = &chip->chip_geom;

	bbt = malloc(sizeof(struct nand_bbt), M_NAND, M_ZERO | M_WAITOK);
	if (!bbt) {
		device_printf(chip->dev,
		    "Cannot allocate memory for bad block struct");
		return (ENOMEM);
	}

	bbt->chip = chip;
	bbt->active = BBT_NONE;
	bbt->primary_map = cg->chip_size - cg->block_size;
	bbt->secondary_map = cg->chip_size - 2 * cg->block_size;
	bbt->tab_len = cg->blks_per_chip * sizeof(uint32_t);
	bbt->hdr = malloc(sizeof(struct bbt_header) + bbt->tab_len, M_NAND,
	    M_WAITOK);
	if (!bbt->hdr) {
		device_printf(chip->dev, "Cannot allocate %d bytes for BB "
		    "Table", bbt->tab_len);
		free(bbt, M_NAND);
		return (ENOMEM);
	}
	bbt->hdr->seq_nr = 0;
	bbt->table = (uint32_t *)((uint8_t *)bbt->hdr +
	    sizeof(struct bbt_header));

	err = nand_bbt_load_table(bbt);
	if (err) {
		free(bbt->table, M_NAND);
		free(bbt, M_NAND);
		return (err);
	}

	chip->bbt = bbt;
	if (bbt->active == BBT_NONE) {
		bbt->active = BBT_PRIMARY;
		memset(bbt->table, 0xff, bbt->tab_len);
		nand_bbt_prescan(bbt);
		nand_bbt_save(bbt);
	} else
		device_printf(chip->dev, "Found BBT table for chip\n");

	return (0);
}

void
nand_destroy_bbt(struct nand_chip *chip)
{

	if (chip->bbt) {
		nand_bbt_save(chip->bbt);

		free(chip->bbt->hdr, M_NAND);
		free(chip->bbt, M_NAND);
		chip->bbt = NULL;
	}
}

int
nand_update_bbt(struct nand_chip *chip)
{

	nand_bbt_save(chip->bbt);

	return (0);
}

static int
nand_bbt_save(struct nand_bbt *bbt)
{
	enum bbt_place next;
	uint32_t addr;
	int32_t err;

	if (bbt->active == BBT_PRIMARY) {
		addr = bbt->secondary_map;
		bbt->hdr->pattern = BBT_SECONDARY_PATTERN;
		next = BBT_SECONDARY;
	} else {
		addr = bbt->primary_map;
		bbt->hdr->pattern = BBT_PRIMARY_PATTERN;
		next = BBT_PRIMARY;
	}

	err = nand_erase_blocks(bbt->chip, addr,
	    bbt->chip->chip_geom.block_size);
	if (err)
		return (err);

	bbt->hdr->seq_nr++;

	err = nand_prog_pages_raw(bbt->chip, addr, bbt->hdr,
	    bbt->tab_len + sizeof(struct bbt_header));
	if (err)
		return (err);

	bbt->active = next;
	return (0);
}

static int
nand_bbt_load_hdr(struct nand_bbt *bbt, struct bbt_header *hdr, int8_t primary)
{
	uint32_t addr;

	if (primary)
		addr = bbt->primary_map;
	else
		addr = bbt->secondary_map;

	return (nand_read_pages_raw(bbt->chip, addr, hdr,
	    sizeof(struct bbt_header)));
}

static int
nand_bbt_load_table(struct nand_bbt *bbt)
{
	struct bbt_header hdr1, hdr2;
	uint32_t address = 0;
	int err = 0;

	bzero(&hdr1, sizeof(hdr1));
	bzero(&hdr2, sizeof(hdr2));

	nand_bbt_load_hdr(bbt, &hdr1, 1);
	if (hdr1.pattern == BBT_PRIMARY_PATTERN) {
		bbt->active = BBT_PRIMARY;
		address = bbt->primary_map;
	} else
		bzero(&hdr1, sizeof(hdr1));


	nand_bbt_load_hdr(bbt, &hdr2, 0);
	if ((hdr2.pattern == BBT_SECONDARY_PATTERN) &&
	    (hdr2.seq_nr > hdr1.seq_nr)) {
		bbt->active = BBT_SECONDARY;
		address = bbt->secondary_map;
	} else
		bzero(&hdr2, sizeof(hdr2));

	if (bbt->active != BBT_NONE)
		err = nand_read_pages_raw(bbt->chip, address, bbt->hdr,
		    bbt->tab_len + sizeof(struct bbt_header));

	return (err);
}

static int
nand_bbt_prescan(struct nand_bbt *bbt)
{
	int32_t i;
	uint8_t bad;
	bool printed_hash = 0;

	device_printf(bbt->chip->dev, "No BBT found. Prescan chip...\n");
	for (i = 0; i < bbt->chip->chip_geom.blks_per_chip; i++) {
		if (NAND_IS_BLK_BAD(bbt->chip->dev, i, &bad))
			return (ENXIO);

		if (bad) {
			device_printf(bbt->chip->dev, "Bad block(%d)\n", i);
			bbt->table[i] = 0x0FFFFFFF;
		}
		if (!(i % 100)) {
			printf("#");
			printed_hash = 1;
		}
	}

	if (printed_hash)
		printf("\n");

	return (0);
}

int
nand_check_bad_block(struct nand_chip *chip, uint32_t block_number)
{

	if (!chip || !chip->bbt)
		return (0);

	if ((chip->bbt->table[block_number] & 0xF0000000) == 0)
		return (1);

	return (0);
}

int
nand_mark_bad_block(struct nand_chip *chip, uint32_t block_number)
{

	chip->bbt->table[block_number] = 0x0FFFFFFF;

	return (0);
}
OpenPOWER on IntegriCloud