nand.c 42.1 KB
Newer Older
oharboe's avatar
oharboe committed
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2007 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
oharboe's avatar
oharboe committed
5
6
 *   Partially based on drivers/mtd/nand_ids.c from Linux.                 *
 *   Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de>               *
oharboe's avatar
oharboe committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "nand.h"
#include "time_support.h"
#include "fileio.h"
30

mifi's avatar
mifi committed
31
32
33
34
35
36
37
static int handle_nand_list_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
oharboe's avatar
oharboe committed
38

mifi's avatar
mifi committed
39
static int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
oharboe's avatar
oharboe committed
40

41
42
static int nand_read_page(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
//static int nand_read_plain(struct nand_device_s *device, uint32_t address, uint8_t *data, uint32_t data_size);
oharboe's avatar
oharboe committed
43

44
static int nand_write_page(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
oharboe's avatar
oharboe committed
45
46
47

/* NAND flash controller
 */
zwelch's avatar
zwelch committed
48
extern nand_flash_controller_t davinci_nand_controller;
oharboe's avatar
oharboe committed
49
extern nand_flash_controller_t lpc3180_nand_controller;
50
extern nand_flash_controller_t orion_nand_controller;
oharboe's avatar
oharboe committed
51
52
53
54
55
56
57
extern nand_flash_controller_t s3c2410_nand_controller;
extern nand_flash_controller_t s3c2412_nand_controller;
extern nand_flash_controller_t s3c2440_nand_controller;
extern nand_flash_controller_t s3c2443_nand_controller;

/* extern nand_flash_controller_t boundary_scan_nand_controller; */

mifi's avatar
mifi committed
58
static nand_flash_controller_t *nand_flash_controllers[] =
oharboe's avatar
oharboe committed
59
{
zwelch's avatar
zwelch committed
60
	&davinci_nand_controller,
oharboe's avatar
oharboe committed
61
	&lpc3180_nand_controller,
62
	&orion_nand_controller,
oharboe's avatar
oharboe committed
63
64
65
66
67
68
69
70
71
	&s3c2410_nand_controller,
	&s3c2412_nand_controller,
	&s3c2440_nand_controller,
	&s3c2443_nand_controller,
/*	&boundary_scan_nand_controller, */
	NULL
};

/* configured NAND devices and NAND Flash command handler */
mifi's avatar
mifi committed
72
static nand_device_t *nand_devices = NULL;
oharboe's avatar
oharboe committed
73
74
75
76
77
78
79
80
81
82
83
84
static command_t *nand_cmd;

/*	Chip ID list
 *
 *	Name, ID code, pagesize, chipsize in MegaByte, eraseblock size,
 *	options
 *
 *	Pagesize; 0, 256, 512
 *	0	get this information from the extended chip ID
 *	256	256 Byte page size
 *	512	512 Byte page size
 */
mifi's avatar
mifi committed
85
static nand_info_t nand_flash_ids[] =
oharboe's avatar
oharboe committed
86
{
87
	/* start "museum" IDs */
oharboe's avatar
oharboe committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	{"NAND 1MiB 5V 8-bit",		0x6e, 256, 1, 0x1000, 0},
	{"NAND 2MiB 5V 8-bit",		0x64, 256, 2, 0x1000, 0},
	{"NAND 4MiB 5V 8-bit",		0x6b, 512, 4, 0x2000, 0},
	{"NAND 1MiB 3,3V 8-bit",	0xe8, 256, 1, 0x1000, 0},
	{"NAND 1MiB 3,3V 8-bit",	0xec, 256, 1, 0x1000, 0},
	{"NAND 2MiB 3,3V 8-bit",	0xea, 256, 2, 0x1000, 0},
	{"NAND 4MiB 3,3V 8-bit",	0xd5, 512, 4, 0x2000, 0},
	{"NAND 4MiB 3,3V 8-bit",	0xe3, 512, 4, 0x2000, 0},
	{"NAND 4MiB 3,3V 8-bit",	0xe5, 512, 4, 0x2000, 0},
	{"NAND 8MiB 3,3V 8-bit",	0xd6, 512, 8, 0x2000, 0},

	{"NAND 8MiB 1,8V 8-bit",	0x39, 512, 8, 0x2000, 0},
	{"NAND 8MiB 3,3V 8-bit",	0xe6, 512, 8, 0x2000, 0},
	{"NAND 8MiB 1,8V 16-bit",	0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16},
	{"NAND 8MiB 3,3V 16-bit",	0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16},
103
	/* end "museum" IDs */
oharboe's avatar
oharboe committed
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

	{"NAND 16MiB 1,8V 8-bit",	0x33, 512, 16, 0x4000, 0},
	{"NAND 16MiB 3,3V 8-bit",	0x73, 512, 16, 0x4000, 0},
	{"NAND 16MiB 1,8V 16-bit",	0x43, 512, 16, 0x4000, NAND_BUSWIDTH_16},
	{"NAND 16MiB 3,3V 16-bit",	0x53, 512, 16, 0x4000, NAND_BUSWIDTH_16},

	{"NAND 32MiB 1,8V 8-bit",	0x35, 512, 32, 0x4000, 0},
	{"NAND 32MiB 3,3V 8-bit",	0x75, 512, 32, 0x4000, 0},
	{"NAND 32MiB 1,8V 16-bit",	0x45, 512, 32, 0x4000, NAND_BUSWIDTH_16},
	{"NAND 32MiB 3,3V 16-bit",	0x55, 512, 32, 0x4000, NAND_BUSWIDTH_16},

	{"NAND 64MiB 1,8V 8-bit",	0x36, 512, 64, 0x4000, 0},
	{"NAND 64MiB 3,3V 8-bit",	0x76, 512, 64, 0x4000, 0},
	{"NAND 64MiB 1,8V 16-bit",	0x46, 512, 64, 0x4000, NAND_BUSWIDTH_16},
	{"NAND 64MiB 3,3V 16-bit",	0x56, 512, 64, 0x4000, NAND_BUSWIDTH_16},

	{"NAND 128MiB 1,8V 8-bit",	0x78, 512, 128, 0x4000, 0},
	{"NAND 128MiB 1,8V 8-bit",	0x39, 512, 128, 0x4000, 0},
	{"NAND 128MiB 3,3V 8-bit",	0x79, 512, 128, 0x4000, 0},
	{"NAND 128MiB 1,8V 16-bit",	0x72, 512, 128, 0x4000, NAND_BUSWIDTH_16},
	{"NAND 128MiB 1,8V 16-bit",	0x49, 512, 128, 0x4000, NAND_BUSWIDTH_16},
	{"NAND 128MiB 3,3V 16-bit",	0x74, 512, 128, 0x4000, NAND_BUSWIDTH_16},
	{"NAND 128MiB 3,3V 16-bit",	0x59, 512, 128, 0x4000, NAND_BUSWIDTH_16},

	{"NAND 256MiB 3,3V 8-bit",	0x71, 512, 256, 0x4000, 0},

	{"NAND 64MiB 1,8V 8-bit",	0xA2, 0,  64, 0, LP_OPTIONS},
	{"NAND 64MiB 3,3V 8-bit",	0xF2, 0,  64, 0, LP_OPTIONS},
	{"NAND 64MiB 1,8V 16-bit",	0xB2, 0,  64, 0, LP_OPTIONS16},
	{"NAND 64MiB 3,3V 16-bit",	0xC2, 0,  64, 0, LP_OPTIONS16},

	{"NAND 128MiB 1,8V 8-bit",	0xA1, 0, 128, 0, LP_OPTIONS},
	{"NAND 128MiB 3,3V 8-bit",	0xF1, 0, 128, 0, LP_OPTIONS},
	{"NAND 128MiB 1,8V 16-bit",	0xB1, 0, 128, 0, LP_OPTIONS16},
	{"NAND 128MiB 3,3V 16-bit",	0xC1, 0, 128, 0, LP_OPTIONS16},

	{"NAND 256MiB 1,8V 8-bit",	0xAA, 0, 256, 0, LP_OPTIONS},
	{"NAND 256MiB 3,3V 8-bit",	0xDA, 0, 256, 0, LP_OPTIONS},
	{"NAND 256MiB 1,8V 16-bit",	0xBA, 0, 256, 0, LP_OPTIONS16},
	{"NAND 256MiB 3,3V 16-bit",	0xCA, 0, 256, 0, LP_OPTIONS16},

	{"NAND 512MiB 1,8V 8-bit",	0xAC, 0, 512, 0, LP_OPTIONS},
	{"NAND 512MiB 3,3V 8-bit",	0xDC, 0, 512, 0, LP_OPTIONS},
	{"NAND 512MiB 1,8V 16-bit",	0xBC, 0, 512, 0, LP_OPTIONS16},
	{"NAND 512MiB 3,3V 16-bit",	0xCC, 0, 512, 0, LP_OPTIONS16},

	{"NAND 1GiB 1,8V 8-bit",	0xA3, 0, 1024, 0, LP_OPTIONS},
	{"NAND 1GiB 3,3V 8-bit",	0xD3, 0, 1024, 0, LP_OPTIONS},
	{"NAND 1GiB 1,8V 16-bit",	0xB3, 0, 1024, 0, LP_OPTIONS16},
	{"NAND 1GiB 3,3V 16-bit",	0xC3, 0, 1024, 0, LP_OPTIONS16},

	{"NAND 2GiB 1,8V 8-bit",	0xA5, 0, 2048, 0, LP_OPTIONS},
	{"NAND 2GiB 3,3V 8-bit",	0xD5, 0, 2048, 0, LP_OPTIONS},
	{"NAND 2GiB 1,8V 16-bit",	0xB5, 0, 2048, 0, LP_OPTIONS16},
	{"NAND 2GiB 3,3V 16-bit",	0xC5, 0, 2048, 0, LP_OPTIONS16},

160
	{NULL, 0, 0, 0, 0, 0 }
oharboe's avatar
oharboe committed
161
162
163
164
};

/* Manufacturer ID list
 */
mifi's avatar
mifi committed
165
static nand_manufacturer_t nand_manuf_ids[] =
oharboe's avatar
oharboe committed
166
167
168
169
170
171
172
173
174
{
	{0x0, "unknown"},
	{NAND_MFR_TOSHIBA, "Toshiba"},
	{NAND_MFR_SAMSUNG, "Samsung"},
	{NAND_MFR_FUJITSU, "Fujitsu"},
	{NAND_MFR_NATIONAL, "National"},
	{NAND_MFR_RENESAS, "Renesas"},
	{NAND_MFR_STMICRO, "ST Micro"},
	{NAND_MFR_HYNIX, "Hynix"},
175
	{NAND_MFR_MICRON, "Micron"},
oharboe's avatar
oharboe committed
176
177
178
	{0x0, NULL},
};

179
180
181
182
/*
 * Define default oob placement schemes for large and small page devices
 */

mifi's avatar
mifi committed
183
184
#if 0
static nand_ecclayout_t nand_oob_8 = {
185
186
187
188
189
190
191
192
	.eccbytes = 3,
	.eccpos = {0, 1, 2},
	.oobfree = {
		{.offset = 3,
		 .length = 2},
		{.offset = 6,
		 .length = 2}}
};
mifi's avatar
mifi committed
193
#endif
194

mifi's avatar
mifi committed
195
static nand_ecclayout_t nand_oob_16 = {
196
197
198
199
200
201
202
	.eccbytes = 6,
	.eccpos = {0, 1, 2, 3, 6, 7},
	.oobfree = {
		{.offset = 8,
		 . length = 8}}
};

mifi's avatar
mifi committed
203
static nand_ecclayout_t nand_oob_64 = {
204
205
206
207
208
209
210
211
212
213
	.eccbytes = 24,
	.eccpos = {
		   40, 41, 42, 43, 44, 45, 46, 47,
		   48, 49, 50, 51, 52, 53, 54, 55,
		   56, 57, 58, 59, 60, 61, 62, 63},
	.oobfree = {
		{.offset = 2,
		 .length = 38}}
};

oharboe's avatar
oharboe committed
214
215
/* nand device <nand_controller> [controller options]
 */
mifi's avatar
mifi committed
216
static int handle_nand_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
oharboe's avatar
oharboe committed
217
218
219
{
	int i;
	int retval;
220

oharboe's avatar
oharboe committed
221
222
	if (argc < 1)
	{
223
		LOG_WARNING("incomplete flash device nand configuration");
oharboe's avatar
oharboe committed
224
225
		return ERROR_FLASH_BANK_INVALID;
	}
226

oharboe's avatar
oharboe committed
227
228
229
	for (i = 0; nand_flash_controllers[i]; i++)
	{
		nand_device_t *p, *c;
230

oharboe's avatar
oharboe committed
231
232
233
		if (strcmp(args[0], nand_flash_controllers[i]->name) == 0)
		{
			/* register flash specific commands */
234
			if ((retval = nand_flash_controllers[i]->register_commands(cmd_ctx)) != ERROR_OK)
oharboe's avatar
oharboe committed
235
			{
236
				LOG_ERROR("couldn't register '%s' commands", args[0]);
237
				return retval;
oharboe's avatar
oharboe committed
238
			}
239

oharboe's avatar
oharboe committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
			c = malloc(sizeof(nand_device_t));

			c->controller = nand_flash_controllers[i];
			c->controller_priv = NULL;
			c->manufacturer = NULL;
			c->device = NULL;
			c->bus_width = 0;
			c->address_cycles = 0;
			c->page_size = 0;
			c->use_raw = 0;
			c->next = NULL;

			if ((retval = nand_flash_controllers[i]->nand_device_command(cmd_ctx, cmd, args, argc, c)) != ERROR_OK)
			{
254
				LOG_ERROR("'%s' driver rejected nand flash", c->controller->name);
oharboe's avatar
oharboe committed
255
256
257
				free(c);
				return ERROR_OK;
			}
258

oharboe's avatar
oharboe committed
259
260
261
262
263
264
265
266
267
268
269
270
			/* put NAND device in linked list */
			if (nand_devices)
			{
				/* find last flash device */
				for (p = nand_devices; p && p->next; p = p->next);
				if (p)
					p->next = c;
			}
			else
			{
				nand_devices = c;
			}
271

oharboe's avatar
oharboe committed
272
273
274
275
276
277
278
			return ERROR_OK;
		}
	}

	/* no valid NAND controller was found (i.e. the configuration option,
	 * didn't match one of the compiled-in controllers)
	 */
279
280
	LOG_ERROR("No valid NAND flash controller found (%s)", args[0]);
	LOG_ERROR("compiled-in NAND flash controllers:");
oharboe's avatar
oharboe committed
281
282
	for (i = 0; nand_flash_controllers[i]; i++)
	{
283
		LOG_ERROR("%i: %s", i, nand_flash_controllers[i]->name);
oharboe's avatar
oharboe committed
284
	}
285

oharboe's avatar
oharboe committed
286
287
288
289
290
291
	return ERROR_OK;
}

int nand_register_commands(struct command_context_s *cmd_ctx)
{
	nand_cmd = register_command(cmd_ctx, NULL, "nand", NULL, COMMAND_ANY, "NAND specific commands");
292

oharboe's avatar
oharboe committed
293
	register_command(cmd_ctx, nand_cmd, "device", handle_nand_device_command, COMMAND_CONFIG, NULL);
294

oharboe's avatar
oharboe committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	return ERROR_OK;
}

int nand_init(struct command_context_s *cmd_ctx)
{
	if (nand_devices)
	{
		register_command(cmd_ctx, nand_cmd, "list", handle_nand_list_command, COMMAND_EXEC,
						 "list configured NAND flash devices");
		register_command(cmd_ctx, nand_cmd, "info", handle_nand_info_command, COMMAND_EXEC,
						 "print info about NAND flash device <num>");
		register_command(cmd_ctx, nand_cmd, "probe", handle_nand_probe_command, COMMAND_EXEC,
						 "identify NAND flash device <num>");
		register_command(cmd_ctx, nand_cmd, "check_bad_blocks", handle_nand_check_bad_blocks_command, COMMAND_EXEC,
zwelch's avatar
zwelch committed
309
						 "check NAND flash device <num> for bad blocks [<offset> <length>]");
oharboe's avatar
oharboe committed
310
		register_command(cmd_ctx, nand_cmd, "erase", handle_nand_erase_command, COMMAND_EXEC,
zwelch's avatar
zwelch committed
311
						 "erase blocks on NAND flash device <num> <offset> <length>");
oharboe's avatar
oharboe committed
312
		register_command(cmd_ctx, nand_cmd, "dump", handle_nand_dump_command, COMMAND_EXEC,
313
						 "dump from NAND flash device <num> <filename> "
zwelch's avatar
zwelch committed
314
						 "<offset> <length> [oob_raw | oob_only]");
oharboe's avatar
oharboe committed
315
		register_command(cmd_ctx, nand_cmd, "write", handle_nand_write_command, COMMAND_EXEC,
zwelch's avatar
zwelch committed
316
						 "write to NAND flash device <num> <filename> <offset> [oob_raw | oob_only | oob_softecc | oob_softecc_kw]");
oharboe's avatar
oharboe committed
317
318
319
		register_command(cmd_ctx, nand_cmd, "raw_access", handle_nand_raw_access_command, COMMAND_EXEC,
						 "raw access to NAND flash device <num> ['enable'|'disable']");
	}
320

oharboe's avatar
oharboe committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
	return ERROR_OK;
}

nand_device_t *get_nand_device_by_num(int num)
{
	nand_device_t *p;
	int i = 0;

	for (p = nand_devices; p; p = p->next)
	{
		if (i++ == num)
		{
			return p;
		}
	}
336

oharboe's avatar
oharboe committed
337
338
339
	return NULL;
}

mifi's avatar
mifi committed
340
static int nand_build_bbt(struct nand_device_s *device, int first, int last)
oharboe's avatar
oharboe committed
341
{
342
	uint32_t page = 0x0;
oharboe's avatar
oharboe committed
343
	int i;
344
	uint8_t oob[6];
345

oharboe's avatar
oharboe committed
346
347
	if ((first < 0) || (first >= device->num_blocks))
		first = 0;
348

oharboe's avatar
oharboe committed
349
350
	if ((last >= device->num_blocks) || (last == -1))
		last = device->num_blocks - 1;
351

oharboe's avatar
oharboe committed
352
353
354
	for (i = first; i < last; i++)
	{
		nand_read_page(device, page, NULL, 0, oob, 6);
355

oharboe's avatar
oharboe committed
356
357
358
359
		if (((device->device->options & NAND_BUSWIDTH_16) && ((oob[0] & oob[1]) != 0xff))
			|| (((device->page_size == 512) && (oob[5] != 0xff)) ||
				((device->page_size == 2048) && (oob[0] != 0xff))))
		{
zwelch's avatar
zwelch committed
360
			LOG_WARNING("bad block: %i", i);
oharboe's avatar
oharboe committed
361
362
363
364
365
366
			device->blocks[i].is_bad = 1;
		}
		else
		{
			device->blocks[i].is_bad = 0;
		}
367

oharboe's avatar
oharboe committed
368
369
		page += (device->erase_size / device->page_size);
	}
370

oharboe's avatar
oharboe committed
371
372
373
	return ERROR_OK;
}

374
int nand_read_status(struct nand_device_s *device, uint8_t *status)
oharboe's avatar
oharboe committed
375
376
377
{
	if (!device->device)
		return ERROR_NAND_DEVICE_NOT_PROBED;
378

oharboe's avatar
oharboe committed
379
380
	/* Send read status command */
	device->controller->command(device, NAND_CMD_STATUS);
381

382
	alive_sleep(1);
383

oharboe's avatar
oharboe committed
384
385
386
	/* read status */
	if (device->device->options & NAND_BUSWIDTH_16)
	{
zwelch's avatar
zwelch committed
387
		uint16_t data;
oharboe's avatar
oharboe committed
388
389
390
391
392
393
394
		device->controller->read_data(device, &data);
		*status = data & 0xff;
	}
	else
	{
		device->controller->read_data(device, status);
	}
395

oharboe's avatar
oharboe committed
396
397
398
	return ERROR_OK;
}

mifi's avatar
mifi committed
399
static int nand_poll_ready(struct nand_device_s *device, int timeout)
400
{
401
	uint8_t status;
402
403
404
405

	device->controller->command(device, NAND_CMD_STATUS);
	do {
		if (device->device->options & NAND_BUSWIDTH_16) {
zwelch's avatar
zwelch committed
406
			uint16_t data;
407
408
409
410
411
412
413
414
415
416
417
418
419
			device->controller->read_data(device, &data);
			status = data & 0xff;
		} else {
			device->controller->read_data(device, &status);
		}
		if (status & NAND_STATUS_READY)
			break;
		alive_sleep(1);
	} while (timeout--);

	return (status & NAND_STATUS_READY) != 0;
}

oharboe's avatar
oharboe committed
420
421
int nand_probe(struct nand_device_s *device)
{
422
423
	uint8_t manufacturer_id, device_id;
	uint8_t id_buff[6];
oharboe's avatar
oharboe committed
424
425
426
427
428
429
	int retval;
	int i;

	/* clear device data */
	device->device = NULL;
	device->manufacturer = NULL;
430

oharboe's avatar
oharboe committed
431
432
433
434
435
	/* clear device parameters */
	device->bus_width = 0;
	device->address_cycles = 0;
	device->page_size = 0;
	device->erase_size = 0;
436

oharboe's avatar
oharboe committed
437
438
439
440
441
442
	/* initialize controller (device parameters are zero, use controller default) */
	if ((retval = device->controller->init(device) != ERROR_OK))
	{
		switch (retval)
		{
			case ERROR_NAND_OPERATION_FAILED:
443
				LOG_DEBUG("controller initialization failed");
oharboe's avatar
oharboe committed
444
445
				return ERROR_NAND_OPERATION_FAILED;
			case ERROR_NAND_OPERATION_NOT_SUPPORTED:
446
				LOG_ERROR("BUG: controller reported that it doesn't support default parameters");
oharboe's avatar
oharboe committed
447
448
				return ERROR_NAND_OPERATION_FAILED;
			default:
449
				LOG_ERROR("BUG: unknown controller initialization failure");
oharboe's avatar
oharboe committed
450
451
452
				return ERROR_NAND_OPERATION_FAILED;
		}
	}
453

oharboe's avatar
oharboe committed
454
455
456
457
458
	device->controller->command(device, NAND_CMD_RESET);
	device->controller->reset(device);

	device->controller->command(device, NAND_CMD_READID);
	device->controller->address(device, 0x0);
459

oharboe's avatar
oharboe committed
460
461
462
463
464
465
466
	if (device->bus_width == 8)
	{
		device->controller->read_data(device, &manufacturer_id);
		device->controller->read_data(device, &device_id);
	}
	else
	{
zwelch's avatar
zwelch committed
467
		uint16_t data_buf;
oharboe's avatar
oharboe committed
468
469
470
471
472
		device->controller->read_data(device, &data_buf);
		manufacturer_id = data_buf & 0xff;
		device->controller->read_data(device, &data_buf);
		device_id = data_buf & 0xff;
	}
473

oharboe's avatar
oharboe committed
474
475
476
477
478
479
480
481
	for (i = 0; nand_flash_ids[i].name; i++)
	{
		if (nand_flash_ids[i].id == device_id)
		{
			device->device = &nand_flash_ids[i];
			break;
		}
	}
482

oharboe's avatar
oharboe committed
483
484
485
486
487
488
489
490
	for (i = 0; nand_manuf_ids[i].name; i++)
	{
		if (nand_manuf_ids[i].id == manufacturer_id)
		{
			device->manufacturer = &nand_manuf_ids[i];
			break;
		}
	}
491

oharboe's avatar
oharboe committed
492
493
494
495
496
	if (!device->manufacturer)
	{
		device->manufacturer = &nand_manuf_ids[0];
		device->manufacturer->id = manufacturer_id;
	}
497

oharboe's avatar
oharboe committed
498
499
	if (!device->device)
	{
500
		LOG_ERROR("unknown NAND flash device found, manufacturer id: 0x%2.2x device id: 0x%2.2x",
oharboe's avatar
oharboe committed
501
502
503
			manufacturer_id, device_id);
		return ERROR_NAND_OPERATION_FAILED;
	}
504

505
	LOG_DEBUG("found %s (%s)", device->device->name, device->manufacturer->name);
506

oharboe's avatar
oharboe committed
507
	/* initialize device parameters */
508
509

	/* bus width */
oharboe's avatar
oharboe committed
510
511
512
513
514
515
516
517
518
519
520
	if (device->device->options & NAND_BUSWIDTH_16)
		device->bus_width = 16;
	else
		device->bus_width = 8;

	/* Do we need extended device probe information? */
	if (device->device->page_size == 0 ||
	    device->device->erase_size == 0)
	{
		if (device->bus_width == 8)
		{
zwelch's avatar
zwelch committed
521
522
523
			device->controller->read_data(device, id_buff + 3);
			device->controller->read_data(device, id_buff + 4);
			device->controller->read_data(device, id_buff + 5);
oharboe's avatar
oharboe committed
524
525
526
		}
		else
		{
zwelch's avatar
zwelch committed
527
			uint16_t data_buf;
oharboe's avatar
oharboe committed
528
529
530
531
532
533
534
535
536
537
538

			device->controller->read_data(device, &data_buf);
			id_buff[3] = data_buf;

			device->controller->read_data(device, &data_buf);
			id_buff[4] = data_buf;

			device->controller->read_data(device, &data_buf);
			id_buff[5] = data_buf >> 8;
		}
	}
539

oharboe's avatar
oharboe committed
540
541
542
543
544
545
546
	/* page size */
	if (device->device->page_size == 0)
	{
		device->page_size = 1 << (10 + (id_buff[4] & 3));
	}
	else if (device->device->page_size == 256)
	{
547
		LOG_ERROR("NAND flashes with 256 byte pagesize are not supported");
oharboe's avatar
oharboe committed
548
549
550
551
552
553
		return ERROR_NAND_OPERATION_FAILED;
	}
	else
	{
		device->page_size = device->device->page_size;
	}
554

oharboe's avatar
oharboe committed
555
556
557
558
559
560
561
562
563
564
	/* number of address cycles */
	if (device->page_size <= 512)
	{
		/* small page devices */
		if (device->device->chip_size <= 32)
			device->address_cycles = 3;
		else if (device->device->chip_size <= 8*1024)
			device->address_cycles = 4;
		else
		{
565
			LOG_ERROR("BUG: small page NAND device with more than 8 GiB encountered");
oharboe's avatar
oharboe committed
566
567
568
569
570
571
572
573
574
575
576
577
			device->address_cycles = 5;
		}
	}
	else
	{
		/* large page devices */
		if (device->device->chip_size <= 128)
			device->address_cycles = 4;
		else if (device->device->chip_size <= 32*1024)
			device->address_cycles = 5;
		else
		{
578
			LOG_ERROR("BUG: large page NAND device with more than 32 GiB encountered");
oharboe's avatar
oharboe committed
579
580
581
			device->address_cycles = 6;
		}
	}
582

oharboe's avatar
oharboe committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
	/* erase size */
	if (device->device->erase_size == 0)
	{
		switch ((id_buff[4] >> 4) & 3) {
		case 0:
			device->erase_size = 64 << 10;
			break;
		case 1:
			device->erase_size = 128 << 10;
			break;
		case 2:
			device->erase_size = 256 << 10;
			break;
		case 3:
			device->erase_size =512 << 10;
			break;
		}
	}
	else
	{
		device->erase_size = device->device->erase_size;
	}
605

oharboe's avatar
oharboe committed
606
607
608
609
610
611
	/* initialize controller, but leave parameters at the controllers default */
	if ((retval = device->controller->init(device) != ERROR_OK))
	{
		switch (retval)
		{
			case ERROR_NAND_OPERATION_FAILED:
612
				LOG_DEBUG("controller initialization failed");
oharboe's avatar
oharboe committed
613
614
				return ERROR_NAND_OPERATION_FAILED;
			case ERROR_NAND_OPERATION_NOT_SUPPORTED:
615
				LOG_ERROR("controller doesn't support requested parameters (buswidth: %i, address cycles: %i, page size: %i)",
oharboe's avatar
oharboe committed
616
617
618
					device->bus_width, device->address_cycles, device->page_size);
				return ERROR_NAND_OPERATION_FAILED;
			default:
619
				LOG_ERROR("BUG: unknown controller initialization failure");
oharboe's avatar
oharboe committed
620
621
622
				return ERROR_NAND_OPERATION_FAILED;
		}
	}
623

oharboe's avatar
oharboe committed
624
625
	device->num_blocks = (device->device->chip_size * 1024) / (device->erase_size / 1024);
	device->blocks = malloc(sizeof(nand_block_t) * device->num_blocks);
626

oharboe's avatar
oharboe committed
627
628
629
630
631
632
633
	for (i = 0; i < device->num_blocks; i++)
	{
		device->blocks[i].size = device->erase_size;
		device->blocks[i].offset = i * device->erase_size;
		device->blocks[i].is_erased = -1;
		device->blocks[i].is_bad = -1;
	}
634

oharboe's avatar
oharboe committed
635
636
637
638
639
640
	return ERROR_OK;
}

int nand_erase(struct nand_device_s *device, int first_block, int last_block)
{
	int i;
641
	uint32_t page;
642
	uint8_t status;
oharboe's avatar
oharboe committed
643
	int retval;
644

oharboe's avatar
oharboe committed
645
646
	if (!device->device)
		return ERROR_NAND_DEVICE_NOT_PROBED;
647

oharboe's avatar
oharboe committed
648
649
	if ((first_block < 0) || (last_block > device->num_blocks))
		return ERROR_INVALID_ARGUMENTS;
650

oharboe's avatar
oharboe committed
651
652
653
654
655
656
657
658
659
	/* make sure we know if a block is bad before erasing it */
	for (i = first_block; i <= last_block; i++)
	{
		if (device->blocks[i].is_bad == -1)
		{
			nand_build_bbt(device, i, last_block);
			break;
		}
	}
660

oharboe's avatar
oharboe committed
661
662
663
664
	for (i = first_block; i <= last_block; i++)
	{
		/* Send erase setup command */
		device->controller->command(device, NAND_CMD_ERASE1);
665

oharboe's avatar
oharboe committed
666
		page = i * (device->erase_size / device->page_size);
667

oharboe's avatar
oharboe committed
668
669
670
671
672
673
		/* Send page address */
		if (device->page_size <= 512)
		{
			/* row */
			device->controller->address(device, page & 0xff);
			device->controller->address(device, (page >> 8) & 0xff);
674

oharboe's avatar
oharboe committed
675
676
677
			/* 3rd cycle only on devices with more than 32 MiB */
			if (device->address_cycles >= 4)
				device->controller->address(device, (page >> 16) & 0xff);
678

oharboe's avatar
oharboe committed
679
680
681
682
683
684
685
686
687
			/* 4th cycle only on devices with more than 8 GiB */
			if (device->address_cycles >= 5)
				device->controller->address(device, (page >> 24) & 0xff);
		}
		else
		{
			/* row */
			device->controller->address(device, page & 0xff);
			device->controller->address(device, (page >> 8) & 0xff);
688

oharboe's avatar
oharboe committed
689
690
691
692
			/* 3rd cycle only on devices with more than 128 MiB */
			if (device->address_cycles >= 5)
				device->controller->address(device, (page >> 16) & 0xff);
		}
693

oharboe's avatar
oharboe committed
694
695
		/* Send erase confirm command */
		device->controller->command(device, NAND_CMD_ERASE2);
696
697
698
699
700

		retval = device->controller->nand_ready ?
				device->controller->nand_ready(device, 1000) :
				nand_poll_ready(device, 1000);
		if (!retval) {
701
			LOG_ERROR("timeout waiting for NAND flash block erase to complete");
oharboe's avatar
oharboe committed
702
703
			return ERROR_NAND_OPERATION_TIMEOUT;
		}
704

oharboe's avatar
oharboe committed
705
706
		if ((retval = nand_read_status(device, &status)) != ERROR_OK)
		{
707
			LOG_ERROR("couldn't read status");
oharboe's avatar
oharboe committed
708
709
			return ERROR_NAND_OPERATION_FAILED;
		}
710

oharboe's avatar
oharboe committed
711
712
		if (status & 0x1)
		{
713
			LOG_ERROR("erase operation didn't pass, status: 0x%2.2x", status);
oharboe's avatar
oharboe committed
714
715
			return ERROR_NAND_OPERATION_FAILED;
		}
716
717

		device->blocks[i].is_erased = 1;
oharboe's avatar
oharboe committed
718
	}
719

oharboe's avatar
oharboe committed
720
721
722
	return ERROR_OK;
}

mifi's avatar
mifi committed
723
#if 0
724
static int nand_read_plain(struct nand_device_s *device, uint32_t address, uint8_t *data, uint32_t data_size)
oharboe's avatar
oharboe committed
725
{
726
	uint8_t *page;
727

oharboe's avatar
oharboe committed
728
729
	if (!device->device)
		return ERROR_NAND_DEVICE_NOT_PROBED;
730

oharboe's avatar
oharboe committed
731
732
	if (address % device->page_size)
	{
733
		LOG_ERROR("reads need to be page aligned");
oharboe's avatar
oharboe committed
734
735
		return ERROR_NAND_OPERATION_FAILED;
	}
736

oharboe's avatar
oharboe committed
737
	page = malloc(device->page_size);
738

oharboe's avatar
oharboe committed
739
740
	while (data_size > 0 )
	{
741
742
		uint32_t thisrun_size = (data_size > device->page_size) ? device->page_size : data_size;
		uint32_t page_address;
743
744


oharboe's avatar
oharboe committed
745
		page_address = address / device->page_size;
746

oharboe's avatar
oharboe committed
747
748
749
		nand_read_page(device, page_address, page, device->page_size, NULL, 0);

		memcpy(data, page, thisrun_size);
750

oharboe's avatar
oharboe committed
751
752
753
754
		address += thisrun_size;
		data += thisrun_size;
		data_size -= thisrun_size;
	}
755

oharboe's avatar
oharboe committed
756
	free(page);
757

oharboe's avatar
oharboe committed
758
759
760
	return ERROR_OK;
}

761
static int nand_write_plain(struct nand_device_s *device, uint32_t address, uint8_t *data, uint32_t data_size)
oharboe's avatar
oharboe committed
762
{
763
	uint8_t *page;
764

oharboe's avatar
oharboe committed
765
766
	if (!device->device)
		return ERROR_NAND_DEVICE_NOT_PROBED;
767

oharboe's avatar
oharboe committed
768
769
	if (address % device->page_size)
	{
770
		LOG_ERROR("writes need to be page aligned");
oharboe's avatar
oharboe committed
771
772
		return ERROR_NAND_OPERATION_FAILED;
	}
773

oharboe's avatar
oharboe committed
774
	page = malloc(device->page_size);
775

oharboe's avatar
oharboe committed
776
777
	while (data_size > 0 )
	{
778
779
		uint32_t thisrun_size = (data_size > device->page_size) ? device->page_size : data_size;
		uint32_t page_address;
780

oharboe's avatar
oharboe committed
781
782
		memset(page, 0xff, device->page_size);
		memcpy(page, data, thisrun_size);
783

oharboe's avatar
oharboe committed
784
		page_address = address / device->page_size;
785

oharboe's avatar
oharboe committed
786
		nand_write_page(device, page_address, page, device->page_size, NULL, 0);
787

oharboe's avatar
oharboe committed
788
789
790
791
		address += thisrun_size;
		data += thisrun_size;
		data_size -= thisrun_size;
	}
792

oharboe's avatar
oharboe committed
793
	free(page);
794

oharboe's avatar
oharboe committed
795
796
	return ERROR_OK;
}
mifi's avatar
mifi committed
797
#endif
oharboe's avatar
oharboe committed
798

799
int nand_write_page(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
oharboe's avatar
oharboe committed
800
{
801
	uint32_t block;
802

oharboe's avatar
oharboe committed
803
804
	if (!device->device)
		return ERROR_NAND_DEVICE_NOT_PROBED;
805

806
807
808
809
	block = page / (device->erase_size / device->page_size);
	if (device->blocks[block].is_erased == 1)
		device->blocks[block].is_erased = 0;

oharboe's avatar
oharboe committed
810
811
812
813
814
815
	if (device->use_raw || device->controller->write_page == NULL)
		return nand_write_page_raw(device, page, data, data_size, oob, oob_size);
	else
		return device->controller->write_page(device, page, data, data_size, oob, oob_size);
}

816
static int nand_read_page(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
oharboe's avatar
oharboe committed
817
818
819
{
	if (!device->device)
		return ERROR_NAND_DEVICE_NOT_PROBED;
820

oharboe's avatar
oharboe committed
821
822
823
824
825
826
	if (device->use_raw || device->controller->read_page == NULL)
		return nand_read_page_raw(device, page, data, data_size, oob, oob_size);
	else
		return device->controller->read_page(device, page, data, data_size, oob, oob_size);
}

827
int nand_read_page_raw(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
oharboe's avatar
oharboe committed
828
{
829
	uint32_t i;
830

oharboe's avatar
oharboe committed
831
832
833
834
835
836
837
838
839
840
	if (!device->device)
		return ERROR_NAND_DEVICE_NOT_PROBED;

	if (device->page_size <= 512)
	{
		/* small page device */
		if (data)
			device->controller->command(device, NAND_CMD_READ0);
		else
			device->controller->command(device, NAND_CMD_READOOB);
841

oharboe's avatar
oharboe committed
842
843
		/* column (always 0, we start at the beginning of a page/OOB area) */
		device->controller->address(device, 0x0);
844

oharboe's avatar
oharboe committed
845
846
847
		/* row */
		device->controller->address(device, page & 0xff);
		device->controller->address(device, (page >> 8) & 0xff);
848

oharboe's avatar
oharboe committed
849
850
851
852
853
854
855
856
857
858
859
860
		/* 4th cycle only on devices with more than 32 MiB */
		if (device->address_cycles >= 4)
			device->controller->address(device, (page >> 16) & 0xff);

		/* 5th cycle only on devices with more than 8 GiB */
		if (device->address_cycles >= 5)
			device->controller->address(device, (page >> 24) & 0xff);
	}
	else
	{
		/* large page device */
		device->controller->command(device, NAND_CMD_READ0);
861

oharboe's avatar
oharboe committed
862
863
864
865
		/* column (0 when we start at the beginning of a page,
		 * or 2048 for the beginning of OOB area)
		 */
		device->controller->address(device, 0x0);
866
867
868
869
		if (data)
			device->controller->address(device, 0x0);
		else
			device->controller->address(device, 0x8);
870

oharboe's avatar
oharboe committed
871
872
873
874
875
876
877
878
879
880
881
		/* row */
		device->controller->address(device, page & 0xff);
		device->controller->address(device, (page >> 8) & 0xff);

		/* 5th cycle only on devices with more than 128 MiB */
		if (device->address_cycles >= 5)
			device->controller->address(device, (page >> 16) & 0xff);

		/* large page devices need a start command */
		device->controller->command(device, NAND_CMD_READSTART);
	}
882

883
884
885
886
887
888
	if (device->controller->nand_ready) {
		if (!device->controller->nand_ready(device, 100))
			return ERROR_NAND_OPERATION_TIMEOUT;
	} else {
		alive_sleep(1);
	}
889

oharboe's avatar
oharboe committed
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
	if (data)
	{
		if (device->controller->read_block_data != NULL)
			(device->controller->read_block_data)(device, data, data_size);
		else
		{
			for (i = 0; i < data_size;)
			{
				if (device->device->options & NAND_BUSWIDTH_16)
				{
					device->controller->read_data(device, data);
					data += 2;
					i += 2;
				}
				else
				{
					device->controller->read_data(device, data);
					data += 1;
					i += 1;
				}
			}
		}
	}
913

oharboe's avatar
oharboe committed
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
	if (oob)
	{
		if (device->controller->read_block_data != NULL)
			(device->controller->read_block_data)(device, oob, oob_size);
		else
		{
			for (i = 0; i < oob_size;)
			{
				if (device->device->options & NAND_BUSWIDTH_16)
				{
					device->controller->read_data(device, oob);
					oob += 2;
					i += 2;
				}
				else
				{
					device->controller->read_data(device, oob);
					oob += 1;
					i += 1;
				}
			}
		}
	}
937
938

	return ERROR_OK;
oharboe's avatar
oharboe committed
939
940
}

941
int nand_write_page_raw(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
oharboe's avatar
oharboe committed
942
{
943
	uint32_t i;
oharboe's avatar
oharboe committed
944
	int retval;
945
	uint8_t status;
946

oharboe's avatar
oharboe committed
947
948
949
950
	if (!device->device)
		return ERROR_NAND_DEVICE_NOT_PROBED;

	device->controller->command(device, NAND_CMD_SEQIN);
951

oharboe's avatar
oharboe committed
952
953
954
955
	if (device->page_size <= 512)
	{
		/* column (always 0, we start at the beginning of a page/OOB area) */
		device->controller->address(device, 0x0);
956

oharboe's avatar
oharboe committed
957
958
959
		/* row */
		device->controller->address(device, page & 0xff);
		device->controller->address(device, (page >> 8) & 0xff);
960

oharboe's avatar
oharboe committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
		/* 4th cycle only on devices with more than 32 MiB */
		if (device->address_cycles >= 4)
			device->controller->address(device, (page >> 16) & 0xff);

		/* 5th cycle only on devices with more than 8 GiB */
		if (device->address_cycles >= 5)
			device->controller->address(device, (page >> 24) & 0xff);
	}
	else
	{
		/* column (0 when we start at the beginning of a page,
		 * or 2048 for the beginning of OOB area)
		 */
		device->controller->address(device, 0x0);
975
976
977
978
		if (data)
			device->controller->address(device, 0x0);
		else
			device->controller->address(device, 0x8);
979

oharboe's avatar
oharboe committed
980
981
982
983
984
985
986
987
		/* row */
		device->controller->address(device, page & 0xff);
		device->controller->address(device, (page >> 8) & 0xff);

		/* 5th cycle only on devices with more than 128 MiB */
		if (device->address_cycles >= 5)
			device->controller->address(device, (page >> 16) & 0xff);
	}
988

oharboe's avatar
oharboe committed
989
990
991
992
993
994
995
996
997
998
	if (data)
	{
		if (device->controller->write_block_data != NULL)
			(device->controller->write_block_data)(device, data, data_size);
		else
		{
			for (i = 0; i < data_size;)
			{
				if (device->device->options & NAND_BUSWIDTH_16)
				{
zwelch's avatar
zwelch committed
999
					uint16_t data_buf = le_to_h_u16(data);
oharboe's avatar
oharboe committed
1000
					device->controller->write_data(device, data_buf);
For faster browsing, not all history is shown. View entire blame