nand_base.c 72.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 *  drivers/mtd/nand.c
 *
 *  Overview:
 *   This is the generic MTD driver for NAND flash devices. It should be
 *   capable of working with almost all NAND chips currently available.
 *   Basic support for AG-AND chips is provided.
8
 *
L
Linus Torvalds 已提交
9 10
 *	Additional technical information is available on
 *	http://www.linux-mtd.infradead.org/tech/nand.html
11
 *
L
Linus Torvalds 已提交
12
 *  Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13
 *		  2002 Thomas Gleixner (tglx@linutronix.de)
L
Linus Torvalds 已提交
14
 *
15
 *  02-08-2004  tglx: support for strange chips, which cannot auto increment
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23
 *		pages on read / read_oob
 *
 *  03-17-2004  tglx: Check ready before auto increment check. Simon Bayes
 *		pointed this out, as he marked an auto increment capable chip
 *		as NOAUTOINCR in the board driver.
 *		Make reads over block boundaries work too
 *
 *  04-14-2004	tglx: first working version for 2k page size chips
24
 *
L
Linus Torvalds 已提交
25 26 27
 *  05-19-2004  tglx: Basic support for Renesas AG-AND chips
 *
 *  09-24-2004  tglx: add support for hardware controllers (e.g. ECC) shared
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 *		among multiple independend devices. Suggestions and initial
 *		patch from Ben Dooks <ben-mtd@fluff.org>
 *
 *  12-05-2004	dmarlin: add workaround for Renesas AG-AND chips "disturb"
 *		issue. Basically, any block not rewritten may lose data when
 *		surrounding blocks are rewritten many times.  JFFS2 ensures
 *		this doesn't happen for blocks it uses, but the Bad Block
 *		Table(s) may not be rewritten.  To ensure they do not lose
 *		data, force them to be rewritten when some of the surrounding
 *		blocks are erased.  Rather than tracking a specific nearby
 *		block (which could itself go bad), use a page address 'mask' to
 *		select several blocks in the same area, and rewrite the BBT
 *		when any of them are erased.
 *
 *  01-03-2005	dmarlin: added support for the device recovery command sequence
 *		for Renesas AG-AND chips.  If there was a sudden loss of power
 *		during an erase operation, a "device recovery" operation must
 *		be performed when power is restored to ensure correct
 *		operation.
 *
 *  01-20-2005	dmarlin: added support for optional hardware specific callback
 *		routine to perform extra error status checks on erase and write
 *		failures.  This required adding a wrapper function for
 *		nand_read_ecc.
52
 *
53 54
 * 08-20-2005	vwool: suspend/resume added
 *
L
Linus Torvalds 已提交
55
 * Credits:
56 57
 *	David Woodhouse for adding multichip support
 *
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67
 *	Aleph One Ltd. and Toby Churchill Ltd. for supporting the
 *	rework for 2K page size chips
 *
 * TODO:
 *	Enable cached programming for 2k page size chips
 *	Check, if mtd->ecctype should be set to MTD_ECC_HW
 *	if we have HW ecc support.
 *	The AG-AND chips have nice features for speed improvement,
 *	which are not supported yet. Read / program 4 pages in one go.
 *
68
 * $Id: nand_base.c,v 1.150 2005/09/15 13:58:48 vwool Exp $
L
Linus Torvalds 已提交
69 70 71 72 73 74 75
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

76
#include <linux/module.h>
L
Linus Torvalds 已提交
77 78
#include <linux/delay.h>
#include <linux/errno.h>
T
Thomas Gleixner 已提交
79
#include <linux/err.h>
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/nand_ecc.h>
#include <linux/mtd/compatmac.h>
#include <linux/interrupt.h>
#include <linux/bitops.h>
89
#include <linux/leds.h>
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97 98 99 100
#include <asm/io.h>

#ifdef CONFIG_MTD_PARTITIONS
#include <linux/mtd/partitions.h>
#endif

/* Define default oob placement schemes for large and small page devices */
static struct nand_oobinfo nand_oob_8 = {
	.useecc = MTD_NANDECC_AUTOPLACE,
	.eccbytes = 3,
	.eccpos = {0, 1, 2},
101
	.oobfree = {{3, 2}, {6, 2}}
L
Linus Torvalds 已提交
102 103 104 105 106 107
};

static struct nand_oobinfo nand_oob_16 = {
	.useecc = MTD_NANDECC_AUTOPLACE,
	.eccbytes = 6,
	.eccpos = {0, 1, 2, 3, 6, 7},
108
	.oobfree = {{8, 8}}
L
Linus Torvalds 已提交
109 110 111 112 113 114
};

static struct nand_oobinfo nand_oob_64 = {
	.useecc = MTD_NANDECC_AUTOPLACE,
	.eccbytes = 24,
	.eccpos = {
115 116 117 118
		   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 = {{2, 38}}
L
Linus Torvalds 已提交
119 120 121
};

/* This is used for padding purposes in nand_write_oob */
122
static uint8_t ffchars[] = {
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};

/*
 * NAND low-level MTD interface functions
 */
136 137 138
static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len);
static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
L
Linus Torvalds 已提交
139

140
static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
141
		     size_t *retlen, uint8_t *buf);
142
static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
143
			 size_t *retlen, uint8_t *buf);
144
static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
145
		      size_t *retlen, const uint8_t *buf);
146
static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
147
			  size_t *retlen, const uint8_t *buf);
148 149
static int nand_erase(struct mtd_info *mtd, struct erase_info *instr);
static void nand_sync(struct mtd_info *mtd);
L
Linus Torvalds 已提交
150 151

/* Some internal functions */
152
static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this,
153
			   int page, uint8_t * oob_buf,
154
			   struct nand_oobinfo *oobsel, int mode);
L
Linus Torvalds 已提交
155
#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
156
static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this,
157
			     int page, int numpages, uint8_t *oob_buf,
158 159
			     struct nand_oobinfo *oobsel, int chipnr,
			     int oobmode);
L
Linus Torvalds 已提交
160 161 162
#else
#define nand_verify_pages(...) (0)
#endif
163

164 165
static int nand_get_device(struct nand_chip *this, struct mtd_info *mtd,
			   int new_state);
L
Linus Torvalds 已提交
166 167 168 169

/**
 * nand_release_device - [GENERIC] release chip
 * @mtd:	MTD device structure
170 171
 *
 * Deselect, release chip lock and wake up anyone waiting on the device
L
Linus Torvalds 已提交
172
 */
173
static void nand_release_device(struct mtd_info *mtd)
L
Linus Torvalds 已提交
174 175 176 177 178
{
	struct nand_chip *this = mtd->priv;

	/* De-select the NAND device */
	this->select_chip(mtd, -1);
179

T
Thomas Gleixner 已提交
180 181 182 183 184 185
	/* Release the controller and the chip */
	spin_lock(&this->controller->lock);
	this->controller->active = NULL;
	this->state = FL_READY;
	wake_up(&this->controller->wq);
	spin_unlock(&this->controller->lock);
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193
}

/**
 * nand_read_byte - [DEFAULT] read one byte from the chip
 * @mtd:	MTD device structure
 *
 * Default read function for 8bit buswith
 */
194
static uint8_t nand_read_byte(struct mtd_info *mtd)
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202 203 204 205 206
{
	struct nand_chip *this = mtd->priv;
	return readb(this->IO_ADDR_R);
}

/**
 * nand_write_byte - [DEFAULT] write one byte to the chip
 * @mtd:	MTD device structure
 * @byte:	pointer to data byte to write
 *
 * Default write function for 8it buswith
 */
207
static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
L
Linus Torvalds 已提交
208 209 210 211 212 213 214 215 216
{
	struct nand_chip *this = mtd->priv;
	writeb(byte, this->IO_ADDR_W);
}

/**
 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
 * @mtd:	MTD device structure
 *
217
 * Default read function for 16bit buswith with
L
Linus Torvalds 已提交
218 219
 * endianess conversion
 */
220
static uint8_t nand_read_byte16(struct mtd_info *mtd)
L
Linus Torvalds 已提交
221 222
{
	struct nand_chip *this = mtd->priv;
223
	return (uint8_t) cpu_to_le16(readw(this->IO_ADDR_R));
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233
}

/**
 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
 * @mtd:	MTD device structure
 * @byte:	pointer to data byte to write
 *
 * Default write function for 16bit buswith with
 * endianess conversion
 */
234
static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243
{
	struct nand_chip *this = mtd->priv;
	writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
}

/**
 * nand_read_word - [DEFAULT] read one word from the chip
 * @mtd:	MTD device structure
 *
244
 * Default read function for 16bit buswith without
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257
 * endianess conversion
 */
static u16 nand_read_word(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;
	return readw(this->IO_ADDR_R);
}

/**
 * nand_write_word - [DEFAULT] write one word to the chip
 * @mtd:	MTD device structure
 * @word:	data word to write
 *
258
 * Default write function for 16bit buswith without
L
Linus Torvalds 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
 * endianess conversion
 */
static void nand_write_word(struct mtd_info *mtd, u16 word)
{
	struct nand_chip *this = mtd->priv;
	writew(word, this->IO_ADDR_W);
}

/**
 * nand_select_chip - [DEFAULT] control CE line
 * @mtd:	MTD device structure
 * @chip:	chipnumber to select, -1 for deselect
 *
 * Default select function for 1 chip devices.
 */
static void nand_select_chip(struct mtd_info *mtd, int chip)
{
	struct nand_chip *this = mtd->priv;
277
	switch (chip) {
L
Linus Torvalds 已提交
278
	case -1:
279
		this->hwcontrol(mtd, NAND_CTL_CLRNCE);
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		break;
	case 0:
		this->hwcontrol(mtd, NAND_CTL_SETNCE);
		break;

	default:
		BUG();
	}
}

/**
 * nand_write_buf - [DEFAULT] write buffer to chip
 * @mtd:	MTD device structure
 * @buf:	data buffer
 * @len:	number of bytes to write
 *
 * Default write function for 8bit buswith
 */
298
static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
L
Linus Torvalds 已提交
299 300 301 302
{
	int i;
	struct nand_chip *this = mtd->priv;

303
	for (i = 0; i < len; i++)
L
Linus Torvalds 已提交
304 305 306 307
		writeb(buf[i], this->IO_ADDR_W);
}

/**
308
 * nand_read_buf - [DEFAULT] read chip data into buffer
L
Linus Torvalds 已提交
309 310 311 312 313 314
 * @mtd:	MTD device structure
 * @buf:	buffer to store date
 * @len:	number of bytes to read
 *
 * Default read function for 8bit buswith
 */
315
static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
L
Linus Torvalds 已提交
316 317 318 319
{
	int i;
	struct nand_chip *this = mtd->priv;

320
	for (i = 0; i < len; i++)
L
Linus Torvalds 已提交
321 322 323 324
		buf[i] = readb(this->IO_ADDR_R);
}

/**
325
 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
L
Linus Torvalds 已提交
326 327 328 329 330 331
 * @mtd:	MTD device structure
 * @buf:	buffer containing the data to compare
 * @len:	number of bytes to compare
 *
 * Default verify function for 8bit buswith
 */
332
static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
L
Linus Torvalds 已提交
333 334 335 336
{
	int i;
	struct nand_chip *this = mtd->priv;

337
	for (i = 0; i < len; i++)
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351
		if (buf[i] != readb(this->IO_ADDR_R))
			return -EFAULT;

	return 0;
}

/**
 * nand_write_buf16 - [DEFAULT] write buffer to chip
 * @mtd:	MTD device structure
 * @buf:	data buffer
 * @len:	number of bytes to write
 *
 * Default write function for 16bit buswith
 */
352
static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
L
Linus Torvalds 已提交
353 354 355 356 357
{
	int i;
	struct nand_chip *this = mtd->priv;
	u16 *p = (u16 *) buf;
	len >>= 1;
358

359
	for (i = 0; i < len; i++)
L
Linus Torvalds 已提交
360
		writew(p[i], this->IO_ADDR_W);
361

L
Linus Torvalds 已提交
362 363 364
}

/**
365
 * nand_read_buf16 - [DEFAULT] read chip data into buffer
L
Linus Torvalds 已提交
366 367 368 369 370 371
 * @mtd:	MTD device structure
 * @buf:	buffer to store date
 * @len:	number of bytes to read
 *
 * Default read function for 16bit buswith
 */
372
static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
L
Linus Torvalds 已提交
373 374 375 376 377 378
{
	int i;
	struct nand_chip *this = mtd->priv;
	u16 *p = (u16 *) buf;
	len >>= 1;

379
	for (i = 0; i < len; i++)
L
Linus Torvalds 已提交
380 381 382 383
		p[i] = readw(this->IO_ADDR_R);
}

