blob: 95e96fa1fcebb61522ebf342952b23d30acb9623 [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.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002 Thomas Gleixner (tglx@linutronix.de)
14 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000015 * 02-08-2004 tglx: support for strange chips, which cannot auto increment
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000024 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000033 * are rewritten many times. JFFS2 ensures this doesn't happen for blocks
David A. Marlin30f464b2005-01-17 18:35:25 +000034 * 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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000036 * 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
David A. Marlin30f464b2005-01-17 18:35:25 +000038 * in the same area, and rewrite the BBT when any of them are erased.
39 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000040 * 01-03-2005 dmarlin: added support for the device recovery command sequence for Renesas
David A. Marlin30f464b2005-01-17 18:35:25 +000041 * 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 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000045 * 01-20-2005 dmarlin: added support for optional hardware specific callback routine to
David A. Marlin068e3c02005-01-24 03:07:46 +000046 * perform extra error status checks on erase and write failures. This required
47 * adding a wrapper function for nand_read_ecc.
48 *
Vitaly Wool962034f2005-09-15 14:58:53 +010049 * 08-20-2005 vwool: suspend/resume added
50 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000052 * David Woodhouse for adding multichip support
53 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
55 * rework for 2K page size chips
56 *
57 * TODO:
58 * Enable cached programming for 2k page size chips
59 * Check, if mtd->ecctype should be set to MTD_ECC_HW
60 * if we have HW ecc support.
61 * The AG-AND chips have nice features for speed improvement,
62 * which are not supported yet. Read / program 4 pages in one go.
63 *
Vitaly Wool962034f2005-09-15 14:58:53 +010064 * $Id: nand_base.c,v 1.150 2005/09/15 13:58:48 vwool Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
66 * This program is free software; you can redistribute it and/or modify
67 * it under the terms of the GNU General Public License version 2 as
68 * published by the Free Software Foundation.
69 *
70 */
71
72#include <linux/delay.h>
73#include <linux/errno.h>
74#include <linux/sched.h>
75#include <linux/slab.h>
76#include <linux/types.h>
77#include <linux/mtd/mtd.h>
78#include <linux/mtd/nand.h>
79#include <linux/mtd/nand_ecc.h>
80#include <linux/mtd/compatmac.h>
81#include <linux/interrupt.h>
82#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080083#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <asm/io.h>
85
86#ifdef CONFIG_MTD_PARTITIONS
87#include <linux/mtd/partitions.h>
88#endif
89
90/* Define default oob placement schemes for large and small page devices */
91static struct nand_oobinfo nand_oob_8 = {
92 .useecc = MTD_NANDECC_AUTOPLACE,
93 .eccbytes = 3,
94 .eccpos = {0, 1, 2},
95 .oobfree = { {3, 2}, {6, 2} }
96};
97
98static struct nand_oobinfo nand_oob_16 = {
99 .useecc = MTD_NANDECC_AUTOPLACE,
100 .eccbytes = 6,
101 .eccpos = {0, 1, 2, 3, 6, 7},
102 .oobfree = { {8, 8} }
103};
104
105static struct nand_oobinfo nand_oob_64 = {
106 .useecc = MTD_NANDECC_AUTOPLACE,
107 .eccbytes = 24,
108 .eccpos = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000109 40, 41, 42, 43, 44, 45, 46, 47,
110 48, 49, 50, 51, 52, 53, 54, 55,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 56, 57, 58, 59, 60, 61, 62, 63},
112 .oobfree = { {2, 38} }
113};
114
115/* This is used for padding purposes in nand_write_oob */
116static u_char ffchars[] = {
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 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};
126
127/*
128 * NAND low-level MTD interface functions
129 */
130static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
131static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
132static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
133
134static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
135static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
136 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
137static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
138static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
139static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
140 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
141static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
142static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
143 unsigned long count, loff_t to, size_t * retlen);
144static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
145 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
146static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
147static void nand_sync (struct mtd_info *mtd);
148
149/* Some internal functions */
150static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
151 struct nand_oobinfo *oobsel, int mode);
152#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000153static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
155#else
156#define nand_verify_pages(...) (0)
157#endif
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000158
Vitaly Wool962034f2005-09-15 14:58:53 +0100159static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/**
162 * nand_release_device - [GENERIC] release chip
163 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000164 *
165 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167static void nand_release_device (struct mtd_info *mtd)
168{
169 struct nand_chip *this = mtd->priv;
170
171 /* De-select the NAND device */
172 this->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (this->controller) {
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100175 /* Release the controller and the chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 spin_lock(&this->controller->lock);
177 this->controller->active = NULL;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100178 this->state = FL_READY;
179 wake_up(&this->controller->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 spin_unlock(&this->controller->lock);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100181 } else {
182 /* Release the chip */
183 spin_lock(&this->chip_lock);
184 this->state = FL_READY;
185 wake_up(&this->wq);
186 spin_unlock(&this->chip_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/**
191 * nand_read_byte - [DEFAULT] read one byte from the chip
192 * @mtd: MTD device structure
193 *
194 * Default read function for 8bit buswith
195 */
196static u_char nand_read_byte(struct mtd_info *mtd)
197{
198 struct nand_chip *this = mtd->priv;
199 return readb(this->IO_ADDR_R);
200}
201
202/**
203 * nand_write_byte - [DEFAULT] write one byte to the chip
204 * @mtd: MTD device structure
205 * @byte: pointer to data byte to write
206 *
207 * Default write function for 8it buswith
208 */
209static void nand_write_byte(struct mtd_info *mtd, u_char byte)
210{
211 struct nand_chip *this = mtd->priv;
212 writeb(byte, this->IO_ADDR_W);
213}
214
215/**
216 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
217 * @mtd: MTD device structure
218 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000219 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * endianess conversion
221 */
222static u_char nand_read_byte16(struct mtd_info *mtd)
223{
224 struct nand_chip *this = mtd->priv;
225 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
226}
227
228/**
229 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
230 * @mtd: MTD device structure
231 * @byte: pointer to data byte to write
232 *
233 * Default write function for 16bit buswith with
234 * endianess conversion
235 */
236static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
237{
238 struct nand_chip *this = mtd->priv;
239 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
240}
241
242/**
243 * nand_read_word - [DEFAULT] read one word from the chip
244 * @mtd: MTD device structure
245 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000246 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * endianess conversion
248 */
249static u16 nand_read_word(struct mtd_info *mtd)
250{
251 struct nand_chip *this = mtd->priv;
252 return readw(this->IO_ADDR_R);
253}
254
255/**
256 * nand_write_word - [DEFAULT] write one word to the chip
257 * @mtd: MTD device structure
258 * @word: data word to write
259 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000260 * Default write function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * endianess conversion
262 */
263static void nand_write_word(struct mtd_info *mtd, u16 word)
264{
265 struct nand_chip *this = mtd->priv;
266 writew(word, this->IO_ADDR_W);
267}
268
269/**
270 * nand_select_chip - [DEFAULT] control CE line
271 * @mtd: MTD device structure
272 * @chip: chipnumber to select, -1 for deselect
273 *
274 * Default select function for 1 chip devices.
275 */
276static void nand_select_chip(struct mtd_info *mtd, int chip)
277{
278 struct nand_chip *this = mtd->priv;
279 switch(chip) {
280 case -1:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000281 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 case 0:
284 this->hwcontrol(mtd, NAND_CTL_SETNCE);
285 break;
286
287 default:
288 BUG();
289 }
290}
291
292/**
293 * nand_write_buf - [DEFAULT] write buffer to chip
294 * @mtd: MTD device structure
295 * @buf: data buffer
296 * @len: number of bytes to write
297 *
298 * Default write function for 8bit buswith
299 */
300static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
301{
302 int i;
303 struct nand_chip *this = mtd->priv;
304
305 for (i=0; i<len; i++)
306 writeb(buf[i], this->IO_ADDR_W);
307}
308
309/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000310 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * @mtd: MTD device structure
312 * @buf: buffer to store date
313 * @len: number of bytes to read
314 *
315 * Default read function for 8bit buswith
316 */
317static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
318{
319 int i;
320 struct nand_chip *this = mtd->priv;
321
322 for (i=0; i<len; i++)
323 buf[i] = readb(this->IO_ADDR_R);
324}
325
326/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000327 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * @mtd: MTD device structure
329 * @buf: buffer containing the data to compare
330 * @len: number of bytes to compare
331 *
332 * Default verify function for 8bit buswith
333 */
334static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
335{
336 int i;
337 struct nand_chip *this = mtd->priv;
338
339 for (i=0; i<len; i++)
340 if (buf[i] != readb(this->IO_ADDR_R))
341 return -EFAULT;
342
343 return 0;
344}
345
346/**
347 * nand_write_buf16 - [DEFAULT] write buffer to chip
348 * @mtd: MTD device structure
349 * @buf: data buffer
350 * @len: number of bytes to write
351 *
352 * Default write function for 16bit buswith
353 */
354static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
355{
356 int i;
357 struct nand_chip *this = mtd->priv;
358 u16 *p = (u16 *) buf;
359 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 for (i=0; i<len; i++)
362 writew(p[i], this->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000367 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * @mtd: MTD device structure
369 * @buf: buffer to store date
370 * @len: number of bytes to read
371 *
372 * Default read function for 16bit buswith
373 */
374static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
375{
376 int i;
377 struct nand_chip *this = mtd->priv;
378 u16 *p = (u16 *) buf;
379 len >>= 1;
380
381 for (i=0; i<len; i++)
382 p[i] = readw(this->IO_ADDR_R);
383}
384
385/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000386 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * @mtd: MTD device structure
388 * @buf: buffer containing the data to compare
389 * @len: number of bytes to compare
390 *
391 * Default verify function for 16bit buswith
392 */
393static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
394{
395 int i;
396 struct nand_chip *this = mtd->priv;
397 u16 *p = (u16 *) buf;
398 len >>= 1;
399
400 for (i=0; i<len; i++)
401 if (p[i] != readw(this->IO_ADDR_R))
402 return -EFAULT;
403
404 return 0;
405}
406
407/**
408 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
409 * @mtd: MTD device structure
410 * @ofs: offset from device start
411 * @getchip: 0, if the chip is already selected
412 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000413 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
415static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
416{
417 int page, chipnr, res = 0;
418 struct nand_chip *this = mtd->priv;
419 u16 bad;
420
421 if (getchip) {
422 page = (int)(ofs >> this->page_shift);
423 chipnr = (int)(ofs >> this->chip_shift);
424
425 /* Grab the lock and see if the device is available */
426 nand_get_device (this, mtd, FL_READING);
427
428 /* Select the NAND device */
429 this->select_chip(mtd, chipnr);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000430 } else
431 page = (int) ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (this->options & NAND_BUSWIDTH_16) {
434 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
435 bad = cpu_to_le16(this->read_word(mtd));
436 if (this->badblockpos & 0x1)
Vitaly Wool49196f32005-11-02 16:54:46 +0000437 bad >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if ((bad & 0xFF) != 0xff)
439 res = 1;
440 } else {
441 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
442 if (this->read_byte(mtd) != 0xff)
443 res = 1;
444 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (getchip) {
447 /* Deselect and wake up anyone waiting on the device */
448 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000449 }
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return res;
452}
453
454/**
455 * nand_default_block_markbad - [DEFAULT] mark a block bad
456 * @mtd: MTD device structure
457 * @ofs: offset from device start
458 *
459 * This is the default implementation, which can be overridden by
460 * a hardware specific driver.
461*/
462static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
463{
464 struct nand_chip *this = mtd->priv;
465 u_char buf[2] = {0, 0};
466 size_t retlen;
467 int block;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Get block number */
470 block = ((int) ofs) >> this->bbt_erase_shift;
Artem B. Bityuckiy41ce9212005-02-09 14:50:00 +0000471 if (this->bbt)
472 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* Do we have a flash based bad block table ? */
475 if (this->options & NAND_USE_FLASH_BBT)
476 return nand_update_bbt (mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* We write two bytes, so we dont have to mess with 16 bit access */
479 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
480 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
481}
482
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000483/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * nand_check_wp - [GENERIC] check if the chip is write protected
485 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000486 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000488 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 */
490static int nand_check_wp (struct mtd_info *mtd)
491{
492 struct nand_chip *this = mtd->priv;
493 /* Check the WP bit */
494 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000495 return (this->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/**
499 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
500 * @mtd: MTD device structure
501 * @ofs: offset from device start
502 * @getchip: 0, if the chip is already selected
503 * @allowbbt: 1, if its allowed to access the bbt area
504 *
505 * Check, if the block is bad. Either by reading the bad block table or
506 * calling of the scan function.
507 */
508static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
509{
510 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!this->bbt)
513 return this->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* Return info from the table */
516 return nand_isbad_bbt (mtd, ofs, allowbbt);
517}
518
Richard Purdie8fe833c2006-03-31 02:31:14 -0800519DEFINE_LED_TRIGGER(nand_led_trigger);
520
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000521/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000522 * Wait for the ready pin, after a command
523 * The timeout is catched later.
524 */
525static void nand_wait_ready(struct mtd_info *mtd)
526{
527 struct nand_chip *this = mtd->priv;
528 unsigned long timeo = jiffies + 2;
529
Richard Purdie8fe833c2006-03-31 02:31:14 -0800530 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000531 /* wait until command is processed or timeout occures */
532 do {
533 if (this->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800534 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700535 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000536 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800537 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/**
541 * nand_command - [DEFAULT] Send command to NAND device
542 * @mtd: MTD device structure
543 * @command: the command to be sent
544 * @column: the column address for this command, -1 if none
545 * @page_addr: the page address for this command, -1 if none
546 *
547 * Send command to NAND device. This function is used for small page
548 * devices (256/512 Bytes per page)
549 */
550static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
551{
552 register struct nand_chip *this = mtd->priv;
553
554 /* Begin command latch cycle */
555 this->hwcontrol(mtd, NAND_CTL_SETCLE);
556 /*
557 * Write out the command to the device.
558 */
559 if (command == NAND_CMD_SEQIN) {
560 int readcmd;
561
562 if (column >= mtd->oobblock) {
563 /* OOB area */
564 column -= mtd->oobblock;
565 readcmd = NAND_CMD_READOOB;
566 } else if (column < 256) {
567 /* First 256 bytes --> READ0 */
568 readcmd = NAND_CMD_READ0;
569 } else {
570 column -= 256;
571 readcmd = NAND_CMD_READ1;
572 }
573 this->write_byte(mtd, readcmd);
574 }
575 this->write_byte(mtd, command);
576
577 /* Set ALE and clear CLE to start address cycle */
578 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
579
580 if (column != -1 || page_addr != -1) {
581 this->hwcontrol(mtd, NAND_CTL_SETALE);
582
583 /* Serially input address */
584 if (column != -1) {
585 /* Adjust columns for 16 bit buswidth */
586 if (this->options & NAND_BUSWIDTH_16)
587 column >>= 1;
588 this->write_byte(mtd, column);
589 }
590 if (page_addr != -1) {
591 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
592 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
593 /* One more address cycle for devices > 32MiB */
594 if (this->chipsize > (32 << 20))
595 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
596 }
597 /* Latch in address */
598 this->hwcontrol(mtd, NAND_CTL_CLRALE);
599 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000600
601 /*
602 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * status and sequential in needs no delay
604 */
605 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 case NAND_CMD_PAGEPROG:
608 case NAND_CMD_ERASE1:
609 case NAND_CMD_ERASE2:
610 case NAND_CMD_SEQIN:
611 case NAND_CMD_STATUS:
612 return;
613
614 case NAND_CMD_RESET:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000615 if (this->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617 udelay(this->chip_delay);
618 this->hwcontrol(mtd, NAND_CTL_SETCLE);
619 this->write_byte(mtd, NAND_CMD_STATUS);
620 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
David A. Marlina4ab4c52005-01-23 18:30:53 +0000621 while ( !(this->read_byte(mtd) & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return;
623
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000624 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000626 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * If we don't have access to the busy pin, we apply the given
628 * command delay
629 */
630 if (!this->dev_ready) {
631 udelay (this->chip_delay);
632 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Apply this short delay always to ensure that we do wait tWB in
636 * any case on any machine. */
637 ndelay (100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000638
639 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/**
643 * nand_command_lp - [DEFAULT] Send command to NAND large page device
644 * @mtd: MTD device structure
645 * @command: the command to be sent
646 * @column: the column address for this command, -1 if none
647 * @page_addr: the page address for this command, -1 if none
648 *
649 * Send command to NAND device. This is the version for the new large page devices
650 * We dont have the seperate regions as we have in the small page devices.
651 * We must emulate NAND_CMD_READOOB to keep the code compatible.
652 *
653 */
654static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
655{
656 register struct nand_chip *this = mtd->priv;
657
658 /* Emulate NAND_CMD_READOOB */
659 if (command == NAND_CMD_READOOB) {
660 column += mtd->oobblock;
661 command = NAND_CMD_READ0;
662 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000663
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Begin command latch cycle */
666 this->hwcontrol(mtd, NAND_CTL_SETCLE);
667 /* Write out the command to the device. */
David A. Marlin30f464b2005-01-17 18:35:25 +0000668 this->write_byte(mtd, (command & 0xff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* End command latch cycle */
670 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
671
672 if (column != -1 || page_addr != -1) {
673 this->hwcontrol(mtd, NAND_CTL_SETALE);
674
675 /* Serially input address */
676 if (column != -1) {
677 /* Adjust columns for 16 bit buswidth */
678 if (this->options & NAND_BUSWIDTH_16)
679 column >>= 1;
680 this->write_byte(mtd, column & 0xff);
681 this->write_byte(mtd, column >> 8);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (page_addr != -1) {
684 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
685 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
686 /* One more address cycle for devices > 128MiB */
687 if (this->chipsize > (128 << 20))
688 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
689 }
690 /* Latch in address */
691 this->hwcontrol(mtd, NAND_CTL_CLRALE);
692 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000693
694 /*
695 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000696 * status, sequential in, and deplete1 need no delay
697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 case NAND_CMD_CACHEDPROG:
701 case NAND_CMD_PAGEPROG:
702 case NAND_CMD_ERASE1:
703 case NAND_CMD_ERASE2:
704 case NAND_CMD_SEQIN:
705 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000706 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return;
708
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709 /*
David A. Marlin30f464b2005-01-17 18:35:25 +0000710 * read error status commands require only a short delay
711 */
712 case NAND_CMD_STATUS_ERROR:
713 case NAND_CMD_STATUS_ERROR0:
714 case NAND_CMD_STATUS_ERROR1:
715 case NAND_CMD_STATUS_ERROR2:
716 case NAND_CMD_STATUS_ERROR3:
717 udelay(this->chip_delay);
718 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 case NAND_CMD_RESET:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000721 if (this->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723 udelay(this->chip_delay);
724 this->hwcontrol(mtd, NAND_CTL_SETCLE);
725 this->write_byte(mtd, NAND_CMD_STATUS);
726 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
David A. Marlina4ab4c52005-01-23 18:30:53 +0000727 while ( !(this->read_byte(mtd) & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return;
729
730 case NAND_CMD_READ0:
731 /* Begin command latch cycle */
732 this->hwcontrol(mtd, NAND_CTL_SETCLE);
733 /* Write out the start read command */
734 this->write_byte(mtd, NAND_CMD_READSTART);
735 /* End command latch cycle */
736 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
737 /* Fall through into ready check */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000738
739 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000741 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * If we don't have access to the busy pin, we apply the given
743 * command delay
744 */
745 if (!this->dev_ready) {
746 udelay (this->chip_delay);
747 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* Apply this short delay always to ensure that we do wait tWB in
752 * any case on any machine. */
753 ndelay (100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000754
755 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758/**
759 * nand_get_device - [GENERIC] Get chip for selected access
760 * @this: the nand chip descriptor
761 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000762 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 *
764 * Get the device and lock it for exclusive access
765 */
Vitaly Wool962034f2005-09-15 14:58:53 +0100766static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100768 struct nand_chip *active;
769 spinlock_t *lock;
770 wait_queue_head_t *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 DECLARE_WAITQUEUE (wait, current);
772
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100773 lock = (this->controller) ? &this->controller->lock : &this->chip_lock;
774 wq = (this->controller) ? &this->controller->wq : &this->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100776 active = this;
777 spin_lock(lock);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* Hardware controller shared among independend devices */
780 if (this->controller) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (this->controller->active)
782 active = this->controller->active;
783 else
784 this->controller->active = this;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100786 if (active == this && this->state == FL_READY) {
787 this->state = new_state;
788 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100789 return 0;
790 }
791 if (new_state == FL_PM_SUSPENDED) {
792 spin_unlock(lock);
793 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100794 }
795 set_current_state(TASK_UNINTERRUPTIBLE);
796 add_wait_queue(wq, &wait);
797 spin_unlock(lock);
798 schedule();
799 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto retry;
801}
802
803/**
804 * nand_wait - [DEFAULT] wait until the command is done
805 * @mtd: MTD device structure
806 * @this: NAND chip structure
807 * @state: state to select the max. timeout value
808 *
809 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000810 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * general NAND and SmartMedia specs
812 *
813*/
814static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
815{
816
817 unsigned long timeo = jiffies;
818 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (state == FL_ERASING)
821 timeo += (HZ * 400) / 1000;
822 else
823 timeo += (HZ * 20) / 1000;
824
Richard Purdie8fe833c2006-03-31 02:31:14 -0800825 led_trigger_event(nand_led_trigger, LED_FULL);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* Apply this short delay always to ensure that we do wait tWB in
828 * any case on any machine. */
829 ndelay (100);
830
831 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
832 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000833 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
835
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000836 while (time_before(jiffies, timeo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* Check, if we were interrupted */
838 if (this->state != state)
839 return 0;
840
841 if (this->dev_ready) {
842 if (this->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 } else {
845 if (this->read_byte(mtd) & NAND_STATUS_READY)
846 break;
847 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000848 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800850 led_trigger_event(nand_led_trigger, LED_OFF);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 status = (int) this->read_byte(mtd);
853 return status;
854}
855
856/**
857 * nand_write_page - [GENERIC] write one page
858 * @mtd: MTD device structure
859 * @this: NAND chip structure
860 * @page: startpage inside the chip, must be called with (page & this->pagemask)
861 * @oob_buf: out of band data buffer
862 * @oobsel: out of band selecttion structre
863 * @cached: 1 = enable cached programming if supported by chip
864 *
865 * Nand_page_program function is used for write and writev !
866 * This function will always program a full page of data
867 * If you call it with a non page aligned buffer, you're lost :)
868 *
869 * Cached programming is not supported yet.
870 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000871static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
873{
874 int i, status;
Jarkko Lavinen0a18cde2005-04-11 15:16:11 +0100875 u_char ecc_code[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
877 int *oob_config = oobsel->eccpos;
878 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
879 int eccbytes = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* FIXME: Enable cached programming */
882 cached = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* Send command to begin auto page programming */
885 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
886
887 /* Write out complete page of data, take care of eccmode */
888 switch (eccmode) {
889 /* No ecc, write all */
890 case NAND_ECC_NONE:
891 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
892 this->write_buf(mtd, this->data_poi, mtd->oobblock);
893 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* Software ecc 3/256, write all */
896 case NAND_ECC_SOFT:
897 for (; eccsteps; eccsteps--) {
898 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
899 for (i = 0; i < 3; i++, eccidx++)
900 oob_buf[oob_config[eccidx]] = ecc_code[i];
901 datidx += this->eccsize;
902 }
903 this->write_buf(mtd, this->data_poi, mtd->oobblock);
904 break;
905 default:
906 eccbytes = this->eccbytes;
907 for (; eccsteps; eccsteps--) {
908 /* enable hardware ecc logic for write */
909 this->enable_hwecc(mtd, NAND_ECC_WRITE);
910 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
911 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
912 for (i = 0; i < eccbytes; i++, eccidx++)
913 oob_buf[oob_config[eccidx]] = ecc_code[i];
914 /* If the hardware ecc provides syndromes then
915 * the ecc code must be written immidiately after
916 * the data bytes (words) */
917 if (this->options & NAND_HWECC_SYNDROME)
918 this->write_buf(mtd, ecc_code, eccbytes);
919 datidx += this->eccsize;
920 }
921 break;
922 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* Write out OOB data */
925 if (this->options & NAND_HWECC_SYNDROME)
926 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000927 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 this->write_buf(mtd, oob_buf, mtd->oobsize);
929
930 /* Send command to actually program the data */
931 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
932
933 if (!cached) {
934 /* call wait ready function */
935 status = this->waitfunc (mtd, this, FL_WRITING);
David A. Marlin068e3c02005-01-24 03:07:46 +0000936
937 /* See if operation failed and additional status checks are available */
938 if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
939 status = this->errstat(mtd, this, FL_WRITING, status, page);
940 }
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* See if device thinks it succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +0000943 if (status & NAND_STATUS_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
945 return -EIO;
946 }
947 } else {
948 /* FIXME: Implement cached programming ! */
949 /* wait until cache is ready*/
950 // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
951 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
956/**
957 * nand_verify_pages - [GENERIC] verify the chip contents after a write
958 * @mtd: MTD device structure
959 * @this: NAND chip structure
960 * @page: startpage inside the chip, must be called with (page & this->pagemask)
961 * @numpages: number of pages to verify
962 * @oob_buf: out of band data buffer
963 * @oobsel: out of band selecttion structre
964 * @chipnr: number of the current chip
965 * @oobmode: 1 = full buffer verify, 0 = ecc only
966 *
967 * The NAND device assumes that it is always writing to a cleanly erased page.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000968 * Hence, it performs its internal write verification only on bits that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * transitioned from 1 to 0. The device does NOT verify the whole page on a
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000970 * byte by byte basis. It is possible that the page was not completely erased
971 * or the page is becoming unusable due to wear. The read with ECC would catch
972 * the error later when the ECC page check fails, but we would rather catch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * it early in the page write stage. Better to write no data than invalid data.
974 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000975static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
977{
978 int i, j, datidx = 0, oobofs = 0, res = -EIO;
979 int eccsteps = this->eccsteps;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000980 int hweccbytes;
Jarkko Lavinen0a18cde2005-04-11 15:16:11 +0100981 u_char oobdata[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
984
985 /* Send command to read back the first page */
986 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
987
988 for(;;) {
989 for (j = 0; j < eccsteps; j++) {
990 /* Loop through and verify the data */
991 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
992 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
993 goto out;
994 }
995 datidx += mtd->eccsize;
996 /* Have we a hw generator layout ? */
997 if (!hweccbytes)
998 continue;
999 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
1000 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1001 goto out;
1002 }
1003 oobofs += hweccbytes;
1004 }
1005
1006 /* check, if we must compare all data or if we just have to
1007 * compare the ecc bytes
1008 */
1009 if (oobmode) {
1010 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1011 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1012 goto out;
1013 }
1014 } else {
1015 /* Read always, else autoincrement fails */
1016 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1017
1018 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1019 int ecccnt = oobsel->eccbytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 for (i = 0; i < ecccnt; i++) {
1022 int idx = oobsel->eccpos[i];
1023 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
1024 DEBUG (MTD_DEBUG_LEVEL0,
1025 "%s: Failed ECC write "
1026 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
1027 goto out;
1028 }
1029 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1033 page++;
1034 numpages--;
1035
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001036 /* Apply delay or wait for ready/busy pin
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * Do this before the AUTOINCR check, so no problems
1038 * arise if a chip which does auto increment
1039 * is marked as NOAUTOINCR by the board driver.
1040 * Do this also before returning, so the chip is
1041 * ready for the next command.
1042 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001043 if (!this->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 udelay (this->chip_delay);
1045 else
Thomas Gleixner3b887752005-02-22 21:56:49 +00001046 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /* All done, return happy */
1049 if (!numpages)
1050 return 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001051
1052
1053 /* Check, if the chip supports auto page increment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!NAND_CANAUTOINCR(this))
1055 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1056 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001057 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 * Terminate the read command. We come here in case of an error
1059 * So we must issue a reset command.
1060 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001061out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1063 return res;
1064}
1065#endif
1066
1067/**
David A. Marlin068e3c02005-01-24 03:07:46 +00001068 * nand_read - [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 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001075 * This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL
1076 * and flags = 0xff
1077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1079{
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001080 return nand_do_read_ecc (mtd, from, len, retlen, buf, NULL, &mtd->oobinfo, 0xff);
1081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083
1084/**
David A. Marlin068e3c02005-01-24 03:07:46 +00001085 * nand_read_ecc - [MTD Interface] MTD compability function for nand_do_read_ecc
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 * @mtd: MTD device structure
1087 * @from: offset to read from
1088 * @len: number of bytes to read
1089 * @retlen: pointer to variable to store the number of read bytes
1090 * @buf: the databuffer to put data
1091 * @oob_buf: filesystem supplied oob data buffer
1092 * @oobsel: oob selection structure
1093 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001094 * This function simply calls nand_do_read_ecc with flags = 0xff
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
1096static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1097 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1098{
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001099 /* use userspace supplied oobinfo, if zero */
1100 if (oobsel == NULL)
1101 oobsel = &mtd->oobinfo;
David A. Marlin068e3c02005-01-24 03:07:46 +00001102 return nand_do_read_ecc(mtd, from, len, retlen, buf, oob_buf, oobsel, 0xff);
1103}
1104
1105
1106/**
1107 * nand_do_read_ecc - [MTD Interface] Read data with ECC
1108 * @mtd: MTD device structure
1109 * @from: offset to read from
1110 * @len: number of bytes to read
1111 * @retlen: pointer to variable to store the number of read bytes
1112 * @buf: the databuffer to put data
Dan Brownbb75ba42005-04-04 19:02:26 +01001113 * @oob_buf: filesystem supplied oob data buffer (can be NULL)
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001114 * @oobsel: oob selection structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001115 * @flags: flag to indicate if nand_get_device/nand_release_device should be preformed
1116 * and how many corrected error bits are acceptable:
1117 * bits 0..7 - number of tolerable errors
1118 * bit 8 - 0 == do not get/release chip, 1 == get/release chip
1119 *
1120 * NAND read with ECC
1121 */
1122int nand_do_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001123 size_t * retlen, u_char * buf, u_char * oob_buf,
David A. Marlin068e3c02005-01-24 03:07:46 +00001124 struct nand_oobinfo *oobsel, int flags)
1125{
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1128 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1129 struct nand_chip *this = mtd->priv;
1130 u_char *data_poi, *oob_data = oob_buf;
Jarkko Lavinen0a18cde2005-04-11 15:16:11 +01001131 u_char ecc_calc[32];
1132 u_char ecc_code[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 int eccmode, eccsteps;
1134 int *oob_config, datidx;
1135 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1136 int eccbytes;
1137 int compareecc = 1;
1138 int oobreadlen;
1139
1140
1141 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1142
1143 /* Do not allow reads past end of device */
1144 if ((from + len) > mtd->size) {
1145 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1146 *retlen = 0;
1147 return -EINVAL;
1148 }
1149
1150 /* Grab the lock and see if the device is available */
David A. Marlin068e3c02005-01-24 03:07:46 +00001151 if (flags & NAND_GET_DEVICE)
1152 nand_get_device (this, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* Autoplace of oob data ? Use the default placement scheme */
1155 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1156 oobsel = this->autooob;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1159 oob_config = oobsel->eccpos;
1160
1161 /* Select the NAND device */
1162 chipnr = (int)(from >> this->chip_shift);
1163 this->select_chip(mtd, chipnr);
1164
1165 /* First we calculate the starting page */
1166 realpage = (int) (from >> this->page_shift);
1167 page = realpage & this->pagemask;
1168
1169 /* Get raw starting column */
1170 col = from & (mtd->oobblock - 1);
1171
1172 end = mtd->oobblock;
1173 ecc = this->eccsize;
1174 eccbytes = this->eccbytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1177 compareecc = 0;
1178
1179 oobreadlen = mtd->oobsize;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001180 if (this->options & NAND_HWECC_SYNDROME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 oobreadlen -= oobsel->eccbytes;
1182
1183 /* Loop until all data read */
1184 while (read < len) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int aligned = (!col && (len - read) >= end);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001187 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 * If the read is not page aligned, we have to read into data buffer
1189 * due to ecc, else we read into return buffer direct
1190 */
1191 if (aligned)
1192 data_poi = &buf[read];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001193 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 data_poi = this->data_buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001195
1196 /* Check, if we have this page in the buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 *
1198 * FIXME: Make it work when we must provide oob data too,
1199 * check the usage of data_buf oob field
1200 */
1201 if (realpage == this->pagebuf && !oob_buf) {
1202 /* aligned read ? */
1203 if (aligned)
1204 memcpy (data_poi, this->data_buf, end);
1205 goto readdata;
1206 }
1207
1208 /* Check, if we must send the read command */
1209 if (sndcmd) {
1210 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1211 sndcmd = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /* get oob area, if we have no oob buffer from fs-driver */
Thomas Gleixner90e260c2005-05-19 17:10:26 +01001215 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1216 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 oob_data = &this->data_buf[end];
1218
1219 eccsteps = this->eccsteps;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 switch (eccmode) {
1222 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1223 static unsigned long lastwhinge = 0;
1224 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1225 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1226 lastwhinge = jiffies;
1227 }
1228 this->read_buf(mtd, data_poi, end);
1229 break;
1230 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1233 this->read_buf(mtd, data_poi, end);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001234 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 default:
1239 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1240 this->enable_hwecc(mtd, NAND_ECC_READ);
1241 this->read_buf(mtd, &data_poi[datidx], ecc);
1242
1243 /* HW ecc with syndrome calculation must read the
1244 * syndrome from flash immidiately after the data */
1245 if (!compareecc) {
1246 /* Some hw ecc generators need to know when the
1247 * syndrome is read from flash */
1248 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1249 this->read_buf(mtd, &oob_data[i], eccbytes);
1250 /* We calc error correction directly, it checks the hw
1251 * generator for an error, reads back the syndrome and
1252 * does the error correction on the fly */
David A. Marlin068e3c02005-01-24 03:07:46 +00001253 ecc_status = this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]);
1254 if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001255 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1257 ecc_failed++;
1258 }
1259 } else {
1260 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
1266 /* read oobdata */
1267 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1268
1269 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1270 if (!compareecc)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001271 goto readoob;
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /* Pick the ECC bytes out of the oob data */
1274 for (j = 0; j < oobsel->eccbytes; j++)
1275 ecc_code[j] = oob_data[oob_config[j]];
1276
1277 /* correct data, if neccecary */
1278 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1279 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /* Get next chunk of ecc bytes */
1282 j += eccbytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001283
1284 /* Check, if we have a fs supplied oob-buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 * This is the legacy mode. Used by YAFFS1
1286 * Should go away some day
1287 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001288 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int *p = (int *)(&oob_data[mtd->oobsize]);
1290 p[i] = ecc_status;
1291 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001292
1293 if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1295 ecc_failed++;
1296 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 readoob:
1300 /* check, if we have a fs supplied oob-buffer */
1301 if (oob_buf) {
1302 /* without autoplace. Legacy mode used by YAFFS1 */
1303 switch(oobsel->useecc) {
1304 case MTD_NANDECC_AUTOPLACE:
Thomas Gleixner90e260c2005-05-19 17:10:26 +01001305 case MTD_NANDECC_AUTOPL_USR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* Walk through the autoplace chunks */
Dan Brown82e1d192005-04-06 21:13:09 +01001307 for (i = 0; oobsel->oobfree[i][1]; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int from = oobsel->oobfree[i][0];
1309 int num = oobsel->oobfree[i][1];
1310 memcpy(&oob_buf[oob], &oob_data[from], num);
Dan Brown82e1d192005-04-06 21:13:09 +01001311 oob += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 case MTD_NANDECC_PLACE:
1315 /* YAFFS1 legacy mode */
1316 oob_data += this->eccsteps * sizeof (int);
1317 default:
1318 oob_data += mtd->oobsize;
1319 }
1320 }
1321 readdata:
1322 /* Partial page read, transfer data into fs buffer */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001323 if (!aligned) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 for (j = col; j < end && read < len; j++)
1325 buf[read++] = data_poi[j];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001326 this->pagebuf = realpage;
1327 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 read += mtd->oobblock;
1329
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001330 /* Apply delay or wait for ready/busy pin
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * Do this before the AUTOINCR check, so no problems
1332 * arise if a chip which does auto increment
1333 * is marked as NOAUTOINCR by the board driver.
1334 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001335 if (!this->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 udelay (this->chip_delay);
1337 else
Thomas Gleixner3b887752005-02-22 21:56:49 +00001338 nand_wait_ready(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (read == len)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /* For subsequent reads align to page boundary. */
1344 col = 0;
1345 /* Increment page address */
1346 realpage++;
1347
1348 page = realpage & this->pagemask;
1349 /* Check, if we cross a chip boundary */
1350 if (!page) {
1351 chipnr++;
1352 this->select_chip(mtd, -1);
1353 this->select_chip(mtd, chipnr);
1354 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001355 /* Check, if the chip supports auto page increment
1356 * or if we have hit a block boundary.
1357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001359 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361
1362 /* Deselect and wake up anyone waiting on the device */
David A. Marlin068e3c02005-01-24 03:07:46 +00001363 if (flags & NAND_GET_DEVICE)
1364 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /*
1367 * Return success, if no ECC failures, else -EBADMSG
1368 * fs driver will take care of that, because
1369 * retlen == desired len and result == -EBADMSG
1370 */
1371 *retlen = read;
1372 return ecc_failed ? -EBADMSG : 0;
1373}
1374
1375/**
1376 * nand_read_oob - [MTD Interface] NAND read out-of-band
1377 * @mtd: MTD device structure
1378 * @from: offset to read from
1379 * @len: number of bytes to read
1380 * @retlen: pointer to variable to store the number of read bytes
1381 * @buf: the databuffer to put data
1382 *
1383 * NAND read out-of-band data from the spare area
1384 */
1385static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1386{
1387 int i, col, page, chipnr;
1388 struct nand_chip *this = mtd->priv;
1389 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1390
1391 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1392
1393 /* Shift to get page */
1394 page = (int)(from >> this->page_shift);
1395 chipnr = (int)(from >> this->chip_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* Mask to get column */
1398 col = from & (mtd->oobsize - 1);
1399
1400 /* Initialize return length value */
1401 *retlen = 0;
1402
1403 /* Do not allow reads past end of device */
1404 if ((from + len) > mtd->size) {
1405 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1406 *retlen = 0;
1407 return -EINVAL;
1408 }
1409
1410 /* Grab the lock and see if the device is available */
1411 nand_get_device (this, mtd , FL_READING);
1412
1413 /* Select the NAND device */
1414 this->select_chip(mtd, chipnr);
1415
1416 /* Send the read command */
1417 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001418 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * Read the data, if we read more than one page
1420 * oob data, let the device transfer the data !
1421 */
1422 i = 0;
1423 while (i < len) {
1424 int thislen = mtd->oobsize - col;
1425 thislen = min_t(int, thislen, len);
1426 this->read_buf(mtd, &buf[i], thislen);
1427 i += thislen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 /* Read more ? */
1430 if (i < len) {
1431 page++;
1432 col = 0;
1433
1434 /* Check, if we cross a chip boundary */
1435 if (!(page & this->pagemask)) {
1436 chipnr++;
1437 this->select_chip(mtd, -1);
1438 this->select_chip(mtd, chipnr);
1439 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001440
1441 /* Apply delay or wait for ready/busy pin
Thomas Gleixner19870da2005-07-15 14:53:51 +01001442 * Do this before the AUTOINCR check, so no problems
1443 * arise if a chip which does auto increment
1444 * is marked as NOAUTOINCR by the board driver.
1445 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001446 if (!this->dev_ready)
Thomas Gleixner19870da2005-07-15 14:53:51 +01001447 udelay (this->chip_delay);
1448 else
1449 nand_wait_ready(mtd);
1450
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001451 /* Check, if the chip supports auto page increment
1452 * or if we have hit a block boundary.
1453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1455 /* For subsequent page reads set offset to 0 */
1456 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1457 }
1458 }
1459 }
1460
1461 /* Deselect and wake up anyone waiting on the device */
1462 nand_release_device(mtd);
1463
1464 /* Return happy */
1465 *retlen = len;
1466 return 0;
1467}
1468
1469/**
1470 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1471 * @mtd: MTD device structure
1472 * @buf: temporary buffer
1473 * @from: offset to read from
1474 * @len: number of bytes to read
1475 * @ooblen: number of oob data bytes to read
1476 *
1477 * Read raw data including oob into buffer
1478 */
1479int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1480{
1481 struct nand_chip *this = mtd->priv;
1482 int page = (int) (from >> this->page_shift);
1483 int chip = (int) (from >> this->chip_shift);
1484 int sndcmd = 1;
1485 int cnt = 0;
1486 int pagesize = mtd->oobblock + mtd->oobsize;
1487 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1488
1489 /* Do not allow reads past end of device */
1490 if ((from + len) > mtd->size) {
1491 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1492 return -EINVAL;
1493 }
1494
1495 /* Grab the lock and see if the device is available */
1496 nand_get_device (this, mtd , FL_READING);
1497
1498 this->select_chip (mtd, chip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* Add requested oob length */
1501 len += ooblen;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 while (len) {
1504 if (sndcmd)
1505 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001506 sndcmd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 this->read_buf (mtd, &buf[cnt], pagesize);
1509
1510 len -= pagesize;
1511 cnt += pagesize;
1512 page++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001513
1514 if (!this->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 udelay (this->chip_delay);
1516 else
Thomas Gleixner3b887752005-02-22 21:56:49 +00001517 nand_wait_ready(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001518
1519 /* Check, if the chip supports auto page increment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1521 sndcmd = 1;
1522 }
1523
1524 /* Deselect and wake up anyone waiting on the device */
1525 nand_release_device(mtd);
1526 return 0;
1527}
1528
1529
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001530/**
1531 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 * @mtd: MTD device structure
1533 * @fsbuf: buffer given by fs driver
1534 * @oobsel: out of band selection structre
1535 * @autoplace: 1 = place given buffer into the oob bytes
1536 * @numpages: number of pages to prepare
1537 *
1538 * Return:
1539 * 1. Filesystem buffer available and autoplacement is off,
1540 * return filesystem buffer
1541 * 2. No filesystem buffer or autoplace is off, return internal
1542 * buffer
1543 * 3. Filesystem buffer is given and autoplace selected
1544 * put data from fs buffer into internal buffer and
1545 * retrun internal buffer
1546 *
1547 * Note: The internal buffer is filled with 0xff. This must
1548 * be done only once, when no autoplacement happens
1549 * Autoplacement sets the buffer dirty flag, which
1550 * forces the 0xff fill before using the buffer again.
1551 *
1552*/
1553static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1554 int autoplace, int numpages)
1555{
1556 struct nand_chip *this = mtd->priv;
1557 int i, len, ofs;
1558
1559 /* Zero copy fs supplied buffer */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001560 if (fsbuf && !autoplace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return fsbuf;
1562
1563 /* Check, if the buffer must be filled with ff again */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001564 if (this->oobdirty) {
1565 memset (this->oob_buf, 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1567 this->oobdirty = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001568 }
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* If we have no autoplacement or no fs buffer use the internal one */
1571 if (!autoplace || !fsbuf)
1572 return this->oob_buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /* Walk through the pages and place the data */
1575 this->oobdirty = 1;
1576 ofs = 0;
1577 while (numpages--) {
1578 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1579 int to = ofs + oobsel->oobfree[i][0];
1580 int num = oobsel->oobfree[i][1];
1581 memcpy (&this->oob_buf[to], fsbuf, num);
1582 len += num;
1583 fsbuf += num;
1584 }
1585 ofs += mtd->oobavail;
1586 }
1587 return this->oob_buf;
1588}
1589
1590#define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1591
1592/**
1593 * nand_write - [MTD Interface] compability function for nand_write_ecc
1594 * @mtd: MTD device structure
1595 * @to: offset to write to
1596 * @len: number of bytes to write
1597 * @retlen: pointer to variable to store the number of written bytes
1598 * @buf: the data to write
1599 *
1600 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1601 *
1602*/
1603static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1604{
1605 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1606}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/**
1609 * nand_write_ecc - [MTD Interface] NAND write with ECC
1610 * @mtd: MTD device structure
1611 * @to: offset to write to
1612 * @len: number of bytes to write
1613 * @retlen: pointer to variable to store the number of written bytes
1614 * @buf: the data to write
1615 * @eccbuf: filesystem supplied oob data buffer
1616 * @oobsel: oob selection structure
1617 *
1618 * NAND write with ECC
1619 */
1620static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1621 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1622{
1623 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1624 int autoplace = 0, numpages, totalpages;
1625 struct nand_chip *this = mtd->priv;
1626 u_char *oobbuf, *bufstart;
1627 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1628
1629 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1630
1631 /* Initialize retlen, in case of early exit */
1632 *retlen = 0;
1633
1634 /* Do not allow write past end of device */
1635 if ((to + len) > mtd->size) {
1636 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1637 return -EINVAL;
1638 }
1639
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001640 /* reject writes, which are not page aligned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1642 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1643 return -EINVAL;
1644 }
1645
1646 /* Grab the lock and see if the device is available */
1647 nand_get_device (this, mtd, FL_WRITING);
1648
1649 /* Calculate chipnr */
1650 chipnr = (int)(to >> this->chip_shift);
1651 /* Select the NAND device */
1652 this->select_chip(mtd, chipnr);
1653
1654 /* Check, if it is write protected */
1655 if (nand_check_wp(mtd))
1656 goto out;
1657
1658 /* if oobsel is NULL, use chip defaults */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001659 if (oobsel == NULL)
1660 oobsel = &mtd->oobinfo;
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /* Autoplace of oob data ? Use the default placement scheme */
1663 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1664 oobsel = this->autooob;
1665 autoplace = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001666 }
Thomas Gleixner90e260c2005-05-19 17:10:26 +01001667 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1668 autoplace = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 /* Setup variables and oob buffer */
1671 totalpages = len >> this->page_shift;
1672 page = (int) (to >> this->page_shift);
1673 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001674 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 this->pagebuf = -1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* Set it relative to chip */
1678 page &= this->pagemask;
1679 startpage = page;
1680 /* Calc number of pages we can write in one go */
1681 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1682 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1683 bufstart = (u_char *)buf;
1684
1685 /* Loop until all data is written */
1686 while (written < len) {
1687
1688 this->data_poi = (u_char*) &buf[written];
1689 /* Write one page. If this is the last page to write
1690 * or the last page in this block, then use the
1691 * real pageprogram command, else select cached programming
1692 * if supported by the chip.
1693 */
1694 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1695 if (ret) {
1696 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1697 goto out;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* Next oob page */
1700 oob += mtd->oobsize;
1701 /* Update written bytes count */
1702 written += mtd->oobblock;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001703 if (written == len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 goto cmp;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* Increment page address */
1707 page++;
1708
1709 /* Have we hit a block boundary ? Then we have to verify and
1710 * if verify is ok, we have to setup the oob buffer for
1711 * the next pages.
1712 */
1713 if (!(page & (ppblock - 1))){
1714 int ofs;
1715 this->data_poi = bufstart;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001716 ret = nand_verify_pages (mtd, this, startpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 page - startpage,
1718 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1719 if (ret) {
1720 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1721 goto out;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *retlen = written;
1724
1725 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1726 if (eccbuf)
1727 eccbuf += (page - startpage) * ofs;
1728 totalpages -= page - startpage;
1729 numpages = min (totalpages, ppblock);
1730 page &= this->pagemask;
1731 startpage = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001732 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 autoplace, numpages);
Todd Poynor868801e2005-11-05 03:21:15 +00001734 oob = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /* Check, if we cross a chip boundary */
1736 if (!page) {
1737 chipnr++;
1738 this->select_chip(mtd, -1);
1739 this->select_chip(mtd, chipnr);
1740 }
1741 }
1742 }
1743 /* Verify the remaining pages */
1744cmp:
1745 this->data_poi = bufstart;
1746 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1747 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1748 if (!ret)
1749 *retlen = written;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001750 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1752
1753out:
1754 /* Deselect and wake up anyone waiting on the device */
1755 nand_release_device(mtd);
1756
1757 return ret;
1758}
1759
1760
1761/**
1762 * nand_write_oob - [MTD Interface] NAND write out-of-band
1763 * @mtd: MTD device structure
1764 * @to: offset to write to
1765 * @len: number of bytes to write
1766 * @retlen: pointer to variable to store the number of written bytes
1767 * @buf: the data to write
1768 *
1769 * NAND write out-of-band
1770 */
1771static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1772{
1773 int column, page, status, ret = -EIO, chipnr;
1774 struct nand_chip *this = mtd->priv;
1775
1776 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1777
1778 /* Shift to get page */
1779 page = (int) (to >> this->page_shift);
1780 chipnr = (int) (to >> this->chip_shift);
1781
1782 /* Mask to get column */
1783 column = to & (mtd->oobsize - 1);
1784
1785 /* Initialize return length value */
1786 *retlen = 0;
1787
1788 /* Do not allow write past end of page */
1789 if ((column + len) > mtd->oobsize) {
1790 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1791 return -EINVAL;
1792 }
1793
1794 /* Grab the lock and see if the device is available */
1795 nand_get_device (this, mtd, FL_WRITING);
1796
1797 /* Select the NAND device */
1798 this->select_chip(mtd, chipnr);
1799
1800 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1801 in one of my DiskOnChip 2000 test units) will clear the whole
1802 data page too if we don't do this. I have no clue why, but
1803 I seem to have 'fixed' it in the doc2000 driver in
1804 August 1999. dwmw2. */
1805 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1806
1807 /* Check, if it is write protected */
1808 if (nand_check_wp(mtd))
1809 goto out;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* Invalidate the page cache, if we write to the cached page */
1812 if (page == this->pagebuf)
1813 this->pagebuf = -1;
1814
1815 if (NAND_MUST_PAD(this)) {
1816 /* Write out desired data */
1817 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1818 /* prepad 0xff for partial programming */
1819 this->write_buf(mtd, ffchars, column);
1820 /* write data */
1821 this->write_buf(mtd, buf, len);
1822 /* postpad 0xff for partial programming */
1823 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1824 } else {
1825 /* Write out desired data */
1826 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1827 /* write data */
1828 this->write_buf(mtd, buf, len);
1829 }
1830 /* Send command to program the OOB data */
1831 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1832
1833 status = this->waitfunc (mtd, this, FL_WRITING);
1834
1835 /* See if device thinks it succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00001836 if (status & NAND_STATUS_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1838 ret = -EIO;
1839 goto out;
1840 }
1841 /* Return happy */
1842 *retlen = len;
1843
1844#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1845 /* Send command to read back the data */
1846 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1847
1848 if (this->verify_buf(mtd, buf, len)) {
1849 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1850 ret = -EIO;
1851 goto out;
1852 }
1853#endif
1854 ret = 0;
1855out:
1856 /* Deselect and wake up anyone waiting on the device */
1857 nand_release_device(mtd);
1858
1859 return ret;
1860}
1861
1862
1863/**
1864 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1865 * @mtd: MTD device structure
1866 * @vecs: the iovectors to write
1867 * @count: number of vectors
1868 * @to: offset to write to
1869 * @retlen: pointer to variable to store the number of written bytes
1870 *
1871 * NAND write with kvec. This just calls the ecc function
1872 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001873static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 loff_t to, size_t * retlen)
1875{
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001876 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879/**
1880 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1881 * @mtd: MTD device structure
1882 * @vecs: the iovectors to write
1883 * @count: number of vectors
1884 * @to: offset to write to
1885 * @retlen: pointer to variable to store the number of written bytes
1886 * @eccbuf: filesystem supplied oob data buffer
1887 * @oobsel: oob selection structure
1888 *
1889 * NAND write with iovec with ecc
1890 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001891static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1893{
1894 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1895 int oob, numpages, autoplace = 0, startpage;
1896 struct nand_chip *this = mtd->priv;
1897 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1898 u_char *oobbuf, *bufstart;
1899
1900 /* Preset written len for early exit */
1901 *retlen = 0;
1902
1903 /* Calculate total length of data */
1904 total_len = 0;
1905 for (i = 0; i < count; i++)
1906 total_len += (int) vecs[i].iov_len;
1907
1908 DEBUG (MTD_DEBUG_LEVEL3,
1909 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1910
1911 /* Do not allow write past end of page */
1912 if ((to + total_len) > mtd->size) {
1913 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1914 return -EINVAL;
1915 }
1916
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001917 /* reject writes, which are not page aligned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1919 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1920 return -EINVAL;
1921 }
1922
1923 /* Grab the lock and see if the device is available */
1924 nand_get_device (this, mtd, FL_WRITING);
1925
1926 /* Get the current chip-nr */
1927 chipnr = (int) (to >> this->chip_shift);
1928 /* Select the NAND device */
1929 this->select_chip(mtd, chipnr);
1930
1931 /* Check, if it is write protected */
1932 if (nand_check_wp(mtd))
1933 goto out;
1934
1935 /* if oobsel is NULL, use chip defaults */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001936 if (oobsel == NULL)
1937 oobsel = &mtd->oobinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* Autoplace of oob data ? Use the default placement scheme */
1940 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1941 oobsel = this->autooob;
1942 autoplace = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001943 }
Thomas Gleixner90e260c2005-05-19 17:10:26 +01001944 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1945 autoplace = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /* Setup start page */
1948 page = (int) (to >> this->page_shift);
1949 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001950 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 this->pagebuf = -1;
1952
1953 startpage = page & this->pagemask;
1954
1955 /* Loop until all kvec' data has been written */
1956 len = 0;
1957 while (count) {
1958 /* If the given tuple is >= pagesize then
1959 * write it out from the iov
1960 */
1961 if ((vecs->iov_len - len) >= mtd->oobblock) {
1962 /* Calc number of pages we can write
1963 * out of this iov in one go */
1964 numpages = (vecs->iov_len - len) >> this->page_shift;
1965 /* Do not cross block boundaries */
1966 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1967 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1968 bufstart = (u_char *)vecs->iov_base;
1969 bufstart += len;
1970 this->data_poi = bufstart;
1971 oob = 0;
1972 for (i = 1; i <= numpages; i++) {
1973 /* Write one page. If this is the last page to write
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001974 * then use the real pageprogram command, else select
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 * cached programming if supported by the chip.
1976 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001977 ret = nand_write_page (mtd, this, page & this->pagemask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 &oobbuf[oob], oobsel, i != numpages);
1979 if (ret)
1980 goto out;
1981 this->data_poi += mtd->oobblock;
1982 len += mtd->oobblock;
1983 oob += mtd->oobsize;
1984 page++;
1985 }
1986 /* Check, if we have to switch to the next tuple */
1987 if (len >= (int) vecs->iov_len) {
1988 vecs++;
1989 len = 0;
1990 count--;
1991 }
1992 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001993 /* We must use the internal buffer, read data out of each
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 * tuple until we have a full page to write
1995 */
1996 int cnt = 0;
1997 while (cnt < mtd->oobblock) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001998 if (vecs->iov_base != NULL && vecs->iov_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
2000 /* Check, if we have to switch to the next tuple */
2001 if (len >= (int) vecs->iov_len) {
2002 vecs++;
2003 len = 0;
2004 count--;
2005 }
2006 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002007 this->pagebuf = page;
2008 this->data_poi = this->data_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 bufstart = this->data_poi;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002010 numpages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2012 ret = nand_write_page (mtd, this, page & this->pagemask,
2013 oobbuf, oobsel, 0);
2014 if (ret)
2015 goto out;
2016 page++;
2017 }
2018
2019 this->data_poi = bufstart;
2020 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2021 if (ret)
2022 goto out;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 written += mtd->oobblock * numpages;
2025 /* All done ? */
2026 if (!count)
2027 break;
2028
2029 startpage = page & this->pagemask;
2030 /* Check, if we cross a chip boundary */
2031 if (!startpage) {
2032 chipnr++;
2033 this->select_chip(mtd, -1);
2034 this->select_chip(mtd, chipnr);
2035 }
2036 }
2037 ret = 0;
2038out:
2039 /* Deselect and wake up anyone waiting on the device */
2040 nand_release_device(mtd);
2041
2042 *retlen = written;
2043 return ret;
2044}
2045
2046/**
2047 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2048 * @mtd: MTD device structure
2049 * @page: the page address of the block which will be erased
2050 *
2051 * Standard erase command for NAND chips
2052 */
2053static void single_erase_cmd (struct mtd_info *mtd, int page)
2054{
2055 struct nand_chip *this = mtd->priv;
2056 /* Send commands to erase a block */
2057 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2058 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2059}
2060
2061/**
2062 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2063 * @mtd: MTD device structure
2064 * @page: the page address of the block which will be erased
2065 *
2066 * AND multi block erase command function
2067 * Erase 4 consecutive blocks
2068 */
2069static void multi_erase_cmd (struct mtd_info *mtd, int page)
2070{
2071 struct nand_chip *this = mtd->priv;
2072 /* Send commands to erase a block */
2073 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2074 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2075 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2076 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2077 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2078}
2079
2080/**
2081 * nand_erase - [MTD Interface] erase block(s)
2082 * @mtd: MTD device structure
2083 * @instr: erase instruction
2084 *
2085 * Erase one ore more blocks
2086 */
2087static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2088{
2089 return nand_erase_nand (mtd, instr, 0);
2090}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002091
David A. Marlin30f464b2005-01-17 18:35:25 +00002092#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/**
2094 * nand_erase_intern - [NAND Interface] erase block(s)
2095 * @mtd: MTD device structure
2096 * @instr: erase instruction
2097 * @allowbbt: allow erasing the bbt area
2098 *
2099 * Erase one ore more blocks
2100 */
2101int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2102{
2103 int page, len, status, pages_per_block, ret, chipnr;
2104 struct nand_chip *this = mtd->priv;
David A. Marlin30f464b2005-01-17 18:35:25 +00002105 int rewrite_bbt[NAND_MAX_CHIPS]={0}; /* flags to indicate the page, if bbt needs to be rewritten. */
2106 unsigned int bbt_masked_page; /* bbt mask to compare to page being erased. */
2107 /* It is used to see if the current page is in the same */
2108 /* 256 block group and the same bank as the bbt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 DEBUG (MTD_DEBUG_LEVEL3,
2111 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2112
2113 /* Start address must align on block boundary */
2114 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2115 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2116 return -EINVAL;
2117 }
2118
2119 /* Length must align on block boundary */
2120 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2121 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2122 return -EINVAL;
2123 }
2124
2125 /* Do not allow erase past end of device */
2126 if ((instr->len + instr->addr) > mtd->size) {
2127 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2128 return -EINVAL;
2129 }
2130
2131 instr->fail_addr = 0xffffffff;
2132
2133 /* Grab the lock and see if the device is available */
2134 nand_get_device (this, mtd, FL_ERASING);
2135
2136 /* Shift to get first page */
2137 page = (int) (instr->addr >> this->page_shift);
2138 chipnr = (int) (instr->addr >> this->chip_shift);
2139
2140 /* Calculate pages in each block */
2141 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2142
2143 /* Select the NAND device */
2144 this->select_chip(mtd, chipnr);
2145
2146 /* Check the WP bit */
2147 /* Check, if it is write protected */
2148 if (nand_check_wp(mtd)) {
2149 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2150 instr->state = MTD_ERASE_FAILED;
2151 goto erase_exit;
2152 }
2153
David A. Marlin30f464b2005-01-17 18:35:25 +00002154 /* if BBT requires refresh, set the BBT page mask to see if the BBT should be rewritten */
2155 if (this->options & BBT_AUTO_REFRESH) {
2156 bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2157 } else {
2158 bbt_masked_page = 0xffffffff; /* should not match anything */
2159 }
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /* Loop through the pages */
2162 len = instr->len;
2163
2164 instr->state = MTD_ERASING;
2165
2166 while (len) {
2167 /* Check if we have a bad block, we do not erase bad blocks ! */
2168 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2169 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2170 instr->state = MTD_ERASE_FAILED;
2171 goto erase_exit;
2172 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002173
2174 /* Invalidate the page cache, if we erase the block which contains
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 the current cached page */
2176 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2177 this->pagebuf = -1;
2178
2179 this->erase_cmd (mtd, page & this->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 status = this->waitfunc (mtd, this, FL_ERASING);
2182
David A. Marlin068e3c02005-01-24 03:07:46 +00002183 /* See if operation failed and additional status checks are available */
2184 if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
2185 status = this->errstat(mtd, this, FL_ERASING, status, page);
2186 }
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002189 if (status & NAND_STATUS_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2191 instr->state = MTD_ERASE_FAILED;
2192 instr->fail_addr = (page << this->page_shift);
2193 goto erase_exit;
2194 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002195
2196 /* if BBT requires refresh, set the BBT rewrite flag to the page being erased */
2197 if (this->options & BBT_AUTO_REFRESH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002198 if (((page & BBT_PAGE_MASK) == bbt_masked_page) &&
David A. Marlin30f464b2005-01-17 18:35:25 +00002199 (page != this->bbt_td->pages[chipnr])) {
2200 rewrite_bbt[chipnr] = (page << this->page_shift);
2201 }
2202 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* Increment page address and decrement length */
2205 len -= (1 << this->phys_erase_shift);
2206 page += pages_per_block;
2207
2208 /* Check, if we cross a chip boundary */
2209 if (len && !(page & this->pagemask)) {
2210 chipnr++;
2211 this->select_chip(mtd, -1);
2212 this->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002213
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002214 /* if BBT requires refresh and BBT-PERCHIP,
David A. Marlin30f464b2005-01-17 18:35:25 +00002215 * set the BBT page mask to see if this BBT should be rewritten */
2216 if ((this->options & BBT_AUTO_REFRESH) && (this->bbt_td->options & NAND_BBT_PERCHIP)) {
2217 bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2218 }
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221 }
2222 instr->state = MTD_ERASE_DONE;
2223
2224erase_exit:
2225
2226 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2227 /* Do call back function */
2228 if (!ret)
2229 mtd_erase_callback(instr);
2230
2231 /* Deselect and wake up anyone waiting on the device */
2232 nand_release_device(mtd);
2233
David A. Marlin30f464b2005-01-17 18:35:25 +00002234 /* if BBT requires refresh and erase was successful, rewrite any selected bad block tables */
2235 if ((this->options & BBT_AUTO_REFRESH) && (!ret)) {
2236 for (chipnr = 0; chipnr < this->numchips; chipnr++) {
2237 if (rewrite_bbt[chipnr]) {
2238 /* update the BBT for chip */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002239 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt (%d:0x%0x 0x%0x)\n",
David A. Marlin30f464b2005-01-17 18:35:25 +00002240 chipnr, rewrite_bbt[chipnr], this->bbt_td->pages[chipnr]);
2241 nand_update_bbt (mtd, rewrite_bbt[chipnr]);
2242 }
2243 }
2244 }
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /* Return more or less happy */
2247 return ret;
2248}
2249
2250/**
2251 * nand_sync - [MTD Interface] sync
2252 * @mtd: MTD device structure
2253 *
2254 * Sync is actually a wait for chip ready function
2255 */
2256static void nand_sync (struct mtd_info *mtd)
2257{
2258 struct nand_chip *this = mtd->priv;
2259
2260 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2261
2262 /* Grab the lock and see if the device is available */
2263 nand_get_device (this, mtd, FL_SYNCING);
2264 /* Release it and go back */
2265 nand_release_device (mtd);
2266}
2267
2268
2269/**
2270 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2271 * @mtd: MTD device structure
2272 * @ofs: offset relative to mtd start
2273 */
2274static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2275{
2276 /* Check for invalid offset */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002277 if (ofs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return nand_block_checkbad (mtd, ofs, 1, 0);
2281}
2282
2283/**
2284 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2285 * @mtd: MTD device structure
2286 * @ofs: offset relative to mtd start
2287 */
2288static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2289{
2290 struct nand_chip *this = mtd->priv;
2291 int ret;
2292
2293 if ((ret = nand_block_isbad(mtd, ofs))) {
2294 /* If it was bad already, return success and do nothing. */
2295 if (ret > 0)
2296 return 0;
2297 return ret;
2298 }
2299
2300 return this->block_markbad(mtd, ofs);
2301}
2302
2303/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002304 * nand_suspend - [MTD Interface] Suspend the NAND flash
2305 * @mtd: MTD device structure
2306 */
2307static int nand_suspend(struct mtd_info *mtd)
2308{
2309 struct nand_chip *this = mtd->priv;
2310
2311 return nand_get_device (this, mtd, FL_PM_SUSPENDED);
2312}
2313
2314/**
2315 * nand_resume - [MTD Interface] Resume the NAND flash
2316 * @mtd: MTD device structure
2317 */
2318static void nand_resume(struct mtd_info *mtd)
2319{
2320 struct nand_chip *this = mtd->priv;
2321
2322 if (this->state == FL_PM_SUSPENDED)
2323 nand_release_device(mtd);
2324 else
2325 printk(KERN_ERR "resume() called for the chip which is not "
2326 "in suspended state\n");
2327
2328}
2329
2330
2331/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * nand_scan - [NAND Interface] Scan for the NAND device
2333 * @mtd: MTD device structure
2334 * @maxchips: Number of chips to scan for
2335 *
2336 * This fills out all the not initialized function pointers
2337 * with the defaults.
2338 * The flash ID is read and the mtd/chip structures are
2339 * filled with the appropriate values. Buffers are allocated if
2340 * they are not provided by the board driver
2341 *
2342 */
2343int nand_scan (struct mtd_info *mtd, int maxchips)
2344{
Ben Dooks3b946e32005-03-14 18:30:48 +00002345 int i, nand_maf_id, nand_dev_id, busw, maf_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 struct nand_chip *this = mtd->priv;
2347
2348 /* Get buswidth to select the correct functions*/
2349 busw = this->options & NAND_BUSWIDTH_16;
2350
2351 /* check for proper chip_delay setup, set 20us if not */
2352 if (!this->chip_delay)
2353 this->chip_delay = 20;
2354
2355 /* check, if a user supplied command function given */
2356 if (this->cmdfunc == NULL)
2357 this->cmdfunc = nand_command;
2358
2359 /* check, if a user supplied wait function given */
2360 if (this->waitfunc == NULL)
2361 this->waitfunc = nand_wait;
2362
2363 if (!this->select_chip)
2364 this->select_chip = nand_select_chip;
2365 if (!this->write_byte)
2366 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2367 if (!this->read_byte)
2368 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2369 if (!this->write_word)
2370 this->write_word = nand_write_word;
2371 if (!this->read_word)
2372 this->read_word = nand_read_word;
2373 if (!this->block_bad)
2374 this->block_bad = nand_block_bad;
2375 if (!this->block_markbad)
2376 this->block_markbad = nand_default_block_markbad;
2377 if (!this->write_buf)
2378 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2379 if (!this->read_buf)
2380 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2381 if (!this->verify_buf)
2382 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2383 if (!this->scan_bbt)
2384 this->scan_bbt = nand_default_bbt;
2385
2386 /* Select the device */
2387 this->select_chip(mtd, 0);
2388
2389 /* Send the command for reading device ID */
2390 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2391
2392 /* Read manufacturer and device IDs */
2393 nand_maf_id = this->read_byte(mtd);
2394 nand_dev_id = this->read_byte(mtd);
2395
2396 /* Print and store flash device information */
2397 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002398
2399 if (nand_dev_id != nand_flash_ids[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 continue;
2401
2402 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2403 this->chipsize = nand_flash_ids[i].chipsize << 20;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /* New devices have all the information in additional id bytes */
2406 if (!nand_flash_ids[i].pagesize) {
2407 int extid;
2408 /* The 3rd id byte contains non relevant data ATM */
2409 extid = this->read_byte(mtd);
2410 /* The 4th id byte is the important one */
2411 extid = this->read_byte(mtd);
2412 /* Calc pagesize */
2413 mtd->oobblock = 1024 << (extid & 0x3);
2414 extid >>= 2;
2415 /* Calc oobsize */
Thomas Gleixnerd4094662005-08-11 18:13:46 +01002416 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->oobblock >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 extid >>= 2;
2418 /* Calc blocksize. Blocksize is multiples of 64KiB */
2419 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2420 extid >>= 2;
2421 /* Get buswidth information */
2422 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 } else {
2425 /* Old devices have this data hardcoded in the
2426 * device id table */
2427 mtd->erasesize = nand_flash_ids[i].erasesize;
2428 mtd->oobblock = nand_flash_ids[i].pagesize;
2429 mtd->oobsize = mtd->oobblock / 32;
2430 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2431 }
2432
Kyungmin Park0ea4a752005-02-16 09:39:39 +00002433 /* Try to identify manufacturer */
2434 for (maf_id = 0; nand_manuf_ids[maf_id].id != 0x0; maf_id++) {
2435 if (nand_manuf_ids[maf_id].id == nand_maf_id)
2436 break;
2437 }
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /* Check, if buswidth is correct. Hardware drivers should set
2440 * this correct ! */
2441 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2442 printk (KERN_INFO "NAND device: Manufacturer ID:"
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002443 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
Kyungmin Park0ea4a752005-02-16 09:39:39 +00002444 nand_manuf_ids[maf_id].name , mtd->name);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002445 printk (KERN_WARNING
2446 "NAND bus width %d instead %d bit\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2448 busw ? 16 : 8);
2449 this->select_chip(mtd, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002450 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002452
2453 /* Calculate the address shift from the page size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 this->page_shift = ffs(mtd->oobblock) - 1;
2455 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2456 this->chip_shift = ffs(this->chipsize) - 1;
2457
2458 /* Set the bad block position */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002459 this->badblockpos = mtd->oobblock > 512 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2461
2462 /* Get chip options, preserve non chip based options */
2463 this->options &= ~NAND_CHIPOPTIONS_MSK;
2464 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2465 /* Set this as a default. Board drivers can override it, if neccecary */
2466 this->options |= NAND_NO_AUTOINCR;
2467 /* Check if this is a not a samsung device. Do not clear the options
2468 * for chips which are not having an extended id.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2471 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /* Check for AND chips with 4 page planes */
2474 if (this->options & NAND_4PAGE_ARRAY)
2475 this->erase_cmd = multi_erase_cmd;
2476 else
2477 this->erase_cmd = single_erase_cmd;
2478
2479 /* Do not replace user supplied command function ! */
2480 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2481 this->cmdfunc = nand_command_lp;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 printk (KERN_INFO "NAND device: Manufacturer ID:"
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002484 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
Kyungmin Park0ea4a752005-02-16 09:39:39 +00002485 nand_manuf_ids[maf_id].name , nand_flash_ids[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
2487 }
2488
2489 if (!nand_flash_ids[i].name) {
2490 printk (KERN_WARNING "No NAND device found!!!\n");
2491 this->select_chip(mtd, -1);
2492 return 1;
2493 }
2494
2495 for (i=1; i < maxchips; i++) {
2496 this->select_chip(mtd, i);
2497
2498 /* Send the command for reading device ID */
2499 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2500
2501 /* Read manufacturer and device IDs */
2502 if (nand_maf_id != this->read_byte(mtd) ||
2503 nand_dev_id != this->read_byte(mtd))
2504 break;
2505 }
2506 if (i > 1)
2507 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 /* Allocate buffers, if neccecary */
2510 if (!this->oob_buf) {
2511 size_t len;
2512 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2513 this->oob_buf = kmalloc (len, GFP_KERNEL);
2514 if (!this->oob_buf) {
2515 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2516 return -ENOMEM;
2517 }
2518 this->options |= NAND_OOBBUF_ALLOC;
2519 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (!this->data_buf) {
2522 size_t len;
2523 len = mtd->oobblock + mtd->oobsize;
2524 this->data_buf = kmalloc (len, GFP_KERNEL);
2525 if (!this->data_buf) {
2526 if (this->options & NAND_OOBBUF_ALLOC)
2527 kfree (this->oob_buf);
2528 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2529 return -ENOMEM;
2530 }
2531 this->options |= NAND_DATABUF_ALLOC;
2532 }
2533
2534 /* Store the number of chips and calc total size for mtd */
2535 this->numchips = i;
2536 mtd->size = i * this->chipsize;
2537 /* Convert chipsize to number of pages per chip -1. */
2538 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2539 /* Preset the internal oob buffer */
2540 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2541
2542 /* If no default placement scheme is given, select an
2543 * appropriate one */
2544 if (!this->autooob) {
2545 /* Select the appropriate default oob placement scheme for
2546 * placement agnostic filesystems */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002547 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 case 8:
2549 this->autooob = &nand_oob_8;
2550 break;
2551 case 16:
2552 this->autooob = &nand_oob_16;
2553 break;
2554 case 64:
2555 this->autooob = &nand_oob_64;
2556 break;
2557 default:
2558 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2559 mtd->oobsize);
2560 BUG();
2561 }
2562 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 /* The number of bytes available for the filesystem to place fs dependend
2565 * oob data */
Thomas Gleixner998cf642005-04-01 08:21:48 +01002566 mtd->oobavail = 0;
2567 for (i = 0; this->autooob->oobfree[i][1]; i++)
2568 mtd->oobavail += this->autooob->oobfree[i][1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002570 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 * check ECC mode, default to software
2572 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002573 * fallback to software ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002575 this->eccsize = 256; /* set default eccsize */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 this->eccbytes = 3;
2577
2578 switch (this->eccmode) {
2579 case NAND_ECC_HW12_2048:
2580 if (mtd->oobblock < 2048) {
2581 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2582 mtd->oobblock);
2583 this->eccmode = NAND_ECC_SOFT;
2584 this->calculate_ecc = nand_calculate_ecc;
2585 this->correct_data = nand_correct_data;
2586 } else
2587 this->eccsize = 2048;
2588 break;
2589
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002590 case NAND_ECC_HW3_512:
2591 case NAND_ECC_HW6_512:
2592 case NAND_ECC_HW8_512:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (mtd->oobblock == 256) {
2594 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2595 this->eccmode = NAND_ECC_SOFT;
2596 this->calculate_ecc = nand_calculate_ecc;
2597 this->correct_data = nand_correct_data;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002598 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 this->eccsize = 512; /* set eccsize to 512 */
2600 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 case NAND_ECC_HW3_256:
2603 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002604
2605 case NAND_ECC_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2607 this->eccmode = NAND_ECC_NONE;
2608 break;
2609
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002610 case NAND_ECC_SOFT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 this->calculate_ecc = nand_calculate_ecc;
2612 this->correct_data = nand_correct_data;
2613 break;
2614
2615 default:
2616 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002617 BUG();
2618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002620 /* Check hardware ecc function availability and adjust number of ecc bytes per
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 * calculation step
2622 */
2623 switch (this->eccmode) {
2624 case NAND_ECC_HW12_2048:
2625 this->eccbytes += 4;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002626 case NAND_ECC_HW8_512:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 this->eccbytes += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002628 case NAND_ECC_HW6_512:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 this->eccbytes += 3;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002630 case NAND_ECC_HW3_512:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 case NAND_ECC_HW3_256:
2632 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2633 break;
2634 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002635 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 mtd->eccsize = this->eccsize;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 /* Set the number of read / write steps for one page to ensure ECC generation */
2641 switch (this->eccmode) {
2642 case NAND_ECC_HW12_2048:
2643 this->eccsteps = mtd->oobblock / 2048;
2644 break;
2645 case NAND_ECC_HW3_512:
2646 case NAND_ECC_HW6_512:
2647 case NAND_ECC_HW8_512:
2648 this->eccsteps = mtd->oobblock / 512;
2649 break;
2650 case NAND_ECC_HW3_256:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002651 case NAND_ECC_SOFT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 this->eccsteps = mtd->oobblock / 256;
2653 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002654
2655 case NAND_ECC_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 this->eccsteps = 1;
2657 break;
2658 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 /* Initialize state, waitqueue and spinlock */
2661 this->state = FL_READY;
2662 init_waitqueue_head (&this->wq);
2663 spin_lock_init (&this->chip_lock);
2664
2665 /* De-select the device */
2666 this->select_chip(mtd, -1);
2667
2668 /* Invalidate the pagebuffer reference */
2669 this->pagebuf = -1;
2670
2671 /* Fill in remaining MTD driver data */
2672 mtd->type = MTD_NANDFLASH;
2673 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2674 mtd->ecctype = MTD_ECC_SW;
2675 mtd->erase = nand_erase;
2676 mtd->point = NULL;
2677 mtd->unpoint = NULL;
2678 mtd->read = nand_read;
2679 mtd->write = nand_write;
2680 mtd->read_ecc = nand_read_ecc;
2681 mtd->write_ecc = nand_write_ecc;
2682 mtd->read_oob = nand_read_oob;
2683 mtd->write_oob = nand_write_oob;
2684 mtd->readv = NULL;
2685 mtd->writev = nand_writev;
2686 mtd->writev_ecc = nand_writev_ecc;
2687 mtd->sync = nand_sync;
2688 mtd->lock = NULL;
2689 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002690 mtd->suspend = nand_suspend;
2691 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 mtd->block_isbad = nand_block_isbad;
2693 mtd->block_markbad = nand_block_markbad;
2694
2695 /* and make the autooob the default one */
2696 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2697
2698 mtd->owner = THIS_MODULE;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002699
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002700 /* Check, if we should skip the bad block table scan */
2701 if (this->options & NAND_SKIP_BBTSCAN)
2702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 /* Build bad block table */
2705 return this->scan_bbt (mtd);
2706}
2707
2708/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002709 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 * @mtd: MTD device structure
2711*/
2712void nand_release (struct mtd_info *mtd)
2713{
2714 struct nand_chip *this = mtd->priv;
2715
2716#ifdef CONFIG_MTD_PARTITIONS
2717 /* Deregister partitions */
2718 del_mtd_partitions (mtd);
2719#endif
2720 /* Deregister the device */
2721 del_mtd_device (mtd);
2722
Jesper Juhlfa671642005-11-07 01:01:27 -08002723 /* Free bad block table memory */
2724 kfree (this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* Buffer allocated by nand_scan ? */
2726 if (this->options & NAND_OOBBUF_ALLOC)
2727 kfree (this->oob_buf);
2728 /* Buffer allocated by nand_scan ? */
2729 if (this->options & NAND_DATABUF_ALLOC)
2730 kfree (this->data_buf);
2731}
2732
Thomas Gleixnerd7e78d42005-06-17 16:02:09 +01002733EXPORT_SYMBOL_GPL (nand_scan);
2734EXPORT_SYMBOL_GPL (nand_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Richard Purdie8fe833c2006-03-31 02:31:14 -08002736
2737static int __init nand_base_init(void)
2738{
2739 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2740 return 0;
2741}
2742
2743static void __exit nand_base_exit(void)
2744{
2745 led_trigger_unregister_simple(nand_led_trigger);
2746}
2747
2748module_init(nand_base_init);
2749module_exit(nand_base_exit);
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751MODULE_LICENSE ("GPL");
2752MODULE_AUTHOR ("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2753MODULE_DESCRIPTION ("Generic NAND flash driver code");