blob: 740d3fcc3737e637c731daa93ca4e93fd737b8d3 [file] [log] [blame]
Wolfgang Denk932394a2005-08-17 12:55:25 +02001/*
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.
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02008 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02009 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020011 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020013 * 2002 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk932394a2005-08-17 12:55:25 +020014 *
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020015 * 02-08-2004 tglx: support for strange chips, which cannot auto increment
Wolfgang Denk932394a2005-08-17 12:55:25 +020016 * 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
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020024 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020025 * 05-19-2004 tglx: Basic support for Renesas AG-AND chips
26 *
27 * 09-24-2004 tglx: add support for hardware controllers (e.g. ECC) shared
28 * among multiple independend devices. Suggestions and initial patch
29 * from Ben Dooks <ben-mtd@fluff.org>
30 *
31 * Credits:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020032 * David Woodhouse for adding multichip support
33 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020034 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
35 * rework for 2K page size chips
36 *
37 * TODO:
38 * Enable cached programming for 2k page size chips
39 * Check, if mtd->ecctype should be set to MTD_ECC_HW
40 * if we have HW ecc support.
41 * The AG-AND chips have nice features for speed improvement,
42 * which are not supported yet. Read / program 4 pages in one go.
43 *
44 * $Id: nand_base.c,v 1.126 2004/12/13 11:22:25 lavinen Exp $
45 *
46 * This program is free software; you can redistribute it and/or modify
47 * it under the terms of the GNU General Public License version 2 as
48 * published by the Free Software Foundation.
49 *
50 */
51
52/* XXX U-BOOT XXX */
53#if 0
54#include <linux/delay.h>
55#include <linux/errno.h>
56#include <linux/sched.h>
57#include <linux/slab.h>
58#include <linux/types.h>
59#include <linux/mtd/mtd.h>
60#include <linux/mtd/nand.h>
61#include <linux/mtd/nand_ecc.h>
62#include <linux/mtd/compatmac.h>
63#include <linux/interrupt.h>
64#include <linux/bitops.h>
65#include <asm/io.h>
66
67#ifdef CONFIG_MTD_PARTITIONS
68#include <linux/mtd/partitions.h>
69#endif
70
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020071#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020072
73#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010074
Jon Loeligercb51c0b2007-07-09 17:39:42 -050075#if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
Wolfgang Denk932394a2005-08-17 12:55:25 +020076
77#include <malloc.h>
78#include <watchdog.h>
79#include <linux/mtd/compat.h>
80#include <linux/mtd/mtd.h>
81#include <linux/mtd/nand.h>
82#include <linux/mtd/nand_ecc.h>
83
84#include <asm/io.h>
85#include <asm/errno.h>
86
87#ifdef CONFIG_JFFS2_NAND
88#include <jffs2/jffs2.h>
89#endif
90
Wolfgang Denk932394a2005-08-17 12:55:25 +020091/* Define default oob placement schemes for large and small page devices */
92static struct nand_oobinfo nand_oob_8 = {
93 .useecc = MTD_NANDECC_AUTOPLACE,
94 .eccbytes = 3,
95 .eccpos = {0, 1, 2},
96 .oobfree = { {3, 2}, {6, 2} }
97};
98
99static struct nand_oobinfo nand_oob_16 = {
100 .useecc = MTD_NANDECC_AUTOPLACE,
101 .eccbytes = 6,
102 .eccpos = {0, 1, 2, 3, 6, 7},
103 .oobfree = { {8, 8} }
104};
105
106static struct nand_oobinfo nand_oob_64 = {
107 .useecc = MTD_NANDECC_AUTOPLACE,
108 .eccbytes = 24,
109 .eccpos = {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200110 40, 41, 42, 43, 44, 45, 46, 47,
111 48, 49, 50, 51, 52, 53, 54, 55,
Wolfgang Denk932394a2005-08-17 12:55:25 +0200112 56, 57, 58, 59, 60, 61, 62, 63},
113 .oobfree = { {2, 38} }
114};
115
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200116static struct nand_oobinfo nand_oob_128 = {
117 .useecc = MTD_NANDECC_AUTOPLACE,
118 .eccbytes = 48,
119 .eccpos = {
120 80, 81, 82, 83, 84, 85, 86, 87,
121 88, 89, 90, 91, 92, 93, 94, 95,
122 96, 97, 98, 99, 100, 101, 102, 103,
123 104, 105, 106, 107, 108, 109, 110, 111,
124 112, 113, 114, 115, 116, 117, 118, 119,
125 120, 121, 122, 123, 124, 125, 126, 127},
126 .oobfree = { {2, 78} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200127};
128
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200129/* This is used for padding purposes in nand_write_oob */
130static u_char *ffchars;
131
Wolfgang Denk932394a2005-08-17 12:55:25 +0200132/*
133 * NAND low-level MTD interface functions
134 */
135static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
136static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
137static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
138
139static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
140static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
141 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
142static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
143static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
144static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
145 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
146static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
147/* XXX U-BOOT XXX */
148#if 0
149static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
150 unsigned long count, loff_t to, size_t * retlen);
151static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
152 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
153#endif
154static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
155static void nand_sync (struct mtd_info *mtd);
156
157/* Some internal functions */
158static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
159 struct nand_oobinfo *oobsel, int mode);
160#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200161static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
Wolfgang Denk932394a2005-08-17 12:55:25 +0200162 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
163#else
164#define nand_verify_pages(...) (0)
165#endif
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200166
Wolfgang Denk932394a2005-08-17 12:55:25 +0200167static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
168
169/**
170 * nand_release_device - [GENERIC] release chip
171 * @mtd: MTD device structure
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200172 *
173 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk932394a2005-08-17 12:55:25 +0200174 */
175/* XXX U-BOOT XXX */
176#if 0
177static void nand_release_device (struct mtd_info *mtd)
178{
179 struct nand_chip *this = mtd->priv;
180
181 /* De-select the NAND device */
182 this->select_chip(mtd, -1);
183 /* Do we have a hardware controller ? */
184 if (this->controller) {
185 spin_lock(&this->controller->lock);
186 this->controller->active = NULL;
187 spin_unlock(&this->controller->lock);
188 }
189 /* Release the chip */
190 spin_lock (&this->chip_lock);
191 this->state = FL_READY;
192 wake_up (&this->wq);
193 spin_unlock (&this->chip_lock);
194}
195#else
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100196static void nand_release_device (struct mtd_info *mtd)
197{
198 struct nand_chip *this = mtd->priv;
199 this->select_chip(mtd, -1); /* De-select the NAND device */
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200200 if (ffchars) {
201 kfree(ffchars);
202 ffchars = NULL;
203 }
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100204}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200205#endif
206
207/**
208 * nand_read_byte - [DEFAULT] read one byte from the chip
209 * @mtd: MTD device structure
210 *
211 * Default read function for 8bit buswith
212 */
213static u_char nand_read_byte(struct mtd_info *mtd)
214{
215 struct nand_chip *this = mtd->priv;
216 return readb(this->IO_ADDR_R);
217}
218
219/**
220 * nand_write_byte - [DEFAULT] write one byte to the chip
221 * @mtd: MTD device structure
222 * @byte: pointer to data byte to write
223 *
224 * Default write function for 8it buswith
225 */
226static void nand_write_byte(struct mtd_info *mtd, u_char byte)
227{
228 struct nand_chip *this = mtd->priv;
229 writeb(byte, this->IO_ADDR_W);
230}
231
232/**
233 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
234 * @mtd: MTD device structure
235 *
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200236 * Default read function for 16bit buswith with
Wolfgang Denk932394a2005-08-17 12:55:25 +0200237 * endianess conversion
238 */
239static u_char nand_read_byte16(struct mtd_info *mtd)
240{
241 struct nand_chip *this = mtd->priv;
242 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
243}
244
245/**
246 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
247 * @mtd: MTD device structure
248 * @byte: pointer to data byte to write
249 *
250 * Default write function for 16bit buswith with
251 * endianess conversion
252 */
253static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
254{
255 struct nand_chip *this = mtd->priv;
256 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
257}
258
259/**
260 * nand_read_word - [DEFAULT] read one word from the chip
261 * @mtd: MTD device structure
262 *
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200263 * Default read function for 16bit buswith without
Wolfgang Denk932394a2005-08-17 12:55:25 +0200264 * endianess conversion
265 */
266static u16 nand_read_word(struct mtd_info *mtd)
267{
268 struct nand_chip *this = mtd->priv;
269 return readw(this->IO_ADDR_R);
270}
271
272/**
273 * nand_write_word - [DEFAULT] write one word to the chip
274 * @mtd: MTD device structure
275 * @word: data word to write
276 *
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200277 * Default write function for 16bit buswith without
Wolfgang Denk932394a2005-08-17 12:55:25 +0200278 * endianess conversion
279 */
280static void nand_write_word(struct mtd_info *mtd, u16 word)
281{
282 struct nand_chip *this = mtd->priv;
283 writew(word, this->IO_ADDR_W);
284}
285
286/**
287 * nand_select_chip - [DEFAULT] control CE line
288 * @mtd: MTD device structure
289 * @chip: chipnumber to select, -1 for deselect
290 *
291 * Default select function for 1 chip devices.
292 */
293static void nand_select_chip(struct mtd_info *mtd, int chip)
294{
295 struct nand_chip *this = mtd->priv;
296 switch(chip) {
297 case -1:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200298 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200299 break;
300 case 0:
301 this->hwcontrol(mtd, NAND_CTL_SETNCE);
302 break;
303
304 default:
305 BUG();
306 }
307}
308
309/**
310 * nand_write_buf - [DEFAULT] write buffer to chip
311 * @mtd: MTD device structure
312 * @buf: data buffer
313 * @len: number of bytes to write
314 *
315 * Default write function for 8bit buswith
316 */
317static void nand_write_buf(struct mtd_info *mtd, const 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 writeb(buf[i], this->IO_ADDR_W);
324}
325
326/**
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200327 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200328 * @mtd: MTD device structure
329 * @buf: buffer to store date
330 * @len: number of bytes to read
331 *
332 * Default read function for 8bit buswith
333 */
334static void nand_read_buf(struct mtd_info *mtd, 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 buf[i] = readb(this->IO_ADDR_R);
341}
342
343/**
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200344 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200345 * @mtd: MTD device structure
346 * @buf: buffer containing the data to compare
347 * @len: number of bytes to compare
348 *
349 * Default verify function for 8bit buswith
350 */
351static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
352{
353 int i;
354 struct nand_chip *this = mtd->priv;
355
356 for (i=0; i<len; i++)
357 if (buf[i] != readb(this->IO_ADDR_R))
358 return -EFAULT;
359
360 return 0;
361}
362
363/**
364 * nand_write_buf16 - [DEFAULT] write buffer to chip
365 * @mtd: MTD device structure
366 * @buf: data buffer
367 * @len: number of bytes to write
368 *
369 * Default write function for 16bit buswith
370 */
371static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
372{
373 int i;
374 struct nand_chip *this = mtd->priv;
375 u16 *p = (u16 *) buf;
376 len >>= 1;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200377
Wolfgang Denk932394a2005-08-17 12:55:25 +0200378 for (i=0; i<len; i++)
379 writew(p[i], this->IO_ADDR_W);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200380
Wolfgang Denk932394a2005-08-17 12:55:25 +0200381}
382
383/**
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200384 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200385 * @mtd: MTD device structure
386 * @buf: buffer to store date
387 * @len: number of bytes to read
388 *
389 * Default read function for 16bit buswith
390 */
391static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
392{
393 int i;
394 struct nand_chip *this = mtd->priv;
395 u16 *p = (u16 *) buf;
396 len >>= 1;
397
398 for (i=0; i<len; i++)
399 p[i] = readw(this->IO_ADDR_R);
400}
401
402/**
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200403 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200404 * @mtd: MTD device structure
405 * @buf: buffer containing the data to compare
406 * @len: number of bytes to compare
407 *
408 * Default verify function for 16bit buswith
409 */
410static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
411{
412 int i;
413 struct nand_chip *this = mtd->priv;
414 u16 *p = (u16 *) buf;
415 len >>= 1;
416
417 for (i=0; i<len; i++)
418 if (p[i] != readw(this->IO_ADDR_R))
419 return -EFAULT;
420
421 return 0;
422}
423
424/**
425 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
426 * @mtd: MTD device structure
427 * @ofs: offset from device start
428 * @getchip: 0, if the chip is already selected
429 *
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200430 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200431 */
432static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
433{
434 int page, chipnr, res = 0;
435 struct nand_chip *this = mtd->priv;
436 u16 bad;
437
Thomas Knoblocha7988652007-05-05 07:04:42 +0200438 page = (int)(ofs >> this->page_shift) & this->pagemask;
439
Wolfgang Denk932394a2005-08-17 12:55:25 +0200440 if (getchip) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200441 chipnr = (int)(ofs >> this->chip_shift);
442
443 /* Grab the lock and see if the device is available */
444 nand_get_device (this, mtd, FL_READING);
445
446 /* Select the NAND device */
447 this->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200448 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200449
450 if (this->options & NAND_BUSWIDTH_16) {
Thomas Knoblocha7988652007-05-05 07:04:42 +0200451 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200452 bad = cpu_to_le16(this->read_word(mtd));
453 if (this->badblockpos & 0x1)
454 bad >>= 1;
455 if ((bad & 0xFF) != 0xff)
456 res = 1;
457 } else {
Thomas Knoblocha7988652007-05-05 07:04:42 +0200458 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200459 if (this->read_byte(mtd) != 0xff)
460 res = 1;
461 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200462
Wolfgang Denk932394a2005-08-17 12:55:25 +0200463 if (getchip) {
464 /* Deselect and wake up anyone waiting on the device */
465 nand_release_device(mtd);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200466 }
467
Wolfgang Denk932394a2005-08-17 12:55:25 +0200468 return res;
469}
470
471/**
472 * nand_default_block_markbad - [DEFAULT] mark a block bad
473 * @mtd: MTD device structure
474 * @ofs: offset from device start
475 *
476 * This is the default implementation, which can be overridden by
477 * a hardware specific driver.
478*/
479static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
480{
481 struct nand_chip *this = mtd->priv;
482 u_char buf[2] = {0, 0};
483 size_t retlen;
484 int block;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200485
Wolfgang Denk932394a2005-08-17 12:55:25 +0200486 /* Get block number */
487 block = ((int) ofs) >> this->bbt_erase_shift;
488 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
489
490 /* Do we have a flash based bad block table ? */
491 if (this->options & NAND_USE_FLASH_BBT)
492 return nand_update_bbt (mtd, ofs);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200493
Wolfgang Denk932394a2005-08-17 12:55:25 +0200494 /* We write two bytes, so we dont have to mess with 16 bit access */
495 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
496 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
497}
498
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200499/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200500 * nand_check_wp - [GENERIC] check if the chip is write protected
501 * @mtd: MTD device structure
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200502 * Check, if the device is write protected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200503 *
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200504 * The function expects, that the device is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200505 */
506static int nand_check_wp (struct mtd_info *mtd)
507{
508 struct nand_chip *this = mtd->priv;
509 /* Check the WP bit */
510 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200511 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200512}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100513
Wolfgang Denk932394a2005-08-17 12:55:25 +0200514/**
515 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
516 * @mtd: MTD device structure
517 * @ofs: offset from device start
518 * @getchip: 0, if the chip is already selected
519 * @allowbbt: 1, if its allowed to access the bbt area
520 *
521 * Check, if the block is bad. Either by reading the bad block table or
522 * calling of the scan function.
523 */
524static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
525{
526 struct nand_chip *this = mtd->priv;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200527
Wolfgang Denk932394a2005-08-17 12:55:25 +0200528 if (!this->bbt)
529 return this->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200530
Wolfgang Denk932394a2005-08-17 12:55:25 +0200531 /* Return info from the table */
532 return nand_isbad_bbt (mtd, ofs, allowbbt);
533}
534
535/**
536 * nand_command - [DEFAULT] Send command to NAND device
537 * @mtd: MTD device structure
538 * @command: the command to be sent
539 * @column: the column address for this command, -1 if none
540 * @page_addr: the page address for this command, -1 if none
541 *
542 * Send command to NAND device. This function is used for small page
543 * devices (256/512 Bytes per page)
544 */
545static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
546{
547 register struct nand_chip *this = mtd->priv;
548
549 /* Begin command latch cycle */
550 this->hwcontrol(mtd, NAND_CTL_SETCLE);
551 /*
552 * Write out the command to the device.
553 */
554 if (command == NAND_CMD_SEQIN) {
555 int readcmd;
556
557 if (column >= mtd->oobblock) {
558 /* OOB area */
559 column -= mtd->oobblock;
560 readcmd = NAND_CMD_READOOB;
561 } else if (column < 256) {
562 /* First 256 bytes --> READ0 */
563 readcmd = NAND_CMD_READ0;
564 } else {
565 column -= 256;
566 readcmd = NAND_CMD_READ1;
567 }
568 this->write_byte(mtd, readcmd);
569 }
570 this->write_byte(mtd, command);
571
572 /* Set ALE and clear CLE to start address cycle */
573 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
574
575 if (column != -1 || page_addr != -1) {
576 this->hwcontrol(mtd, NAND_CTL_SETALE);
577
578 /* Serially input address */
579 if (column != -1) {
580 /* Adjust columns for 16 bit buswidth */
581 if (this->options & NAND_BUSWIDTH_16)
582 column >>= 1;
583 this->write_byte(mtd, column);
584 }
585 if (page_addr != -1) {
586 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
587 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
588 /* One more address cycle for devices > 32MiB */
589 if (this->chipsize > (32 << 20))
590 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
591 }
592 /* Latch in address */
593 this->hwcontrol(mtd, NAND_CTL_CLRALE);
594 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200595
596 /*
597 * program and erase have their own busy handlers
Wolfgang Denk932394a2005-08-17 12:55:25 +0200598 * status and sequential in needs no delay
599 */
600 switch (command) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200601
Wolfgang Denk932394a2005-08-17 12:55:25 +0200602 case NAND_CMD_PAGEPROG:
603 case NAND_CMD_ERASE1:
604 case NAND_CMD_ERASE2:
605 case NAND_CMD_SEQIN:
606 case NAND_CMD_STATUS:
607 return;
608
609 case NAND_CMD_RESET:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200610 if (this->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200611 break;
612 udelay(this->chip_delay);
613 this->hwcontrol(mtd, NAND_CTL_SETCLE);
614 this->write_byte(mtd, NAND_CMD_STATUS);
615 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
616 while ( !(this->read_byte(mtd) & 0x40));
617 return;
618
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200619 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200620 default:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200621 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200622 * If we don't have access to the busy pin, we apply the given
623 * command delay
624 */
625 if (!this->dev_ready) {
626 udelay (this->chip_delay);
627 return;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200628 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200629 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200630
Wolfgang Denk932394a2005-08-17 12:55:25 +0200631 /* Apply this short delay always to ensure that we do wait tWB in
632 * any case on any machine. */
633 ndelay (100);
634 /* wait until command is processed */
635 while (!this->dev_ready(mtd));
636}
637
638/**
639 * nand_command_lp - [DEFAULT] Send command to NAND large page device
640 * @mtd: MTD device structure
641 * @command: the command to be sent
642 * @column: the column address for this command, -1 if none
643 * @page_addr: the page address for this command, -1 if none
644 *
645 * Send command to NAND device. This is the version for the new large page devices
646 * We dont have the seperate regions as we have in the small page devices.
647 * We must emulate NAND_CMD_READOOB to keep the code compatible.
648 *
649 */
650static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
651{
652 register struct nand_chip *this = mtd->priv;
653
654 /* Emulate NAND_CMD_READOOB */
655 if (command == NAND_CMD_READOOB) {
656 column += mtd->oobblock;
657 command = NAND_CMD_READ0;
658 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200659
660
Wolfgang Denk932394a2005-08-17 12:55:25 +0200661 /* Begin command latch cycle */
662 this->hwcontrol(mtd, NAND_CTL_SETCLE);
663 /* Write out the command to the device. */
664 this->write_byte(mtd, command);
665 /* End command latch cycle */
666 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
667
668 if (column != -1 || page_addr != -1) {
669 this->hwcontrol(mtd, NAND_CTL_SETALE);
670
671 /* Serially input address */
672 if (column != -1) {
673 /* Adjust columns for 16 bit buswidth */
674 if (this->options & NAND_BUSWIDTH_16)
675 column >>= 1;
676 this->write_byte(mtd, column & 0xff);
677 this->write_byte(mtd, column >> 8);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200678 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200679 if (page_addr != -1) {
680 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
681 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
682 /* One more address cycle for devices > 128MiB */
683 if (this->chipsize > (128 << 20))
684 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
685 }
686 /* Latch in address */
687 this->hwcontrol(mtd, NAND_CTL_CLRALE);
688 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200689
690 /*
691 * program and erase have their own busy handlers
Wolfgang Denk932394a2005-08-17 12:55:25 +0200692 * status and sequential in needs no delay
693 */
694 switch (command) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200695
Wolfgang Denk932394a2005-08-17 12:55:25 +0200696 case NAND_CMD_CACHEDPROG:
697 case NAND_CMD_PAGEPROG:
698 case NAND_CMD_ERASE1:
699 case NAND_CMD_ERASE2:
700 case NAND_CMD_SEQIN:
701 case NAND_CMD_STATUS:
702 return;
703
704
705 case NAND_CMD_RESET:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200706 if (this->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200707 break;
708 udelay(this->chip_delay);
709 this->hwcontrol(mtd, NAND_CTL_SETCLE);
710 this->write_byte(mtd, NAND_CMD_STATUS);
711 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
712 while ( !(this->read_byte(mtd) & 0x40));
713 return;
714
715 case NAND_CMD_READ0:
716 /* Begin command latch cycle */
717 this->hwcontrol(mtd, NAND_CTL_SETCLE);
718 /* Write out the start read command */
719 this->write_byte(mtd, NAND_CMD_READSTART);
720 /* End command latch cycle */
721 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
722 /* Fall through into ready check */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200723
724 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200725 default:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200726 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200727 * If we don't have access to the busy pin, we apply the given
728 * command delay
729 */
730 if (!this->dev_ready) {
731 udelay (this->chip_delay);
732 return;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200733 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200734 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200735
Wolfgang Denk932394a2005-08-17 12:55:25 +0200736 /* Apply this short delay always to ensure that we do wait tWB in
737 * any case on any machine. */
738 ndelay (100);
739 /* wait until command is processed */
740 while (!this->dev_ready(mtd));
741}
742
743/**
744 * nand_get_device - [GENERIC] Get chip for selected access
745 * @this: the nand chip descriptor
746 * @mtd: MTD device structure
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200747 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200748 *
749 * Get the device and lock it for exclusive access
750 */
751/* XXX U-BOOT XXX */
752#if 0
753static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
754{
755 struct nand_chip *active = this;
756
757 DECLARE_WAITQUEUE (wait, current);
758
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200759 /*
760 * Grab the lock and see if the device is available
Wolfgang Denk932394a2005-08-17 12:55:25 +0200761 */
762retry:
763 /* Hardware controller shared among independend devices */
764 if (this->controller) {
765 spin_lock (&this->controller->lock);
766 if (this->controller->active)
767 active = this->controller->active;
768 else
769 this->controller->active = this;
770 spin_unlock (&this->controller->lock);
771 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200772
Wolfgang Denk932394a2005-08-17 12:55:25 +0200773 if (active == this) {
774 spin_lock (&this->chip_lock);
775 if (this->state == FL_READY) {
776 this->state = new_state;
777 spin_unlock (&this->chip_lock);
778 return;
779 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200780 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200781 set_current_state (TASK_UNINTERRUPTIBLE);
782 add_wait_queue (&active->wq, &wait);
783 spin_unlock (&active->chip_lock);
784 schedule ();
785 remove_wait_queue (&active->wq, &wait);
786 goto retry;
787}
788#else
789static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) {}
790#endif
791
792/**
793 * nand_wait - [DEFAULT] wait until the command is done
794 * @mtd: MTD device structure
795 * @this: NAND chip structure
796 * @state: state to select the max. timeout value
797 *
798 * Wait for command done. This applies to erase and program only
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200799 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk932394a2005-08-17 12:55:25 +0200800 * general NAND and SmartMedia specs
801 *
802*/
803/* XXX U-BOOT XXX */
804#if 0
805static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
806{
807 unsigned long timeo = jiffies;
808 int status;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200809
Wolfgang Denk932394a2005-08-17 12:55:25 +0200810 if (state == FL_ERASING)
811 timeo += (HZ * 400) / 1000;
812 else
813 timeo += (HZ * 20) / 1000;
814
815 /* Apply this short delay always to ensure that we do wait tWB in
816 * any case on any machine. */
817 ndelay (100);
818
819 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
820 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200821 else
Wolfgang Denk932394a2005-08-17 12:55:25 +0200822 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
823
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200824 while (time_before(jiffies, timeo)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200825 /* Check, if we were interrupted */
826 if (this->state != state)
827 return 0;
828
829 if (this->dev_ready) {
830 if (this->dev_ready(mtd))
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200831 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200832 } else {
833 if (this->read_byte(mtd) & NAND_STATUS_READY)
834 break;
835 }
836 yield ();
837 }
838 status = (int) this->read_byte(mtd);
839 return status;
840
841 return 0;
842}
843#else
844static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
845{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100846 unsigned long timeo;
847
848 if (state == FL_ERASING)
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200849 timeo = (CFG_HZ * 400) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100850 else
Stefan Roesee7f3e9f2006-11-28 11:04:45 +0100851 timeo = (CFG_HZ * 20) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100852
853 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
854 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
855 else
856 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
857
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100858 reset_timer();
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100859
860 while (1) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100861 if (get_timer(0) > timeo) {
862 printf("Timeout!");
Stefan Roese15784862006-11-27 17:22:19 +0100863 return 0x01;
864 }
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100865
866 if (this->dev_ready) {
867 if (this->dev_ready(mtd))
868 break;
869 } else {
870 if (this->read_byte(mtd) & NAND_STATUS_READY)
871 break;
872 }
873 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100874#ifdef PPCHAMELON_NAND_TIMER_HACK
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100875 reset_timer();
876 while (get_timer(0) < 10);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100877#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100878
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100879 return this->read_byte(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200880}
881#endif
882
883/**
884 * nand_write_page - [GENERIC] write one page
885 * @mtd: MTD device structure
886 * @this: NAND chip structure
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200887 * @page: startpage inside the chip, must be called with (page & this->pagemask)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200888 * @oob_buf: out of band data buffer
889 * @oobsel: out of band selecttion structre
890 * @cached: 1 = enable cached programming if supported by chip
891 *
892 * Nand_page_program function is used for write and writev !
893 * This function will always program a full page of data
894 * If you call it with a non page aligned buffer, you're lost :)
895 *
896 * Cached programming is not supported yet.
897 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200898static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
Wolfgang Denk932394a2005-08-17 12:55:25 +0200899 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
900{
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200901 int i, status;
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200902 u_char ecc_code[NAND_MAX_OOBSIZE];
Wolfgang Denk932394a2005-08-17 12:55:25 +0200903 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200904 uint *oob_config = oobsel->eccpos;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200905 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
906 int eccbytes = 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200907
Wolfgang Denk932394a2005-08-17 12:55:25 +0200908 /* FIXME: Enable cached programming */
909 cached = 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200910
Wolfgang Denk932394a2005-08-17 12:55:25 +0200911 /* Send command to begin auto page programming */
912 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
913
914 /* Write out complete page of data, take care of eccmode */
915 switch (eccmode) {
916 /* No ecc, write all */
917 case NAND_ECC_NONE:
918 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
919 this->write_buf(mtd, this->data_poi, mtd->oobblock);
920 break;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200921
Wolfgang Denk932394a2005-08-17 12:55:25 +0200922 /* Software ecc 3/256, write all */
923 case NAND_ECC_SOFT:
924 for (; eccsteps; eccsteps--) {
925 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
926 for (i = 0; i < 3; i++, eccidx++)
927 oob_buf[oob_config[eccidx]] = ecc_code[i];
928 datidx += this->eccsize;
929 }
930 this->write_buf(mtd, this->data_poi, mtd->oobblock);
931 break;
932 default:
933 eccbytes = this->eccbytes;
934 for (; eccsteps; eccsteps--) {
935 /* enable hardware ecc logic for write */
936 this->enable_hwecc(mtd, NAND_ECC_WRITE);
937 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
938 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
939 for (i = 0; i < eccbytes; i++, eccidx++)
940 oob_buf[oob_config[eccidx]] = ecc_code[i];
941 /* If the hardware ecc provides syndromes then
Troy Kisky84c01d32007-09-24 16:41:43 -0700942 * the ecc code must be written immediately after
Wolfgang Denk932394a2005-08-17 12:55:25 +0200943 * the data bytes (words) */
944 if (this->options & NAND_HWECC_SYNDROME)
945 this->write_buf(mtd, ecc_code, eccbytes);
946 datidx += this->eccsize;
947 }
948 break;
949 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200950
Wolfgang Denk932394a2005-08-17 12:55:25 +0200951 /* Write out OOB data */
952 if (this->options & NAND_HWECC_SYNDROME)
953 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200954 else
Wolfgang Denk932394a2005-08-17 12:55:25 +0200955 this->write_buf(mtd, oob_buf, mtd->oobsize);
956
957 /* Send command to actually program the data */
958 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
959
960 if (!cached) {
961 /* call wait ready function */
962 status = this->waitfunc (mtd, this, FL_WRITING);
963 /* See if device thinks it succeeded */
964 if (status & 0x01) {
965 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
966 return -EIO;
967 }
968 } else {
969 /* FIXME: Implement cached programming ! */
970 /* wait until cache is ready*/
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200971 /* status = this->waitfunc (mtd, this, FL_CACHEDRPG); */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200972 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200973 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200974}
975
976#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
977/**
978 * nand_verify_pages - [GENERIC] verify the chip contents after a write
979 * @mtd: MTD device structure
980 * @this: NAND chip structure
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200981 * @page: startpage inside the chip, must be called with (page & this->pagemask)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200982 * @numpages: number of pages to verify
983 * @oob_buf: out of band data buffer
984 * @oobsel: out of band selecttion structre
985 * @chipnr: number of the current chip
986 * @oobmode: 1 = full buffer verify, 0 = ecc only
987 *
988 * The NAND device assumes that it is always writing to a cleanly erased page.
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200989 * Hence, it performs its internal write verification only on bits that
Wolfgang Denk932394a2005-08-17 12:55:25 +0200990 * transitioned from 1 to 0. The device does NOT verify the whole page on a
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200991 * byte by byte basis. It is possible that the page was not completely erased
992 * or the page is becoming unusable due to wear. The read with ECC would catch
993 * the error later when the ECC page check fails, but we would rather catch
Wolfgang Denk932394a2005-08-17 12:55:25 +0200994 * it early in the page write stage. Better to write no data than invalid data.
995 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200996static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
Wolfgang Denk932394a2005-08-17 12:55:25 +0200997 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
998{
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200999 int i, j, datidx = 0, oobofs = 0, res = -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001000 int eccsteps = this->eccsteps;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001001 int hweccbytes;
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001002 u_char oobdata[64];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001003
1004 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
1005
1006 /* Send command to read back the first page */
1007 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
1008
1009 for(;;) {
1010 for (j = 0; j < eccsteps; j++) {
1011 /* Loop through and verify the data */
1012 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
1013 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1014 goto out;
1015 }
1016 datidx += mtd->eccsize;
1017 /* Have we a hw generator layout ? */
1018 if (!hweccbytes)
1019 continue;
1020 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
1021 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1022 goto out;
1023 }
1024 oobofs += hweccbytes;
1025 }
1026
1027 /* check, if we must compare all data or if we just have to
1028 * compare the ecc bytes
1029 */
1030 if (oobmode) {
1031 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1032 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1033 goto out;
1034 }
1035 } else {
1036 /* Read always, else autoincrement fails */
1037 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1038
1039 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1040 int ecccnt = oobsel->eccbytes;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001041
Wolfgang Denk932394a2005-08-17 12:55:25 +02001042 for (i = 0; i < ecccnt; i++) {
1043 int idx = oobsel->eccpos[i];
1044 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
1045 DEBUG (MTD_DEBUG_LEVEL0,
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001046 "%s: Failed ECC write "
Wolfgang Denk932394a2005-08-17 12:55:25 +02001047 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
1048 goto out;
1049 }
1050 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001051 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001052 }
1053 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1054 page++;
1055 numpages--;
1056
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001057 /* Apply delay or wait for ready/busy pin
Wolfgang Denk932394a2005-08-17 12:55:25 +02001058 * Do this before the AUTOINCR check, so no problems
1059 * arise if a chip which does auto increment
1060 * is marked as NOAUTOINCR by the board driver.
1061 * Do this also before returning, so the chip is
1062 * ready for the next command.
1063 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001064 if (!this->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001065 udelay (this->chip_delay);
1066 else
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001067 while (!this->dev_ready(mtd));
Wolfgang Denk932394a2005-08-17 12:55:25 +02001068
1069 /* All done, return happy */
1070 if (!numpages)
1071 return 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001072
1073
1074 /* Check, if the chip supports auto page increment */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001075 if (!NAND_CANAUTOINCR(this))
1076 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1077 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001078 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +02001079 * Terminate the read command. We come here in case of an error
1080 * So we must issue a reset command.
1081 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001082out:
Wolfgang Denk932394a2005-08-17 12:55:25 +02001083 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1084 return res;
1085}
1086#endif
1087
1088/**
1089 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1090 * @mtd: MTD device structure
1091 * @from: offset to read from
1092 * @len: number of bytes to read
1093 * @retlen: pointer to variable to store the number of read bytes
1094 * @buf: the databuffer to put data
1095 *
1096 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1097*/
1098static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1099{
1100 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001101}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001102
1103
1104/**
1105 * nand_read_ecc - [MTD Interface] Read data with ECC
1106 * @mtd: MTD device structure
1107 * @from: offset to read from
1108 * @len: number of bytes to read
1109 * @retlen: pointer to variable to store the number of read bytes
1110 * @buf: the databuffer to put data
1111 * @oob_buf: filesystem supplied oob data buffer
1112 * @oobsel: oob selection structure
1113 *
1114 * NAND read with ECC
1115 */
1116static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1117 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1118{
1119 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1120 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1121 struct nand_chip *this = mtd->priv;
1122 u_char *data_poi, *oob_data = oob_buf;
Sergei Poselenov248ae5c2008-06-06 15:42:43 +02001123 u_char ecc_calc[NAND_MAX_OOBSIZE];
1124 u_char ecc_code[NAND_MAX_OOBSIZE];
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001125 int eccmode, eccsteps;
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +01001126 unsigned *oob_config;
1127 int datidx;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001128 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1129 int eccbytes;
1130 int compareecc = 1;
1131 int oobreadlen;
1132
1133
1134 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1135
1136 /* Do not allow reads past end of device */
1137 if ((from + len) > mtd->size) {
1138 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1139 *retlen = 0;
1140 return -EINVAL;
1141 }
1142
1143 /* Grab the lock and see if the device is available */
1144 nand_get_device (this, mtd ,FL_READING);
1145
1146 /* use userspace supplied oobinfo, if zero */
1147 if (oobsel == NULL)
1148 oobsel = &mtd->oobinfo;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001149
Wolfgang Denk932394a2005-08-17 12:55:25 +02001150 /* Autoplace of oob data ? Use the default placement scheme */
1151 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1152 oobsel = this->autooob;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001153
Wolfgang Denk932394a2005-08-17 12:55:25 +02001154 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1155 oob_config = oobsel->eccpos;
1156
1157 /* Select the NAND device */
1158 chipnr = (int)(from >> this->chip_shift);
1159 this->select_chip(mtd, chipnr);
1160
1161 /* First we calculate the starting page */
1162 realpage = (int) (from >> this->page_shift);
1163 page = realpage & this->pagemask;
1164
1165 /* Get raw starting column */
1166 col = from & (mtd->oobblock - 1);
1167
1168 end = mtd->oobblock;
1169 ecc = this->eccsize;
1170 eccbytes = this->eccbytes;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001171
Wolfgang Denk932394a2005-08-17 12:55:25 +02001172 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1173 compareecc = 0;
1174
1175 oobreadlen = mtd->oobsize;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001176 if (this->options & NAND_HWECC_SYNDROME)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001177 oobreadlen -= oobsel->eccbytes;
1178
1179 /* Loop until all data read */
1180 while (read < len) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001181
Wolfgang Denk932394a2005-08-17 12:55:25 +02001182 int aligned = (!col && (len - read) >= end);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001183 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +02001184 * If the read is not page aligned, we have to read into data buffer
1185 * due to ecc, else we read into return buffer direct
1186 */
1187 if (aligned)
1188 data_poi = &buf[read];
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001189 else
Wolfgang Denk932394a2005-08-17 12:55:25 +02001190 data_poi = this->data_buf;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001191
1192 /* Check, if we have this page in the buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +02001193 *
1194 * FIXME: Make it work when we must provide oob data too,
1195 * check the usage of data_buf oob field
1196 */
1197 if (realpage == this->pagebuf && !oob_buf) {
1198 /* aligned read ? */
1199 if (aligned)
1200 memcpy (data_poi, this->data_buf, end);
1201 goto readdata;
1202 }
1203
1204 /* Check, if we must send the read command */
1205 if (sndcmd) {
1206 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1207 sndcmd = 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001208 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001209
1210 /* get oob area, if we have no oob buffer from fs-driver */
1211 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1212 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1213 oob_data = &this->data_buf[end];
1214
1215 eccsteps = this->eccsteps;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001216
Wolfgang Denk932394a2005-08-17 12:55:25 +02001217 switch (eccmode) {
1218 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1219/* XXX U-BOOT XXX */
1220#if 0
1221 static unsigned long lastwhinge = 0;
1222 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1223 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1224 lastwhinge = jiffies;
1225 }
1226#else
1227 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1228#endif
1229 this->read_buf(mtd, data_poi, end);
1230 break;
1231 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001232
Wolfgang Denk932394a2005-08-17 12:55:25 +02001233 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1234 this->read_buf(mtd, data_poi, end);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001235 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001236 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001237 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001238
1239 default:
1240 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1241 this->enable_hwecc(mtd, NAND_ECC_READ);
1242 this->read_buf(mtd, &data_poi[datidx], ecc);
1243
1244 /* HW ecc with syndrome calculation must read the
1245 * syndrome from flash immidiately after the data */
1246 if (!compareecc) {
1247 /* Some hw ecc generators need to know when the
1248 * syndrome is read from flash */
1249 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1250 this->read_buf(mtd, &oob_data[i], eccbytes);
1251 /* We calc error correction directly, it checks the hw
1252 * generator for an error, reads back the syndrome and
1253 * does the error correction on the fly */
1254 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001255 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
Wolfgang Denk932394a2005-08-17 12:55:25 +02001256 "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]);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001261 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001262 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001263 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001264 }
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)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001271 goto readoob;
1272
Wolfgang Denk932394a2005-08-17 12:55:25 +02001273 /* 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]);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001280
Wolfgang Denk932394a2005-08-17 12:55:25 +02001281 /* Get next chunk of ecc bytes */
1282 j += eccbytes;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001283
1284 /* Check, if we have a fs supplied oob-buffer,
Wolfgang Denk932394a2005-08-17 12:55:25 +02001285 * This is the legacy mode. Used by YAFFS1
1286 * Should go away some day
1287 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001288 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001289 int *p = (int *)(&oob_data[mtd->oobsize]);
1290 p[i] = ecc_status;
1291 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001292
1293 if (ecc_status == -1) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001294 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1295 ecc_failed++;
1296 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001297 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001298
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:
1305 case MTD_NANDECC_AUTOPL_USR:
1306 /* Walk through the autoplace chunks */
1307 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1308 int from = oobsel->oobfree[i][0];
1309 int num = oobsel->oobfree[i][1];
Troy Kisky84c01d32007-09-24 16:41:43 -07001310 memcpy(&oob_buf[oob+j], &oob_data[from], num);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001311 j+= num;
1312 }
1313 oob += mtd->oobavail;
1314 break;
1315 case MTD_NANDECC_PLACE:
1316 /* YAFFS1 legacy mode */
1317 oob_data += this->eccsteps * sizeof (int);
1318 default:
1319 oob_data += mtd->oobsize;
1320 }
1321 }
1322 readdata:
1323 /* Partial page read, transfer data into fs buffer */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001324 if (!aligned) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001325 for (j = col; j < end && read < len; j++)
1326 buf[read++] = data_poi[j];
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001327 this->pagebuf = realpage;
1328 } else
Wolfgang Denk932394a2005-08-17 12:55:25 +02001329 read += mtd->oobblock;
1330
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001331 /* Apply delay or wait for ready/busy pin
Wolfgang Denk932394a2005-08-17 12:55:25 +02001332 * Do this before the AUTOINCR check, so no problems
1333 * arise if a chip which does auto increment
1334 * is marked as NOAUTOINCR by the board driver.
1335 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001336 if (!this->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001337 udelay (this->chip_delay);
1338 else
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001339 while (!this->dev_ready(mtd));
1340
Wolfgang Denk932394a2005-08-17 12:55:25 +02001341 if (read == len)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001342 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001343
1344 /* For subsequent reads align to page boundary. */
1345 col = 0;
1346 /* Increment page address */
1347 realpage++;
1348
1349 page = realpage & this->pagemask;
1350 /* Check, if we cross a chip boundary */
1351 if (!page) {
1352 chipnr++;
1353 this->select_chip(mtd, -1);
1354 this->select_chip(mtd, chipnr);
1355 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001356 /* Check, if the chip supports auto page increment
1357 * or if we have hit a block boundary.
1358 */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001359 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001360 sndcmd = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001361 }
1362
1363 /* Deselect and wake up anyone waiting on the device */
1364 nand_release_device(mtd);
1365
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);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001396
Wolfgang Denk932394a2005-08-17 12:55:25 +02001397 /* 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);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001418 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +02001419 * 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;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001428
1429 /* Apply delay or wait for ready/busy pin
Wolfgang Denk932394a2005-08-17 12:55:25 +02001430 * Do this before the AUTOINCR check, so no problems
1431 * arise if a chip which does auto increment
1432 * is marked as NOAUTOINCR by the board driver.
1433 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001434 if (!this->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001435 udelay (this->chip_delay);
1436 else
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001437 while (!this->dev_ready(mtd));
Wolfgang Denk932394a2005-08-17 12:55:25 +02001438
1439 /* Read more ? */
1440 if (i < len) {
1441 page++;
1442 col = 0;
1443
1444 /* Check, if we cross a chip boundary */
1445 if (!(page & this->pagemask)) {
1446 chipnr++;
1447 this->select_chip(mtd, -1);
1448 this->select_chip(mtd, chipnr);
1449 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001450
1451 /* Check, if the chip supports auto page increment
1452 * or if we have hit a block boundary.
1453 */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001454 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1455 /* For subsequent page reads set offset to 0 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001456 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001457 }
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);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001499
Wolfgang Denk932394a2005-08-17 12:55:25 +02001500 /* Add requested oob length */
1501 len += ooblen;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001502
Wolfgang Denk932394a2005-08-17 12:55:25 +02001503 while (len) {
1504 if (sndcmd)
1505 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001506 sndcmd = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001507
1508 this->read_buf (mtd, &buf[cnt], pagesize);
1509
1510 len -= pagesize;
1511 cnt += pagesize;
1512 page++;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001513
1514 if (!this->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001515 udelay (this->chip_delay);
1516 else
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001517 while (!this->dev_ready(mtd));
1518
1519 /* Check, if the chip supports auto page increment */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001520 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
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001530/**
1531 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +02001532 * @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 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001560 if (fsbuf && !autoplace)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001561 return fsbuf;
1562
1563 /* Check, if the buffer must be filled with ff again */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001564 if (this->oobdirty) {
1565 memset (this->oob_buf, 0xff,
Wolfgang Denk932394a2005-08-17 12:55:25 +02001566 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1567 this->oobdirty = 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001568 }
1569
Wolfgang Denk932394a2005-08-17 12:55:25 +02001570 /* If we have no autoplacement or no fs buffer use the internal one */
1571 if (!autoplace || !fsbuf)
1572 return this->oob_buf;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001573
Wolfgang Denk932394a2005-08-17 12:55:25 +02001574 /* 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}
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001607
Wolfgang Denk932394a2005-08-17 12:55:25 +02001608/**
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
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001640 /* reject writes, which are not page aligned */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001641 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 */
Troy Kisky84c01d32007-09-24 16:41:43 -07001655 if (nand_check_wp(mtd)) {
1656 printk (KERN_NOTICE "nand_write_ecc: Device is write protected\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02001657 goto out;
Troy Kisky84c01d32007-09-24 16:41:43 -07001658 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001659
1660 /* if oobsel is NULL, use chip defaults */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001661 if (oobsel == NULL)
1662 oobsel = &mtd->oobinfo;
1663
Wolfgang Denk932394a2005-08-17 12:55:25 +02001664 /* Autoplace of oob data ? Use the default placement scheme */
1665 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1666 oobsel = this->autooob;
1667 autoplace = 1;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001668 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001669 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1670 autoplace = 1;
1671
1672 /* Setup variables and oob buffer */
1673 totalpages = len >> this->page_shift;
1674 page = (int) (to >> this->page_shift);
1675 /* Invalidate the page cache, if we write to the cached page */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001676 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
Wolfgang Denk932394a2005-08-17 12:55:25 +02001677 this->pagebuf = -1;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001678
Wolfgang Denk932394a2005-08-17 12:55:25 +02001679 /* Set it relative to chip */
1680 page &= this->pagemask;
1681 startpage = page;
1682 /* Calc number of pages we can write in one go */
1683 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1684 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1685 bufstart = (u_char *)buf;
1686
1687 /* Loop until all data is written */
1688 while (written < len) {
1689
1690 this->data_poi = (u_char*) &buf[written];
1691 /* Write one page. If this is the last page to write
1692 * or the last page in this block, then use the
1693 * real pageprogram command, else select cached programming
1694 * if supported by the chip.
1695 */
1696 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1697 if (ret) {
1698 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1699 goto out;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001700 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001701 /* Next oob page */
1702 oob += mtd->oobsize;
1703 /* Update written bytes count */
1704 written += mtd->oobblock;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001705 if (written == len)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001706 goto cmp;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001707
Wolfgang Denk932394a2005-08-17 12:55:25 +02001708 /* Increment page address */
1709 page++;
1710
1711 /* Have we hit a block boundary ? Then we have to verify and
1712 * if verify is ok, we have to setup the oob buffer for
1713 * the next pages.
1714 */
1715 if (!(page & (ppblock - 1))){
1716 int ofs;
1717 this->data_poi = bufstart;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001718 ret = nand_verify_pages (mtd, this, startpage,
Wolfgang Denk932394a2005-08-17 12:55:25 +02001719 page - startpage,
1720 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1721 if (ret) {
1722 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1723 goto out;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001724 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001725 *retlen = written;
Nick Spence04f899f2006-09-30 00:32:59 -07001726 bufstart = (u_char*) &buf[written];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001727
1728 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1729 if (eccbuf)
1730 eccbuf += (page - startpage) * ofs;
1731 totalpages -= page - startpage;
1732 numpages = min (totalpages, ppblock);
1733 page &= this->pagemask;
1734 startpage = page;
Markus Klotzbücherf9e02912006-03-06 13:45:42 +01001735 oob = 0;
1736 this->oobdirty = 1;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001737 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
Wolfgang Denk932394a2005-08-17 12:55:25 +02001738 autoplace, numpages);
1739 /* Check, if we cross a chip boundary */
1740 if (!page) {
1741 chipnr++;
1742 this->select_chip(mtd, -1);
1743 this->select_chip(mtd, chipnr);
1744 }
1745 }
1746 }
1747 /* Verify the remaining pages */
1748cmp:
1749 this->data_poi = bufstart;
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001750 ret = nand_verify_pages (mtd, this, startpage, totalpages,
Wolfgang Denk932394a2005-08-17 12:55:25 +02001751 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1752 if (!ret)
1753 *retlen = written;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001754 else
Wolfgang Denk932394a2005-08-17 12:55:25 +02001755 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1756
1757out:
1758 /* Deselect and wake up anyone waiting on the device */
1759 nand_release_device(mtd);
1760
1761 return ret;
1762}
1763
1764
1765/**
1766 * nand_write_oob - [MTD Interface] NAND write out-of-band
1767 * @mtd: MTD device structure
1768 * @to: offset to write to
1769 * @len: number of bytes to write
1770 * @retlen: pointer to variable to store the number of written bytes
1771 * @buf: the data to write
1772 *
1773 * NAND write out-of-band
1774 */
1775static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1776{
1777 int column, page, status, ret = -EIO, chipnr;
1778 struct nand_chip *this = mtd->priv;
1779
1780 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1781
1782 /* Shift to get page */
1783 page = (int) (to >> this->page_shift);
1784 chipnr = (int) (to >> this->chip_shift);
1785
1786 /* Mask to get column */
1787 column = to & (mtd->oobsize - 1);
1788
1789 /* Initialize return length value */
1790 *retlen = 0;
1791
1792 /* Do not allow write past end of page */
1793 if ((column + len) > mtd->oobsize) {
1794 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1795 return -EINVAL;
1796 }
1797
1798 /* Grab the lock and see if the device is available */
1799 nand_get_device (this, mtd, FL_WRITING);
1800
1801 /* Select the NAND device */
1802 this->select_chip(mtd, chipnr);
1803
1804 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1805 in one of my DiskOnChip 2000 test units) will clear the whole
1806 data page too if we don't do this. I have no clue why, but
1807 I seem to have 'fixed' it in the doc2000 driver in
1808 August 1999. dwmw2. */
1809 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1810
1811 /* Check, if it is write protected */
1812 if (nand_check_wp(mtd))
1813 goto out;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001814
Wolfgang Denk932394a2005-08-17 12:55:25 +02001815 /* Invalidate the page cache, if we write to the cached page */
1816 if (page == this->pagebuf)
1817 this->pagebuf = -1;
1818
1819 if (NAND_MUST_PAD(this)) {
1820 /* Write out desired data */
1821 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
Sergei Poselenov248ae5c2008-06-06 15:42:43 +02001822 if (!ffchars) {
1823 if (!(ffchars = kmalloc (mtd->oobsize, GFP_KERNEL))) {
1824 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1825 "No memory for padding array, need %d bytes", mtd->oobsize);
1826 ret = -ENOMEM;
1827 goto out;
1828 }
1829 memset(ffchars, 0xff, mtd->oobsize);
1830 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001831 /* prepad 0xff for partial programming */
1832 this->write_buf(mtd, ffchars, column);
1833 /* write data */
1834 this->write_buf(mtd, buf, len);
1835 /* postpad 0xff for partial programming */
1836 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1837 } else {
1838 /* Write out desired data */
1839 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1840 /* write data */
1841 this->write_buf(mtd, buf, len);
1842 }
1843 /* Send command to program the OOB data */
1844 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1845
1846 status = this->waitfunc (mtd, this, FL_WRITING);
1847
1848 /* See if device thinks it succeeded */
1849 if (status & 0x01) {
1850 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1851 ret = -EIO;
1852 goto out;
1853 }
1854 /* Return happy */
1855 *retlen = len;
1856
1857#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1858 /* Send command to read back the data */
1859 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1860
1861 if (this->verify_buf(mtd, buf, len)) {
1862 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1863 ret = -EIO;
1864 goto out;
1865 }
1866#endif
1867 ret = 0;
1868out:
1869 /* Deselect and wake up anyone waiting on the device */
1870 nand_release_device(mtd);
1871
1872 return ret;
1873}
1874
1875/* XXX U-BOOT XXX */
1876#if 0
1877/**
1878 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1879 * @mtd: MTD device structure
1880 * @vecs: the iovectors to write
1881 * @count: number of vectors
1882 * @to: offset to write to
1883 * @retlen: pointer to variable to store the number of written bytes
1884 *
1885 * NAND write with kvec. This just calls the ecc function
1886 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001887static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
Wolfgang Denk932394a2005-08-17 12:55:25 +02001888 loff_t to, size_t * retlen)
1889{
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001890 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
Wolfgang Denk932394a2005-08-17 12:55:25 +02001891}
1892
1893/**
1894 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1895 * @mtd: MTD device structure
1896 * @vecs: the iovectors to write
1897 * @count: number of vectors
1898 * @to: offset to write to
1899 * @retlen: pointer to variable to store the number of written bytes
1900 * @eccbuf: filesystem supplied oob data buffer
1901 * @oobsel: oob selection structure
1902 *
1903 * NAND write with iovec with ecc
1904 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001905static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
Wolfgang Denk932394a2005-08-17 12:55:25 +02001906 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1907{
1908 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1909 int oob, numpages, autoplace = 0, startpage;
1910 struct nand_chip *this = mtd->priv;
1911 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1912 u_char *oobbuf, *bufstart;
1913
1914 /* Preset written len for early exit */
1915 *retlen = 0;
1916
1917 /* Calculate total length of data */
1918 total_len = 0;
1919 for (i = 0; i < count; i++)
1920 total_len += (int) vecs[i].iov_len;
1921
1922 DEBUG (MTD_DEBUG_LEVEL3,
1923 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1924
1925 /* Do not allow write past end of page */
1926 if ((to + total_len) > mtd->size) {
1927 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1928 return -EINVAL;
1929 }
1930
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001931 /* reject writes, which are not page aligned */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001932 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1933 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1934 return -EINVAL;
1935 }
1936
1937 /* Grab the lock and see if the device is available */
1938 nand_get_device (this, mtd, FL_WRITING);
1939
1940 /* Get the current chip-nr */
1941 chipnr = (int) (to >> this->chip_shift);
1942 /* Select the NAND device */
1943 this->select_chip(mtd, chipnr);
1944
1945 /* Check, if it is write protected */
1946 if (nand_check_wp(mtd))
1947 goto out;
1948
1949 /* if oobsel is NULL, use chip defaults */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001950 if (oobsel == NULL)
1951 oobsel = &mtd->oobinfo;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001952
1953 /* Autoplace of oob data ? Use the default placement scheme */
1954 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1955 oobsel = this->autooob;
1956 autoplace = 1;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001957 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001958 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1959 autoplace = 1;
1960
1961 /* Setup start page */
1962 page = (int) (to >> this->page_shift);
1963 /* Invalidate the page cache, if we write to the cached page */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001964 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
Wolfgang Denk932394a2005-08-17 12:55:25 +02001965 this->pagebuf = -1;
1966
1967 startpage = page & this->pagemask;
1968
1969 /* Loop until all kvec' data has been written */
1970 len = 0;
1971 while (count) {
1972 /* If the given tuple is >= pagesize then
1973 * write it out from the iov
1974 */
1975 if ((vecs->iov_len - len) >= mtd->oobblock) {
1976 /* Calc number of pages we can write
1977 * out of this iov in one go */
1978 numpages = (vecs->iov_len - len) >> this->page_shift;
1979 /* Do not cross block boundaries */
1980 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1981 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1982 bufstart = (u_char *)vecs->iov_base;
1983 bufstart += len;
1984 this->data_poi = bufstart;
1985 oob = 0;
1986 for (i = 1; i <= numpages; i++) {
1987 /* Write one page. If this is the last page to write
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001988 * then use the real pageprogram command, else select
Wolfgang Denk932394a2005-08-17 12:55:25 +02001989 * cached programming if supported by the chip.
1990 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001991 ret = nand_write_page (mtd, this, page & this->pagemask,
Wolfgang Denk932394a2005-08-17 12:55:25 +02001992 &oobbuf[oob], oobsel, i != numpages);
1993 if (ret)
1994 goto out;
1995 this->data_poi += mtd->oobblock;
1996 len += mtd->oobblock;
1997 oob += mtd->oobsize;
1998 page++;
1999 }
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 } else {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002007 /* We must use the internal buffer, read data out of each
Wolfgang Denk932394a2005-08-17 12:55:25 +02002008 * tuple until we have a full page to write
2009 */
2010 int cnt = 0;
2011 while (cnt < mtd->oobblock) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002012 if (vecs->iov_base != NULL && vecs->iov_len)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002013 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
2014 /* Check, if we have to switch to the next tuple */
2015 if (len >= (int) vecs->iov_len) {
2016 vecs++;
2017 len = 0;
2018 count--;
2019 }
2020 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002021 this->pagebuf = page;
2022 this->data_poi = this->data_buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002023 bufstart = this->data_poi;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002024 numpages = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002025 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2026 ret = nand_write_page (mtd, this, page & this->pagemask,
2027 oobbuf, oobsel, 0);
2028 if (ret)
2029 goto out;
2030 page++;
2031 }
2032
2033 this->data_poi = bufstart;
2034 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2035 if (ret)
2036 goto out;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002037
Wolfgang Denk932394a2005-08-17 12:55:25 +02002038 written += mtd->oobblock * numpages;
2039 /* All done ? */
2040 if (!count)
2041 break;
2042
2043 startpage = page & this->pagemask;
2044 /* Check, if we cross a chip boundary */
2045 if (!startpage) {
2046 chipnr++;
2047 this->select_chip(mtd, -1);
2048 this->select_chip(mtd, chipnr);
2049 }
2050 }
2051 ret = 0;
2052out:
2053 /* Deselect and wake up anyone waiting on the device */
2054 nand_release_device(mtd);
2055
2056 *retlen = written;
2057 return ret;
2058}
2059#endif
2060
2061/**
2062 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2063 * @mtd: MTD device structure
2064 * @page: the page address of the block which will be erased
2065 *
2066 * Standard erase command for NAND chips
2067 */
2068static void single_erase_cmd (struct mtd_info *mtd, int page)
2069{
2070 struct nand_chip *this = mtd->priv;
2071 /* Send commands to erase a block */
2072 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2073 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2074}
2075
2076/**
2077 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2078 * @mtd: MTD device structure
2079 * @page: the page address of the block which will be erased
2080 *
2081 * AND multi block erase command function
2082 * Erase 4 consecutive blocks
2083 */
2084static void multi_erase_cmd (struct mtd_info *mtd, int page)
2085{
2086 struct nand_chip *this = mtd->priv;
2087 /* Send commands to erase a block */
2088 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2089 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2090 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2091 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2092 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2093}
2094
2095/**
2096 * nand_erase - [MTD Interface] erase block(s)
2097 * @mtd: MTD device structure
2098 * @instr: erase instruction
2099 *
2100 * Erase one ore more blocks
2101 */
2102static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2103{
2104 return nand_erase_nand (mtd, instr, 0);
2105}
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002106
Wolfgang Denk932394a2005-08-17 12:55:25 +02002107/**
2108 * nand_erase_intern - [NAND Interface] erase block(s)
2109 * @mtd: MTD device structure
2110 * @instr: erase instruction
2111 * @allowbbt: allow erasing the bbt area
2112 *
2113 * Erase one ore more blocks
2114 */
2115int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2116{
2117 int page, len, status, pages_per_block, ret, chipnr;
2118 struct nand_chip *this = mtd->priv;
2119
2120 DEBUG (MTD_DEBUG_LEVEL3,
2121 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2122
2123 /* Start address must align on block boundary */
2124 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2125 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2126 return -EINVAL;
2127 }
2128
2129 /* Length must align on block boundary */
2130 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2131 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2132 return -EINVAL;
2133 }
2134
2135 /* Do not allow erase past end of device */
2136 if ((instr->len + instr->addr) > mtd->size) {
2137 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2138 return -EINVAL;
2139 }
2140
2141 instr->fail_addr = 0xffffffff;
2142
2143 /* Grab the lock and see if the device is available */
2144 nand_get_device (this, mtd, FL_ERASING);
2145
2146 /* Shift to get first page */
2147 page = (int) (instr->addr >> this->page_shift);
2148 chipnr = (int) (instr->addr >> this->chip_shift);
2149
2150 /* Calculate pages in each block */
2151 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2152
2153 /* Select the NAND device */
2154 this->select_chip(mtd, chipnr);
2155
2156 /* Check the WP bit */
2157 /* Check, if it is write protected */
2158 if (nand_check_wp(mtd)) {
2159 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2160 instr->state = MTD_ERASE_FAILED;
2161 goto erase_exit;
2162 }
2163
2164 /* Loop through the pages */
2165 len = instr->len;
2166
2167 instr->state = MTD_ERASING;
2168
2169 while (len) {
Markus Klotzbücherf9e02912006-03-06 13:45:42 +01002170#ifndef NAND_ALLOW_ERASE_ALL
Wolfgang Denk932394a2005-08-17 12:55:25 +02002171 /* Check if we have a bad block, we do not erase bad blocks ! */
2172 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2173 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2174 instr->state = MTD_ERASE_FAILED;
2175 goto erase_exit;
2176 }
Markus Klotzbücherf9e02912006-03-06 13:45:42 +01002177#endif
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002178 /* Invalidate the page cache, if we erase the block which contains
Wolfgang Denk932394a2005-08-17 12:55:25 +02002179 the current cached page */
2180 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2181 this->pagebuf = -1;
2182
2183 this->erase_cmd (mtd, page & this->pagemask);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002184
Wolfgang Denk932394a2005-08-17 12:55:25 +02002185 status = this->waitfunc (mtd, this, FL_ERASING);
2186
2187 /* See if block erase succeeded */
2188 if (status & 0x01) {
2189 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2190 instr->state = MTD_ERASE_FAILED;
2191 instr->fail_addr = (page << this->page_shift);
2192 goto erase_exit;
2193 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002194
Wolfgang Denk932394a2005-08-17 12:55:25 +02002195 /* Increment page address and decrement length */
2196 len -= (1 << this->phys_erase_shift);
2197 page += pages_per_block;
2198
2199 /* Check, if we cross a chip boundary */
2200 if (len && !(page & this->pagemask)) {
2201 chipnr++;
2202 this->select_chip(mtd, -1);
2203 this->select_chip(mtd, chipnr);
2204 }
2205 }
2206 instr->state = MTD_ERASE_DONE;
2207
2208erase_exit:
2209
2210 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2211 /* Do call back function */
2212 if (!ret)
2213 mtd_erase_callback(instr);
2214
2215 /* Deselect and wake up anyone waiting on the device */
2216 nand_release_device(mtd);
2217
2218 /* Return more or less happy */
2219 return ret;
2220}
2221
2222/**
2223 * nand_sync - [MTD Interface] sync
2224 * @mtd: MTD device structure
2225 *
2226 * Sync is actually a wait for chip ready function
2227 */
2228static void nand_sync (struct mtd_info *mtd)
2229{
2230 struct nand_chip *this = mtd->priv;
2231
2232 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2233
2234 /* Grab the lock and see if the device is available */
2235 nand_get_device (this, mtd, FL_SYNCING);
2236 /* Release it and go back */
2237 nand_release_device (mtd);
2238}
2239
2240
2241/**
2242 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2243 * @mtd: MTD device structure
2244 * @ofs: offset relative to mtd start
2245 */
2246static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2247{
2248 /* Check for invalid offset */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002249 if (ofs > mtd->size)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002250 return -EINVAL;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002251
Wolfgang Denk932394a2005-08-17 12:55:25 +02002252 return nand_block_checkbad (mtd, ofs, 1, 0);
2253}
2254
2255/**
2256 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2257 * @mtd: MTD device structure
2258 * @ofs: offset relative to mtd start
2259 */
2260static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2261{
2262 struct nand_chip *this = mtd->priv;
2263 int ret;
2264
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002265 if ((ret = nand_block_isbad(mtd, ofs))) {
2266 /* If it was bad already, return success and do nothing. */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002267 if (ret > 0)
2268 return 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002269 return ret;
2270 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002271
2272 return this->block_markbad(mtd, ofs);
2273}
2274
2275/**
2276 * nand_scan - [NAND Interface] Scan for the NAND device
2277 * @mtd: MTD device structure
2278 * @maxchips: Number of chips to scan for
2279 *
2280 * This fills out all the not initialized function pointers
2281 * with the defaults.
2282 * The flash ID is read and the mtd/chip structures are
2283 * filled with the appropriate values. Buffers are allocated if
2284 * they are not provided by the board driver
2285 *
2286 */
2287int nand_scan (struct mtd_info *mtd, int maxchips)
2288{
2289 int i, j, nand_maf_id, nand_dev_id, busw;
2290 struct nand_chip *this = mtd->priv;
2291
2292 /* Get buswidth to select the correct functions*/
2293 busw = this->options & NAND_BUSWIDTH_16;
2294
2295 /* check for proper chip_delay setup, set 20us if not */
2296 if (!this->chip_delay)
2297 this->chip_delay = 20;
2298
2299 /* check, if a user supplied command function given */
2300 if (this->cmdfunc == NULL)
2301 this->cmdfunc = nand_command;
2302
2303 /* check, if a user supplied wait function given */
2304 if (this->waitfunc == NULL)
2305 this->waitfunc = nand_wait;
2306
2307 if (!this->select_chip)
2308 this->select_chip = nand_select_chip;
2309 if (!this->write_byte)
2310 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2311 if (!this->read_byte)
2312 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2313 if (!this->write_word)
2314 this->write_word = nand_write_word;
2315 if (!this->read_word)
2316 this->read_word = nand_read_word;
2317 if (!this->block_bad)
2318 this->block_bad = nand_block_bad;
2319 if (!this->block_markbad)
2320 this->block_markbad = nand_default_block_markbad;
2321 if (!this->write_buf)
2322 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2323 if (!this->read_buf)
2324 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2325 if (!this->verify_buf)
2326 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2327 if (!this->scan_bbt)
2328 this->scan_bbt = nand_default_bbt;
2329
2330 /* Select the device */
2331 this->select_chip(mtd, 0);
2332
2333 /* Send the command for reading device ID */
2334 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2335
2336 /* Read manufacturer and device IDs */
2337 nand_maf_id = this->read_byte(mtd);
2338 nand_dev_id = this->read_byte(mtd);
2339
2340 /* Print and store flash device information */
2341 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002342
2343 if (nand_dev_id != nand_flash_ids[i].id)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002344 continue;
2345
2346 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2347 this->chipsize = nand_flash_ids[i].chipsize << 20;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002348
Wolfgang Denk932394a2005-08-17 12:55:25 +02002349 /* New devices have all the information in additional id bytes */
2350 if (!nand_flash_ids[i].pagesize) {
2351 int extid;
2352 /* The 3rd id byte contains non relevant data ATM */
2353 extid = this->read_byte(mtd);
2354 /* The 4th id byte is the important one */
2355 extid = this->read_byte(mtd);
2356 /* Calc pagesize */
2357 mtd->oobblock = 1024 << (extid & 0x3);
2358 extid >>= 2;
2359 /* Calc oobsize */
Stefan Roesecd1d9372007-01-05 11:46:05 +01002360 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->oobblock / 512);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002361 extid >>= 2;
2362 /* Calc blocksize. Blocksize is multiples of 64KiB */
2363 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2364 extid >>= 2;
2365 /* Get buswidth information */
2366 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002367
Wolfgang Denk932394a2005-08-17 12:55:25 +02002368 } else {
2369 /* Old devices have this data hardcoded in the
2370 * device id table */
2371 mtd->erasesize = nand_flash_ids[i].erasesize;
2372 mtd->oobblock = nand_flash_ids[i].pagesize;
2373 mtd->oobsize = mtd->oobblock / 32;
2374 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2375 }
2376
2377 /* Check, if buswidth is correct. Hardware drivers should set
2378 * this correct ! */
2379 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2380 printk (KERN_INFO "NAND device: Manufacturer ID:"
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002381 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
Wolfgang Denk932394a2005-08-17 12:55:25 +02002382 nand_manuf_ids[i].name , mtd->name);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002383 printk (KERN_WARNING
2384 "NAND bus width %d instead %d bit\n",
Wolfgang Denk932394a2005-08-17 12:55:25 +02002385 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2386 busw ? 16 : 8);
2387 this->select_chip(mtd, -1);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002388 return 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002389 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002390
2391 /* Calculate the address shift from the page size */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002392 this->page_shift = ffs(mtd->oobblock) - 1;
2393 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2394 this->chip_shift = ffs(this->chipsize) - 1;
2395
2396 /* Set the bad block position */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002397 this->badblockpos = mtd->oobblock > 512 ?
Wolfgang Denk932394a2005-08-17 12:55:25 +02002398 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2399
2400 /* Get chip options, preserve non chip based options */
2401 this->options &= ~NAND_CHIPOPTIONS_MSK;
2402 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2403 /* Set this as a default. Board drivers can override it, if neccecary */
2404 this->options |= NAND_NO_AUTOINCR;
2405 /* Check if this is a not a samsung device. Do not clear the options
2406 * for chips which are not having an extended id.
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002407 */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002408 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2409 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002410
Wolfgang Denk932394a2005-08-17 12:55:25 +02002411 /* Check for AND chips with 4 page planes */
2412 if (this->options & NAND_4PAGE_ARRAY)
2413 this->erase_cmd = multi_erase_cmd;
2414 else
2415 this->erase_cmd = single_erase_cmd;
2416
2417 /* Do not replace user supplied command function ! */
2418 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2419 this->cmdfunc = nand_command_lp;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002420
Wolfgang Denk932394a2005-08-17 12:55:25 +02002421 /* Try to identify manufacturer */
2422 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2423 if (nand_manuf_ids[j].id == nand_maf_id)
2424 break;
2425 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002426 break;
2427 }
2428
2429 if (!nand_flash_ids[i].name) {
Stefan Roese5c912cb2006-10-07 11:36:51 +02002430#ifndef CFG_NAND_QUIET_TEST
Wolfgang Denk932394a2005-08-17 12:55:25 +02002431 printk (KERN_WARNING "No NAND device found!!!\n");
Stefan Roese5c912cb2006-10-07 11:36:51 +02002432#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +02002433 this->select_chip(mtd, -1);
2434 return 1;
2435 }
2436
2437 for (i=1; i < maxchips; i++) {
2438 this->select_chip(mtd, i);
2439
2440 /* Send the command for reading device ID */
2441 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2442
2443 /* Read manufacturer and device IDs */
2444 if (nand_maf_id != this->read_byte(mtd) ||
2445 nand_dev_id != this->read_byte(mtd))
2446 break;
2447 }
2448 if (i > 1)
2449 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002450
Wolfgang Denk932394a2005-08-17 12:55:25 +02002451 /* Allocate buffers, if neccecary */
2452 if (!this->oob_buf) {
2453 size_t len;
2454 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2455 this->oob_buf = kmalloc (len, GFP_KERNEL);
2456 if (!this->oob_buf) {
2457 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2458 return -ENOMEM;
2459 }
2460 this->options |= NAND_OOBBUF_ALLOC;
2461 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002462
Wolfgang Denk932394a2005-08-17 12:55:25 +02002463 if (!this->data_buf) {
2464 size_t len;
2465 len = mtd->oobblock + mtd->oobsize;
2466 this->data_buf = kmalloc (len, GFP_KERNEL);
2467 if (!this->data_buf) {
2468 if (this->options & NAND_OOBBUF_ALLOC)
2469 kfree (this->oob_buf);
2470 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2471 return -ENOMEM;
2472 }
2473 this->options |= NAND_DATABUF_ALLOC;
2474 }
2475
2476 /* Store the number of chips and calc total size for mtd */
2477 this->numchips = i;
2478 mtd->size = i * this->chipsize;
2479 /* Convert chipsize to number of pages per chip -1. */
2480 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2481 /* Preset the internal oob buffer */
2482 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2483
2484 /* If no default placement scheme is given, select an
2485 * appropriate one */
2486 if (!this->autooob) {
2487 /* Select the appropriate default oob placement scheme for
2488 * placement agnostic filesystems */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002489 switch (mtd->oobsize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002490 case 8:
2491 this->autooob = &nand_oob_8;
2492 break;
2493 case 16:
2494 this->autooob = &nand_oob_16;
2495 break;
2496 case 64:
2497 this->autooob = &nand_oob_64;
2498 break;
Sergei Poselenov248ae5c2008-06-06 15:42:43 +02002499 case 128:
2500 this->autooob = &nand_oob_128;
2501 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002502 default:
2503 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2504 mtd->oobsize);
2505/* BUG(); */
2506 }
2507 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002508
Wolfgang Denk932394a2005-08-17 12:55:25 +02002509 /* The number of bytes available for the filesystem to place fs dependend
2510 * oob data */
Troy Kisky84c01d32007-09-24 16:41:43 -07002511 mtd->oobavail = 0;
2512 for (i=0; this->autooob->oobfree[i][1]; i++)
2513 mtd->oobavail += this->autooob->oobfree[i][1];
Wolfgang Denk932394a2005-08-17 12:55:25 +02002514
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002515 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +02002516 * check ECC mode, default to software
2517 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002518 * fallback to software ECC
Wolfgang Denk932394a2005-08-17 12:55:25 +02002519 */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002520 this->eccsize = 256; /* set default eccsize */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002521 this->eccbytes = 3;
2522
2523 switch (this->eccmode) {
2524 case NAND_ECC_HW12_2048:
2525 if (mtd->oobblock < 2048) {
2526 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2527 mtd->oobblock);
2528 this->eccmode = NAND_ECC_SOFT;
2529 this->calculate_ecc = nand_calculate_ecc;
2530 this->correct_data = nand_correct_data;
2531 } else
2532 this->eccsize = 2048;
2533 break;
2534
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002535 case NAND_ECC_HW3_512:
2536 case NAND_ECC_HW6_512:
2537 case NAND_ECC_HW8_512:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002538 if (mtd->oobblock == 256) {
2539 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2540 this->eccmode = NAND_ECC_SOFT;
2541 this->calculate_ecc = nand_calculate_ecc;
2542 this->correct_data = nand_correct_data;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002543 } else
Wolfgang Denk932394a2005-08-17 12:55:25 +02002544 this->eccsize = 512; /* set eccsize to 512 */
2545 break;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002546
Wolfgang Denk932394a2005-08-17 12:55:25 +02002547 case NAND_ECC_HW3_256:
2548 break;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002549
2550 case NAND_ECC_NONE:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002551 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2552 this->eccmode = NAND_ECC_NONE;
2553 break;
2554
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002555 case NAND_ECC_SOFT:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002556 this->calculate_ecc = nand_calculate_ecc;
2557 this->correct_data = nand_correct_data;
2558 break;
2559
2560 default:
2561 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2562/* BUG(); */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002563 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002564
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002565 /* Check hardware ecc function availability and adjust number of ecc bytes per
Wolfgang Denk932394a2005-08-17 12:55:25 +02002566 * calculation step
2567 */
2568 switch (this->eccmode) {
2569 case NAND_ECC_HW12_2048:
2570 this->eccbytes += 4;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002571 case NAND_ECC_HW8_512:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002572 this->eccbytes += 2;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002573 case NAND_ECC_HW6_512:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002574 this->eccbytes += 3;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002575 case NAND_ECC_HW3_512:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002576 case NAND_ECC_HW3_256:
2577 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2578 break;
2579 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2580/* BUG(); */
2581 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002582
Wolfgang Denk932394a2005-08-17 12:55:25 +02002583 mtd->eccsize = this->eccsize;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002584
Wolfgang Denk932394a2005-08-17 12:55:25 +02002585 /* Set the number of read / write steps for one page to ensure ECC generation */
2586 switch (this->eccmode) {
2587 case NAND_ECC_HW12_2048:
2588 this->eccsteps = mtd->oobblock / 2048;
2589 break;
2590 case NAND_ECC_HW3_512:
2591 case NAND_ECC_HW6_512:
2592 case NAND_ECC_HW8_512:
2593 this->eccsteps = mtd->oobblock / 512;
2594 break;
2595 case NAND_ECC_HW3_256:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002596 case NAND_ECC_SOFT:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002597 this->eccsteps = mtd->oobblock / 256;
2598 break;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002599
2600 case NAND_ECC_NONE:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002601 this->eccsteps = 1;
2602 break;
2603 }
2604
2605/* XXX U-BOOT XXX */
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002606#if 0
Wolfgang Denk932394a2005-08-17 12:55:25 +02002607 /* Initialize state, waitqueue and spinlock */
2608 this->state = FL_READY;
2609 init_waitqueue_head (&this->wq);
2610 spin_lock_init (&this->chip_lock);
2611#endif
2612
2613 /* De-select the device */
2614 this->select_chip(mtd, -1);
2615
2616 /* Invalidate the pagebuffer reference */
2617 this->pagebuf = -1;
2618
2619 /* Fill in remaining MTD driver data */
2620 mtd->type = MTD_NANDFLASH;
2621 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2622 mtd->ecctype = MTD_ECC_SW;
2623 mtd->erase = nand_erase;
2624 mtd->point = NULL;
2625 mtd->unpoint = NULL;
2626 mtd->read = nand_read;
2627 mtd->write = nand_write;
2628 mtd->read_ecc = nand_read_ecc;
2629 mtd->write_ecc = nand_write_ecc;
2630 mtd->read_oob = nand_read_oob;
2631 mtd->write_oob = nand_write_oob;
2632/* XXX U-BOOT XXX */
2633#if 0
2634 mtd->readv = NULL;
2635 mtd->writev = nand_writev;
2636 mtd->writev_ecc = nand_writev_ecc;
2637#endif
2638 mtd->sync = nand_sync;
2639/* XXX U-BOOT XXX */
2640#if 0
2641 mtd->lock = NULL;
2642 mtd->unlock = NULL;
2643 mtd->suspend = NULL;
2644 mtd->resume = NULL;
2645#endif
2646 mtd->block_isbad = nand_block_isbad;
2647 mtd->block_markbad = nand_block_markbad;
2648
2649 /* and make the autooob the default one */
2650 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2651/* XXX U-BOOT XXX */
2652#if 0
2653 mtd->owner = THIS_MODULE;
2654#endif
2655 /* Build bad block table */
2656 return this->scan_bbt (mtd);
2657}
2658
2659/**
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002660 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk932394a2005-08-17 12:55:25 +02002661 * @mtd: MTD device structure
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002662 */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002663void nand_release (struct mtd_info *mtd)
2664{
2665 struct nand_chip *this = mtd->priv;
2666
2667#ifdef CONFIG_MTD_PARTITIONS
2668 /* Deregister partitions */
2669 del_mtd_partitions (mtd);
2670#endif
2671 /* Deregister the device */
2672/* XXX U-BOOT XXX */
2673#if 0
2674 del_mtd_device (mtd);
2675#endif
2676 /* Free bad block table memory, if allocated */
2677 if (this->bbt)
2678 kfree (this->bbt);
2679 /* Buffer allocated by nand_scan ? */
2680 if (this->options & NAND_OOBBUF_ALLOC)
2681 kfree (this->oob_buf);
2682 /* Buffer allocated by nand_scan ? */
2683 if (this->options & NAND_DATABUF_ALLOC)
2684 kfree (this->data_buf);
2685}
2686
2687#endif