/**
384
 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
L
Linus Torvalds 已提交
385 386 387 388 389 390
 * @mtd:	MTD device structure
 * @buf:	buffer containing the data to compare
 * @len:	number of bytes to compare
 *
 * Default verify function for 16bit buswith
 */
391
static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
L
Linus Torvalds 已提交
392 393 394 395 396 397
{
	int i;
	struct nand_chip *this = mtd->priv;
	u16 *p = (u16 *) buf;
	len >>= 1;

398
	for (i = 0; i < len; i++)
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410
		if (p[i] != readw(this->IO_ADDR_R))
			return -EFAULT;

	return 0;
}

/**
 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
 * @mtd:	MTD device structure
 * @ofs:	offset from device start
 * @getchip:	0, if the chip is already selected
 *
411
 * Check, if the block is bad.
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423
 */
static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
{
	int page, chipnr, res = 0;
	struct nand_chip *this = mtd->priv;
	u16 bad;

	if (getchip) {
		page = (int)(ofs >> this->page_shift);
		chipnr = (int)(ofs >> this->chip_shift);

		/* Grab the lock and see if the device is available */
424
		nand_get_device(this, mtd, FL_READING);
L
Linus Torvalds 已提交
425 426 427

		/* Select the NAND device */
		this->select_chip(mtd, chipnr);
428
	} else
429
		page = (int)ofs;
L
Linus Torvalds 已提交
430 431

	if (this->options & NAND_BUSWIDTH_16) {
432 433
		this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE,
			      page & this->pagemask);
L
Linus Torvalds 已提交
434 435
		bad = cpu_to_le16(this->read_word(mtd));
		if (this->badblockpos & 0x1)
436
			bad >>= 8;
L
Linus Torvalds 已提交
437 438 439
		if ((bad & 0xFF) != 0xff)
			res = 1;
	} else {
440 441
		this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos,
			      page & this->pagemask);
L
Linus Torvalds 已提交
442 443 444
		if (this->read_byte(mtd) != 0xff)
			res = 1;
	}
445

L
Linus Torvalds 已提交
446 447 448
	if (getchip) {
		/* Deselect and wake up anyone waiting on the device */
		nand_release_device(mtd);
449 450
	}

L
Linus Torvalds 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464
	return res;
}

/**
 * nand_default_block_markbad - [DEFAULT] mark a block bad
 * @mtd:	MTD device structure
 * @ofs:	offset from device start
 *
 * This is the default implementation, which can be overridden by
 * a hardware specific driver.
*/
static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
{
	struct nand_chip *this = mtd->priv;
465
	uint8_t buf[2] = { 0, 0 };
466
	size_t retlen;
L
Linus Torvalds 已提交
467
	int block;
468

L
Linus Torvalds 已提交
469
	/* Get block number */
470
	block = ((int)ofs) >> this->bbt_erase_shift;
471 472
	if (this->bbt)
		this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
L
Linus Torvalds 已提交
473 474 475

	/* Do we have a flash based bad block table ? */
	if (this->options & NAND_USE_FLASH_BBT)
476
		return nand_update_bbt(mtd, ofs);
477

L
Linus Torvalds 已提交
478 479
	/* We write two bytes, so we dont have to mess with 16 bit access */
	ofs += mtd->oobsize + (this->badblockpos & ~0x01);
480
	return nand_write_oob(mtd, ofs, 2, &retlen, buf);
L
Linus Torvalds 已提交
481 482
}

483
/**
L
Linus Torvalds 已提交
484 485
 * nand_check_wp - [GENERIC] check if the chip is write protected
 * @mtd:	MTD device structure
486
 * Check, if the device is write protected
L
Linus Torvalds 已提交
487
 *
488
 * The function expects, that the device is already selected
L
Linus Torvalds 已提交
489
 */
490
static int nand_check_wp(struct mtd_info *mtd)
L
Linus Torvalds 已提交
491 492 493
{
	struct nand_chip *this = mtd->priv;
	/* Check the WP bit */
494
	this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
495
	return (this->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503 504 505 506 507
}

/**
 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
 * @mtd:	MTD device structure
 * @ofs:	offset from device start
 * @getchip:	0, if the chip is already selected
 * @allowbbt:	1, if its allowed to access the bbt area
 *
 * Check, if the block is bad. Either by reading the bad block table or
 * calling of the scan function.
 */
508 509
static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
			       int allowbbt)
L
Linus Torvalds 已提交
510 511
{
	struct nand_chip *this = mtd->priv;
512

L
Linus Torvalds 已提交
513 514
	if (!this->bbt)
		return this->block_bad(mtd, ofs, getchip);
515

L
Linus Torvalds 已提交
516
	/* Return info from the table */
517
	return nand_isbad_bbt(mtd, ofs, allowbbt);
L
Linus Torvalds 已提交
518 519
}

520 521
DEFINE_LED_TRIGGER(nand_led_trigger);

522
/*
523 524 525 526 527 528
 * Wait for the ready pin, after a command
 * The timeout is catched later.
 */
static void nand_wait_ready(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;
529
	unsigned long timeo = jiffies + 2;
530

531
	led_trigger_event(nand_led_trigger, LED_FULL);
532 533 534
	/* wait until command is processed or timeout occures */
	do {
		if (this->dev_ready(mtd))
535
			break;
I
Ingo Molnar 已提交
536
		touch_softlockup_watchdog();
537
	} while (time_before(jiffies, timeo));
538
	led_trigger_event(nand_led_trigger, LED_OFF);
539 540
}

L
Linus Torvalds 已提交
541 542 543 544 545 546 547 548 549 550
/**
 * nand_command - [DEFAULT] Send command to NAND device
 * @mtd:	MTD device structure
 * @command:	the command to be sent
 * @column:	the column address for this command, -1 if none
 * @page_addr:	the page address for this command, -1 if none
 *
 * Send command to NAND device. This function is used for small page
 * devices (256/512 Bytes per page)
 */
551 552
static void nand_command(struct mtd_info *mtd, unsigned command, int column,
			 int page_addr)
L
Linus Torvalds 已提交
553 554 555 556 557 558 559 560 561 562 563
{
	register struct nand_chip *this = mtd->priv;

	/* Begin command latch cycle */
	this->hwcontrol(mtd, NAND_CTL_SETCLE);
	/*
	 * Write out the command to the device.
	 */
	if (command == NAND_CMD_SEQIN) {
		int readcmd;

J
Joern Engel 已提交
564
		if (column >= mtd->writesize) {
L
Linus Torvalds 已提交
565
			/* OOB area */
J
Joern Engel 已提交
566
			column -= mtd->writesize;
L
Linus Torvalds 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
			readcmd = NAND_CMD_READOOB;
		} else if (column < 256) {
			/* First 256 bytes --> READ0 */
			readcmd = NAND_CMD_READ0;
		} else {
			column -= 256;
			readcmd = NAND_CMD_READ1;
		}
		this->write_byte(mtd, readcmd);
	}
	this->write_byte(mtd, command);

	/* Set ALE and clear CLE to start address cycle */
	this->hwcontrol(mtd, NAND_CTL_CLRCLE);

	if (column != -1 || page_addr != -1) {
		this->hwcontrol(mtd, NAND_CTL_SETALE);

		/* Serially input address */
		if (column != -1) {
			/* Adjust columns for 16 bit buswidth */
			if (this->options & NAND_BUSWIDTH_16)
				column >>= 1;
			this->write_byte(mtd, column);
		}
		if (page_addr != -1) {
593 594
			this->write_byte(mtd, (uint8_t)(page_addr & 0xff));
			this->write_byte(mtd, (uint8_t)((page_addr >> 8) & 0xff));
L
Linus Torvalds 已提交
595 596
			/* One more address cycle for devices > 32MiB */
			if (this->chipsize > (32 << 20))
597
				this->write_byte(mtd, (uint8_t)((page_addr >> 16) & 0x0f));
L
Linus Torvalds 已提交
598 599 600 601
		}
		/* Latch in address */
		this->hwcontrol(mtd, NAND_CTL_CLRALE);
	}
602 603 604

	/*
	 * program and erase have their own busy handlers
L
Linus Torvalds 已提交
605
	 * status and sequential in needs no delay
606
	 */
L
Linus Torvalds 已提交
607
	switch (command) {
608

L
Linus Torvalds 已提交
609 610 611 612 613 614 615 616
	case NAND_CMD_PAGEPROG:
	case NAND_CMD_ERASE1:
	case NAND_CMD_ERASE2:
	case NAND_CMD_SEQIN:
	case NAND_CMD_STATUS:
		return;

	case NAND_CMD_RESET:
617
		if (this->dev_ready)
L
Linus Torvalds 已提交
618 619 620 621 622
			break;
		udelay(this->chip_delay);
		this->hwcontrol(mtd, NAND_CTL_SETCLE);
		this->write_byte(mtd, NAND_CMD_STATUS);
		this->hwcontrol(mtd, NAND_CTL_CLRCLE);
623
		while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ;
L
Linus Torvalds 已提交
624 625
		return;

626
		/* This applies to read commands */
L
Linus Torvalds 已提交
627
	default:
628
		/*
L
Linus Torvalds 已提交
629 630
		 * If we don't have access to the busy pin, we apply the given
		 * command delay
631
		 */
L
Linus Torvalds 已提交
632
		if (!this->dev_ready) {
633
			udelay(this->chip_delay);
L
Linus Torvalds 已提交
634
			return;
635
		}
L
Linus Torvalds 已提交
636 637 638
	}
	/* Apply this short delay always to ensure that we do wait tWB in
	 * any case on any machine. */
639
	ndelay(100);
640 641

	nand_wait_ready(mtd);
L
Linus Torvalds 已提交
642 643 644 645 646 647 648 649 650 651
}

/**
 * nand_command_lp - [DEFAULT] Send command to NAND large page device
 * @mtd:	MTD device structure
 * @command:	the command to be sent
 * @column:	the column address for this command, -1 if none
 * @page_addr:	the page address for this command, -1 if none
 *
 * Send command to NAND device. This is the version for the new large page devices
652
 * We dont have the separate regions as we have in the small page devices.
L
Linus Torvalds 已提交
653 654 655
 * We must emulate NAND_CMD_READOOB to keep the code compatible.
 *
 */
656
static void nand_command_lp(struct mtd_info *mtd, unsigned command, int column, int page_addr)
L
Linus Torvalds 已提交
657 658 659 660 661
{
	register struct nand_chip *this = mtd->priv;

	/* Emulate NAND_CMD_READOOB */
	if (command == NAND_CMD_READOOB) {
J
Joern Engel 已提交
662
		column += mtd->writesize;
L
Linus Torvalds 已提交
663 664
		command = NAND_CMD_READ0;
	}
665

L
Linus Torvalds 已提交
666 667 668
	/* Begin command latch cycle */
	this->hwcontrol(mtd, NAND_CTL_SETCLE);
	/* Write out the command to the device. */
669
	this->write_byte(mtd, (command & 0xff));
L
Linus Torvalds 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682
	/* End command latch cycle */
	this->hwcontrol(mtd, NAND_CTL_CLRCLE);

	if (column != -1 || page_addr != -1) {
		this->hwcontrol(mtd, NAND_CTL_SETALE);

		/* Serially input address */
		if (column != -1) {
			/* Adjust columns for 16 bit buswidth */
			if (this->options & NAND_BUSWIDTH_16)
				column >>= 1;
			this->write_byte(mtd, column & 0xff);
			this->write_byte(mtd, column >> 8);
683
		}
L
Linus Torvalds 已提交
684
		if (page_addr != -1) {
685 686
			this->write_byte(mtd, (uint8_t)(page_addr & 0xff));
			this->write_byte(mtd, (uint8_t)((page_addr >> 8) & 0xff));
L
Linus Torvalds 已提交
687 688
			/* One more address cycle for devices > 128MiB */
			if (this->chipsize > (128 << 20))
689
				this->write_byte(mtd, (uint8_t)((page_addr >> 16) & 0xff));
L
Linus Torvalds 已提交
690 691 692 693
		}
		/* Latch in address */
		this->hwcontrol(mtd, NAND_CTL_CLRALE);
	}
694 695 696

	/*
	 * program and erase have their own busy handlers
697 698
	 * status, sequential in, and deplete1 need no delay
	 */
L
Linus Torvalds 已提交
699
	switch (command) {
700

L
Linus Torvalds 已提交
701 702 703 704 705 706
	case NAND_CMD_CACHEDPROG:
	case NAND_CMD_PAGEPROG:
	case NAND_CMD_ERASE1:
	case NAND_CMD_ERASE2:
	case NAND_CMD_SEQIN:
	case NAND_CMD_STATUS:
707
	case NAND_CMD_DEPLETE1:
L
Linus Torvalds 已提交
708 709
		return;

710 711 712
		/*
		 * read error status commands require only a short delay
		 */
713 714 715 716 717 718 719
	case NAND_CMD_STATUS_ERROR:
	case NAND_CMD_STATUS_ERROR0:
	case NAND_CMD_STATUS_ERROR1:
	case NAND_CMD_STATUS_ERROR2:
	case NAND_CMD_STATUS_ERROR3:
		udelay(this->chip_delay);
		return;
L
Linus Torvalds 已提交
720 721

	case NAND_CMD_RESET:
722
		if (this->dev_ready)
L
Linus Torvalds 已提交
723 724 725 726 727
			break;
		udelay(this->chip_delay);
		this->hwcontrol(mtd, NAND_CTL_SETCLE);
		this->write_byte(mtd, NAND_CMD_STATUS);
		this->hwcontrol(mtd, NAND_CTL_CLRCLE);
728
		while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ;
L
Linus Torvalds 已提交
729 730 731 732 733 734 735 736 737 738
		return;

	case NAND_CMD_READ0:
		/* Begin command latch cycle */
		this->hwcontrol(mtd, NAND_CTL_SETCLE);
		/* Write out the start read command */
		this->write_byte(mtd, NAND_CMD_READSTART);
		/* End command latch cycle */
		this->hwcontrol(mtd, NAND_CTL_CLRCLE);
		/* Fall through into ready check */
739

740
		/* This applies to read commands */
L
Linus Torvalds 已提交
741
	default:
742
		/*
L
Linus Torvalds 已提交
743 744
		 * If we don't have access to the busy pin, we apply the given
		 * command delay
745
		 */
L
Linus Torvalds 已提交
746
		if (!this->dev_ready) {
747
			udelay(this->chip_delay);
L
Linus Torvalds 已提交
748
			return;
749
		}
L
Linus Torvalds 已提交
750
	}
751

L
Linus Torvalds 已提交
752 753
	/* Apply this short delay always to ensure that we do wait tWB in
	 * any case on any machine. */
754
	ndelay(100);
755 756

	nand_wait_ready(mtd);
L
Linus Torvalds 已提交
757 758 759 760 761 762
}

