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