]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/nand/nand_base.c
Merge with /home/wd/git/u-boot/testing-NAND/ to add new NAND handling.
[people/ms/u-boot.git] / drivers / nand / nand_base.c
CommitLineData
932394ac
WD
1/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
ac7eb8a3 8 *
932394ac
WD
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
ac7eb8a3 11 *
932394ac
WD
12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002 Thomas Gleixner (tglx@linutronix.de)
14 *
ac7eb8a3 15 * 02-08-2004 tglx: support for strange chips, which cannot auto increment
932394ac
WD
16 * pages on read / read_oob
17 *
18 * 03-17-2004 tglx: Check ready before auto increment check. Simon Bayes
19 * pointed this out, as he marked an auto increment capable chip
20 * as NOAUTOINCR in the board driver.
21 * Make reads over block boundaries work too
22 *
23 * 04-14-2004 tglx: first working version for 2k page size chips
ac7eb8a3 24 *
932394ac
WD
25 * 05-19-2004 tglx: Basic support for Renesas AG-AND chips
26 *
27 * 09-24-2004 tglx: add support for hardware controllers (e.g. ECC) shared
28 * among multiple independend devices. Suggestions and initial patch
29 * from Ben Dooks <ben-mtd@fluff.org>
30 *
31 * Credits:
ac7eb8a3
WD
32 * David Woodhouse for adding multichip support
33 *
932394ac
WD
34 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
35 * rework for 2K page size chips
36 *
37 * TODO:
38 * Enable cached programming for 2k page size chips
39 * Check, if mtd->ecctype should be set to MTD_ECC_HW
40 * if we have HW ecc support.
41 * The AG-AND chips have nice features for speed improvement,
42 * which are not supported yet. Read / program 4 pages in one go.
43 *
44 * $Id: nand_base.c,v 1.126 2004/12/13 11:22:25 lavinen Exp $
45 *
46 * This program is free software; you can redistribute it and/or modify
47 * it under the terms of the GNU General Public License version 2 as
48 * published by the Free Software Foundation.
49 *
50 */
51
52/* XXX U-BOOT XXX */
53#if 0
54#include <linux/delay.h>
55#include <linux/errno.h>
56#include <linux/sched.h>
57#include <linux/slab.h>
58#include <linux/types.h>
59#include <linux/mtd/mtd.h>
60#include <linux/mtd/nand.h>
61#include <linux/mtd/nand_ecc.h>
62#include <linux/mtd/compatmac.h>
63#include <linux/interrupt.h>
64#include <linux/bitops.h>
65#include <asm/io.h>
66
67#ifdef CONFIG_MTD_PARTITIONS
68#include <linux/mtd/partitions.h>
69#endif
70
ac7eb8a3 71#endif
932394ac
WD
72
73#include <common.h>
038ccac5 74#ifdef CONFIG_NEW_NAND_CODE
932394ac
WD
75
76#if (CONFIG_COMMANDS & CFG_CMD_NAND)
77
78#include <malloc.h>
79#include <watchdog.h>
80#include <linux/mtd/compat.h>
81#include <linux/mtd/mtd.h>
82#include <linux/mtd/nand.h>
83#include <linux/mtd/nand_ecc.h>
84
85#include <asm/io.h>
86#include <asm/errno.h>
87
88#ifdef CONFIG_JFFS2_NAND
89#include <jffs2/jffs2.h>
90#endif
91
932394ac
WD
92/* Define default oob placement schemes for large and small page devices */
93static struct nand_oobinfo nand_oob_8 = {
94 .useecc = MTD_NANDECC_AUTOPLACE,
95 .eccbytes = 3,
96 .eccpos = {0, 1, 2},
97 .oobfree = { {3, 2}, {6, 2} }
98};
99
100static struct nand_oobinfo nand_oob_16 = {
101 .useecc = MTD_NANDECC_AUTOPLACE,
102 .eccbytes = 6,
103 .eccpos = {0, 1, 2, 3, 6, 7},
104 .oobfree = { {8, 8} }
105};
106
107static struct nand_oobinfo nand_oob_64 = {
108 .useecc = MTD_NANDECC_AUTOPLACE,
109 .eccbytes = 24,
110 .eccpos = {
ac7eb8a3
WD
111 40, 41, 42, 43, 44, 45, 46, 47,
112 48, 49, 50, 51, 52, 53, 54, 55,
932394ac
WD
113 56, 57, 58, 59, 60, 61, 62, 63},
114 .oobfree = { {2, 38} }
115};
116
117/* This is used for padding purposes in nand_write_oob */
118static u_char ffchars[] = {
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
125 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
126 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
127};
128
129/*
130 * NAND low-level MTD interface functions
131 */
132static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
133static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
134static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
135
136static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
137static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
138 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
139static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
140static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
141static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
142 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
143static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
144/* XXX U-BOOT XXX */
145#if 0
146static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
147 unsigned long count, loff_t to, size_t * retlen);
148static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
149 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
150#endif
151static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
152static void nand_sync (struct mtd_info *mtd);
153
154/* Some internal functions */
155static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
156 struct nand_oobinfo *oobsel, int mode);
157#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
ac7eb8a3 158static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
932394ac
WD
159 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
160#else
161#define nand_verify_pages(...) (0)
162#endif
ac7eb8a3 163
932394ac
WD
164static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
165
166/**
167 * nand_release_device - [GENERIC] release chip
168 * @mtd: MTD device structure
ac7eb8a3
WD
169 *
170 * Deselect, release chip lock and wake up anyone waiting on the device
932394ac
WD
171 */
172/* XXX U-BOOT XXX */
173#if 0
174static void nand_release_device (struct mtd_info *mtd)
175{
176 struct nand_chip *this = mtd->priv;
177
178 /* De-select the NAND device */
179 this->select_chip(mtd, -1);
180 /* Do we have a hardware controller ? */
181 if (this->controller) {
182 spin_lock(&this->controller->lock);
183 this->controller->active = NULL;
184 spin_unlock(&this->controller->lock);
185 }
186 /* Release the chip */
187 spin_lock (&this->chip_lock);
188 this->state = FL_READY;
189 wake_up (&this->wq);
190 spin_unlock (&this->chip_lock);
191}
192#else
8e9655f8
WD
193static void nand_release_device (struct mtd_info *mtd)
194{
195 struct nand_chip *this = mtd->priv;
196 this->select_chip(mtd, -1); /* De-select the NAND device */
197}
932394ac
WD
198#endif
199
200/**
201 * nand_read_byte - [DEFAULT] read one byte from the chip
202 * @mtd: MTD device structure
203 *
204 * Default read function for 8bit buswith
205 */
206static u_char nand_read_byte(struct mtd_info *mtd)
207{
208 struct nand_chip *this = mtd->priv;
209 return readb(this->IO_ADDR_R);
210}
211
212/**
213 * nand_write_byte - [DEFAULT] write one byte to the chip
214 * @mtd: MTD device structure
215 * @byte: pointer to data byte to write
216 *
217 * Default write function for 8it buswith
218 */
219static void nand_write_byte(struct mtd_info *mtd, u_char byte)
220{
221 struct nand_chip *this = mtd->priv;
222 writeb(byte, this->IO_ADDR_W);
223}
224
225/**
226 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
227 * @mtd: MTD device structure
228 *
ac7eb8a3 229 * Default read function for 16bit buswith with
932394ac
WD
230 * endianess conversion
231 */
232static u_char nand_read_byte16(struct mtd_info *mtd)
233{
234 struct nand_chip *this = mtd->priv;
235 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
236}
237
238/**
239 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
240 * @mtd: MTD device structure
241 * @byte: pointer to data byte to write
242 *
243 * Default write function for 16bit buswith with
244 * endianess conversion
245 */
246static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
247{
248 struct nand_chip *this = mtd->priv;
249 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
250}
251
252/**
253 * nand_read_word - [DEFAULT] read one word from the chip
254 * @mtd: MTD device structure
255 *
ac7eb8a3 256 * Default read function for 16bit buswith without
932394ac
WD
257 * endianess conversion
258 */
259static u16 nand_read_word(struct mtd_info *mtd)
260{
261 struct nand_chip *this = mtd->priv;
262 return readw(this->IO_ADDR_R);
263}
264
265/**
266 * nand_write_word - [DEFAULT] write one word to the chip
267 * @mtd: MTD device structure
268 * @word: data word to write
269 *
ac7eb8a3 270 * Default write function for 16bit buswith without
932394ac
WD
271 * endianess conversion
272 */
273static void nand_write_word(struct mtd_info *mtd, u16 word)
274{
275 struct nand_chip *this = mtd->priv;
276 writew(word, this->IO_ADDR_W);
277}
278
279/**
280 * nand_select_chip - [DEFAULT] control CE line
281 * @mtd: MTD device structure
282 * @chip: chipnumber to select, -1 for deselect
283 *
284 * Default select function for 1 chip devices.
285 */
286static void nand_select_chip(struct mtd_info *mtd, int chip)
287{
288 struct nand_chip *this = mtd->priv;
289 switch(chip) {
290 case -1:
ac7eb8a3 291 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
932394ac
WD
292 break;
293 case 0:
294 this->hwcontrol(mtd, NAND_CTL_SETNCE);
295 break;
296
297 default:
298 BUG();
299 }
300}
301
302/**
303 * nand_write_buf - [DEFAULT] write buffer to chip
304 * @mtd: MTD device structure
305 * @buf: data buffer
306 * @len: number of bytes to write
307 *
308 * Default write function for 8bit buswith
309 */
310static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
311{
312 int i;
313 struct nand_chip *this = mtd->priv;
314
315 for (i=0; i<len; i++)
316 writeb(buf[i], this->IO_ADDR_W);
317}
318
319/**
ac7eb8a3 320 * nand_read_buf - [DEFAULT] read chip data into buffer
932394ac
WD
321 * @mtd: MTD device structure
322 * @buf: buffer to store date
323 * @len: number of bytes to read
324 *
325 * Default read function for 8bit buswith
326 */
327static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
328{
329 int i;
330 struct nand_chip *this = mtd->priv;
331
332 for (i=0; i<len; i++)
333 buf[i] = readb(this->IO_ADDR_R);
334}
335
336/**
ac7eb8a3 337 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
932394ac
WD
338 * @mtd: MTD device structure
339 * @buf: buffer containing the data to compare
340 * @len: number of bytes to compare
341 *
342 * Default verify function for 8bit buswith
343 */
344static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
345{
346 int i;
347 struct nand_chip *this = mtd->priv;
348
349 for (i=0; i<len; i++)
350 if (buf[i] != readb(this->IO_ADDR_R))
351 return -EFAULT;
352
353 return 0;
354}
355
356/**
357 * nand_write_buf16 - [DEFAULT] write buffer to chip
358 * @mtd: MTD device structure
359 * @buf: data buffer
360 * @len: number of bytes to write
361 *
362 * Default write function for 16bit buswith
363 */
364static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
365{
366 int i;
367 struct nand_chip *this = mtd->priv;
368 u16 *p = (u16 *) buf;
369 len >>= 1;
ac7eb8a3 370
932394ac
WD
371 for (i=0; i<len; i++)
372 writew(p[i], this->IO_ADDR_W);
ac7eb8a3 373
932394ac
WD
374}
375
376/**
ac7eb8a3 377 * nand_read_buf16 - [DEFAULT] read chip data into buffer
932394ac
WD
378 * @mtd: MTD device structure
379 * @buf: buffer to store date
380 * @len: number of bytes to read
381 *
382 * Default read function for 16bit buswith
383 */
384static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
385{
386 int i;
387 struct nand_chip *this = mtd->priv;
388 u16 *p = (u16 *) buf;
389 len >>= 1;
390
391 for (i=0; i<len; i++)
392 p[i] = readw(this->IO_ADDR_R);
393}
394
395/**
ac7eb8a3 396 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
932394ac
WD
397 * @mtd: MTD device structure
398 * @buf: buffer containing the data to compare
399 * @len: number of bytes to compare
400 *
401 * Default verify function for 16bit buswith
402 */
403static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
404{
405 int i;
406 struct nand_chip *this = mtd->priv;
407 u16 *p = (u16 *) buf;
408 len >>= 1;
409
410 for (i=0; i<len; i++)
411 if (p[i] != readw(this->IO_ADDR_R))
412 return -EFAULT;
413
414 return 0;
415}
416
417/**
418 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
419 * @mtd: MTD device structure
420 * @ofs: offset from device start
421 * @getchip: 0, if the chip is already selected
422 *
ac7eb8a3 423 * Check, if the block is bad.
932394ac
WD
424 */
425static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
426{
427 int page, chipnr, res = 0;
428 struct nand_chip *this = mtd->priv;
429 u16 bad;
430
431 if (getchip) {
432 page = (int)(ofs >> this->page_shift);
433 chipnr = (int)(ofs >> this->chip_shift);
434
435 /* Grab the lock and see if the device is available */
436 nand_get_device (this, mtd, FL_READING);
437
438 /* Select the NAND device */
439 this->select_chip(mtd, chipnr);
ac7eb8a3
WD
440 } else
441 page = (int) ofs;
932394ac
WD
442
443 if (this->options & NAND_BUSWIDTH_16) {
444 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
445 bad = cpu_to_le16(this->read_word(mtd));
446 if (this->badblockpos & 0x1)
447 bad >>= 1;
448 if ((bad & 0xFF) != 0xff)
449 res = 1;
450 } else {
451 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
452 if (this->read_byte(mtd) != 0xff)
453 res = 1;
454 }
ac7eb8a3 455
932394ac
WD
456 if (getchip) {
457 /* Deselect and wake up anyone waiting on the device */
458 nand_release_device(mtd);
ac7eb8a3
WD
459 }
460
932394ac
WD
461 return res;
462}
463
464/**
465 * nand_default_block_markbad - [DEFAULT] mark a block bad
466 * @mtd: MTD device structure
467 * @ofs: offset from device start
468 *
469 * This is the default implementation, which can be overridden by
470 * a hardware specific driver.
471*/
472static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
473{
474 struct nand_chip *this = mtd->priv;
475 u_char buf[2] = {0, 0};
476 size_t retlen;
477 int block;
ac7eb8a3 478
932394ac
WD
479 /* Get block number */
480 block = ((int) ofs) >> this->bbt_erase_shift;
481 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
482
483 /* Do we have a flash based bad block table ? */
484 if (this->options & NAND_USE_FLASH_BBT)
485 return nand_update_bbt (mtd, ofs);
ac7eb8a3 486
932394ac
WD
487 /* We write two bytes, so we dont have to mess with 16 bit access */
488 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
489 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
490}
491
ac7eb8a3 492/**
932394ac
WD
493 * nand_check_wp - [GENERIC] check if the chip is write protected
494 * @mtd: MTD device structure
ac7eb8a3 495 * Check, if the device is write protected
932394ac 496 *
ac7eb8a3 497 * The function expects, that the device is already selected
932394ac
WD
498 */
499static int nand_check_wp (struct mtd_info *mtd)
500{
501 struct nand_chip *this = mtd->priv;
502 /* Check the WP bit */
503 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
ac7eb8a3 504 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
932394ac
WD
505}
506
507/**
508 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
509 * @mtd: MTD device structure
510 * @ofs: offset from device start
511 * @getchip: 0, if the chip is already selected
512 * @allowbbt: 1, if its allowed to access the bbt area
513 *
514 * Check, if the block is bad. Either by reading the bad block table or
515 * calling of the scan function.
516 */
517static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
518{
519 struct nand_chip *this = mtd->priv;
ac7eb8a3 520
932394ac
WD
521 if (!this->bbt)
522 return this->block_bad(mtd, ofs, getchip);
ac7eb8a3 523
932394ac
WD
524 /* Return info from the table */
525 return nand_isbad_bbt (mtd, ofs, allowbbt);
526}
527
528/**
529 * nand_command - [DEFAULT] Send command to NAND device
530 * @mtd: MTD device structure
531 * @command: the command to be sent
532 * @column: the column address for this command, -1 if none
533 * @page_addr: the page address for this command, -1 if none
534 *
535 * Send command to NAND device. This function is used for small page
536 * devices (256/512 Bytes per page)
537 */
538static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
539{
540 register struct nand_chip *this = mtd->priv;
541
542 /* Begin command latch cycle */
543 this->hwcontrol(mtd, NAND_CTL_SETCLE);
544 /*
545 * Write out the command to the device.
546 */
547 if (command == NAND_CMD_SEQIN) {
548 int readcmd;
549
550 if (column >= mtd->oobblock) {
551 /* OOB area */
552 column -= mtd->oobblock;
553 readcmd = NAND_CMD_READOOB;
554 } else if (column < 256) {
555 /* First 256 bytes --> READ0 */
556 readcmd = NAND_CMD_READ0;
557 } else {
558 column -= 256;
559 readcmd = NAND_CMD_READ1;
560 }
561 this->write_byte(mtd, readcmd);
562 }
563 this->write_byte(mtd, command);
564
565 /* Set ALE and clear CLE to start address cycle */
566 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
567
568 if (column != -1 || page_addr != -1) {
569 this->hwcontrol(mtd, NAND_CTL_SETALE);
570
571 /* Serially input address */
572 if (column != -1) {
573 /* Adjust columns for 16 bit buswidth */
574 if (this->options & NAND_BUSWIDTH_16)
575 column >>= 1;
576 this->write_byte(mtd, column);
577 }
578 if (page_addr != -1) {
579 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
580 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
581 /* One more address cycle for devices > 32MiB */
582 if (this->chipsize > (32 << 20))
583 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
584 }
585 /* Latch in address */
586 this->hwcontrol(mtd, NAND_CTL_CLRALE);
587 }
ac7eb8a3
WD
588
589 /*
590 * program and erase have their own busy handlers
932394ac
WD
591 * status and sequential in needs no delay
592 */
593 switch (command) {
ac7eb8a3 594
932394ac
WD
595 case NAND_CMD_PAGEPROG:
596 case NAND_CMD_ERASE1:
597 case NAND_CMD_ERASE2:
598 case NAND_CMD_SEQIN:
599 case NAND_CMD_STATUS:
600 return;
601
602 case NAND_CMD_RESET:
ac7eb8a3 603 if (this->dev_ready)
932394ac
WD
604 break;
605 udelay(this->chip_delay);
606 this->hwcontrol(mtd, NAND_CTL_SETCLE);
607 this->write_byte(mtd, NAND_CMD_STATUS);
608 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
609 while ( !(this->read_byte(mtd) & 0x40));
610 return;
611
ac7eb8a3 612 /* This applies to read commands */
932394ac 613 default:
ac7eb8a3 614 /*
932394ac
WD
615 * If we don't have access to the busy pin, we apply the given
616 * command delay
617 */
618 if (!this->dev_ready) {
619 udelay (this->chip_delay);
620 return;
ac7eb8a3 621 }
932394ac 622 }
ac7eb8a3 623
932394ac
WD
624 /* Apply this short delay always to ensure that we do wait tWB in
625 * any case on any machine. */
626 ndelay (100);
627 /* wait until command is processed */
628 while (!this->dev_ready(mtd));
629}
630
631/**
632 * nand_command_lp - [DEFAULT] Send command to NAND large page device
633 * @mtd: MTD device structure
634 * @command: the command to be sent
635 * @column: the column address for this command, -1 if none
636 * @page_addr: the page address for this command, -1 if none
637 *
638 * Send command to NAND device. This is the version for the new large page devices
639 * We dont have the seperate regions as we have in the small page devices.
640 * We must emulate NAND_CMD_READOOB to keep the code compatible.
641 *
642 */
643static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
644{
645 register struct nand_chip *this = mtd->priv;
646
647 /* Emulate NAND_CMD_READOOB */
648 if (command == NAND_CMD_READOOB) {
649 column += mtd->oobblock;
650 command = NAND_CMD_READ0;
651 }
ac7eb8a3
WD
652
653
932394ac
WD
654 /* Begin command latch cycle */
655 this->hwcontrol(mtd, NAND_CTL_SETCLE);
656 /* Write out the command to the device. */
657 this->write_byte(mtd, command);
658 /* End command latch cycle */
659 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
660
661 if (column != -1 || page_addr != -1) {
662 this->hwcontrol(mtd, NAND_CTL_SETALE);
663
664 /* Serially input address */
665 if (column != -1) {
666 /* Adjust columns for 16 bit buswidth */
667 if (this->options & NAND_BUSWIDTH_16)
668 column >>= 1;
669 this->write_byte(mtd, column & 0xff);
670 this->write_byte(mtd, column >> 8);
ac7eb8a3 671 }
932394ac
WD
672 if (page_addr != -1) {
673 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
674 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
675 /* One more address cycle for devices > 128MiB */
676 if (this->chipsize > (128 << 20))
677 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
678 }
679 /* Latch in address */
680 this->hwcontrol(mtd, NAND_CTL_CLRALE);
681 }
ac7eb8a3
WD
682
683 /*
684 * program and erase have their own busy handlers
932394ac
WD
685 * status and sequential in needs no delay
686 */
687 switch (command) {
ac7eb8a3 688
932394ac
WD
689 case NAND_CMD_CACHEDPROG:
690 case NAND_CMD_PAGEPROG:
691 case NAND_CMD_ERASE1:
692 case NAND_CMD_ERASE2:
693 case NAND_CMD_SEQIN:
694 case NAND_CMD_STATUS:
695 return;
696
697
698 case NAND_CMD_RESET:
ac7eb8a3 699 if (this->dev_ready)
932394ac
WD
700 break;
701 udelay(this->chip_delay);
702 this->hwcontrol(mtd, NAND_CTL_SETCLE);
703 this->write_byte(mtd, NAND_CMD_STATUS);
704 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
705 while ( !(this->read_byte(mtd) & 0x40));
706 return;
707
708 case NAND_CMD_READ0:
709 /* Begin command latch cycle */
710 this->hwcontrol(mtd, NAND_CTL_SETCLE);
711 /* Write out the start read command */
712 this->write_byte(mtd, NAND_CMD_READSTART);
713 /* End command latch cycle */
714 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
715 /* Fall through into ready check */
ac7eb8a3
WD
716
717 /* This applies to read commands */
932394ac 718 default:
ac7eb8a3 719 /*
932394ac
WD
720 * If we don't have access to the busy pin, we apply the given
721 * command delay
722 */
723 if (!this->dev_ready) {
724 udelay (this->chip_delay);
725 return;
ac7eb8a3 726 }
932394ac 727 }
ac7eb8a3 728
932394ac
WD
729 /* Apply this short delay always to ensure that we do wait tWB in
730 * any case on any machine. */
731 ndelay (100);
732 /* wait until command is processed */
733 while (!this->dev_ready(mtd));
734}
735
736/**
737 * nand_get_device - [GENERIC] Get chip for selected access
738 * @this: the nand chip descriptor
739 * @mtd: MTD device structure
ac7eb8a3 740 * @new_state: the state which is requested
932394ac
WD
741 *
742 * Get the device and lock it for exclusive access
743 */
744/* XXX U-BOOT XXX */
745#if 0
746static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
747{
748 struct nand_chip *active = this;
749
750 DECLARE_WAITQUEUE (wait, current);
751
ac7eb8a3
WD
752 /*
753 * Grab the lock and see if the device is available
932394ac
WD
754 */
755retry:
756 /* Hardware controller shared among independend devices */
757 if (this->controller) {
758 spin_lock (&this->controller->lock);
759 if (this->controller->active)
760 active = this->controller->active;
761 else
762 this->controller->active = this;
763 spin_unlock (&this->controller->lock);
764 }
ac7eb8a3 765
932394ac
WD
766 if (active == this) {
767 spin_lock (&this->chip_lock);
768 if (this->state == FL_READY) {
769 this->state = new_state;
770 spin_unlock (&this->chip_lock);
771 return;
772 }
ac7eb8a3 773 }
932394ac
WD
774 set_current_state (TASK_UNINTERRUPTIBLE);
775 add_wait_queue (&active->wq, &wait);
776 spin_unlock (&active->chip_lock);
777 schedule ();
778 remove_wait_queue (&active->wq, &wait);
779 goto retry;
780}
781#else
782static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) {}
783#endif
784
785/**
786 * nand_wait - [DEFAULT] wait until the command is done
787 * @mtd: MTD device structure
788 * @this: NAND chip structure
789 * @state: state to select the max. timeout value
790 *
791 * Wait for command done. This applies to erase and program only
ac7eb8a3 792 * Erase can take up to 400ms and program up to 20ms according to
932394ac
WD
793 * general NAND and SmartMedia specs
794 *
795*/
796/* XXX U-BOOT XXX */
797#if 0
798static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
799{
800 unsigned long timeo = jiffies;
801 int status;
ac7eb8a3 802
932394ac
WD
803 if (state == FL_ERASING)
804 timeo += (HZ * 400) / 1000;
805 else
806 timeo += (HZ * 20) / 1000;
807
808 /* Apply this short delay always to ensure that we do wait tWB in
809 * any case on any machine. */
810 ndelay (100);
811
812 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
813 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
ac7eb8a3 814 else
932394ac
WD
815 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
816
ac7eb8a3 817 while (time_before(jiffies, timeo)) {
932394ac
WD
818 /* Check, if we were interrupted */
819 if (this->state != state)
820 return 0;
821
822 if (this->dev_ready) {
823 if (this->dev_ready(mtd))
ac7eb8a3 824 break;
932394ac
WD
825 } else {
826 if (this->read_byte(mtd) & NAND_STATUS_READY)
827 break;
828 }
829 yield ();
830 }
831 status = (int) this->read_byte(mtd);
832 return status;
833
834 return 0;
835}
836#else
837static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
838{
8e9655f8
WD
839 unsigned long timeo;
840
841 if (state == FL_ERASING)
842 timeo = CFG_HZ * 400;
843 else
844 timeo = CFG_HZ * 20;
845
846 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
847 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
848 else
849 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
850
038ccac5 851 reset_timer();
8e9655f8
WD
852
853 while (1) {
038ccac5
BS
854 if (get_timer(0) > timeo) {
855 printf("Timeout!");
8e9655f8 856 return 0;
038ccac5 857 }
8e9655f8
WD
858
859 if (this->dev_ready) {
860 if (this->dev_ready(mtd))
861 break;
862 } else {
863 if (this->read_byte(mtd) & NAND_STATUS_READY)
864 break;
865 }
866 }
867
038ccac5
BS
868 /* XXX nand device 1 on dave (PPChameleonEVB) needs more time */
869 reset_timer();
870 while (get_timer(0) < 10);
871
8e9655f8 872 return this->read_byte(mtd);
932394ac
WD
873}
874#endif
875
876/**
877 * nand_write_page - [GENERIC] write one page
878 * @mtd: MTD device structure
879 * @this: NAND chip structure
880 * @page: startpage inside the chip, must be called with (page & this->pagemask)
881 * @oob_buf: out of band data buffer
882 * @oobsel: out of band selecttion structre
883 * @cached: 1 = enable cached programming if supported by chip
884 *
885 * Nand_page_program function is used for write and writev !
886 * This function will always program a full page of data
887 * If you call it with a non page aligned buffer, you're lost :)
888 *
889 * Cached programming is not supported yet.
890 */
ac7eb8a3 891static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
932394ac
WD
892 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
893{
894 int i, status;
895 u_char ecc_code[32];
896 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
897 int *oob_config = oobsel->eccpos;
898 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
899 int eccbytes = 0;
ac7eb8a3 900
932394ac
WD
901 /* FIXME: Enable cached programming */
902 cached = 0;
ac7eb8a3 903
932394ac
WD
904 /* Send command to begin auto page programming */
905 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
906
907 /* Write out complete page of data, take care of eccmode */
908 switch (eccmode) {
909 /* No ecc, write all */
910 case NAND_ECC_NONE:
911 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
912 this->write_buf(mtd, this->data_poi, mtd->oobblock);
913 break;
ac7eb8a3 914
932394ac
WD
915 /* Software ecc 3/256, write all */
916 case NAND_ECC_SOFT:
917 for (; eccsteps; eccsteps--) {
918 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
919 for (i = 0; i < 3; i++, eccidx++)
920 oob_buf[oob_config[eccidx]] = ecc_code[i];
921 datidx += this->eccsize;
922 }
923 this->write_buf(mtd, this->data_poi, mtd->oobblock);
924 break;
925 default:
926 eccbytes = this->eccbytes;
927 for (; eccsteps; eccsteps--) {
928 /* enable hardware ecc logic for write */
929 this->enable_hwecc(mtd, NAND_ECC_WRITE);
930 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
931 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
932 for (i = 0; i < eccbytes; i++, eccidx++)
933 oob_buf[oob_config[eccidx]] = ecc_code[i];
934 /* If the hardware ecc provides syndromes then
935 * the ecc code must be written immidiately after
936 * the data bytes (words) */
937 if (this->options & NAND_HWECC_SYNDROME)
938 this->write_buf(mtd, ecc_code, eccbytes);
939 datidx += this->eccsize;
940 }
941 break;
942 }
ac7eb8a3 943
932394ac
WD
944 /* Write out OOB data */
945 if (this->options & NAND_HWECC_SYNDROME)
946 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
ac7eb8a3 947 else
932394ac
WD
948 this->write_buf(mtd, oob_buf, mtd->oobsize);
949
950 /* Send command to actually program the data */
951 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
952
953 if (!cached) {
954 /* call wait ready function */
955 status = this->waitfunc (mtd, this, FL_WRITING);
956 /* See if device thinks it succeeded */
957 if (status & 0x01) {
958 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
959 return -EIO;
960 }
961 } else {
962 /* FIXME: Implement cached programming ! */
963 /* wait until cache is ready*/
ac7eb8a3 964 /* status = this->waitfunc (mtd, this, FL_CACHEDRPG); */
932394ac 965 }
ac7eb8a3 966 return 0;
932394ac
WD
967}
968
969#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
970/**
971 * nand_verify_pages - [GENERIC] verify the chip contents after a write
972 * @mtd: MTD device structure
973 * @this: NAND chip structure
974 * @page: startpage inside the chip, must be called with (page & this->pagemask)
975 * @numpages: number of pages to verify
976 * @oob_buf: out of band data buffer
977 * @oobsel: out of band selecttion structre
978 * @chipnr: number of the current chip
979 * @oobmode: 1 = full buffer verify, 0 = ecc only
980 *
981 * The NAND device assumes that it is always writing to a cleanly erased page.
ac7eb8a3 982 * Hence, it performs its internal write verification only on bits that
932394ac 983 * transitioned from 1 to 0. The device does NOT verify the whole page on a
ac7eb8a3
WD
984 * byte by byte basis. It is possible that the page was not completely erased
985 * or the page is becoming unusable due to wear. The read with ECC would catch
986 * the error later when the ECC page check fails, but we would rather catch
932394ac
WD
987 * it early in the page write stage. Better to write no data than invalid data.
988 */
ac7eb8a3 989static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
932394ac
WD
990 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
991{
992 int i, j, datidx = 0, oobofs = 0, res = -EIO;
993 int eccsteps = this->eccsteps;
ac7eb8a3 994 int hweccbytes;
932394ac
WD
995 u_char oobdata[64];
996
997 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
998
999 /* Send command to read back the first page */
1000 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
1001
1002 for(;;) {
1003 for (j = 0; j < eccsteps; j++) {
1004 /* Loop through and verify the data */
1005 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
1006 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1007 goto out;
1008 }
1009 datidx += mtd->eccsize;
1010 /* Have we a hw generator layout ? */
1011 if (!hweccbytes)
1012 continue;
1013 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
1014 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1015 goto out;
1016 }
1017 oobofs += hweccbytes;
1018 }
1019
1020 /* check, if we must compare all data or if we just have to
1021 * compare the ecc bytes
1022 */
1023 if (oobmode) {
1024 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1025 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1026 goto out;
1027 }
1028 } else {
1029 /* Read always, else autoincrement fails */
1030 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1031
1032 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1033 int ecccnt = oobsel->eccbytes;
ac7eb8a3 1034
932394ac
WD
1035 for (i = 0; i < ecccnt; i++) {
1036 int idx = oobsel->eccpos[i];
1037 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
1038 DEBUG (MTD_DEBUG_LEVEL0,
1039 "%s: Failed ECC write "
1040 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
1041 goto out;
1042 }
1043 }
ac7eb8a3 1044 }
932394ac
WD
1045 }
1046 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1047 page++;
1048 numpages--;
1049
ac7eb8a3 1050 /* Apply delay or wait for ready/busy pin
932394ac
WD
1051 * Do this before the AUTOINCR check, so no problems
1052 * arise if a chip which does auto increment
1053 * is marked as NOAUTOINCR by the board driver.
1054 * Do this also before returning, so the chip is
1055 * ready for the next command.
1056 */
ac7eb8a3 1057 if (!this->dev_ready)
932394ac
WD
1058 udelay (this->chip_delay);
1059 else
ac7eb8a3 1060 while (!this->dev_ready(mtd));
932394ac
WD
1061
1062 /* All done, return happy */
1063 if (!numpages)
1064 return 0;
ac7eb8a3
WD
1065
1066
1067 /* Check, if the chip supports auto page increment */
932394ac
WD
1068 if (!NAND_CANAUTOINCR(this))
1069 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1070 }
ac7eb8a3 1071 /*
932394ac
WD
1072 * Terminate the read command. We come here in case of an error
1073 * So we must issue a reset command.
1074 */
ac7eb8a3 1075out:
932394ac
WD
1076 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1077 return res;
1078}
1079#endif
1080
1081/**
1082 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1083 * @mtd: MTD device structure
1084 * @from: offset to read from
1085 * @len: number of bytes to read
1086 * @retlen: pointer to variable to store the number of read bytes
1087 * @buf: the databuffer to put data
1088 *
1089 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1090*/
1091static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1092{
1093 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
ac7eb8a3 1094}
932394ac
WD
1095
1096
1097/**
1098 * nand_read_ecc - [MTD Interface] Read data with ECC
1099 * @mtd: MTD device structure
1100 * @from: offset to read from
1101 * @len: number of bytes to read
1102 * @retlen: pointer to variable to store the number of read bytes
1103 * @buf: the databuffer to put data
1104 * @oob_buf: filesystem supplied oob data buffer
1105 * @oobsel: oob selection structure
1106 *
1107 * NAND read with ECC
1108 */
1109static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1110 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1111{
1112 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1113 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1114 struct nand_chip *this = mtd->priv;
1115 u_char *data_poi, *oob_data = oob_buf;
1116 u_char ecc_calc[32];
1117 u_char ecc_code[32];
ac7eb8a3 1118 int eccmode, eccsteps;
932394ac
WD
1119 int *oob_config, datidx;
1120 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1121 int eccbytes;
1122 int compareecc = 1;
1123 int oobreadlen;
1124
1125
1126 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1127
1128 /* Do not allow reads past end of device */
1129 if ((from + len) > mtd->size) {
1130 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1131 *retlen = 0;
1132 return -EINVAL;
1133 }
1134
1135 /* Grab the lock and see if the device is available */
1136 nand_get_device (this, mtd ,FL_READING);
1137
1138 /* use userspace supplied oobinfo, if zero */
1139 if (oobsel == NULL)
1140 oobsel = &mtd->oobinfo;
ac7eb8a3 1141
932394ac
WD
1142 /* Autoplace of oob data ? Use the default placement scheme */
1143 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1144 oobsel = this->autooob;
ac7eb8a3 1145
932394ac
WD
1146 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1147 oob_config = oobsel->eccpos;
1148
1149 /* Select the NAND device */
1150 chipnr = (int)(from >> this->chip_shift);
1151 this->select_chip(mtd, chipnr);
1152
1153 /* First we calculate the starting page */
1154 realpage = (int) (from >> this->page_shift);
1155 page = realpage & this->pagemask;
1156
1157 /* Get raw starting column */
1158 col = from & (mtd->oobblock - 1);
1159
1160 end = mtd->oobblock;
1161 ecc = this->eccsize;
1162 eccbytes = this->eccbytes;
ac7eb8a3 1163
932394ac
WD
1164 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1165 compareecc = 0;
1166
1167 oobreadlen = mtd->oobsize;
ac7eb8a3 1168 if (this->options & NAND_HWECC_SYNDROME)
932394ac
WD
1169 oobreadlen -= oobsel->eccbytes;
1170
1171 /* Loop until all data read */
1172 while (read < len) {
ac7eb8a3 1173
932394ac 1174 int aligned = (!col && (len - read) >= end);
ac7eb8a3 1175 /*
932394ac
WD
1176 * If the read is not page aligned, we have to read into data buffer
1177 * due to ecc, else we read into return buffer direct
1178 */
1179 if (aligned)
1180 data_poi = &buf[read];
ac7eb8a3 1181 else
932394ac 1182 data_poi = this->data_buf;
ac7eb8a3
WD
1183
1184 /* Check, if we have this page in the buffer
932394ac
WD
1185 *
1186 * FIXME: Make it work when we must provide oob data too,
1187 * check the usage of data_buf oob field
1188 */
1189 if (realpage == this->pagebuf && !oob_buf) {
1190 /* aligned read ? */
1191 if (aligned)
1192 memcpy (data_poi, this->data_buf, end);
1193 goto readdata;
1194 }
1195
1196 /* Check, if we must send the read command */
1197 if (sndcmd) {
1198 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1199 sndcmd = 0;
ac7eb8a3 1200 }
932394ac
WD
1201
1202 /* get oob area, if we have no oob buffer from fs-driver */
1203 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1204 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1205 oob_data = &this->data_buf[end];
1206
1207 eccsteps = this->eccsteps;
ac7eb8a3 1208
932394ac
WD
1209 switch (eccmode) {
1210 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1211/* XXX U-BOOT XXX */
1212#if 0
1213 static unsigned long lastwhinge = 0;
1214 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1215 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1216 lastwhinge = jiffies;
1217 }
1218#else
1219 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1220#endif
1221 this->read_buf(mtd, data_poi, end);
1222 break;
1223 }
ac7eb8a3 1224
932394ac
WD
1225 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1226 this->read_buf(mtd, data_poi, end);
ac7eb8a3 1227 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
932394ac 1228 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
ac7eb8a3 1229 break;
932394ac
WD
1230
1231 default:
1232 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1233 this->enable_hwecc(mtd, NAND_ECC_READ);
1234 this->read_buf(mtd, &data_poi[datidx], ecc);
1235
1236 /* HW ecc with syndrome calculation must read the
1237 * syndrome from flash immidiately after the data */
1238 if (!compareecc) {
1239 /* Some hw ecc generators need to know when the
1240 * syndrome is read from flash */
1241 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1242 this->read_buf(mtd, &oob_data[i], eccbytes);
1243 /* We calc error correction directly, it checks the hw
1244 * generator for an error, reads back the syndrome and
1245 * does the error correction on the fly */
1246 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
ac7eb8a3 1247 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
932394ac
WD
1248 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1249 ecc_failed++;
1250 }
1251 } else {
1252 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
ac7eb8a3 1253 }
932394ac 1254 }
ac7eb8a3 1255 break;
932394ac
WD
1256 }
1257
1258 /* read oobdata */
1259 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1260
1261 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1262 if (!compareecc)
ac7eb8a3
WD
1263 goto readoob;
1264
932394ac
WD
1265 /* Pick the ECC bytes out of the oob data */
1266 for (j = 0; j < oobsel->eccbytes; j++)
1267 ecc_code[j] = oob_data[oob_config[j]];
1268
1269 /* correct data, if neccecary */
1270 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1271 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
ac7eb8a3 1272
932394ac
WD
1273 /* Get next chunk of ecc bytes */
1274 j += eccbytes;
ac7eb8a3
WD
1275
1276 /* Check, if we have a fs supplied oob-buffer,
932394ac
WD
1277 * This is the legacy mode. Used by YAFFS1
1278 * Should go away some day
1279 */
ac7eb8a3 1280 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
932394ac
WD
1281 int *p = (int *)(&oob_data[mtd->oobsize]);
1282 p[i] = ecc_status;
1283 }
ac7eb8a3
WD
1284
1285 if (ecc_status == -1) {
932394ac
WD
1286 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1287 ecc_failed++;
1288 }
ac7eb8a3 1289 }
932394ac
WD
1290
1291 readoob:
1292 /* check, if we have a fs supplied oob-buffer */
1293 if (oob_buf) {
1294 /* without autoplace. Legacy mode used by YAFFS1 */
1295 switch(oobsel->useecc) {
1296 case MTD_NANDECC_AUTOPLACE:
1297 case MTD_NANDECC_AUTOPL_USR:
1298 /* Walk through the autoplace chunks */
1299 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1300 int from = oobsel->oobfree[i][0];
1301 int num = oobsel->oobfree[i][1];
1302 memcpy(&oob_buf[oob], &oob_data[from], num);
1303 j+= num;
1304 }
1305 oob += mtd->oobavail;
1306 break;
1307 case MTD_NANDECC_PLACE:
1308 /* YAFFS1 legacy mode */
1309 oob_data += this->eccsteps * sizeof (int);
1310 default:
1311 oob_data += mtd->oobsize;
1312 }
1313 }
1314 readdata:
1315 /* Partial page read, transfer data into fs buffer */
ac7eb8a3 1316 if (!aligned) {
932394ac
WD
1317 for (j = col; j < end && read < len; j++)
1318 buf[read++] = data_poi[j];
ac7eb8a3
WD
1319 this->pagebuf = realpage;
1320 } else
932394ac
WD
1321 read += mtd->oobblock;
1322
ac7eb8a3 1323 /* Apply delay or wait for ready/busy pin
932394ac
WD
1324 * Do this before the AUTOINCR check, so no problems
1325 * arise if a chip which does auto increment
1326 * is marked as NOAUTOINCR by the board driver.
1327 */
ac7eb8a3 1328 if (!this->dev_ready)
932394ac
WD
1329 udelay (this->chip_delay);
1330 else
ac7eb8a3
WD
1331 while (!this->dev_ready(mtd));
1332
932394ac 1333 if (read == len)
ac7eb8a3 1334 break;
932394ac
WD
1335
1336 /* For subsequent reads align to page boundary. */
1337 col = 0;
1338 /* Increment page address */
1339 realpage++;
1340
1341 page = realpage & this->pagemask;
1342 /* Check, if we cross a chip boundary */
1343 if (!page) {
1344 chipnr++;
1345 this->select_chip(mtd, -1);
1346 this->select_chip(mtd, chipnr);
1347 }
ac7eb8a3
WD
1348 /* Check, if the chip supports auto page increment
1349 * or if we have hit a block boundary.
1350 */
932394ac 1351 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
ac7eb8a3 1352 sndcmd = 1;
932394ac
WD
1353 }
1354
1355 /* Deselect and wake up anyone waiting on the device */
1356 nand_release_device(mtd);
1357
1358 /*
1359 * Return success, if no ECC failures, else -EBADMSG
1360 * fs driver will take care of that, because
1361 * retlen == desired len and result == -EBADMSG
1362 */
1363 *retlen = read;
1364 return ecc_failed ? -EBADMSG : 0;
1365}
1366
1367/**
1368 * nand_read_oob - [MTD Interface] NAND read out-of-band
1369 * @mtd: MTD device structure
1370 * @from: offset to read from
1371 * @len: number of bytes to read
1372 * @retlen: pointer to variable to store the number of read bytes
1373 * @buf: the databuffer to put data
1374 *
1375 * NAND read out-of-band data from the spare area
1376 */
1377static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1378{
1379 int i, col, page, chipnr;
1380 struct nand_chip *this = mtd->priv;
1381 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1382
1383 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1384
1385 /* Shift to get page */
1386 page = (int)(from >> this->page_shift);
1387 chipnr = (int)(from >> this->chip_shift);
ac7eb8a3 1388
932394ac
WD
1389 /* Mask to get column */
1390 col = from & (mtd->oobsize - 1);
1391
1392 /* Initialize return length value */
1393 *retlen = 0;
1394
1395 /* Do not allow reads past end of device */
1396 if ((from + len) > mtd->size) {
1397 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1398 *retlen = 0;
1399 return -EINVAL;
1400 }
1401
1402 /* Grab the lock and see if the device is available */
1403 nand_get_device (this, mtd , FL_READING);
1404
1405 /* Select the NAND device */
1406 this->select_chip(mtd, chipnr);
1407
1408 /* Send the read command */
1409 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
ac7eb8a3 1410 /*
932394ac
WD
1411 * Read the data, if we read more than one page
1412 * oob data, let the device transfer the data !
1413 */
1414 i = 0;
1415 while (i < len) {
1416 int thislen = mtd->oobsize - col;
1417 thislen = min_t(int, thislen, len);
1418 this->read_buf(mtd, &buf[i], thislen);
1419 i += thislen;
ac7eb8a3
WD
1420
1421 /* Apply delay or wait for ready/busy pin
932394ac
WD
1422 * Do this before the AUTOINCR check, so no problems
1423 * arise if a chip which does auto increment
1424 * is marked as NOAUTOINCR by the board driver.
1425 */
ac7eb8a3 1426 if (!this->dev_ready)
932394ac
WD
1427 udelay (this->chip_delay);
1428 else
ac7eb8a3 1429 while (!this->dev_ready(mtd));
932394ac
WD
1430
1431 /* Read more ? */
1432 if (i < len) {
1433 page++;
1434 col = 0;
1435
1436 /* Check, if we cross a chip boundary */
1437 if (!(page & this->pagemask)) {
1438 chipnr++;
1439 this->select_chip(mtd, -1);
1440 this->select_chip(mtd, chipnr);
1441 }
ac7eb8a3
WD
1442
1443 /* Check, if the chip supports auto page increment
1444 * or if we have hit a block boundary.
1445 */
932394ac
WD
1446 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1447 /* For subsequent page reads set offset to 0 */
ac7eb8a3 1448 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
932394ac
WD
1449 }
1450 }
1451 }
1452
1453 /* Deselect and wake up anyone waiting on the device */
1454 nand_release_device(mtd);
1455
1456 /* Return happy */
1457 *retlen = len;
1458 return 0;
1459}
1460
1461/**
1462 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1463 * @mtd: MTD device structure
1464 * @buf: temporary buffer
1465 * @from: offset to read from
1466 * @len: number of bytes to read
1467 * @ooblen: number of oob data bytes to read
1468 *
1469 * Read raw data including oob into buffer
1470 */
1471int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1472{
1473 struct nand_chip *this = mtd->priv;
1474 int page = (int) (from >> this->page_shift);
1475 int chip = (int) (from >> this->chip_shift);
1476 int sndcmd = 1;
1477 int cnt = 0;
1478 int pagesize = mtd->oobblock + mtd->oobsize;
1479 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1480
1481 /* Do not allow reads past end of device */
1482 if ((from + len) > mtd->size) {
1483 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1484 return -EINVAL;
1485 }
1486
1487 /* Grab the lock and see if the device is available */
1488 nand_get_device (this, mtd , FL_READING);
1489
1490 this->select_chip (mtd, chip);
ac7eb8a3 1491
932394ac
WD
1492 /* Add requested oob length */
1493 len += ooblen;
ac7eb8a3 1494
932394ac
WD
1495 while (len) {
1496 if (sndcmd)
1497 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
ac7eb8a3 1498 sndcmd = 0;
932394ac
WD
1499
1500 this->read_buf (mtd, &buf[cnt], pagesize);
1501
1502 len -= pagesize;
1503 cnt += pagesize;
1504 page++;
ac7eb8a3
WD
1505
1506 if (!this->dev_ready)
932394ac
WD
1507 udelay (this->chip_delay);
1508 else
ac7eb8a3
WD
1509 while (!this->dev_ready(mtd));
1510
1511 /* Check, if the chip supports auto page increment */
932394ac
WD
1512 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1513 sndcmd = 1;
1514 }
1515
1516 /* Deselect and wake up anyone waiting on the device */
1517 nand_release_device(mtd);
1518 return 0;
1519}
1520
1521
ac7eb8a3
WD
1522/**
1523 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
932394ac
WD
1524 * @mtd: MTD device structure
1525 * @fsbuf: buffer given by fs driver
1526 * @oobsel: out of band selection structre
1527 * @autoplace: 1 = place given buffer into the oob bytes
1528 * @numpages: number of pages to prepare
1529 *
1530 * Return:
1531 * 1. Filesystem buffer available and autoplacement is off,
1532 * return filesystem buffer
1533 * 2. No filesystem buffer or autoplace is off, return internal
1534 * buffer
1535 * 3. Filesystem buffer is given and autoplace selected
1536 * put data from fs buffer into internal buffer and
1537 * retrun internal buffer
1538 *
1539 * Note: The internal buffer is filled with 0xff. This must
1540 * be done only once, when no autoplacement happens
1541 * Autoplacement sets the buffer dirty flag, which
1542 * forces the 0xff fill before using the buffer again.
1543 *
1544*/
1545static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1546 int autoplace, int numpages)
1547{
1548 struct nand_chip *this = mtd->priv;
1549 int i, len, ofs;
1550
1551 /* Zero copy fs supplied buffer */
ac7eb8a3 1552 if (fsbuf && !autoplace)
932394ac
WD
1553 return fsbuf;
1554
1555 /* Check, if the buffer must be filled with ff again */
ac7eb8a3
WD
1556 if (this->oobdirty) {
1557 memset (this->oob_buf, 0xff,
932394ac
WD
1558 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1559 this->oobdirty = 0;
ac7eb8a3
WD
1560 }
1561
932394ac
WD
1562 /* If we have no autoplacement or no fs buffer use the internal one */
1563 if (!autoplace || !fsbuf)
1564 return this->oob_buf;
ac7eb8a3 1565
932394ac
WD
1566 /* Walk through the pages and place the data */
1567 this->oobdirty = 1;
1568 ofs = 0;
1569 while (numpages--) {
1570 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1571 int to = ofs + oobsel->oobfree[i][0];
1572 int num = oobsel->oobfree[i][1];
1573 memcpy (&this->oob_buf[to], fsbuf, num);
1574 len += num;
1575 fsbuf += num;
1576 }
1577 ofs += mtd->oobavail;
1578 }
1579 return this->oob_buf;
1580}
1581
1582#define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1583
1584/**
1585 * nand_write - [MTD Interface] compability function for nand_write_ecc
1586 * @mtd: MTD device structure
1587 * @to: offset to write to
1588 * @len: number of bytes to write
1589 * @retlen: pointer to variable to store the number of written bytes
1590 * @buf: the data to write
1591 *
1592 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1593 *
1594*/
1595static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1596{
1597 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1598}
ac7eb8a3 1599
932394ac
WD
1600/**
1601 * nand_write_ecc - [MTD Interface] NAND write with ECC
1602 * @mtd: MTD device structure
1603 * @to: offset to write to
1604 * @len: number of bytes to write
1605 * @retlen: pointer to variable to store the number of written bytes
1606 * @buf: the data to write
1607 * @eccbuf: filesystem supplied oob data buffer
1608 * @oobsel: oob selection structure
1609 *
1610 * NAND write with ECC
1611 */
1612static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1613 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1614{
1615 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1616 int autoplace = 0, numpages, totalpages;
1617 struct nand_chip *this = mtd->priv;
1618 u_char *oobbuf, *bufstart;
1619 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1620
1621 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1622
1623 /* Initialize retlen, in case of early exit */
1624 *retlen = 0;
1625
1626 /* Do not allow write past end of device */
1627 if ((to + len) > mtd->size) {
1628 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1629 return -EINVAL;
1630 }
1631
ac7eb8a3 1632 /* reject writes, which are not page aligned */
932394ac
WD
1633 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1634 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1635 return -EINVAL;
1636 }
1637
1638 /* Grab the lock and see if the device is available */
1639 nand_get_device (this, mtd, FL_WRITING);
1640
1641 /* Calculate chipnr */
1642 chipnr = (int)(to >> this->chip_shift);
1643 /* Select the NAND device */
1644 this->select_chip(mtd, chipnr);
1645
1646 /* Check, if it is write protected */
1647 if (nand_check_wp(mtd))
1648 goto out;
1649
1650 /* if oobsel is NULL, use chip defaults */
ac7eb8a3
WD
1651 if (oobsel == NULL)
1652 oobsel = &mtd->oobinfo;
1653
932394ac
WD
1654 /* Autoplace of oob data ? Use the default placement scheme */
1655 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1656 oobsel = this->autooob;
1657 autoplace = 1;
ac7eb8a3 1658 }
932394ac
WD
1659 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1660 autoplace = 1;
1661
1662 /* Setup variables and oob buffer */
1663 totalpages = len >> this->page_shift;
1664 page = (int) (to >> this->page_shift);
1665 /* Invalidate the page cache, if we write to the cached page */
ac7eb8a3 1666 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
932394ac 1667 this->pagebuf = -1;
ac7eb8a3 1668
932394ac
WD
1669 /* Set it relative to chip */
1670 page &= this->pagemask;
1671 startpage = page;
1672 /* Calc number of pages we can write in one go */
1673 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1674 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1675 bufstart = (u_char *)buf;
1676
1677 /* Loop until all data is written */
1678 while (written < len) {
1679
1680 this->data_poi = (u_char*) &buf[written];
1681 /* Write one page. If this is the last page to write
1682 * or the last page in this block, then use the
1683 * real pageprogram command, else select cached programming
1684 * if supported by the chip.
1685 */
1686 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1687 if (ret) {
1688 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1689 goto out;
ac7eb8a3 1690 }
932394ac
WD
1691 /* Next oob page */
1692 oob += mtd->oobsize;
1693 /* Update written bytes count */
1694 written += mtd->oobblock;
ac7eb8a3 1695 if (written == len)
932394ac 1696 goto cmp;
ac7eb8a3 1697
932394ac
WD
1698 /* Increment page address */
1699 page++;
1700
1701 /* Have we hit a block boundary ? Then we have to verify and
1702 * if verify is ok, we have to setup the oob buffer for
1703 * the next pages.
1704 */
1705 if (!(page & (ppblock - 1))){
1706 int ofs;
1707 this->data_poi = bufstart;
ac7eb8a3 1708 ret = nand_verify_pages (mtd, this, startpage,
932394ac
WD
1709 page - startpage,
1710 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1711 if (ret) {
1712 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1713 goto out;
ac7eb8a3 1714 }
932394ac
WD
1715 *retlen = written;
1716
1717 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1718 if (eccbuf)
1719 eccbuf += (page - startpage) * ofs;
1720 totalpages -= page - startpage;
1721 numpages = min (totalpages, ppblock);
1722 page &= this->pagemask;
1723 startpage = page;
ac7eb8a3 1724 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
932394ac
WD
1725 autoplace, numpages);
1726 /* Check, if we cross a chip boundary */
1727 if (!page) {
1728 chipnr++;
1729 this->select_chip(mtd, -1);
1730 this->select_chip(mtd, chipnr);
1731 }
1732 }
1733 }
1734 /* Verify the remaining pages */
1735cmp:
1736 this->data_poi = bufstart;
1737 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1738 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1739 if (!ret)
1740 *retlen = written;
ac7eb8a3 1741 else
932394ac
WD
1742 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1743
1744out:
1745 /* Deselect and wake up anyone waiting on the device */
1746 nand_release_device(mtd);
1747
1748 return ret;
1749}
1750
1751
1752/**
1753 * nand_write_oob - [MTD Interface] NAND write out-of-band
1754 * @mtd: MTD device structure
1755 * @to: offset to write to
1756 * @len: number of bytes to write
1757 * @retlen: pointer to variable to store the number of written bytes
1758 * @buf: the data to write
1759 *
1760 * NAND write out-of-band
1761 */
1762static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1763{
1764 int column, page, status, ret = -EIO, chipnr;
1765 struct nand_chip *this = mtd->priv;
1766
1767 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1768
1769 /* Shift to get page */
1770 page = (int) (to >> this->page_shift);
1771 chipnr = (int) (to >> this->chip_shift);
1772
1773 /* Mask to get column */
1774 column = to & (mtd->oobsize - 1);
1775
1776 /* Initialize return length value */
1777 *retlen = 0;
1778
1779 /* Do not allow write past end of page */
1780 if ((column + len) > mtd->oobsize) {
1781 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1782 return -EINVAL;
1783 }
1784
1785 /* Grab the lock and see if the device is available */
1786 nand_get_device (this, mtd, FL_WRITING);
1787
1788 /* Select the NAND device */
1789 this->select_chip(mtd, chipnr);
1790
1791 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1792 in one of my DiskOnChip 2000 test units) will clear the whole
1793 data page too if we don't do this. I have no clue why, but
1794 I seem to have 'fixed' it in the doc2000 driver in
1795 August 1999. dwmw2. */
1796 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1797
1798 /* Check, if it is write protected */
1799 if (nand_check_wp(mtd))
1800 goto out;
ac7eb8a3 1801
932394ac
WD
1802 /* Invalidate the page cache, if we write to the cached page */
1803 if (page == this->pagebuf)
1804 this->pagebuf = -1;
1805
1806 if (NAND_MUST_PAD(this)) {
1807 /* Write out desired data */
1808 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1809 /* prepad 0xff for partial programming */
1810 this->write_buf(mtd, ffchars, column);
1811 /* write data */
1812 this->write_buf(mtd, buf, len);
1813 /* postpad 0xff for partial programming */
1814 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1815 } else {
1816 /* Write out desired data */
1817 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1818 /* write data */
1819 this->write_buf(mtd, buf, len);
1820 }
1821 /* Send command to program the OOB data */
1822 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1823
1824 status = this->waitfunc (mtd, this, FL_WRITING);
1825
1826 /* See if device thinks it succeeded */
1827 if (status & 0x01) {
1828 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1829 ret = -EIO;
1830 goto out;
1831 }
1832 /* Return happy */
1833 *retlen = len;
1834
1835#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1836 /* Send command to read back the data */
1837 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1838
1839 if (this->verify_buf(mtd, buf, len)) {
1840 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1841 ret = -EIO;
1842 goto out;
1843 }
1844#endif
1845 ret = 0;
1846out:
1847 /* Deselect and wake up anyone waiting on the device */
1848 nand_release_device(mtd);
1849
1850 return ret;
1851}
1852
1853/* XXX U-BOOT XXX */
1854#if 0
1855/**
1856 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1857 * @mtd: MTD device structure
1858 * @vecs: the iovectors to write
1859 * @count: number of vectors
1860 * @to: offset to write to
1861 * @retlen: pointer to variable to store the number of written bytes
1862 *
1863 * NAND write with kvec. This just calls the ecc function
1864 */
ac7eb8a3 1865static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
932394ac
WD
1866 loff_t to, size_t * retlen)
1867{
ac7eb8a3 1868 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
932394ac
WD
1869}
1870
1871/**
1872 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1873 * @mtd: MTD device structure
1874 * @vecs: the iovectors to write
1875 * @count: number of vectors
1876 * @to: offset to write to
1877 * @retlen: pointer to variable to store the number of written bytes
1878 * @eccbuf: filesystem supplied oob data buffer
1879 * @oobsel: oob selection structure
1880 *
1881 * NAND write with iovec with ecc
1882 */
ac7eb8a3 1883static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
932394ac
WD
1884 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1885{
1886 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1887 int oob, numpages, autoplace = 0, startpage;
1888 struct nand_chip *this = mtd->priv;
1889 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1890 u_char *oobbuf, *bufstart;
1891
1892 /* Preset written len for early exit */
1893 *retlen = 0;
1894
1895 /* Calculate total length of data */
1896 total_len = 0;
1897 for (i = 0; i < count; i++)
1898 total_len += (int) vecs[i].iov_len;
1899
1900 DEBUG (MTD_DEBUG_LEVEL3,
1901 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1902
1903 /* Do not allow write past end of page */
1904 if ((to + total_len) > mtd->size) {
1905 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1906 return -EINVAL;
1907 }
1908
ac7eb8a3 1909 /* reject writes, which are not page aligned */
932394ac
WD
1910 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1911 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1912 return -EINVAL;
1913 }
1914
1915 /* Grab the lock and see if the device is available */
1916 nand_get_device (this, mtd, FL_WRITING);
1917
1918 /* Get the current chip-nr */
1919 chipnr = (int) (to >> this->chip_shift);
1920 /* Select the NAND device */
1921 this->select_chip(mtd, chipnr);
1922
1923 /* Check, if it is write protected */
1924 if (nand_check_wp(mtd))
1925 goto out;
1926
1927 /* if oobsel is NULL, use chip defaults */
ac7eb8a3
WD
1928 if (oobsel == NULL)
1929 oobsel = &mtd->oobinfo;
932394ac
WD
1930
1931 /* Autoplace of oob data ? Use the default placement scheme */
1932 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1933 oobsel = this->autooob;
1934 autoplace = 1;
ac7eb8a3 1935 }
932394ac
WD
1936 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1937 autoplace = 1;
1938
1939 /* Setup start page */
1940 page = (int) (to >> this->page_shift);
1941 /* Invalidate the page cache, if we write to the cached page */
ac7eb8a3 1942 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
932394ac
WD
1943 this->pagebuf = -1;
1944
1945 startpage = page & this->pagemask;
1946
1947 /* Loop until all kvec' data has been written */
1948 len = 0;
1949 while (count) {
1950 /* If the given tuple is >= pagesize then
1951 * write it out from the iov
1952 */
1953 if ((vecs->iov_len - len) >= mtd->oobblock) {
1954 /* Calc number of pages we can write
1955 * out of this iov in one go */
1956 numpages = (vecs->iov_len - len) >> this->page_shift;
1957 /* Do not cross block boundaries */
1958 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1959 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1960 bufstart = (u_char *)vecs->iov_base;
1961 bufstart += len;
1962 this->data_poi = bufstart;
1963 oob = 0;
1964 for (i = 1; i <= numpages; i++) {
1965 /* Write one page. If this is the last page to write
ac7eb8a3 1966 * then use the real pageprogram command, else select
932394ac
WD
1967 * cached programming if supported by the chip.
1968 */
ac7eb8a3 1969 ret = nand_write_page (mtd, this, page & this->pagemask,
932394ac
WD
1970 &oobbuf[oob], oobsel, i != numpages);
1971 if (ret)
1972 goto out;
1973 this->data_poi += mtd->oobblock;
1974 len += mtd->oobblock;
1975 oob += mtd->oobsize;
1976 page++;
1977 }
1978 /* Check, if we have to switch to the next tuple */
1979 if (len >= (int) vecs->iov_len) {
1980 vecs++;
1981 len = 0;
1982 count--;
1983 }
1984 } else {
ac7eb8a3 1985 /* We must use the internal buffer, read data out of each
932394ac
WD
1986 * tuple until we have a full page to write
1987 */
1988 int cnt = 0;
1989 while (cnt < mtd->oobblock) {
ac7eb8a3 1990 if (vecs->iov_base != NULL && vecs->iov_len)
932394ac
WD
1991 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
1992 /* Check, if we have to switch to the next tuple */
1993 if (len >= (int) vecs->iov_len) {
1994 vecs++;
1995 len = 0;
1996 count--;
1997 }
1998 }
ac7eb8a3
WD
1999 this->pagebuf = page;
2000 this->data_poi = this->data_buf;
932394ac 2001 bufstart = this->data_poi;
ac7eb8a3 2002 numpages = 1;
932394ac
WD
2003 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2004 ret = nand_write_page (mtd, this, page & this->pagemask,
2005 oobbuf, oobsel, 0);
2006 if (ret)
2007 goto out;
2008 page++;
2009 }
2010
2011 this->data_poi = bufstart;
2012 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2013 if (ret)
2014 goto out;
ac7eb8a3 2015
932394ac
WD
2016 written += mtd->oobblock * numpages;
2017 /* All done ? */
2018 if (!count)
2019 break;
2020
2021 startpage = page & this->pagemask;
2022 /* Check, if we cross a chip boundary */
2023 if (!startpage) {
2024 chipnr++;
2025 this->select_chip(mtd, -1);
2026 this->select_chip(mtd, chipnr);
2027 }
2028 }
2029 ret = 0;
2030out:
2031 /* Deselect and wake up anyone waiting on the device */
2032 nand_release_device(mtd);
2033
2034 *retlen = written;
2035 return ret;
2036}
2037#endif
2038
2039/**
2040 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2041 * @mtd: MTD device structure
2042 * @page: the page address of the block which will be erased
2043 *
2044 * Standard erase command for NAND chips
2045 */
2046static void single_erase_cmd (struct mtd_info *mtd, int page)
2047{
2048 struct nand_chip *this = mtd->priv;
2049 /* Send commands to erase a block */
2050 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2051 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2052}
2053
2054/**
2055 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2056 * @mtd: MTD device structure
2057 * @page: the page address of the block which will be erased
2058 *
2059 * AND multi block erase command function
2060 * Erase 4 consecutive blocks
2061 */
2062static void multi_erase_cmd (struct mtd_info *mtd, int page)
2063{
2064 struct nand_chip *this = mtd->priv;
2065 /* Send commands to erase a block */
2066 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2067 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2068 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2069 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2070 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2071}
2072
2073/**
2074 * nand_erase - [MTD Interface] erase block(s)
2075 * @mtd: MTD device structure
2076 * @instr: erase instruction
2077 *
2078 * Erase one ore more blocks
2079 */
2080static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2081{
2082 return nand_erase_nand (mtd, instr, 0);
2083}
ac7eb8a3 2084
932394ac
WD
2085/**
2086 * nand_erase_intern - [NAND Interface] erase block(s)
2087 * @mtd: MTD device structure
2088 * @instr: erase instruction
2089 * @allowbbt: allow erasing the bbt area
2090 *
2091 * Erase one ore more blocks
2092 */
2093int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2094{
2095 int page, len, status, pages_per_block, ret, chipnr;
2096 struct nand_chip *this = mtd->priv;
2097
2098 DEBUG (MTD_DEBUG_LEVEL3,
2099 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2100
2101 /* Start address must align on block boundary */
2102 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2103 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2104 return -EINVAL;
2105 }
2106
2107 /* Length must align on block boundary */
2108 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2109 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2110 return -EINVAL;
2111 }
2112
2113 /* Do not allow erase past end of device */
2114 if ((instr->len + instr->addr) > mtd->size) {
2115 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2116 return -EINVAL;
2117 }
2118
2119 instr->fail_addr = 0xffffffff;
2120
2121 /* Grab the lock and see if the device is available */
2122 nand_get_device (this, mtd, FL_ERASING);
2123
2124 /* Shift to get first page */
2125 page = (int) (instr->addr >> this->page_shift);
2126 chipnr = (int) (instr->addr >> this->chip_shift);
2127
2128 /* Calculate pages in each block */
2129 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2130
2131 /* Select the NAND device */
2132 this->select_chip(mtd, chipnr);
2133
2134 /* Check the WP bit */
2135 /* Check, if it is write protected */
2136 if (nand_check_wp(mtd)) {
2137 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2138 instr->state = MTD_ERASE_FAILED;
2139 goto erase_exit;
2140 }
2141
2142 /* Loop through the pages */
2143 len = instr->len;
2144
2145 instr->state = MTD_ERASING;
2146
2147 while (len) {
2148 /* Check if we have a bad block, we do not erase bad blocks ! */
2149 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2150 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2151 instr->state = MTD_ERASE_FAILED;
2152 goto erase_exit;
2153 }
ac7eb8a3
WD
2154
2155 /* Invalidate the page cache, if we erase the block which contains
932394ac
WD
2156 the current cached page */
2157 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2158 this->pagebuf = -1;
2159
2160 this->erase_cmd (mtd, page & this->pagemask);
ac7eb8a3 2161
932394ac
WD
2162 status = this->waitfunc (mtd, this, FL_ERASING);
2163
2164 /* See if block erase succeeded */
2165 if (status & 0x01) {
2166 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2167 instr->state = MTD_ERASE_FAILED;
2168 instr->fail_addr = (page << this->page_shift);
2169 goto erase_exit;
2170 }
ac7eb8a3 2171
932394ac
WD
2172 /* Increment page address and decrement length */
2173 len -= (1 << this->phys_erase_shift);
2174 page += pages_per_block;
2175
2176 /* Check, if we cross a chip boundary */
2177 if (len && !(page & this->pagemask)) {
2178 chipnr++;
2179 this->select_chip(mtd, -1);
2180 this->select_chip(mtd, chipnr);
2181 }
2182 }
2183 instr->state = MTD_ERASE_DONE;
2184
2185erase_exit:
2186
2187 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2188 /* Do call back function */
2189 if (!ret)
2190 mtd_erase_callback(instr);
2191
2192 /* Deselect and wake up anyone waiting on the device */
2193 nand_release_device(mtd);
2194
2195 /* Return more or less happy */
2196 return ret;
2197}
2198
2199/**
2200 * nand_sync - [MTD Interface] sync
2201 * @mtd: MTD device structure
2202 *
2203 * Sync is actually a wait for chip ready function
2204 */
2205static void nand_sync (struct mtd_info *mtd)
2206{
2207 struct nand_chip *this = mtd->priv;
2208
2209 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2210
2211 /* Grab the lock and see if the device is available */
2212 nand_get_device (this, mtd, FL_SYNCING);
2213 /* Release it and go back */
2214 nand_release_device (mtd);
2215}
2216
2217
2218/**
2219 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2220 * @mtd: MTD device structure
2221 * @ofs: offset relative to mtd start
2222 */
2223static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2224{
2225 /* Check for invalid offset */
ac7eb8a3 2226 if (ofs > mtd->size)
932394ac 2227 return -EINVAL;
ac7eb8a3 2228
932394ac
WD
2229 return nand_block_checkbad (mtd, ofs, 1, 0);
2230}
2231
2232/**
2233 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2234 * @mtd: MTD device structure
2235 * @ofs: offset relative to mtd start
2236 */
2237static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2238{
2239 struct nand_chip *this = mtd->priv;
2240 int ret;
2241
ac7eb8a3
WD
2242 if ((ret = nand_block_isbad(mtd, ofs))) {
2243 /* If it was bad already, return success and do nothing. */
932394ac
WD
2244 if (ret > 0)
2245 return 0;
ac7eb8a3
WD
2246 return ret;
2247 }
932394ac
WD
2248
2249 return this->block_markbad(mtd, ofs);
2250}
2251
2252/**
2253 * nand_scan - [NAND Interface] Scan for the NAND device
2254 * @mtd: MTD device structure
2255 * @maxchips: Number of chips to scan for
2256 *
2257 * This fills out all the not initialized function pointers
2258 * with the defaults.
2259 * The flash ID is read and the mtd/chip structures are
2260 * filled with the appropriate values. Buffers are allocated if
2261 * they are not provided by the board driver
2262 *
2263 */
2264int nand_scan (struct mtd_info *mtd, int maxchips)
2265{
2266 int i, j, nand_maf_id, nand_dev_id, busw;
2267 struct nand_chip *this = mtd->priv;
2268
2269 /* Get buswidth to select the correct functions*/
2270 busw = this->options & NAND_BUSWIDTH_16;
2271
2272 /* check for proper chip_delay setup, set 20us if not */
2273 if (!this->chip_delay)
2274 this->chip_delay = 20;
2275
2276 /* check, if a user supplied command function given */
2277 if (this->cmdfunc == NULL)
2278 this->cmdfunc = nand_command;
2279
2280 /* check, if a user supplied wait function given */
2281 if (this->waitfunc == NULL)
2282 this->waitfunc = nand_wait;
2283
2284 if (!this->select_chip)
2285 this->select_chip = nand_select_chip;
2286 if (!this->write_byte)
2287 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2288 if (!this->read_byte)
2289 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2290 if (!this->write_word)
2291 this->write_word = nand_write_word;
2292 if (!this->read_word)
2293 this->read_word = nand_read_word;
2294 if (!this->block_bad)
2295 this->block_bad = nand_block_bad;
2296 if (!this->block_markbad)
2297 this->block_markbad = nand_default_block_markbad;
2298 if (!this->write_buf)
2299 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2300 if (!this->read_buf)
2301 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2302 if (!this->verify_buf)
2303 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2304 if (!this->scan_bbt)
2305 this->scan_bbt = nand_default_bbt;
2306
2307 /* Select the device */
2308 this->select_chip(mtd, 0);
2309
2310 /* Send the command for reading device ID */
2311 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2312
2313 /* Read manufacturer and device IDs */
2314 nand_maf_id = this->read_byte(mtd);
2315 nand_dev_id = this->read_byte(mtd);
2316
2317 /* Print and store flash device information */
2318 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
ac7eb8a3
WD
2319
2320 if (nand_dev_id != nand_flash_ids[i].id)
932394ac
WD
2321 continue;
2322
2323 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2324 this->chipsize = nand_flash_ids[i].chipsize << 20;
ac7eb8a3 2325
932394ac
WD
2326 /* New devices have all the information in additional id bytes */
2327 if (!nand_flash_ids[i].pagesize) {
2328 int extid;
2329 /* The 3rd id byte contains non relevant data ATM */
2330 extid = this->read_byte(mtd);
2331 /* The 4th id byte is the important one */
2332 extid = this->read_byte(mtd);
2333 /* Calc pagesize */
2334 mtd->oobblock = 1024 << (extid & 0x3);
2335 extid >>= 2;
2336 /* Calc oobsize */
2337 mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
2338 extid >>= 2;
2339 /* Calc blocksize. Blocksize is multiples of 64KiB */
2340 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2341 extid >>= 2;
2342 /* Get buswidth information */
2343 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
ac7eb8a3 2344
932394ac
WD
2345 } else {
2346 /* Old devices have this data hardcoded in the
2347 * device id table */
2348 mtd->erasesize = nand_flash_ids[i].erasesize;
2349 mtd->oobblock = nand_flash_ids[i].pagesize;
2350 mtd->oobsize = mtd->oobblock / 32;
2351 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2352 }
2353
2354 /* Check, if buswidth is correct. Hardware drivers should set
2355 * this correct ! */
2356 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2357 printk (KERN_INFO "NAND device: Manufacturer ID:"
ac7eb8a3 2358 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
932394ac 2359 nand_manuf_ids[i].name , mtd->name);
ac7eb8a3
WD
2360 printk (KERN_WARNING
2361 "NAND bus width %d instead %d bit\n",
932394ac
WD
2362 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2363 busw ? 16 : 8);
2364 this->select_chip(mtd, -1);
ac7eb8a3 2365 return 1;
932394ac 2366 }
ac7eb8a3
WD
2367
2368 /* Calculate the address shift from the page size */
932394ac
WD
2369 this->page_shift = ffs(mtd->oobblock) - 1;
2370 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2371 this->chip_shift = ffs(this->chipsize) - 1;
2372
2373 /* Set the bad block position */
ac7eb8a3 2374 this->badblockpos = mtd->oobblock > 512 ?
932394ac
WD
2375 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2376
2377 /* Get chip options, preserve non chip based options */
2378 this->options &= ~NAND_CHIPOPTIONS_MSK;
2379 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2380 /* Set this as a default. Board drivers can override it, if neccecary */
2381 this->options |= NAND_NO_AUTOINCR;
2382 /* Check if this is a not a samsung device. Do not clear the options
2383 * for chips which are not having an extended id.
ac7eb8a3 2384 */
932394ac
WD
2385 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2386 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
ac7eb8a3 2387
932394ac
WD
2388 /* Check for AND chips with 4 page planes */
2389 if (this->options & NAND_4PAGE_ARRAY)
2390 this->erase_cmd = multi_erase_cmd;
2391 else
2392 this->erase_cmd = single_erase_cmd;
2393
2394 /* Do not replace user supplied command function ! */
2395 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2396 this->cmdfunc = nand_command_lp;
ac7eb8a3 2397
932394ac
WD
2398 /* Try to identify manufacturer */
2399 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2400 if (nand_manuf_ids[j].id == nand_maf_id)
2401 break;
2402 }
932394ac
WD
2403 break;
2404 }
2405
2406 if (!nand_flash_ids[i].name) {
2407 printk (KERN_WARNING "No NAND device found!!!\n");
2408 this->select_chip(mtd, -1);
2409 return 1;
2410 }
2411
2412 for (i=1; i < maxchips; i++) {
2413 this->select_chip(mtd, i);
2414
2415 /* Send the command for reading device ID */
2416 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2417
2418 /* Read manufacturer and device IDs */
2419 if (nand_maf_id != this->read_byte(mtd) ||
2420 nand_dev_id != this->read_byte(mtd))
2421 break;
2422 }
2423 if (i > 1)
2424 printk(KERN_INFO "%d NAND chips detected\n", i);
ac7eb8a3 2425
932394ac
WD
2426 /* Allocate buffers, if neccecary */
2427 if (!this->oob_buf) {
2428 size_t len;
2429 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2430 this->oob_buf = kmalloc (len, GFP_KERNEL);
2431 if (!this->oob_buf) {
2432 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2433 return -ENOMEM;
2434 }
2435 this->options |= NAND_OOBBUF_ALLOC;
2436 }
ac7eb8a3 2437
932394ac
WD
2438 if (!this->data_buf) {
2439 size_t len;
2440 len = mtd->oobblock + mtd->oobsize;
2441 this->data_buf = kmalloc (len, GFP_KERNEL);
2442 if (!this->data_buf) {
2443 if (this->options & NAND_OOBBUF_ALLOC)
2444 kfree (this->oob_buf);
2445 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2446 return -ENOMEM;
2447 }
2448 this->options |= NAND_DATABUF_ALLOC;
2449 }
2450
2451 /* Store the number of chips and calc total size for mtd */
2452 this->numchips = i;
2453 mtd->size = i * this->chipsize;
2454 /* Convert chipsize to number of pages per chip -1. */
2455 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2456 /* Preset the internal oob buffer */
2457 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2458
2459 /* If no default placement scheme is given, select an
2460 * appropriate one */
2461 if (!this->autooob) {
2462 /* Select the appropriate default oob placement scheme for
2463 * placement agnostic filesystems */
ac7eb8a3 2464 switch (mtd->oobsize) {
932394ac
WD
2465 case 8:
2466 this->autooob = &nand_oob_8;
2467 break;
2468 case 16:
2469 this->autooob = &nand_oob_16;
2470 break;
2471 case 64:
2472 this->autooob = &nand_oob_64;
2473 break;
2474 default:
2475 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2476 mtd->oobsize);
2477/* BUG(); */
2478 }
2479 }
ac7eb8a3 2480
932394ac
WD
2481 /* The number of bytes available for the filesystem to place fs dependend
2482 * oob data */
2483 if (this->options & NAND_BUSWIDTH_16) {
2484 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
2485 if (this->autooob->eccbytes & 0x01)
2486 mtd->oobavail--;
2487 } else
2488 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
2489
ac7eb8a3 2490 /*
932394ac
WD
2491 * check ECC mode, default to software
2492 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
ac7eb8a3 2493 * fallback to software ECC
932394ac 2494 */
ac7eb8a3 2495 this->eccsize = 256; /* set default eccsize */
932394ac
WD
2496 this->eccbytes = 3;
2497
2498 switch (this->eccmode) {
2499 case NAND_ECC_HW12_2048:
2500 if (mtd->oobblock < 2048) {
2501 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2502 mtd->oobblock);
2503 this->eccmode = NAND_ECC_SOFT;
2504 this->calculate_ecc = nand_calculate_ecc;
2505 this->correct_data = nand_correct_data;
2506 } else
2507 this->eccsize = 2048;
2508 break;
2509
ac7eb8a3
WD
2510 case NAND_ECC_HW3_512:
2511 case NAND_ECC_HW6_512:
2512 case NAND_ECC_HW8_512:
932394ac
WD
2513 if (mtd->oobblock == 256) {
2514 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2515 this->eccmode = NAND_ECC_SOFT;
2516 this->calculate_ecc = nand_calculate_ecc;
2517 this->correct_data = nand_correct_data;
ac7eb8a3 2518 } else
932394ac
WD
2519 this->eccsize = 512; /* set eccsize to 512 */
2520 break;
ac7eb8a3 2521
932394ac
WD
2522 case NAND_ECC_HW3_256:
2523 break;
ac7eb8a3
WD
2524
2525 case NAND_ECC_NONE:
932394ac
WD
2526 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2527 this->eccmode = NAND_ECC_NONE;
2528 break;
2529
ac7eb8a3 2530 case NAND_ECC_SOFT:
932394ac
WD
2531 this->calculate_ecc = nand_calculate_ecc;
2532 this->correct_data = nand_correct_data;
2533 break;
2534
2535 default:
2536 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2537/* BUG(); */
ac7eb8a3 2538 }
932394ac 2539
ac7eb8a3 2540 /* Check hardware ecc function availability and adjust number of ecc bytes per
932394ac
WD
2541 * calculation step
2542 */
2543 switch (this->eccmode) {
2544 case NAND_ECC_HW12_2048:
2545 this->eccbytes += 4;
ac7eb8a3 2546 case NAND_ECC_HW8_512:
932394ac 2547 this->eccbytes += 2;
ac7eb8a3 2548 case NAND_ECC_HW6_512:
932394ac 2549 this->eccbytes += 3;
ac7eb8a3 2550 case NAND_ECC_HW3_512:
932394ac
WD
2551 case NAND_ECC_HW3_256:
2552 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2553 break;
2554 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2555/* BUG(); */
2556 }
ac7eb8a3 2557
932394ac 2558 mtd->eccsize = this->eccsize;
ac7eb8a3 2559
932394ac
WD
2560 /* Set the number of read / write steps for one page to ensure ECC generation */
2561 switch (this->eccmode) {
2562 case NAND_ECC_HW12_2048:
2563 this->eccsteps = mtd->oobblock / 2048;
2564 break;
2565 case NAND_ECC_HW3_512:
2566 case NAND_ECC_HW6_512:
2567 case NAND_ECC_HW8_512:
2568 this->eccsteps = mtd->oobblock / 512;
2569 break;
2570 case NAND_ECC_HW3_256:
ac7eb8a3 2571 case NAND_ECC_SOFT:
932394ac
WD
2572 this->eccsteps = mtd->oobblock / 256;
2573 break;
ac7eb8a3
WD
2574
2575 case NAND_ECC_NONE:
932394ac
WD
2576 this->eccsteps = 1;
2577 break;
2578 }
2579
2580/* XXX U-BOOT XXX */
ac7eb8a3 2581#if 0
932394ac
WD
2582 /* Initialize state, waitqueue and spinlock */
2583 this->state = FL_READY;
2584 init_waitqueue_head (&this->wq);
2585 spin_lock_init (&this->chip_lock);
2586#endif
2587
2588 /* De-select the device */
2589 this->select_chip(mtd, -1);
2590
2591 /* Invalidate the pagebuffer reference */
2592 this->pagebuf = -1;
2593
2594 /* Fill in remaining MTD driver data */
2595 mtd->type = MTD_NANDFLASH;
2596 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2597 mtd->ecctype = MTD_ECC_SW;
2598 mtd->erase = nand_erase;
2599 mtd->point = NULL;
2600 mtd->unpoint = NULL;
2601 mtd->read = nand_read;
2602 mtd->write = nand_write;
2603 mtd->read_ecc = nand_read_ecc;
2604 mtd->write_ecc = nand_write_ecc;
2605 mtd->read_oob = nand_read_oob;
2606 mtd->write_oob = nand_write_oob;
2607/* XXX U-BOOT XXX */
2608#if 0
2609 mtd->readv = NULL;
2610 mtd->writev = nand_writev;
2611 mtd->writev_ecc = nand_writev_ecc;
2612#endif
2613 mtd->sync = nand_sync;
2614/* XXX U-BOOT XXX */
2615#if 0
2616 mtd->lock = NULL;
2617 mtd->unlock = NULL;
2618 mtd->suspend = NULL;
2619 mtd->resume = NULL;
2620#endif
2621 mtd->block_isbad = nand_block_isbad;
2622 mtd->block_markbad = nand_block_markbad;
2623
2624 /* and make the autooob the default one */
2625 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2626/* XXX U-BOOT XXX */
2627#if 0
2628 mtd->owner = THIS_MODULE;
2629#endif
2630 /* Build bad block table */
2631 return this->scan_bbt (mtd);
2632}
2633
2634/**
ac7eb8a3 2635 * nand_release - [NAND Interface] Free resources held by the NAND device
932394ac 2636 * @mtd: MTD device structure
ac7eb8a3 2637 */
932394ac
WD
2638void nand_release (struct mtd_info *mtd)
2639{
2640 struct nand_chip *this = mtd->priv;
2641
2642#ifdef CONFIG_MTD_PARTITIONS
2643 /* Deregister partitions */
2644 del_mtd_partitions (mtd);
2645#endif
2646 /* Deregister the device */
2647/* XXX U-BOOT XXX */
2648#if 0
2649 del_mtd_device (mtd);
2650#endif
2651 /* Free bad block table memory, if allocated */
2652 if (this->bbt)
2653 kfree (this->bbt);
2654 /* Buffer allocated by nand_scan ? */
2655 if (this->options & NAND_OOBBUF_ALLOC)
2656 kfree (this->oob_buf);
2657 /* Buffer allocated by nand_scan ? */
2658 if (this->options & NAND_DATABUF_ALLOC)
2659 kfree (this->data_buf);
2660}
2661
2662#endif
038ccac5
BS
2663#endif /* CONFIG_NEW_NAND_CODE */
2664