/**
 * nand_get_device - [GENERIC] Get chip for selected access
 * @this:	the nand chip descriptor
 * @mtd:	MTD device structure
763
 * @new_state:	the state which is requested
L
Linus Torvalds 已提交
764 765 766
 *
 * Get the device and lock it for exclusive access
 */
767 768
static int
nand_get_device(struct nand_chip *this, struct mtd_info *mtd, int new_state)
L
Linus Torvalds 已提交
769
{
T
Thomas Gleixner 已提交
770 771
	spinlock_t *lock = &this->controller->lock;
	wait_queue_head_t *wq = &this->controller->wq;
772 773
	DECLARE_WAITQUEUE(wait, current);
 retry:
774 775
	spin_lock(lock);

L
Linus Torvalds 已提交
776
	/* Hardware controller shared among independend devices */
T
Thomas Gleixner 已提交
777 778 779 780 781
	/* Hardware controller shared among independend devices */
	if (!this->controller->active)
		this->controller->active = this;

	if (this->controller->active == this && this->state == FL_READY) {
782 783
		this->state = new_state;
		spin_unlock(lock);
784 785 786 787 788
		return 0;
	}
	if (new_state == FL_PM_SUSPENDED) {
		spin_unlock(lock);
		return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
789 790 791 792 793 794
	}
	set_current_state(TASK_UNINTERRUPTIBLE);
	add_wait_queue(wq, &wait);
	spin_unlock(lock);
	schedule();
	remove_wait_queue(wq, &wait);
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802 803 804
	goto retry;
}

/**
 * nand_wait - [DEFAULT]  wait until the command is done
 * @mtd:	MTD device structure
 * @this:	NAND chip structure
 * @state:	state to select the max. timeout value
 *
 * Wait for command done. This applies to erase and program only
805
 * Erase can take up to 400ms and program up to 20ms according to
L
Linus Torvalds 已提交
806 807 808 809 810 811
 * general NAND and SmartMedia specs
 *
*/
static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
{

812 813
	unsigned long timeo = jiffies;
	int status;
814

L
Linus Torvalds 已提交
815
	if (state == FL_ERASING)
816
		timeo += (HZ * 400) / 1000;
L
Linus Torvalds 已提交
817
	else
818
		timeo += (HZ * 20) / 1000;
L
Linus Torvalds 已提交
819

820 821
	led_trigger_event(nand_led_trigger, LED_FULL);

L
Linus Torvalds 已提交
822 823
	/* Apply this short delay always to ensure that we do wait tWB in
	 * any case on any machine. */
824
	ndelay(100);
L
Linus Torvalds 已提交
825 826

	if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
827
		this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
828
	else
829
		this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
L
Linus Torvalds 已提交
830

831
	while (time_before(jiffies, timeo)) {
L
Linus Torvalds 已提交
832 833 834 835 836 837
		/* Check, if we were interrupted */
		if (this->state != state)
			return 0;

		if (this->dev_ready) {
			if (this->dev_ready(mtd))
838
				break;
L
Linus Torvalds 已提交
839 840 841 842
		} else {
			if (this->read_byte(mtd) & NAND_STATUS_READY)
				break;
		}
843
		cond_resched();
L
Linus Torvalds 已提交
844
	}
845 846
	led_trigger_event(nand_led_trigger, LED_OFF);

847
	status = (int)this->read_byte(mtd);
L
Linus Torvalds 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	return status;
}

/**
 * nand_write_page - [GENERIC] write one page
 * @mtd:	MTD device structure
 * @this:	NAND chip structure
 * @page: 	startpage inside the chip, must be called with (page & this->pagemask)
 * @oob_buf:	out of band data buffer
 * @oobsel:	out of band selecttion structre
 * @cached:	1 = enable cached programming if supported by chip
 *
 * Nand_page_program function is used for write and writev !
 * This function will always program a full page of data
 * If you call it with a non page aligned buffer, you're lost :)
 *
 * Cached programming is not supported yet.
 */
866
static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this, int page,
867
			   uint8_t *oob_buf, struct nand_oobinfo *oobsel, int cached)
L
Linus Torvalds 已提交
868
{
869
	int i, status;
870
	uint8_t ecc_code[32];
T
Thomas Gleixner 已提交
871
	int eccmode = oobsel->useecc ? this->ecc.mode : NAND_ECC_NONE;
872
	int *oob_config = oobsel->eccpos;
T
Thomas Gleixner 已提交
873
	int datidx = 0, eccidx = 0, eccsteps = this->ecc.steps;
874
	int eccbytes = 0;
875

L
Linus Torvalds 已提交
876 877
	/* FIXME: Enable cached programming */
	cached = 0;
878

L
Linus Torvalds 已提交
879
	/* Send command to begin auto page programming */
880
	this->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
L
Linus Torvalds 已提交
881 882 883

	/* Write out complete page of data, take care of eccmode */
	switch (eccmode) {
884
		/* No ecc, write all */
L
Linus Torvalds 已提交
885
	case NAND_ECC_NONE:
886
		printk(KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
J
Joern Engel 已提交
887
		this->write_buf(mtd, this->data_poi, mtd->writesize);
L
Linus Torvalds 已提交
888
		break;
889

890
		/* Software ecc 3/256, write all */
L
Linus Torvalds 已提交
891 892
	case NAND_ECC_SOFT:
		for (; eccsteps; eccsteps--) {
T
Thomas Gleixner 已提交
893
			this->ecc.calculate(mtd, &this->data_poi[datidx], ecc_code);
L
Linus Torvalds 已提交
894 895
			for (i = 0; i < 3; i++, eccidx++)
				oob_buf[oob_config[eccidx]] = ecc_code[i];
T
Thomas Gleixner 已提交
896
			datidx += this->ecc.size;
L
Linus Torvalds 已提交
897
		}
J
Joern Engel 已提交
898
		this->write_buf(mtd, this->data_poi, mtd->writesize);
L
Linus Torvalds 已提交
899 900
		break;
	default:
T
Thomas Gleixner 已提交
901
		eccbytes = this->ecc.bytes;
L
Linus Torvalds 已提交
902 903
		for (; eccsteps; eccsteps--) {
			/* enable hardware ecc logic for write */
T
Thomas Gleixner 已提交
904 905 906
			this->ecc.hwctl(mtd, NAND_ECC_WRITE);
			this->write_buf(mtd, &this->data_poi[datidx], this->ecc.size);
			this->ecc.calculate(mtd, &this->data_poi[datidx], ecc_code);
L
Linus Torvalds 已提交
907 908 909 910 911 912 913
			for (i = 0; i < eccbytes; i++, eccidx++)
				oob_buf[oob_config[eccidx]] = ecc_code[i];
			/* If the hardware ecc provides syndromes then
			 * the ecc code must be written immidiately after
			 * the data bytes (words) */
			if (this->options & NAND_HWECC_SYNDROME)
				this->write_buf(mtd, ecc_code, eccbytes);
T
Thomas Gleixner 已提交
914
			datidx += this->ecc.size;
L
Linus Torvalds 已提交
915 916 917
		}
		break;
	}
918

L
Linus Torvalds 已提交
919 920 921
	/* Write out OOB data */
	if (this->options & NAND_HWECC_SYNDROME)
		this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
922
	else
L
Linus Torvalds 已提交
923 924 925
		this->write_buf(mtd, oob_buf, mtd->oobsize);

	/* Send command to actually program the data */
926
	this->cmdfunc(mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
L
Linus Torvalds 已提交
927 928 929

	if (!cached) {
		/* call wait ready function */
930
		status = this->waitfunc(mtd, this, FL_WRITING);
931 932 933 934 935 936

		/* See if operation failed and additional status checks are available */
		if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
			status = this->errstat(mtd, this, FL_WRITING, status, page);
		}

L
Linus Torvalds 已提交
937
		/* See if device thinks it succeeded */
938
		if (status & NAND_STATUS_FAIL) {
939
			DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
L
Linus Torvalds 已提交
940 941 942 943
			return -EIO;
		}
	} else {
		/* FIXME: Implement cached programming ! */
944
		/* wait until cache is ready */
L
Linus Torvalds 已提交
945 946
		// status = this->waitfunc (mtd, this, FL_CACHEDRPG);
	}
947
	return 0;
L
Linus Torvalds 已提交
948 949 950 951 952 953 954
}

#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
/**
 * nand_verify_pages - [GENERIC] verify the chip contents after a write
 * @mtd:	MTD device structure
 * @this:	NAND chip structure
955
 * @page:	startpage inside the chip, must be called with (page & this->pagemask)
L
Linus Torvalds 已提交
956 957 958 959 960 961 962
 * @numpages:	number of pages to verify
 * @oob_buf:	out of band data buffer
 * @oobsel:	out of band selecttion structre
 * @chipnr:	number of the current chip
 * @oobmode:	1 = full buffer verify, 0 = ecc only
 *
 * The NAND device assumes that it is always writing to a cleanly erased page.
963
 * Hence, it performs its internal write verification only on bits that
L
Linus Torvalds 已提交
964
 * transitioned from 1 to 0. The device does NOT verify the whole page on a
965 966 967
 * byte by byte basis. It is possible that the page was not completely erased
 * or the page is becoming unusable due to wear. The read with ECC would catch
 * the error later when the ECC page check fails, but we would rather catch
L
Linus Torvalds 已提交
968 969
 * it early in the page write stage. Better to write no data than invalid data.
 */
970
static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
971
			     uint8_t *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
L
Linus Torvalds 已提交
972
{
973 974 975
	int i, j, datidx = 0, oobofs = 0, res = -EIO;
	int eccsteps = this->eccsteps;
	int hweccbytes;
976
	uint8_t oobdata[64];
L
Linus Torvalds 已提交
977 978 979 980

	hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;

	/* Send command to read back the first page */
981
	this->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
L
Linus Torvalds 已提交
982

983
	for (;;) {
L
Linus Torvalds 已提交
984 985 986
		for (j = 0; j < eccsteps; j++) {
			/* Loop through and verify the data */
			if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
987
				DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
L
Linus Torvalds 已提交
988 989 990 991 992 993 994
				goto out;
			}
			datidx += mtd->eccsize;
			/* Have we a hw generator layout ? */
			if (!hweccbytes)
				continue;
			if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
995
				DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
L
Linus Torvalds 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005
				goto out;
			}
			oobofs += hweccbytes;
		}

		/* check, if we must compare all data or if we just have to
		 * compare the ecc bytes
		 */
		if (oobmode) {
			if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1006
				DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013 1014
				goto out;
			}
		} else {
			/* Read always, else autoincrement fails */
			this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);

			if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
				int ecccnt = oobsel->eccbytes;
1015

L
Linus Torvalds 已提交
1016 1017
				for (i = 0; i < ecccnt; i++) {
					int idx = oobsel->eccpos[i];
1018 1019 1020
					if (oobdata[idx] != oob_buf[oobofs + idx]) {
						DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed ECC write verify, page 0x%08x, %6i bytes were succesful\n",
						      __FUNCTION__, page, i);
L
Linus Torvalds 已提交
1021 1022 1023
						goto out;
					}
				}
1024
			}
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029
		}
		oobofs += mtd->oobsize - hweccbytes * eccsteps;
		page++;
		numpages--;

1030
		/* Apply delay or wait for ready/busy pin
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035
		 * Do this before the AUTOINCR check, so no problems
		 * arise if a chip which does auto increment
		 * is marked as NOAUTOINCR by the board driver.
		 * Do this also before returning, so the chip is
		 * ready for the next command.
1036
		 */
1037
		if (!this->dev_ready)
1038
			udelay(this->chip_delay);
L
Linus Torvalds 已提交
1039
		else
1040
			nand_wait_ready(mtd);
L
Linus Torvalds 已提交
1041 1042 1043 1044

		/* All done, return happy */
		if (!numpages)
			return 0;
1045 1046

		/* Check, if the chip supports auto page increment */
L
Linus Torvalds 已提交
1047
		if (!NAND_CANAUTOINCR(this))
1048
			this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
L
Linus Torvalds 已提交
1049
	}
1050
	/*
L
Linus Torvalds 已提交
1051 1052 1053
	 * Terminate the read command. We come here in case of an error
	 * So we must issue a reset command.
	 */
1054 1055
 out:
	this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060
	return res;
}
#endif

/**
1061
 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
L
Linus Torvalds 已提交
1062 1063 1064 1065 1066 1067
 * @mtd:	MTD device structure
 * @from:	offset to read from
 * @len:	number of bytes to read
 * @retlen:	pointer to variable to store the number of read bytes
 * @buf:	the databuffer to put data
 *
1068 1069 1070
 * This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL
 * and flags = 0xff
 */
1071
static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, uint8_t *buf)
L
Linus Torvalds 已提交
1072
{
1073
	return nand_do_read_ecc(mtd, from, len, retlen, buf, NULL, &mtd->oobinfo, 0xff);
1074
}
L
Linus Torvalds 已提交
1075

1076 1077 1078 1079 1080 1081 1082
/**
 * nand_do_read_ecc - [MTD Interface] Read data with ECC
 * @mtd:	MTD device structure
 * @from:	offset to read from
 * @len:	number of bytes to read
 * @retlen:	pointer to variable to store the number of read bytes
 * @buf:	the databuffer to put data
1083
 * @oob_buf:	filesystem supplied oob data buffer (can be NULL)
1084
 * @oobsel:	oob selection structure
1085 1086 1087 1088 1089 1090 1091
 * @flags:	flag to indicate if nand_get_device/nand_release_device should be preformed
 *		and how many corrected error bits are acceptable:
 *		  bits 0..7 - number of tolerable errors
 *		  bit  8    - 0 == do not get/release chip, 1 == get/release chip
 *
 * NAND read with ECC
 */
1092
int nand_do_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
1093
		     size_t *retlen, uint8_t *buf, uint8_t *oob_buf, struct nand_oobinfo *oobsel, int flags)
L
Linus Torvalds 已提交
1094
{
1095

L
Linus Torvalds 已提交
1096 1097 1098
	int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
	int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
	struct nand_chip *this = mtd->priv;
1099 1100 1101
	uint8_t *data_poi, *oob_data = oob_buf;
	uint8_t ecc_calc[32];
	uint8_t ecc_code[32];
1102 1103 1104 1105 1106 1107
	int eccmode, eccsteps;
	int *oob_config, datidx;
	int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
	int eccbytes;
	int compareecc = 1;
	int oobreadlen;
L
Linus Torvalds 已提交
1108

1109
	DEBUG(MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len);
L
Linus Torvalds 已提交
1110 1111 1112

	/* Do not allow reads past end of device */
	if ((from + len) > mtd->size) {
1113
		DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
L
Linus Torvalds 已提交
1114 1115 1116 1117 1118
		*retlen = 0;
		return -EINVAL;
	}

	/* Grab the lock and see if the device is available */
1119
	if (flags & NAND_GET_DEVICE)
1120
		nand_get_device(this, mtd, FL_READING);
L
Linus Torvalds 已提交
1121 1122 1123 1124

	/* Autoplace of oob data ? Use the default placement scheme */
	if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
		oobsel = this->autooob;
1125

T
Thomas Gleixner 已提交
1126
	eccmode = oobsel->useecc ? this->ecc.mode : NAND_ECC_NONE;
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131 1132 1133
	oob_config = oobsel->eccpos;

	/* Select the NAND device */
	chipnr = (int)(from >> this->chip_shift);
	this->select_chip(mtd, chipnr);

	/* First we calculate the starting page */
1134
	realpage = (int)(from >> this->page_shift);
L
Linus Torvalds 已提交
1135 1136 1137
	page = realpage & this->pagemask;

	/* Get raw starting column */
J
Joern Engel 已提交
1138
	col = from & (mtd->writesize - 1);
L
Linus Torvalds 已提交
1139

J
Joern Engel 已提交
1140
	end = mtd->writesize;
T
Thomas Gleixner 已提交
1141 1142
	ecc = this->ecc.size;
	eccbytes = this->ecc.bytes;
1143

L
Linus Torvalds 已提交
1144 1145 1146 1147
	if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
		compareecc = 0;

	oobreadlen = mtd->oobsize;
1148
	if (this->options & NAND_HWECC_SYNDROME)
L
Linus Torvalds 已提交
1149 1150 1151 1152
		oobreadlen -= oobsel->eccbytes;

	/* Loop until all data read */
	while (read < len) {
1153

L
Linus Torvalds 已提交
1154
		int aligned = (!col && (len - read) >= end);
1155
		/*
L
Linus Torvalds 已提交
1156 1157 1158 1159 1160
		 * If the read is not page aligned, we have to read into data buffer
		 * due to ecc, else we read into return buffer direct
		 */
		if (aligned)
			data_poi = &buf[read];
1161
		else
L
Linus Torvalds 已提交
1162
			data_poi = this->data_buf;
1163 1164

		/* Check, if we have this page in the buffer
L
Linus Torvalds 已提交
1165 1166 1167 1168 1169 1170 1171
		 *
		 * FIXME: Make it work when we must provide oob data too,
		 * check the usage of data_buf oob field
		 */
		if (realpage == this->pagebuf && !oob_buf) {
			/* aligned read ? */
			if (aligned)
1172
				memcpy(data_poi, this->data_buf, end);
L
Linus Torvalds 已提交
1173 1174 1175 1176 1177
			goto readdata;
		}

		/* Check, if we must send the read command */
		if (sndcmd) {
1178
			this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
L
Linus Torvalds 已提交
1179
			sndcmd = 0;
1180
		}
L
Linus Torvalds 已提交
1181 1182

		/* get oob area, if we have no oob buffer from fs-driver */
1183 1184
		if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
			oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
L
Linus Torvalds 已提交
1185 1186
			oob_data = &this->data_buf[end];

T
Thomas Gleixner 已提交
1187
		eccsteps = this->ecc.steps;
1188

L
Linus Torvalds 已提交
1189
		switch (eccmode) {
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
		case NAND_ECC_NONE:{
				/* No ECC, Read in a page */
				static unsigned long lastwhinge = 0;
				if ((lastwhinge / HZ) != (jiffies / HZ)) {
					printk(KERN_WARNING
					       "Reading data from NAND FLASH without ECC is not recommended\n");
					lastwhinge = jiffies;
				}
				this->read_buf(mtd, data_poi, end);
				break;
L
Linus Torvalds 已提交
1200
			}
1201

L
Linus Torvalds 已提交
1202 1203
		case NAND_ECC_SOFT:	/* Software ECC 3/256: Read in a page + oob data */
			this->read_buf(mtd, data_poi, end);
1204
			for (i = 0, datidx = 0; eccsteps; eccsteps--, i += 3, datidx += ecc)
T
Thomas Gleixner 已提交
1205
				this->ecc.calculate(mtd, &data_poi[datidx], &ecc_calc[i]);
1206
			break;
L
Linus Torvalds 已提交
1207 1208

		default:
1209
			for (i = 0, datidx = 0; eccsteps; eccsteps--, i += eccbytes, datidx += ecc) {
T
Thomas Gleixner 已提交
1210
				this->ecc.hwctl(mtd, NAND_ECC_READ);
L
Linus Torvalds 已提交
1211 1212 1213 1214 1215 1216 1217
				this->read_buf(mtd, &data_poi[datidx], ecc);

				/* HW ecc with syndrome calculation must read the
				 * syndrome from flash immidiately after the data */
				if (!compareecc) {
					/* Some hw ecc generators need to know when the
					 * syndrome is read from flash */
T
Thomas Gleixner 已提交
1218
					this->ecc.hwctl(mtd, NAND_ECC_READSYN);
L
Linus Torvalds 已提交
1219 1220 1221 1222
					this->read_buf(mtd, &oob_data[i], eccbytes);
					/* We calc error correction directly, it checks the hw
					 * generator for an error, reads back the syndrome and
					 * does the error correction on the fly */
T
Thomas Gleixner 已提交
1223
					ecc_status = this->ecc.correct(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]);
1224
					if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
1225 1226
						DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: "
						      "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
L
Linus Torvalds 已提交
1227 1228 1229
						ecc_failed++;
					}
				} else {
T
Thomas Gleixner 已提交
1230
					this->ecc.calculate(mtd, &data_poi[datidx], &ecc_calc[i]);
1231
				}
L
Linus Torvalds 已提交
1232
			}
1233
			break;
L
Linus Torvalds 已提交
1234 1235 1236 1237 1238 1239 1240
		}

		/* read oobdata */
		this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);

		/* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
		if (!compareecc)
1241 1242
			goto readoob;

L
Linus Torvalds 已提交
1243 1244 1245 1246
		/* Pick the ECC bytes out of the oob data */
		for (j = 0; j < oobsel->eccbytes; j++)
			ecc_code[j] = oob_data[oob_config[j]];

1247
		/* correct data, if necessary */
T
Thomas Gleixner 已提交
1248 1249
		for (i = 0, j = 0, datidx = 0; i < this->ecc.steps; i++, datidx += ecc) {
			ecc_status = this->ecc.correct(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1250

L
Linus Torvalds 已提交
1251 1252
			/* Get next chunk of ecc bytes */
			j += eccbytes;
1253 1254

			/* Check, if we have a fs supplied oob-buffer,
L
Linus Torvalds 已提交
1255 1256 1257
			 * This is the legacy mode. Used by YAFFS1
			 * Should go away some day
			 */
1258
			if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
L
Linus Torvalds 已提交
1259 1260 1261
				int *p = (int *)(&oob_data[mtd->oobsize]);
				p[i] = ecc_status;
			}
1262 1263

			if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
1264
				DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
L
Linus Torvalds 已提交
1265 1266
				ecc_failed++;
			}
1267
		}
L
Linus Torvalds 已提交
1268

1269
	      readoob:
L
Linus Torvalds 已提交
1270 1271 1272
		/* check, if we have a fs supplied oob-buffer */
		if (oob_buf) {
			/* without autoplace. Legacy mode used by YAFFS1 */
1273
			switch (oobsel->useecc) {
L
Linus Torvalds 已提交
1274
			case MTD_NANDECC_AUTOPLACE:
1275
			case MTD_NANDECC_AUTOPL_USR:
L
Linus Torvalds 已提交
1276
				/* Walk through the autoplace chunks */
1277
				for (i = 0; oobsel->oobfree[i][1]; i++) {
L
Linus Torvalds 已提交
1278 1279 1280
					int from = oobsel->oobfree[i][0];
					int num = oobsel->oobfree[i][1];
					memcpy(&oob_buf[oob], &oob_data[from], num);
1281
					oob += num;
L
Linus Torvalds 已提交
1282 1283 1284 1285
				}
				break;
			case MTD_NANDECC_PLACE:
				/* YAFFS1 legacy mode */
T
Thomas Gleixner 已提交
1286
				oob_data += this->ecc.steps * sizeof(int);
L
Linus Torvalds 已提交
1287 1288 1289 1290 1291 1292
			default:
				oob_data += mtd->oobsize;
			}
		}
	readdata:
		/* Partial page read, transfer data into fs buffer */
1293
		if (!aligned) {
L
Linus Torvalds 已提交
1294 1295
			for (j = col; j < end && read < len; j++)
				buf[read++] = data_poi[j];
1296 1297
			this->pagebuf = realpage;
		} else
J
Joern Engel 已提交
1298
			read += mtd->writesize;
L
Linus Torvalds 已提交
1299

1300
		/* Apply delay or wait for ready/busy pin
L
Linus Torvalds 已提交
1301 1302 1303
		 * Do this before the AUTOINCR check, so no problems
		 * arise if a chip which does auto increment
		 * is marked as NOAUTOINCR by the board driver.
1304
		 */
1305
		if (!this->dev_ready)
1306
			udelay(this->chip_delay);
L
Linus Torvalds 已提交
1307
		else
1308
			nand_wait_ready(mtd);
1309

L
Linus Torvalds 已提交
1310
		if (read == len)
1311
			break;
L
Linus Torvalds 已提交
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324

		/* For subsequent reads align to page boundary. */
		col = 0;
		/* Increment page address */
		realpage++;

		page = realpage & this->pagemask;
		/* Check, if we cross a chip boundary */
		if (!page) {
			chipnr++;
			this->select_chip(mtd, -1);
			this->select_chip(mtd, chipnr);
		}
1325 1326
		/* Check, if the chip supports auto page increment
		 * or if we have hit a block boundary.
1327
		 */
L
Linus Torvalds 已提交
1328
		if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1329
			sndcmd = 1;
L
Linus Torvalds 已提交
1330 1331 1332
	}

	/* Deselect and wake up anyone waiting on the device */
1333 1334
	if (flags & NAND_GET_DEVICE)
		nand_release_device(mtd);
L
Linus Torvalds 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354

	/*
	 * Return success, if no ECC failures, else -EBADMSG
	 * fs driver will take care of that, because
	 * retlen == desired len and result == -EBADMSG
	 */
	*retlen = read;
	return ecc_failed ? -EBADMSG : 0;
}

/**
 * nand_read_oob - [MTD Interface] NAND read out-of-band
 * @mtd:	MTD device structure
 * @from:	offset to read from
 * @len:	number of bytes to read
 * @retlen:	pointer to variable to store the number of read bytes
 * @buf:	the databuffer to put data
 *
 * NAND read out-of-band data from the spare area
 */
1355
static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, uint8_t *buf)
L
Linus Torvalds 已提交
1356 1357 1358
{
	int i, col, page, chipnr;
	struct nand_chip *this = mtd->priv;
1359
	int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
L
Linus Torvalds 已提交
1360

1361
	DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len);
L
Linus Torvalds 已提交
1362 1363 1364 1365

	/* Shift to get page */
	page = (int)(from >> this->page_shift);
	chipnr = (int)(from >> this->chip_shift);
1366

L
Linus Torvalds 已提交
1367 1368 1369 1370 1371 1372 1373 1374
	/* Mask to get column */
	col = from & (mtd->oobsize - 1);

	/* Initialize return length value */
	*retlen = 0;

	/* Do not allow reads past end of device */
	if ((from + len) > mtd->size) {
1375
		DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
L
Linus Torvalds 已提交
1376 1377 1378 1379 1380
		*retlen = 0;
		return -EINVAL;
	}

	/* Grab the lock and see if the device is available */
1381
	nand_get_device(this, mtd, FL_READING);
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386

	/* Select the NAND device */
	this->select_chip(mtd, chipnr);

	/* Send the read command */
1387
	this->cmdfunc(mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1388
	/*
L
Linus Torvalds 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
	 * Read the data, if we read more than one page
	 * oob data, let the device transfer the data !
	 */
	i = 0;
	while (i < len) {
		int thislen = mtd->oobsize - col;
		thislen = min_t(int, thislen, len);
		this->read_buf(mtd, &buf[i], thislen);
		i += thislen;

		/* Read more ? */
		if (i < len) {
			page++;
			col = 0;

			/* Check, if we cross a chip boundary */
			if (!(page & this->pagemask)) {
				chipnr++;
				this->select_chip(mtd, -1);
				this->select_chip(mtd, chipnr);
			}
1410 1411

			/* Apply delay or wait for ready/busy pin
1412 1413 1414 1415
			 * Do this before the AUTOINCR check, so no problems
			 * arise if a chip which does auto increment
			 * is marked as NOAUTOINCR by the board driver.
			 */
1416
			if (!this->dev_ready)
1417
				udelay(this->chip_delay);
1418 1419 1420
			else
				nand_wait_ready(mtd);

1421 1422
			/* Check, if the chip supports auto page increment
			 * or if we have hit a block boundary.
1423
			 */
L
Linus Torvalds 已提交
1424 1425
			if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
				/* For subsequent page reads set offset to 0 */
1426
				this->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
			}
		}
	}

	/* Deselect and wake up anyone waiting on the device */
	nand_release_device(mtd);

	/* Return happy */
	*retlen = len;
	return 0;
}

/**
 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
 * @from:	offset to read from
 * @len:	number of bytes to read
 * @ooblen:	number of oob data bytes to read
 *
 * Read raw data including oob into buffer
 */
1449
int nand_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
L
Linus Torvalds 已提交
1450 1451
{
	struct nand_chip *this = mtd->priv;
1452 1453
	int page = (int)(from >> this->page_shift);
	int chip = (int)(from >> this->chip_shift);
L
Linus Torvalds 已提交
1454 1455
	int sndcmd = 1;
	int cnt = 0;
J
Joern Engel 已提交
1456
	int pagesize = mtd->writesize + mtd->oobsize;
1457
	int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
L
Linus Torvalds 已提交
1458 1459 1460

	/* Do not allow reads past end of device */
	if ((from + len) > mtd->size) {
1461
		DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
L
Linus Torvalds 已提交
1462 1463 1464 1465
		return -EINVAL;
	}

	/* Grab the lock and see if the device is available */
1466
	nand_get_device(this, mtd, FL_READING);
L
Linus Torvalds 已提交
1467

1468
	this->select_chip(mtd, chip);
1469

L
Linus Torvalds 已提交
1470 1471
	/* Add requested oob length */
	len += ooblen;
1472

L
Linus Torvalds 已提交
1473 1474
	while (len) {
		if (sndcmd)
1475
			this->cmdfunc(mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1476
		sndcmd = 0;
L
Linus Torvalds 已提交
1477

1478
		this->read_buf(mtd, &buf[cnt], pagesize);
L
Linus Torvalds 已提交
1479 1480 1481 1482

		len -= pagesize;
		cnt += pagesize;
		page++;
1483 1484

		if (!this->dev_ready)
1485
			udelay(this->chip_delay);
L
Linus Torvalds 已提交
1486
		else
1487
			nand_wait_ready(mtd);
1488 1489

		/* Check, if the chip supports auto page increment */
L
Linus Torvalds 已提交
1490 1491 1492 1493 1494 1495 1496 1497 1498
		if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
			sndcmd = 1;
	}

	/* Deselect and wake up anyone waiting on the device */
	nand_release_device(mtd);
	return 0;
}

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
/**
 * nand_write_raw - [GENERIC] Write raw data including oob
 * @mtd:	MTD device structure
 * @buf:	source buffer
 * @to:		offset to write to
 * @len:	number of bytes to write
 * @buf:	source buffer
 * @oob:	oob buffer
 *
 * Write raw data including oob
 */
int nand_write_raw(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
		   uint8_t *buf, uint8_t *oob)
{
	struct nand_chip *this = mtd->priv;
	int page = (int)(to >> this->page_shift);
	int chip = (int)(to >> this->chip_shift);
	int ret;

	*retlen = 0;

	/* Do not allow writes past end of device */
	if ((to + len) > mtd->size) {
		DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt write "
		      "beyond end of device\n");
		return -EINVAL;
	}

	/* Grab the lock and see if the device is available */
	nand_get_device(this, mtd, FL_WRITING);

	this->select_chip(mtd, chip);
	this->data_poi = buf;

	while (len != *retlen) {
		ret = nand_write_page(mtd, this, page, oob, &mtd->oobinfo, 0);
		if (ret)
			return ret;
		page++;
		*retlen += mtd->writesize;
		this->data_poi += mtd->writesize;
		oob += mtd->oobsize;
	}

	/* Deselect and wake up anyone waiting on the device */
	nand_release_device(mtd);
	return 0;
}
T
Thomas Gleixner 已提交
1547
EXPORT_SYMBOL_GPL(nand_write_raw);
1548

1549 1550
/**
 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
L
Linus Torvalds 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
 * @mtd:	MTD device structure
 * @fsbuf:	buffer given by fs driver
 * @oobsel:	out of band selection structre
 * @autoplace:	1 = place given buffer into the oob bytes
 * @numpages:	number of pages to prepare
 *
 * Return:
 * 1. Filesystem buffer available and autoplacement is off,
 *    return filesystem buffer
 * 2. No filesystem buffer or autoplace is off, return internal
 *    buffer
 * 3. Filesystem buffer is given and autoplace selected
 *    put data from fs buffer into internal buffer and
 *    retrun internal buffer
 *
 * Note: The internal buffer is filled with 0xff. This must
 * be done only once, when no autoplacement happens
 * Autoplacement sets the buffer dirty flag, which
 * forces the 0xff fill before using the buffer again.
 *
*/
1572
static uint8_t *nand_prepare_oobbuf(struct mtd_info *mtd, uint8_t *fsbuf, struct nand_oobinfo *oobsel,
1573
				   int autoplace, int numpages)
L
Linus Torvalds 已提交
1574 1575 1576 1577 1578
{
	struct nand_chip *this = mtd->priv;
	int i, len, ofs;

	/* Zero copy fs supplied buffer */
1579
	if (fsbuf && !autoplace)
L
Linus Torvalds 已提交
1580 1581 1582
		return fsbuf;

	/* Check, if the buffer must be filled with ff again */
1583
	if (this->oobdirty) {
1584
		memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
L
Linus Torvalds 已提交
1585
		this->oobdirty = 0;
1586 1587
	}

L
Linus Torvalds 已提交
1588 1589 1590
	/* If we have no autoplacement or no fs buffer use the internal one */
	if (!autoplace || !fsbuf)
		return this->oob_buf;
1591

L
Linus Torvalds 已提交
1592 1593 1594 1595 1596 1597 1598
	/* Walk through the pages and place the data */
	this->oobdirty = 1;
	ofs = 0;
	while (numpages--) {
		for (i = 0, len = 0; len < mtd->oobavail; i++) {
			int to = ofs + oobsel->oobfree[i][0];
			int num = oobsel->oobfree[i][1];
1599
			memcpy(&this->oob_buf[to], fsbuf, num);
L
Linus Torvalds 已提交
1600 1601 1602 1603 1604 1605 1606 1607
			len += num;
			fsbuf += num;
		}
		ofs += mtd->oobavail;
	}
	return this->oob_buf;
}

J
Joern Engel 已提交
1608
#define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0
L
Linus Torvalds 已提交
1609 1610

/**
1611
 * nand_write - [MTD Interface] NAND write with ECC
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616 1617 1618 1619
 * @mtd:	MTD device structure
 * @to:		offset to write to
 * @len:	number of bytes to write
 * @retlen:	pointer to variable to store the number of written bytes
 * @buf:	the data to write
 *
 * NAND write with ECC
 */
1620 1621
static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
			  size_t *retlen, const uint8_t *buf)
L
Linus Torvalds 已提交
1622 1623 1624 1625
{
	int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
	int autoplace = 0, numpages, totalpages;
	struct nand_chip *this = mtd->priv;
1626
	uint8_t *oobbuf, *bufstart, *eccbuf = NULL;
1627
	int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1628
	struct nand_oobinfo *oobsel = &mtd->oobinfo;
L
Linus Torvalds 已提交
1629

1630
	DEBUG(MTD_DEBUG_LEVEL3, "nand_write: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
L
Linus Torvalds 已提交
1631 1632 1633 1634 1635 1636

	/* Initialize retlen, in case of early exit */
	*retlen = 0;

	/* Do not allow write past end of device */
	if ((to + len) > mtd->size) {
1637
		DEBUG(MTD_DEBUG_LEVEL0, "nand_write: Attempt to write past end of page\n");
L
Linus Torvalds 已提交
1638 1639 1640
		return -EINVAL;
	}

1641
	/* reject writes, which are not page aligned */
1642
	if (NOTALIGNED(to) || NOTALIGNED(len)) {
1643
		printk(KERN_NOTICE "nand_write: Attempt to write not page aligned data\n");
L
Linus Torvalds 已提交
1644 1645 1646 1647
		return -EINVAL;
	}

	/* Grab the lock and see if the device is available */
1648
	nand_get_device(this, mtd, FL_WRITING);
L
Linus Torvalds 已提交
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662

	/* Calculate chipnr */
	chipnr = (int)(to >> this->chip_shift);
	/* Select the NAND device */
	this->select_chip(mtd, chipnr);

	/* Check, if it is write protected */
	if (nand_check_wp(mtd))
		goto out;

	/* Autoplace of oob data ? Use the default placement scheme */
	if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
		oobsel = this->autooob;
		autoplace = 1;
1663
	}
1664 1665
	if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
		autoplace = 1;
L
Linus Torvalds 已提交
1666 1667 1668

	/* Setup variables and oob buffer */
	totalpages = len >> this->page_shift;
1669
	page = (int)(to >> this->page_shift);
L
Linus Torvalds 已提交
1670
	/* Invalidate the page cache, if we write to the cached page */
1671
	if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
L
Linus Torvalds 已提交
1672
		this->pagebuf = -1;
1673

L
Linus Torvalds 已提交
1674 1675 1676 1677
	/* Set it relative to chip */
	page &= this->pagemask;
	startpage = page;
	/* Calc number of pages we can write in one go */
1678 1679
	numpages = min(ppblock - (startpage & (ppblock - 1)), totalpages);
	oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
1680
	bufstart = (uint8_t *) buf;
L
Linus Torvalds 已提交
1681 1682 1683 1684

	/* Loop until all data is written */
	while (written < len) {

1685
		this->data_poi = (uint8_t *) &buf[written];
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690
		/* Write one page. If this is the last page to write
		 * or the last page in this block, then use the
		 * real pageprogram command, else select cached programming
		 * if supported by the chip.
		 */
1691
		ret = nand_write_page(mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
L
Linus Torvalds 已提交
1692
		if (ret) {
1693
			DEBUG(MTD_DEBUG_LEVEL0, "nand_write: write_page failed %d\n", ret);
L
Linus Torvalds 已提交
1694
			goto out;
1695
		}
L
Linus Torvalds 已提交
1696 1697 1698
		/* Next oob page */
		oob += mtd->oobsize;
		/* Update written bytes count */
J
Joern Engel 已提交
1699
		written += mtd->writesize;
1700
		if (written == len)
L
Linus Torvalds 已提交
1701
			goto cmp;
1702

L
Linus Torvalds 已提交
1703 1704 1705 1706 1707 1708
		/* Increment page address */
		page++;

		/* Have we hit a block boundary ? Then we have to verify and
		 * if verify is ok, we have to setup the oob buffer for
		 * the next pages.
1709 1710
		 */
		if (!(page & (ppblock - 1))) {
L
Linus Torvalds 已提交
1711 1712
			int ofs;
			this->data_poi = bufstart;
1713 1714
			ret = nand_verify_pages(mtd, this, startpage, page - startpage,
						oobbuf, oobsel, chipnr, (eccbuf != NULL));
L
Linus Torvalds 已提交
1715
			if (ret) {
1716
				DEBUG(MTD_DEBUG_LEVEL0, "nand_write: verify_pages failed %d\n", ret);
L
Linus Torvalds 已提交
1717
				goto out;
1718
			}
L
Linus Torvalds 已提交
1719 1720 1721 1722 1723 1724
			*retlen = written;

			ofs = autoplace ? mtd->oobavail : mtd->oobsize;
			if (eccbuf)
				eccbuf += (page - startpage) * ofs;
			totalpages -= page - startpage;
1725
			numpages = min(totalpages, ppblock);
L
Linus Torvalds 已提交
1726 1727
			page &= this->pagemask;
			startpage = page;
1728
			oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
1729
			oob = 0;
L
Linus Torvalds 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738
			/* Check, if we cross a chip boundary */
			if (!page) {
				chipnr++;
				this->select_chip(mtd, -1);
				this->select_chip(mtd, chipnr);
			}
		}
	}
	/* Verify the remaining pages */
1739
 cmp:
L
Linus Torvalds 已提交
1740
	this->data_poi = bufstart;
1741
	ret = nand_verify_pages(mtd, this, startpage, totalpages, oobbuf, oobsel, chipnr, (eccbuf != NULL));
L
Linus Torvalds 已提交
1742 1743
	if (!ret)
		*retlen = written;
1744
	else
1745
		DEBUG(MTD_DEBUG_LEVEL0, "nand_write: verify_pages failed %d\n", ret);
L
Linus Torvalds 已提交
1746

1747
 out:
L
Linus Torvalds 已提交
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
	/* Deselect and wake up anyone waiting on the device */
	nand_release_device(mtd);

	return ret;
}

/**
 * nand_write_oob - [MTD Interface] NAND write out-of-band
 * @mtd:	MTD device structure
 * @to:		offset to write to
 * @len:	number of bytes to write
 * @retlen:	pointer to variable to store the number of written bytes
 * @buf:	the data to write
 *
 * NAND write out-of-band
 */
1764
static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const uint8_t *buf)
L
Linus Torvalds 已提交
1765 1766 1767 1768
{
	int column, page, status, ret = -EIO, chipnr;
	struct nand_chip *this = mtd->priv;

1769
	DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
L
Linus Torvalds 已提交
1770 1771

	/* Shift to get page */
1772 1773
	page = (int)(to >> this->page_shift);
	chipnr = (int)(to >> this->chip_shift);
L
Linus Torvalds 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782

	/* Mask to get column */
	column = to & (mtd->oobsize - 1);

	/* Initialize return length value */
	*retlen = 0;

	/* Do not allow write past end of page */
	if ((column + len) > mtd->oobsize) {
1783
		DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
L
Linus Torvalds 已提交
1784 1785 1786 1787
		return -EINVAL;
	}

	/* Grab the lock and see if the device is available */
1788
	nand_get_device(this, mtd, FL_WRITING);
L
Linus Torvalds 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802

	/* Select the NAND device */
	this->select_chip(mtd, chipnr);

	/* Reset the chip. Some chips (like the Toshiba TC5832DC found
	   in one of my DiskOnChip 2000 test units) will clear the whole
	   data page too if we don't do this. I have no clue why, but
	   I seem to have 'fixed' it in the doc2000 driver in
	   August 1999.  dwmw2. */
	this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);

	/* Check, if it is write protected */
	if (nand_check_wp(mtd))
		goto out;
1803

L
Linus Torvalds 已提交
1804 1805 1806 1807 1808 1809
	/* Invalidate the page cache, if we write to the cached page */
	if (page == this->pagebuf)
		this->pagebuf = -1;

	if (NAND_MUST_PAD(this)) {
		/* Write out desired data */
J
Joern Engel 已提交
1810
		this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page & this->pagemask);
L
Linus Torvalds 已提交
1811 1812 1813 1814 1815
		/* prepad 0xff for partial programming */
		this->write_buf(mtd, ffchars, column);
		/* write data */
		this->write_buf(mtd, buf, len);
		/* postpad 0xff for partial programming */
1816
		this->write_buf(mtd, ffchars, mtd->oobsize - (len + column));
L
Linus Torvalds 已提交
1817 1818
	} else {
		/* Write out desired data */
J
Joern Engel 已提交
1819
		this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + column, page & this->pagemask);
L
Linus Torvalds 已提交
1820 1821 1822 1823
		/* write data */
		this->write_buf(mtd, buf, len);
	}
	/* Send command to program the OOB data */
1824
	this->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
L
Linus Torvalds 已提交
1825

1826
	status = this->waitfunc(mtd, this, FL_WRITING);
L
Linus Torvalds 已提交
1827 1828

	/* See if device thinks it succeeded */
1829
	if (status & NAND_STATUS_FAIL) {
1830
		DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
L
Linus Torvalds 已提交
1831 1832 1833 1834 1835 1836 1837 1838
		ret = -EIO;
		goto out;
	}
	/* Return happy */
	*retlen = len;

#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
	/* Send command to read back the data */
1839
	this->cmdfunc(mtd, NAND_CMD_READOOB, column, page & this->pagemask);
L
Linus Torvalds 已提交
1840 1841

	if (this->verify_buf(mtd, buf, len)) {
1842
		DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
L
Linus Torvalds 已提交
1843 1844 1845 1846 1847
		ret = -EIO;
		goto out;
	}
#endif
	ret = 0;
1848
 out:
L
Linus Torvalds 已提交
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
	/* Deselect and wake up anyone waiting on the device */
	nand_release_device(mtd);

	return ret;
}

/**
 * single_erease_cmd - [GENERIC] NAND standard block erase command function
 * @mtd:	MTD device structure
 * @page:	the page address of the block which will be erased
 *
 * Standard erase command for NAND chips
 */
1862
static void single_erase_cmd(struct mtd_info *mtd, int page)
L
Linus Torvalds 已提交
1863 1864 1865
{
	struct nand_chip *this = mtd->priv;
	/* Send commands to erase a block */
1866 1867
	this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
	this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
L
Linus Torvalds 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
}

/**
 * multi_erease_cmd - [GENERIC] AND specific block erase command function
 * @mtd:	MTD device structure
 * @page:	the page address of the block which will be erased
 *
 * AND multi block erase command function
 * Erase 4 consecutive blocks
 */
1878
static void multi_erase_cmd(struct mtd_info *mtd, int page)
L
Linus Torvalds 已提交
1879 1880 1881
{
	struct nand_chip *this = mtd->priv;
	/* Send commands to erase a block */
1882 1883 1884 1885 1886
	this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
	this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
	this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
	this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
	this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
L
Linus Torvalds 已提交
1887 1888 1889 1890 1891 1892 1893 1894 1895
}

/**
 * nand_erase - [MTD Interface] erase block(s)
 * @mtd:	MTD device structure
 * @instr:	erase instruction
 *
 * Erase one ore more blocks
 */
1896
static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
L
Linus Torvalds 已提交
1897
{
1898
	return nand_erase_nand(mtd, instr, 0);
L
Linus Torvalds 已提交
1899
}
1900

1901
#define BBT_PAGE_MASK	0xffffff3f
L
Linus Torvalds 已提交
1902 1903 1904 1905 1906 1907 1908 1909
/**
 * nand_erase_intern - [NAND Interface] erase block(s)
 * @mtd:	MTD device structure
 * @instr:	erase instruction
 * @allowbbt:	allow erasing the bbt area
 *
 * Erase one ore more blocks
 */
1910
int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
L
Linus Torvalds 已提交
1911 1912 1913
{
	int page, len, status, pages_per_block, ret, chipnr;
	struct nand_chip *this = mtd->priv;
1914 1915 1916 1917
	int rewrite_bbt[NAND_MAX_CHIPS]={0};	/* flags to indicate the page, if bbt needs to be rewritten. */
	unsigned int bbt_masked_page;		/* bbt mask to compare to page being erased. */
						/* It is used to see if the current page is in the same */
						/*   256 block group and the same bank as the bbt. */
L
Linus Torvalds 已提交
1918

1919
	DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n", (unsigned int)instr->addr, (unsigned int)instr->len);
L
Linus Torvalds 已提交
1920 1921 1922

	/* Start address must align on block boundary */
	if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
1923
		DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
L
Linus Torvalds 已提交
1924 1925 1926 1927 1928
		return -EINVAL;
	}

	/* Length must align on block boundary */
	if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
1929
		DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934
		return -EINVAL;
	}

	/* Do not allow erase past end of device */
	if ((instr->len + instr->addr) > mtd->size) {
1935
		DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
L
Linus Torvalds 已提交
1936 1937 1938 1939 1940 1941
		return -EINVAL;
	}

	instr->fail_addr = 0xffffffff;

	/* Grab the lock and see if the device is available */
1942
	nand_get_device(this, mtd, FL_ERASING);
L
Linus Torvalds 已提交
1943 1944

	/* Shift to get first page */
1945 1946
	page = (int)(instr->addr >> this->page_shift);
	chipnr = (int)(instr->addr >> this->chip_shift);
L
Linus Torvalds 已提交
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956

	/* Calculate pages in each block */
	pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);

	/* Select the NAND device */
	this->select_chip(mtd, chipnr);

	/* Check the WP bit */
	/* Check, if it is write protected */
	if (nand_check_wp(mtd)) {
1957
		DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
L
Linus Torvalds 已提交
1958 1959 1960 1961
		instr->state = MTD_ERASE_FAILED;
		goto erase_exit;
	}

1962 1963 1964 1965 1966 1967 1968
	/* if BBT requires refresh, set the BBT page mask to see if the BBT should be rewritten */
	if (this->options & BBT_AUTO_REFRESH) {
		bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
	} else {
		bbt_masked_page = 0xffffffff;	/* should not match anything */
	}

L
Linus Torvalds 已提交
1969 1970 1971 1972 1973 1974 1975 1976
	/* Loop through the pages */
	len = instr->len;

	instr->state = MTD_ERASING;

	while (len) {
		/* Check if we have a bad block, we do not erase bad blocks ! */
		if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
1977
			printk(KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
L
Linus Torvalds 已提交
1978 1979 1980
			instr->state = MTD_ERASE_FAILED;
			goto erase_exit;
		}
1981 1982

		/* Invalidate the page cache, if we erase the block which contains
L
Linus Torvalds 已提交
1983 1984 1985 1986
		   the current cached page */
		if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
			this->pagebuf = -1;

1987
		this->erase_cmd(mtd, page & this->pagemask);
1988

1989
		status = this->waitfunc(mtd, this, FL_ERASING);
L
Linus Torvalds 已提交
1990

1991 1992 1993 1994 1995
		/* See if operation failed and additional status checks are available */
		if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
			status = this->errstat(mtd, this, FL_ERASING, status, page);
		}

L
Linus Torvalds 已提交
1996
		/* See if block erase succeeded */
1997
		if (status & NAND_STATUS_FAIL) {
1998
			DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
L
Linus Torvalds 已提交
1999 2000 2001 2002
			instr->state = MTD_ERASE_FAILED;
			instr->fail_addr = (page << this->page_shift);
			goto erase_exit;
		}
2003 2004 2005

		/* if BBT requires refresh, set the BBT rewrite flag to the page being erased */
		if (this->options & BBT_AUTO_REFRESH) {
2006
			if (((page & BBT_PAGE_MASK) == bbt_masked_page) &&
2007 2008 2009 2010
			     (page != this->bbt_td->pages[chipnr])) {
				rewrite_bbt[chipnr] = (page << this->page_shift);
			}
		}
2011

L
Linus Torvalds 已提交
2012 2013 2014 2015 2016 2017 2018 2019 2020
		/* Increment page address and decrement length */
		len -= (1 << this->phys_erase_shift);
		page += pages_per_block;

		/* Check, if we cross a chip boundary */
		if (len && !(page & this->pagemask)) {
			chipnr++;
			this->select_chip(mtd, -1);
			this->select_chip(mtd, chipnr);
2021

2022
			/* if BBT requires refresh and BBT-PERCHIP,
2023 2024 2025 2026 2027
			 *   set the BBT page mask to see if this BBT should be rewritten */
			if ((this->options & BBT_AUTO_REFRESH) && (this->bbt_td->options & NAND_BBT_PERCHIP)) {
				bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
			}

L
Linus Torvalds 已提交
2028 2029 2030 2031
		}
	}
	instr->state = MTD_ERASE_DONE;

2032
 erase_exit:
L
Linus Torvalds 已提交
2033 2034 2035 2036 2037 2038 2039 2040 2041

	ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
	/* Do call back function */
	if (!ret)
		mtd_erase_callback(instr);

	/* Deselect and wake up anyone waiting on the device */
	nand_release_device(mtd);

2042 2043 2044 2045 2046
	/* if BBT requires refresh and erase was successful, rewrite any selected bad block tables */
	if ((this->options & BBT_AUTO_REFRESH) && (!ret)) {
		for (chipnr = 0; chipnr < this->numchips; chipnr++) {
			if (rewrite_bbt[chipnr]) {
				/* update the BBT for chip */
2047 2048 2049
				DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt (%d:0x%0x 0x%0x)\n",
				      chipnr, rewrite_bbt[chipnr], this->bbt_td->pages[chipnr]);
				nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2050 2051 2052 2053
			}
		}
	}

L
Linus Torvalds 已提交
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
	/* Return more or less happy */
	return ret;
}

/**
 * nand_sync - [MTD Interface] sync
 * @mtd:	MTD device structure
 *
 * Sync is actually a wait for chip ready function
 */
2064
static void nand_sync(struct mtd_info *mtd)
L
Linus Torvalds 已提交
2065 2066 2067
{
	struct nand_chip *this = mtd->priv;

2068
	DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
L
Linus Torvalds 已提交
2069 2070

	/* Grab the lock and see if the device is available */
2071
	nand_get_device(this, mtd, FL_SYNCING);
L
Linus Torvalds 已提交
2072
	/* Release it and go back */
2073
	nand_release_device(mtd);
L
Linus Torvalds 已提交
2074 2075 2076 2077 2078 2079 2080
}

/**
 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
 * @mtd:	MTD device structure
 * @ofs:	offset relative to mtd start
 */
2081
static int nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
2082 2083
{
	/* Check for invalid offset */
2084
	if (ofs > mtd->size)
L
Linus Torvalds 已提交
2085
		return -EINVAL;
2086

2087
	return nand_block_checkbad(mtd, ofs, 1, 0);
L
Linus Torvalds 已提交
2088 2089 2090 2091 2092 2093 2094
}

/**
 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
 * @mtd:	MTD device structure
 * @ofs:	offset relative to mtd start
 */
2095
static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
2096 2097 2098 2099
{
	struct nand_chip *this = mtd->priv;
	int ret;

2100 2101
	if ((ret = nand_block_isbad(mtd, ofs))) {
		/* If it was bad already, return success and do nothing. */
L
Linus Torvalds 已提交
2102 2103
		if (ret > 0)
			return 0;
2104 2105
		return ret;
	}
L
Linus Torvalds 已提交
2106 2107 2108 2109

	return this->block_markbad(mtd, ofs);
}

2110 2111 2112 2113 2114 2115 2116 2117
/**
 * nand_suspend - [MTD Interface] Suspend the NAND flash
 * @mtd:	MTD device structure
 */
static int nand_suspend(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;

2118
	return nand_get_device(this, mtd, FL_PM_SUSPENDED);
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
}

/**
 * nand_resume - [MTD Interface] Resume the NAND flash
 * @mtd:	MTD device structure
 */
static void nand_resume(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;

	if (this->state == FL_PM_SUSPENDED)
		nand_release_device(mtd);
	else
2132 2133
		printk(KERN_ERR "nand_resume() called for a chip which is not "
		       "in suspended state\n");
2134 2135
}

T
Thomas Gleixner 已提交
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
/*
 * Free allocated data structures
 */
static void nand_free_kmem(struct nand_chip *this)
{
	/* Buffer allocated by nand_scan ? */
	if (this->options & NAND_OOBBUF_ALLOC)
		kfree(this->oob_buf);
	/* Buffer allocated by nand_scan ? */
	if (this->options & NAND_DATABUF_ALLOC)
		kfree(this->data_buf);
	/* Controller allocated by nand_scan ? */
	if (this->options & NAND_CONTROLLER_ALLOC)
		kfree(this->controller);
}

T
Thomas Gleixner 已提交
2152 2153
/*
 * Allocate buffers and data structures
L
Linus Torvalds 已提交
2154
 */
T
Thomas Gleixner 已提交
2155
static int nand_allocate_kmem(struct mtd_info *mtd, struct nand_chip *this)
L
Linus Torvalds 已提交
2156
{
T
Thomas Gleixner 已提交
2157
	size_t len;
L
Linus Torvalds 已提交
2158

T
Thomas Gleixner 已提交
2159 2160 2161 2162 2163 2164 2165
	if (!this->oob_buf) {
		len = mtd->oobsize <<
			(this->phys_erase_shift - this->page_shift);
		this->oob_buf = kmalloc(len, GFP_KERNEL);
		if (!this->oob_buf)
			goto outerr;
		this->options |= NAND_OOBBUF_ALLOC;
2166 2167
	}

T
Thomas Gleixner 已提交
2168
	if (!this->data_buf) {
2169
		len = mtd->writesize + mtd->oobsize;
T
Thomas Gleixner 已提交
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
		this->data_buf = kmalloc(len, GFP_KERNEL);
		if (!this->data_buf)
			goto outerr;
		this->options |= NAND_DATABUF_ALLOC;
	}

	if (!this->controller) {
		this->controller = kzalloc(sizeof(struct nand_hw_control),
					   GFP_KERNEL);
		if (!this->controller)
			goto outerr;
		this->options |= NAND_CONTROLLER_ALLOC;
	}
	return 0;
L
Linus Torvalds 已提交
2184

T
Thomas Gleixner 已提交
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
 outerr:
	printk(KERN_ERR "nand_scan(): Cannot allocate buffers\n");
	nand_free_kmem(this);
	return -ENOMEM;
}

/*
 * Set default functions
 */
static void nand_set_defaults(struct nand_chip *this, int busw)
{
L
Linus Torvalds 已提交
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
	/* check for proper chip_delay setup, set 20us if not */
	if (!this->chip_delay)
		this->chip_delay = 20;

	/* check, if a user supplied command function given */
	if (this->cmdfunc == NULL)
		this->cmdfunc = nand_command;

	/* check, if a user supplied wait function given */
	if (this->waitfunc == NULL)
		this->waitfunc = nand_wait;

	if (!this->select_chip)
		this->select_chip = nand_select_chip;
	if (!this->write_byte)
		this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
	if (!this->read_byte)
		this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
	if (!this->write_word)
		this->write_word = nand_write_word;
	if (!this->read_word)
		this->read_word = nand_read_word;
	if (!this->block_bad)
		this->block_bad = nand_block_bad;
	if (!this->block_markbad)
		this->block_markbad = nand_default_block_markbad;
	if (!this->write_buf)
		this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
	if (!this->read_buf)
		this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
	if (!this->verify_buf)
		this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
	if (!this->scan_bbt)
		this->scan_bbt = nand_default_bbt;
T
Thomas Gleixner 已提交
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
}

/*
 * Get the flash and manufacturer id and lookup if the typ is supported
 */
static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
						  struct nand_chip *this,
						  int busw, int *maf_id)
{
	struct nand_flash_dev *type = NULL;
	int i, dev_id, maf_idx;
L
Linus Torvalds 已提交
2241 2242 2243 2244 2245

	/* Select the device */
	this->select_chip(mtd, 0);

	/* Send the command for reading device ID */
2246
	this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
L
Linus Torvalds 已提交
2247 2248

	/* Read manufacturer and device IDs */
T
Thomas Gleixner 已提交
2249 2250
	*maf_id = this->read_byte(mtd);
	dev_id = this->read_byte(mtd);
L
Linus Torvalds 已提交
2251

T
Thomas Gleixner 已提交
2252
	/* Lookup the flash id */
L
Linus Torvalds 已提交
2253
	for (i = 0; nand_flash_ids[i].name != NULL; i++) {
T
Thomas Gleixner 已提交
2254 2255 2256 2257 2258
		if (dev_id == nand_flash_ids[i].id) {
			type =  &nand_flash_ids[i];
			break;
		}
	}
2259

T
Thomas Gleixner 已提交
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
	if (!type)
		return ERR_PTR(-ENODEV);

	this->chipsize = nand_flash_ids[i].chipsize << 20;

	/* Newer devices have all the information in additional id bytes */
	if (!nand_flash_ids[i].pagesize) {
		int extid;
		/* The 3rd id byte contains non relevant data ATM */
		extid = this->read_byte(mtd);
		/* The 4th id byte is the important one */
		extid = this->read_byte(mtd);
		/* Calc pagesize */
2273
		mtd->writesize = 1024 << (extid & 0x3);
T
Thomas Gleixner 已提交
2274 2275
		extid >>= 2;
		/* Calc oobsize */
2276
		mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
T
Thomas Gleixner 已提交
2277 2278 2279 2280 2281 2282
		extid >>= 2;
		/* Calc blocksize. Blocksize is multiples of 64KiB */
		mtd->erasesize = (64 * 1024) << (extid & 0x03);
		extid >>= 2;
		/* Get buswidth information */
		busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2283

T
Thomas Gleixner 已提交
2284 2285 2286 2287 2288
	} else {
		/*
		 * Old devices have this data hardcoded in the device id table
		 */
		mtd->erasesize = nand_flash_ids[i].erasesize;
2289 2290
		mtd->writesize = nand_flash_ids[i].pagesize;
		mtd->oobsize = mtd->writesize / 32;
T
Thomas Gleixner 已提交
2291 2292
		busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
	}
L
Linus Torvalds 已提交
2293

T
Thomas Gleixner 已提交
2294 2295 2296 2297 2298
	/* Try to identify manufacturer */
	for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_id++) {
		if (nand_manuf_ids[maf_idx].id == *maf_id)
			break;
	}
2299

T
Thomas Gleixner 已提交
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
	/*
	 * Check, if buswidth is correct. Hardware drivers should set
	 * this correct !
	 */
	if (busw != (this->options & NAND_BUSWIDTH_16)) {
		printk(KERN_INFO "NAND device: Manufacturer ID:"
		       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
		       dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
		printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
		       (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
		       busw ? 16 : 8);
		return ERR_PTR(-EINVAL);
	}
2313

T
Thomas Gleixner 已提交
2314
	/* Calculate the address shift from the page size */
2315
	this->page_shift = ffs(mtd->writesize) - 1;
T
Thomas Gleixner 已提交
2316 2317
	/* Convert chipsize to number of pages per chip -1. */
	this->pagemask = (this->chipsize >> this->page_shift) - 1;
2318

T
Thomas Gleixner 已提交
2319 2320 2321
	this->bbt_erase_shift = this->phys_erase_shift =
		ffs(mtd->erasesize) - 1;
	this->chip_shift = ffs(this->chipsize) - 1;
L
Linus Torvalds 已提交
2322

T
Thomas Gleixner 已提交
2323
	/* Set the bad block position */
2324
	this->badblockpos = mtd->writesize > 512 ?
T
Thomas Gleixner 已提交
2325
		NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2326

T
Thomas Gleixner 已提交
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
	/* Get chip options, preserve non chip based options */
	this->options &= ~NAND_CHIPOPTIONS_MSK;
	this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;

	/*
	 * Set this as a default. Board drivers can override it, if necessary
	 */
	this->options |= NAND_NO_AUTOINCR;

	/* Check if this is a not a samsung device. Do not clear the
	 * options for chips which are not having an extended id.
	 */
	if (*maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
		this->options &= ~NAND_SAMSUNG_LP_OPTIONS;

	/* Check for AND chips with 4 page planes */
	if (this->options & NAND_4PAGE_ARRAY)
		this->erase_cmd = multi_erase_cmd;
	else
		this->erase_cmd = single_erase_cmd;

	/* Do not replace user supplied command function ! */
2349
	if (mtd->writesize > 512 && this->cmdfunc == nand_command)
T
Thomas Gleixner 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
		this->cmdfunc = nand_command_lp;

	printk(KERN_INFO "NAND device: Manufacturer ID:"
	       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
	       nand_manuf_ids[maf_idx].name, type->name);

	return type;
}

/* module_text_address() isn't exported, and it's mostly a pointless
   test if this is a module _anyway_ -- they'd have to try _really_ hard
   to call us from in-kernel code if the core NAND support is modular. */
#ifdef MODULE
#define caller_is_module() (1)
#else
#define caller_is_module() \
	module_text_address((unsigned long)__builtin_return_address(0))
#endif

/**
 * nand_scan - [NAND Interface] Scan for the NAND device
 * @mtd:	MTD device structure
 * @maxchips:	Number of chips to scan for
 *
 * This fills out all the uninitialized function pointers
 * with the defaults.
 * The flash ID is read and the mtd/chip structures are
 * filled with the appropriate values. Buffers are allocated if
 * they are not provided by the board driver
 * The mtd->owner field must be set to the module of the caller
 *
 */
int nand_scan(struct mtd_info *mtd, int maxchips)
{
	int i, busw, nand_maf_id;
	struct nand_chip *this = mtd->priv;
	struct nand_flash_dev *type;

	/* Many callers got this wrong, so check for it for a while... */
	if (!mtd->owner && caller_is_module()) {
		printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
		BUG();
L
Linus Torvalds 已提交
2392 2393
	}

T
Thomas Gleixner 已提交
2394 2395 2396 2397 2398 2399 2400 2401 2402
	/* Get buswidth to select the correct functions */
	busw = this->options & NAND_BUSWIDTH_16;
	/* Set the default functions */
	nand_set_defaults(this, busw);

	/* Read the flash type */
	type = nand_get_flash_type(mtd, this, busw, &nand_maf_id);

	if (IS_ERR(type)) {
2403
		printk(KERN_WARNING "No NAND device found!!!\n");
L
Linus Torvalds 已提交
2404
		this->select_chip(mtd, -1);
T
Thomas Gleixner 已提交
2405
		return PTR_ERR(type);
L
Linus Torvalds 已提交
2406 2407
	}

T
Thomas Gleixner 已提交
2408
	/* Check for a chip array */
2409
	for (i = 1; i < maxchips; i++) {
L
Linus Torvalds 已提交
2410 2411
		this->select_chip(mtd, i);
		/* Send the command for reading device ID */
2412
		this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
L
Linus Torvalds 已提交
2413 2414
		/* Read manufacturer and device IDs */
		if (nand_maf_id != this->read_byte(mtd) ||
T
Thomas Gleixner 已提交
2415
		    type->id != this->read_byte(mtd))
L
Linus Torvalds 已提交
2416 2417 2418 2419
			break;
	}
	if (i > 1)
		printk(KERN_INFO "%d NAND chips detected\n", i);
2420

L
Linus Torvalds 已提交
2421 2422 2423
	/* Store the number of chips and calc total size for mtd */
	this->numchips = i;
	mtd->size = i * this->chipsize;
T
Thomas Gleixner 已提交
2424 2425 2426 2427 2428

	/* Allocate buffers and data structures */
	if (nand_allocate_kmem(mtd, this))
		return -ENOMEM;

L
Linus Torvalds 已提交
2429
	/* Preset the internal oob buffer */
T
Thomas Gleixner 已提交
2430 2431
	memset(this->oob_buf, 0xff,
	       mtd->oobsize << (this->phys_erase_shift - this->page_shift));
L
Linus Torvalds 已提交
2432

T
Thomas Gleixner 已提交
2433 2434 2435
	/*
	 * If no default placement scheme is given, select an appropriate one
	 */
L
Linus Torvalds 已提交
2436
	if (!this->autooob) {
2437
		switch (mtd->oobsize) {
L
Linus Torvalds 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
		case 8:
			this->autooob = &nand_oob_8;
			break;
		case 16:
			this->autooob = &nand_oob_16;
			break;
		case 64:
			this->autooob = &nand_oob_64;
			break;
		default:
T
Thomas Gleixner 已提交
2448 2449
			printk(KERN_WARNING "No oob scheme defined for "
			       "oobsize %d\n", mtd->oobsize);
L
Linus Torvalds 已提交
2450 2451 2452
			BUG();
		}
	}
2453

T
Thomas Gleixner 已提交
2454 2455 2456 2457
	/*
	 * The number of bytes available for the filesystem to place fs
	 * dependend oob data
	 */
2458 2459 2460
	mtd->oobavail = 0;
	for (i = 0; this->autooob->oobfree[i][1]; i++)
		mtd->oobavail += this->autooob->oobfree[i][1];
L
Linus Torvalds 已提交
2461

2462
	/*
T
Thomas Gleixner 已提交
2463 2464
	 * check ECC mode, default to software if 3byte/512byte hardware ECC is
	 * selected and we have 256 byte pagesize fallback to software ECC
2465
	 */
T
Thomas Gleixner 已提交
2466 2467 2468 2469 2470 2471 2472 2473 2474
	switch (this->ecc.mode) {
	case NAND_ECC_HW:
	case NAND_ECC_HW_SYNDROME:
		if (!this->ecc.calculate || !this->ecc.correct ||
		    !this->ecc.hwctl) {
			printk(KERN_WARNING "No ECC functions supplied, "
			       "Hardware ECC not possible\n");
			BUG();
		}
2475
		if (mtd->writesize >= this->ecc.size)
T
Thomas Gleixner 已提交
2476 2477 2478
			break;
		printk(KERN_WARNING "%d byte HW ECC not possible on "
		       "%d byte page size, fallback to SW ECC\n",
2479
		       this->ecc.size, mtd->writesize);
T
Thomas Gleixner 已提交
2480
		this->ecc.mode = NAND_ECC_SOFT;
2481

T
Thomas Gleixner 已提交
2482 2483 2484 2485 2486
	case NAND_ECC_SOFT:
		this->ecc.calculate = nand_calculate_ecc;
		this->ecc.correct = nand_correct_data;
		this->ecc.size = 256;
		this->ecc.bytes = 3;
L
Linus Torvalds 已提交
2487
		break;
2488 2489

	case NAND_ECC_NONE:
T
Thomas Gleixner 已提交
2490 2491
		printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
		       "This is not recommended !!\n");
2492
		this->ecc.size = mtd->writesize;
T
Thomas Gleixner 已提交
2493
		this->ecc.bytes = 0;
L
Linus Torvalds 已提交
2494 2495
		break;
	default:
T
Thomas Gleixner 已提交
2496
		printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
T
Thomas Gleixner 已提交
2497
		       this->ecc.mode);
2498
		BUG();
L
Linus Torvalds 已提交
2499
	}
2500

T
Thomas Gleixner 已提交
2501 2502 2503 2504
	/*
	 * Set the number of read / write steps for one page depending on ECC
	 * mode
	 */
2505 2506
	this->ecc.steps = mtd->writesize / this->ecc.size;
	if(this->ecc.steps * this->ecc.size != mtd->writesize) {
T
Thomas Gleixner 已提交
2507 2508
		printk(KERN_WARNING "Invalid ecc parameters\n");
		BUG();
L
Linus Torvalds 已提交
2509
	}
2510

L
Linus Torvalds 已提交
2511 2512
	/* Initialize state, waitqueue and spinlock */
	this->state = FL_READY;
T
Thomas Gleixner 已提交
2513 2514
	init_waitqueue_head(&this->controller->wq);
	spin_lock_init(&this->controller->lock);
L
Linus Torvalds 已提交
2515 2516 2517 2518 2519 2520 2521 2522 2523

	/* De-select the device */
	this->select_chip(mtd, -1);

	/* Invalidate the pagebuffer reference */
	this->pagebuf = -1;

	/* Fill in remaining MTD driver data */
	mtd->type = MTD_NANDFLASH;
J
Joern Engel 已提交
2524
	mtd->flags = MTD_CAP_NANDFLASH;
L
Linus Torvalds 已提交
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
	mtd->ecctype = MTD_ECC_SW;
	mtd->erase = nand_erase;
	mtd->point = NULL;
	mtd->unpoint = NULL;
	mtd->read = nand_read;
	mtd->write = nand_write;
	mtd->read_oob = nand_read_oob;
	mtd->write_oob = nand_write_oob;
	mtd->sync = nand_sync;
	mtd->lock = NULL;
	mtd->unlock = NULL;
2536 2537
	mtd->suspend = nand_suspend;
	mtd->resume = nand_resume;
L
Linus Torvalds 已提交
2538 2539 2540 2541 2542 2543
	mtd->block_isbad = nand_block_isbad;
	mtd->block_markbad = nand_block_markbad;

	/* and make the autooob the default one */
	memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));

2544 2545 2546
	/* Check, if we should skip the bad block table scan */
	if (this->options & NAND_SKIP_BBTSCAN)
		return 0;
L
Linus Torvalds 已提交
2547 2548

	/* Build bad block table */
2549
	return this->scan_bbt(mtd);
L
Linus Torvalds 已提交
2550 2551 2552
}

/**
2553
 * nand_release - [NAND Interface] Free resources held by the NAND device
L
Linus Torvalds 已提交
2554 2555
 * @mtd:	MTD device structure
*/
2556
void nand_release(struct mtd_info *mtd)
L
Linus Torvalds 已提交
2557 2558 2559 2560 2561
{
	struct nand_chip *this = mtd->priv;

#ifdef CONFIG_MTD_PARTITIONS
	/* Deregister partitions */
2562
	del_mtd_partitions(mtd);
L
Linus Torvalds 已提交
2563 2564
#endif
	/* Deregister the device */
2565
	del_mtd_device(mtd);
L
Linus Torvalds 已提交
2566

J
Jesper Juhl 已提交
2567
	/* Free bad block table memory */
2568
	kfree(this->bbt);
T
Thomas Gleixner 已提交
2569 2570
	/* Free buffers */
	nand_free_kmem(this);
L
Linus Torvalds 已提交
2571 2572
}

2573 2574
EXPORT_SYMBOL_GPL(nand_scan);
EXPORT_SYMBOL_GPL(nand_release);
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589

static int __init nand_base_init(void)
{
	led_trigger_register_simple("nand-disk", &nand_led_trigger);
	return 0;
}

static void __exit nand_base_exit(void)
{
	led_trigger_unregister_simple(nand_led_trigger);
}

module_init(nand_base_init);
module_exit(nand_base_exit);

2590 2591 2592
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
MODULE_DESCRIPTION("Generic NAND flash driver code");