blob: 6416d1529e9d1440d4f89042cc2502ff1006b36f [file] [log] [blame]
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +02008 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020011 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Wolfgang Denka1be4762008-05-20 16:00:29 +020013 * 2002 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020014 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020015 * 02-08-2004 tglx: support for strange chips, which cannot auto increment
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +020024 *
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +020032 * David Woodhouse for adding multichip support
33 *
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +020071#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020072
73#include <common.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010074
Jon Loeliger82ecaad2007-07-09 17:39:42 -050075#if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
Wolfgang Denk2f9b7e42005-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 Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200110 40, 41, 42, 43, 44, 45, 46, 47,
111 48, 49, 50, 51, 52, 53, 54, 55,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200112 56, 57, 58, 59, 60, 61, 62, 63},
113 .oobfree = { {2, 38} }
114};
115
Sergei Poselenov04fbaa02008-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 Denk2f9b7e42005-08-17 12:55:25 +0200127};
128
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200129/* This is used for padding purposes in nand_write_oob */
130static u_char *ffchars;
131
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200161static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200166
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200172 *
173 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk2f9b7e42005-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 Denk9a08dd12005-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 Poselenov04fbaa02008-06-06 15:42:43 +0200200 if (ffchars) {
201 kfree(ffchars);
202 ffchars = NULL;
203 }
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100204}
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200236 * Default read function for 16bit buswith with
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200263 * Default read function for 16bit buswith without
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200277 * Default write function for 16bit buswith without
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200298 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200327 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200344 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200377
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200378 for (i=0; i<len; i++)
379 writew(p[i], this->IO_ADDR_W);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200380
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200381}
382
383/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200384 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200403 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200430 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-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 Knobloch9e2aeaf2007-05-05 07:04:42 +0200438 page = (int)(ofs >> this->page_shift) & this->pagemask;
439
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200440 if (getchip) {
Wolfgang Denk2f9b7e42005-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 Knobloch9e2aeaf2007-05-05 07:04:42 +0200448 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200449
450 if (this->options & NAND_BUSWIDTH_16) {
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200451 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page);
Wolfgang Denk2f9b7e42005-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 Knobloch9e2aeaf2007-05-05 07:04:42 +0200458 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200459 if (this->read_byte(mtd) != 0xff)
460 res = 1;
461 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200462
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200463 if (getchip) {
464 /* Deselect and wake up anyone waiting on the device */
465 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200466 }
467
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200485
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200493
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200499/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200500 * nand_check_wp - [GENERIC] check if the chip is write protected
501 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200502 * Check, if the device is write protected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200503 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200504 * The function expects, that the device is already selected
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200511 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200512}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100513
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200527
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200528 if (!this->bbt)
529 return this->block_bad(mtd, ofs, getchip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200530
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200595
596 /*
597 * program and erase have their own busy handlers
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200598 * status and sequential in needs no delay
599 */
600 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200601
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200610 if (this->dev_ready)
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200619 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200620 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200621 /*
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200628 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200629 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200630
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200659
660
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200678 }
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200689
690 /*
691 * program and erase have their own busy handlers
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200692 * status and sequential in needs no delay
693 */
694 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200695
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200706 if (this->dev_ready)
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200723
724 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200725 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200726 /*
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200733 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200734 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200735
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200747 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200759 /*
760 * Grab the lock and see if the device is available
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200772
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200780 }
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200799 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200809
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200821 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200822 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
823
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200824 while (time_before(jiffies, timeo)) {
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200831 break;
Wolfgang Denk2f9b7e42005-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 Denk9a08dd12005-11-02 14:29:12 +0100846 unsigned long timeo;
847
848 if (state == FL_ERASING)
Wolfgang Denka1be4762008-05-20 16:00:29 +0200849 timeo = (CFG_HZ * 400) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100850 else
Stefan Roese3427cf62006-11-28 11:04:45 +0100851 timeo = (CFG_HZ * 20) / 1000;
Wolfgang Denk9a08dd12005-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 Sieka6eed2ab2006-02-24 09:37:22 +0100858 reset_timer();
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100859
860 while (1) {
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100861 if (get_timer(0) > timeo) {
862 printf("Timeout!");
Stefan Roeseef26d242006-11-27 17:22:19 +0100863 return 0x01;
864 }
Wolfgang Denk9a08dd12005-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 Sieka582f1a32006-03-05 18:57:33 +0100874#ifdef PPCHAMELON_NAND_TIMER_HACK
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100875 reset_timer();
876 while (get_timer(0) < 10);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100877#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100878
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100879 return this->read_byte(mtd);
Wolfgang Denk2f9b7e42005-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 Denka1be4762008-05-20 16:00:29 +0200887 * @page: startpage inside the chip, must be called with (page & this->pagemask)
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200898static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200899 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
900{
Wolfgang Denka1be4762008-05-20 16:00:29 +0200901 int i, status;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200902 u_char ecc_code[NAND_MAX_OOBSIZE];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200903 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200904 uint *oob_config = oobsel->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200905 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
906 int eccbytes = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200907
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200908 /* FIXME: Enable cached programming */
909 cached = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200910
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200921
Wolfgang Denk2f9b7e42005-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 Kisky6dba1dd2007-09-24 16:41:43 -0700942 * the ecc code must be written immediately after
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200950
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200954 else
Wolfgang Denk2f9b7e42005-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) {
Scott Wooddf83c472008-06-20 12:38:57 -0500965 MTDDEBUG (MTD_DEBUG_LEVEL0,
966 "%s: Failed write, page 0x%08x, ",
967 __FUNCTION__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200968 return -EIO;
969 }
970 } else {
971 /* FIXME: Implement cached programming ! */
972 /* wait until cache is ready*/
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200973 /* status = this->waitfunc (mtd, this, FL_CACHEDRPG); */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200974 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200975 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200976}
977
978#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
979/**
980 * nand_verify_pages - [GENERIC] verify the chip contents after a write
981 * @mtd: MTD device structure
982 * @this: NAND chip structure
Wolfgang Denka1be4762008-05-20 16:00:29 +0200983 * @page: startpage inside the chip, must be called with (page & this->pagemask)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200984 * @numpages: number of pages to verify
985 * @oob_buf: out of band data buffer
986 * @oobsel: out of band selecttion structre
987 * @chipnr: number of the current chip
988 * @oobmode: 1 = full buffer verify, 0 = ecc only
989 *
990 * The NAND device assumes that it is always writing to a cleanly erased page.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200991 * Hence, it performs its internal write verification only on bits that
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200992 * transitioned from 1 to 0. The device does NOT verify the whole page on a
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200993 * byte by byte basis. It is possible that the page was not completely erased
994 * or the page is becoming unusable due to wear. The read with ECC would catch
995 * the error later when the ECC page check fails, but we would rather catch
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200996 * it early in the page write stage. Better to write no data than invalid data.
997 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200998static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200999 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
1000{
Wolfgang Denka1be4762008-05-20 16:00:29 +02001001 int i, j, datidx = 0, oobofs = 0, res = -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001002 int eccsteps = this->eccsteps;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001003 int hweccbytes;
Wolfgang Denka1be4762008-05-20 16:00:29 +02001004 u_char oobdata[64];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001005
1006 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
1007
1008 /* Send command to read back the first page */
1009 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
1010
1011 for(;;) {
1012 for (j = 0; j < eccsteps; j++) {
1013 /* Loop through and verify the data */
1014 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
Scott Wooddf83c472008-06-20 12:38:57 -05001015 MTDDEBUG (MTD_DEBUG_LEVEL0, "%s: "
1016 "Failed write verify, page 0x%08x ",
1017 __FUNCTION__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001018 goto out;
1019 }
1020 datidx += mtd->eccsize;
1021 /* Have we a hw generator layout ? */
1022 if (!hweccbytes)
1023 continue;
1024 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
Scott Wooddf83c472008-06-20 12:38:57 -05001025 MTDDEBUG (MTD_DEBUG_LEVEL0, "%s: "
1026 "Failed write verify, page 0x%08x ",
1027 __FUNCTION__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001028 goto out;
1029 }
1030 oobofs += hweccbytes;
1031 }
1032
1033 /* check, if we must compare all data or if we just have to
1034 * compare the ecc bytes
1035 */
1036 if (oobmode) {
1037 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
Scott Wooddf83c472008-06-20 12:38:57 -05001038 MTDDEBUG (MTD_DEBUG_LEVEL0, "%s: "
1039 "Failed write verify, page 0x%08x ",
1040 __FUNCTION__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001041 goto out;
1042 }
1043 } else {
1044 /* Read always, else autoincrement fails */
1045 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1046
1047 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1048 int ecccnt = oobsel->eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001049
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001050 for (i = 0; i < ecccnt; i++) {
1051 int idx = oobsel->eccpos[i];
1052 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
Scott Wooddf83c472008-06-20 12:38:57 -05001053 MTDDEBUG (MTD_DEBUG_LEVEL0,
Wolfgang Denka1be4762008-05-20 16:00:29 +02001054 "%s: Failed ECC write "
Scott Wooddf83c472008-06-20 12:38:57 -05001055 "verify, page 0x%08x, "
1056 "%6i bytes were succesful\n",
1057 __FUNCTION__, page, i);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001058 goto out;
1059 }
1060 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001061 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001062 }
1063 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1064 page++;
1065 numpages--;
1066
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001067 /* Apply delay or wait for ready/busy pin
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001068 * Do this before the AUTOINCR check, so no problems
1069 * arise if a chip which does auto increment
1070 * is marked as NOAUTOINCR by the board driver.
1071 * Do this also before returning, so the chip is
1072 * ready for the next command.
1073 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001074 if (!this->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001075 udelay (this->chip_delay);
1076 else
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001077 while (!this->dev_ready(mtd));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001078
1079 /* All done, return happy */
1080 if (!numpages)
1081 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001082
1083
1084 /* Check, if the chip supports auto page increment */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001085 if (!NAND_CANAUTOINCR(this))
1086 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1087 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001088 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001089 * Terminate the read command. We come here in case of an error
1090 * So we must issue a reset command.
1091 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001092out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001093 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1094 return res;
1095}
1096#endif
1097
1098/**
1099 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1100 * @mtd: MTD device structure
1101 * @from: offset to read from
1102 * @len: number of bytes to read
1103 * @retlen: pointer to variable to store the number of read bytes
1104 * @buf: the databuffer to put data
1105 *
1106 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1107*/
1108static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1109{
1110 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001111}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001112
1113
1114/**
1115 * nand_read_ecc - [MTD Interface] Read data with ECC
1116 * @mtd: MTD device structure
1117 * @from: offset to read from
1118 * @len: number of bytes to read
1119 * @retlen: pointer to variable to store the number of read bytes
1120 * @buf: the databuffer to put data
1121 * @oob_buf: filesystem supplied oob data buffer
1122 * @oobsel: oob selection structure
1123 *
1124 * NAND read with ECC
1125 */
1126static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1127 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1128{
1129 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1130 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1131 struct nand_chip *this = mtd->priv;
1132 u_char *data_poi, *oob_data = oob_buf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02001133 u_char ecc_calc[NAND_MAX_OOBSIZE];
1134 u_char ecc_code[NAND_MAX_OOBSIZE];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001135 int eccmode, eccsteps;
Wolfgang Denk7fa6e902006-03-11 22:53:33 +01001136 unsigned *oob_config;
1137 int datidx;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001138 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1139 int eccbytes;
1140 int compareecc = 1;
1141 int oobreadlen;
1142
1143
Scott Wooddf83c472008-06-20 12:38:57 -05001144 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n",
1145 (unsigned int) from, (int) len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001146
1147 /* Do not allow reads past end of device */
1148 if ((from + len) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05001149 MTDDEBUG (MTD_DEBUG_LEVEL0,
1150 "nand_read_ecc: Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001151 *retlen = 0;
1152 return -EINVAL;
1153 }
1154
1155 /* Grab the lock and see if the device is available */
1156 nand_get_device (this, mtd ,FL_READING);
1157
1158 /* use userspace supplied oobinfo, if zero */
1159 if (oobsel == NULL)
1160 oobsel = &mtd->oobinfo;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001161
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001162 /* Autoplace of oob data ? Use the default placement scheme */
1163 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1164 oobsel = this->autooob;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001165
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001166 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1167 oob_config = oobsel->eccpos;
1168
1169 /* Select the NAND device */
1170 chipnr = (int)(from >> this->chip_shift);
1171 this->select_chip(mtd, chipnr);
1172
1173 /* First we calculate the starting page */
1174 realpage = (int) (from >> this->page_shift);
1175 page = realpage & this->pagemask;
1176
1177 /* Get raw starting column */
1178 col = from & (mtd->oobblock - 1);
1179
1180 end = mtd->oobblock;
1181 ecc = this->eccsize;
1182 eccbytes = this->eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001183
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001184 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1185 compareecc = 0;
1186
1187 oobreadlen = mtd->oobsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001188 if (this->options & NAND_HWECC_SYNDROME)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001189 oobreadlen -= oobsel->eccbytes;
1190
1191 /* Loop until all data read */
1192 while (read < len) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001193
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001194 int aligned = (!col && (len - read) >= end);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001195 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001196 * If the read is not page aligned, we have to read into data buffer
1197 * due to ecc, else we read into return buffer direct
1198 */
1199 if (aligned)
1200 data_poi = &buf[read];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001201 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001202 data_poi = this->data_buf;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001203
1204 /* Check, if we have this page in the buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001205 *
1206 * FIXME: Make it work when we must provide oob data too,
1207 * check the usage of data_buf oob field
1208 */
1209 if (realpage == this->pagebuf && !oob_buf) {
1210 /* aligned read ? */
1211 if (aligned)
1212 memcpy (data_poi, this->data_buf, end);
1213 goto readdata;
1214 }
1215
1216 /* Check, if we must send the read command */
1217 if (sndcmd) {
1218 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1219 sndcmd = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001220 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001221
1222 /* get oob area, if we have no oob buffer from fs-driver */
1223 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1224 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1225 oob_data = &this->data_buf[end];
1226
1227 eccsteps = this->eccsteps;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001228
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001229 switch (eccmode) {
1230 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1231/* XXX U-BOOT XXX */
1232#if 0
1233 static unsigned long lastwhinge = 0;
1234 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1235 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1236 lastwhinge = jiffies;
1237 }
1238#else
1239 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1240#endif
1241 this->read_buf(mtd, data_poi, end);
1242 break;
1243 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001244
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001245 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1246 this->read_buf(mtd, data_poi, end);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001247 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001248 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001249 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001250
1251 default:
1252 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1253 this->enable_hwecc(mtd, NAND_ECC_READ);
1254 this->read_buf(mtd, &data_poi[datidx], ecc);
1255
1256 /* HW ecc with syndrome calculation must read the
1257 * syndrome from flash immidiately after the data */
1258 if (!compareecc) {
1259 /* Some hw ecc generators need to know when the
1260 * syndrome is read from flash */
1261 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1262 this->read_buf(mtd, &oob_data[i], eccbytes);
1263 /* We calc error correction directly, it checks the hw
1264 * generator for an error, reads back the syndrome and
1265 * does the error correction on the fly */
1266 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
Scott Wooddf83c472008-06-20 12:38:57 -05001267 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001268 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1269 ecc_failed++;
1270 }
1271 } else {
1272 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001273 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001274 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001275 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001276 }
1277
1278 /* read oobdata */
1279 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1280
1281 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1282 if (!compareecc)
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001283 goto readoob;
1284
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001285 /* Pick the ECC bytes out of the oob data */
1286 for (j = 0; j < oobsel->eccbytes; j++)
1287 ecc_code[j] = oob_data[oob_config[j]];
1288
1289 /* correct data, if neccecary */
1290 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1291 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001292
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001293 /* Get next chunk of ecc bytes */
1294 j += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001295
1296 /* Check, if we have a fs supplied oob-buffer,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001297 * This is the legacy mode. Used by YAFFS1
1298 * Should go away some day
1299 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001300 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001301 int *p = (int *)(&oob_data[mtd->oobsize]);
1302 p[i] = ecc_status;
1303 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001304
1305 if (ecc_status == -1) {
Scott Wooddf83c472008-06-20 12:38:57 -05001306 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1307 "Failed ECC read, page 0x%08x\n",
1308 page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001309 ecc_failed++;
1310 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001311 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001312
1313 readoob:
1314 /* check, if we have a fs supplied oob-buffer */
1315 if (oob_buf) {
1316 /* without autoplace. Legacy mode used by YAFFS1 */
1317 switch(oobsel->useecc) {
1318 case MTD_NANDECC_AUTOPLACE:
1319 case MTD_NANDECC_AUTOPL_USR:
1320 /* Walk through the autoplace chunks */
1321 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1322 int from = oobsel->oobfree[i][0];
1323 int num = oobsel->oobfree[i][1];
Troy Kisky6dba1dd2007-09-24 16:41:43 -07001324 memcpy(&oob_buf[oob+j], &oob_data[from], num);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001325 j+= num;
1326 }
1327 oob += mtd->oobavail;
1328 break;
1329 case MTD_NANDECC_PLACE:
1330 /* YAFFS1 legacy mode */
1331 oob_data += this->eccsteps * sizeof (int);
1332 default:
1333 oob_data += mtd->oobsize;
1334 }
1335 }
1336 readdata:
1337 /* Partial page read, transfer data into fs buffer */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001338 if (!aligned) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001339 for (j = col; j < end && read < len; j++)
1340 buf[read++] = data_poi[j];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001341 this->pagebuf = realpage;
1342 } else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001343 read += mtd->oobblock;
1344
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001345 /* Apply delay or wait for ready/busy pin
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001346 * Do this before the AUTOINCR check, so no problems
1347 * arise if a chip which does auto increment
1348 * is marked as NOAUTOINCR by the board driver.
1349 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001350 if (!this->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001351 udelay (this->chip_delay);
1352 else
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001353 while (!this->dev_ready(mtd));
1354
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001355 if (read == len)
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001356 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001357
1358 /* For subsequent reads align to page boundary. */
1359 col = 0;
1360 /* Increment page address */
1361 realpage++;
1362
1363 page = realpage & this->pagemask;
1364 /* Check, if we cross a chip boundary */
1365 if (!page) {
1366 chipnr++;
1367 this->select_chip(mtd, -1);
1368 this->select_chip(mtd, chipnr);
1369 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001370 /* Check, if the chip supports auto page increment
1371 * or if we have hit a block boundary.
1372 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001373 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001374 sndcmd = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001375 }
1376
1377 /* Deselect and wake up anyone waiting on the device */
1378 nand_release_device(mtd);
1379
1380 /*
1381 * Return success, if no ECC failures, else -EBADMSG
1382 * fs driver will take care of that, because
1383 * retlen == desired len and result == -EBADMSG
1384 */
1385 *retlen = read;
1386 return ecc_failed ? -EBADMSG : 0;
1387}
1388
1389/**
1390 * nand_read_oob - [MTD Interface] NAND read out-of-band
1391 * @mtd: MTD device structure
1392 * @from: offset to read from
1393 * @len: number of bytes to read
1394 * @retlen: pointer to variable to store the number of read bytes
1395 * @buf: the databuffer to put data
1396 *
1397 * NAND read out-of-band data from the spare area
1398 */
1399static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1400{
1401 int i, col, page, chipnr;
1402 struct nand_chip *this = mtd->priv;
1403 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1404
Scott Wooddf83c472008-06-20 12:38:57 -05001405 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n",
1406 (unsigned int) from, (int) len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001407
1408 /* Shift to get page */
1409 page = (int)(from >> this->page_shift);
1410 chipnr = (int)(from >> this->chip_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001411
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001412 /* Mask to get column */
1413 col = from & (mtd->oobsize - 1);
1414
1415 /* Initialize return length value */
1416 *retlen = 0;
1417
1418 /* Do not allow reads past end of device */
1419 if ((from + len) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05001420 MTDDEBUG (MTD_DEBUG_LEVEL0,
1421 "nand_read_oob: Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001422 *retlen = 0;
1423 return -EINVAL;
1424 }
1425
1426 /* Grab the lock and see if the device is available */
1427 nand_get_device (this, mtd , FL_READING);
1428
1429 /* Select the NAND device */
1430 this->select_chip(mtd, chipnr);
1431
1432 /* Send the read command */
1433 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001434 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001435 * Read the data, if we read more than one page
1436 * oob data, let the device transfer the data !
1437 */
1438 i = 0;
1439 while (i < len) {
1440 int thislen = mtd->oobsize - col;
1441 thislen = min_t(int, thislen, len);
1442 this->read_buf(mtd, &buf[i], thislen);
1443 i += thislen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001444
1445 /* Apply delay or wait for ready/busy pin
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001446 * Do this before the AUTOINCR check, so no problems
1447 * arise if a chip which does auto increment
1448 * is marked as NOAUTOINCR by the board driver.
1449 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001450 if (!this->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001451 udelay (this->chip_delay);
1452 else
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001453 while (!this->dev_ready(mtd));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001454
1455 /* Read more ? */
1456 if (i < len) {
1457 page++;
1458 col = 0;
1459
1460 /* Check, if we cross a chip boundary */
1461 if (!(page & this->pagemask)) {
1462 chipnr++;
1463 this->select_chip(mtd, -1);
1464 this->select_chip(mtd, chipnr);
1465 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001466
1467 /* Check, if the chip supports auto page increment
1468 * or if we have hit a block boundary.
1469 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001470 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1471 /* For subsequent page reads set offset to 0 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001472 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001473 }
1474 }
1475 }
1476
1477 /* Deselect and wake up anyone waiting on the device */
1478 nand_release_device(mtd);
1479
1480 /* Return happy */
1481 *retlen = len;
1482 return 0;
1483}
1484
1485/**
1486 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1487 * @mtd: MTD device structure
1488 * @buf: temporary buffer
1489 * @from: offset to read from
1490 * @len: number of bytes to read
1491 * @ooblen: number of oob data bytes to read
1492 *
1493 * Read raw data including oob into buffer
1494 */
1495int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1496{
1497 struct nand_chip *this = mtd->priv;
1498 int page = (int) (from >> this->page_shift);
1499 int chip = (int) (from >> this->chip_shift);
1500 int sndcmd = 1;
1501 int cnt = 0;
1502 int pagesize = mtd->oobblock + mtd->oobsize;
1503 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1504
1505 /* Do not allow reads past end of device */
1506 if ((from + len) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05001507 MTDDEBUG (MTD_DEBUG_LEVEL0,
1508 "nand_read_raw: Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001509 return -EINVAL;
1510 }
1511
1512 /* Grab the lock and see if the device is available */
1513 nand_get_device (this, mtd , FL_READING);
1514
1515 this->select_chip (mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001516
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001517 /* Add requested oob length */
1518 len += ooblen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001519
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001520 while (len) {
1521 if (sndcmd)
1522 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001523 sndcmd = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001524
1525 this->read_buf (mtd, &buf[cnt], pagesize);
1526
1527 len -= pagesize;
1528 cnt += pagesize;
1529 page++;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001530
1531 if (!this->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001532 udelay (this->chip_delay);
1533 else
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001534 while (!this->dev_ready(mtd));
1535
1536 /* Check, if the chip supports auto page increment */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001537 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1538 sndcmd = 1;
1539 }
1540
1541 /* Deselect and wake up anyone waiting on the device */
1542 nand_release_device(mtd);
1543 return 0;
1544}
1545
1546
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001547/**
1548 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001549 * @mtd: MTD device structure
1550 * @fsbuf: buffer given by fs driver
1551 * @oobsel: out of band selection structre
1552 * @autoplace: 1 = place given buffer into the oob bytes
1553 * @numpages: number of pages to prepare
1554 *
1555 * Return:
1556 * 1. Filesystem buffer available and autoplacement is off,
1557 * return filesystem buffer
1558 * 2. No filesystem buffer or autoplace is off, return internal
1559 * buffer
1560 * 3. Filesystem buffer is given and autoplace selected
1561 * put data from fs buffer into internal buffer and
1562 * retrun internal buffer
1563 *
1564 * Note: The internal buffer is filled with 0xff. This must
1565 * be done only once, when no autoplacement happens
1566 * Autoplacement sets the buffer dirty flag, which
1567 * forces the 0xff fill before using the buffer again.
1568 *
1569*/
1570static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1571 int autoplace, int numpages)
1572{
1573 struct nand_chip *this = mtd->priv;
1574 int i, len, ofs;
1575
1576 /* Zero copy fs supplied buffer */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001577 if (fsbuf && !autoplace)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001578 return fsbuf;
1579
1580 /* Check, if the buffer must be filled with ff again */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001581 if (this->oobdirty) {
1582 memset (this->oob_buf, 0xff,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001583 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1584 this->oobdirty = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001585 }
1586
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001587 /* If we have no autoplacement or no fs buffer use the internal one */
1588 if (!autoplace || !fsbuf)
1589 return this->oob_buf;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001590
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001591 /* Walk through the pages and place the data */
1592 this->oobdirty = 1;
1593 ofs = 0;
1594 while (numpages--) {
1595 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1596 int to = ofs + oobsel->oobfree[i][0];
1597 int num = oobsel->oobfree[i][1];
1598 memcpy (&this->oob_buf[to], fsbuf, num);
1599 len += num;
1600 fsbuf += num;
1601 }
1602 ofs += mtd->oobavail;
1603 }
1604 return this->oob_buf;
1605}
1606
1607#define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1608
1609/**
1610 * nand_write - [MTD Interface] compability function for nand_write_ecc
1611 * @mtd: MTD device structure
1612 * @to: offset to write to
1613 * @len: number of bytes to write
1614 * @retlen: pointer to variable to store the number of written bytes
1615 * @buf: the data to write
1616 *
1617 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1618 *
1619*/
1620static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1621{
1622 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1623}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001624
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001625/**
1626 * nand_write_ecc - [MTD Interface] NAND write with ECC
1627 * @mtd: MTD device structure
1628 * @to: offset to write to
1629 * @len: number of bytes to write
1630 * @retlen: pointer to variable to store the number of written bytes
1631 * @buf: the data to write
1632 * @eccbuf: filesystem supplied oob data buffer
1633 * @oobsel: oob selection structure
1634 *
1635 * NAND write with ECC
1636 */
1637static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1638 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1639{
1640 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1641 int autoplace = 0, numpages, totalpages;
1642 struct nand_chip *this = mtd->priv;
1643 u_char *oobbuf, *bufstart;
1644 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1645
Scott Wooddf83c472008-06-20 12:38:57 -05001646 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n",
1647 (unsigned int) to, (int) len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001648
1649 /* Initialize retlen, in case of early exit */
1650 *retlen = 0;
1651
1652 /* Do not allow write past end of device */
1653 if ((to + len) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05001654 MTDDEBUG (MTD_DEBUG_LEVEL0,
1655 "nand_write_ecc: Attempt to write past end of page\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001656 return -EINVAL;
1657 }
1658
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001659 /* reject writes, which are not page aligned */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001660 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1661 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1662 return -EINVAL;
1663 }
1664
1665 /* Grab the lock and see if the device is available */
1666 nand_get_device (this, mtd, FL_WRITING);
1667
1668 /* Calculate chipnr */
1669 chipnr = (int)(to >> this->chip_shift);
1670 /* Select the NAND device */
1671 this->select_chip(mtd, chipnr);
1672
1673 /* Check, if it is write protected */
Troy Kisky6dba1dd2007-09-24 16:41:43 -07001674 if (nand_check_wp(mtd)) {
1675 printk (KERN_NOTICE "nand_write_ecc: Device is write protected\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001676 goto out;
Troy Kisky6dba1dd2007-09-24 16:41:43 -07001677 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001678
1679 /* if oobsel is NULL, use chip defaults */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001680 if (oobsel == NULL)
1681 oobsel = &mtd->oobinfo;
1682
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001683 /* Autoplace of oob data ? Use the default placement scheme */
1684 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1685 oobsel = this->autooob;
1686 autoplace = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001687 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001688 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1689 autoplace = 1;
1690
1691 /* Setup variables and oob buffer */
1692 totalpages = len >> this->page_shift;
1693 page = (int) (to >> this->page_shift);
1694 /* Invalidate the page cache, if we write to the cached page */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001695 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001696 this->pagebuf = -1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001697
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001698 /* Set it relative to chip */
1699 page &= this->pagemask;
1700 startpage = page;
1701 /* Calc number of pages we can write in one go */
1702 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1703 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1704 bufstart = (u_char *)buf;
1705
1706 /* Loop until all data is written */
1707 while (written < len) {
1708
1709 this->data_poi = (u_char*) &buf[written];
1710 /* Write one page. If this is the last page to write
1711 * or the last page in this block, then use the
1712 * real pageprogram command, else select cached programming
1713 * if supported by the chip.
1714 */
1715 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1716 if (ret) {
Scott Wooddf83c472008-06-20 12:38:57 -05001717 MTDDEBUG (MTD_DEBUG_LEVEL0,
1718 "nand_write_ecc: write_page failed %d\n", ret);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001719 goto out;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001720 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001721 /* Next oob page */
1722 oob += mtd->oobsize;
1723 /* Update written bytes count */
1724 written += mtd->oobblock;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001725 if (written == len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001726 goto cmp;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001727
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001728 /* Increment page address */
1729 page++;
1730
1731 /* Have we hit a block boundary ? Then we have to verify and
1732 * if verify is ok, we have to setup the oob buffer for
1733 * the next pages.
1734 */
1735 if (!(page & (ppblock - 1))){
1736 int ofs;
1737 this->data_poi = bufstart;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001738 ret = nand_verify_pages (mtd, this, startpage,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001739 page - startpage,
1740 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1741 if (ret) {
Scott Wooddf83c472008-06-20 12:38:57 -05001742 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: "
1743 "verify_pages failed %d\n", ret);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001744 goto out;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001745 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001746 *retlen = written;
Nick Spence20362002006-09-30 00:32:59 -07001747 bufstart = (u_char*) &buf[written];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001748
1749 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1750 if (eccbuf)
1751 eccbuf += (page - startpage) * ofs;
1752 totalpages -= page - startpage;
1753 numpages = min (totalpages, ppblock);
1754 page &= this->pagemask;
1755 startpage = page;
Markus Klotzbücher85678e22006-03-06 13:45:42 +01001756 oob = 0;
1757 this->oobdirty = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001758 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001759 autoplace, numpages);
1760 /* Check, if we cross a chip boundary */
1761 if (!page) {
1762 chipnr++;
1763 this->select_chip(mtd, -1);
1764 this->select_chip(mtd, chipnr);
1765 }
1766 }
1767 }
1768 /* Verify the remaining pages */
1769cmp:
1770 this->data_poi = bufstart;
Wolfgang Denka1be4762008-05-20 16:00:29 +02001771 ret = nand_verify_pages (mtd, this, startpage, totalpages,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001772 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1773 if (!ret)
1774 *retlen = written;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001775 else
Scott Wooddf83c472008-06-20 12:38:57 -05001776 MTDDEBUG (MTD_DEBUG_LEVEL0,
1777 "nand_write_ecc: verify_pages failed %d\n", ret);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001778
1779out:
1780 /* Deselect and wake up anyone waiting on the device */
1781 nand_release_device(mtd);
1782
1783 return ret;
1784}
1785
1786
1787/**
1788 * nand_write_oob - [MTD Interface] NAND write out-of-band
1789 * @mtd: MTD device structure
1790 * @to: offset to write to
1791 * @len: number of bytes to write
1792 * @retlen: pointer to variable to store the number of written bytes
1793 * @buf: the data to write
1794 *
1795 * NAND write out-of-band
1796 */
1797static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1798{
1799 int column, page, status, ret = -EIO, chipnr;
1800 struct nand_chip *this = mtd->priv;
1801
Scott Wooddf83c472008-06-20 12:38:57 -05001802 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1803 (unsigned int) to, (int) len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001804
1805 /* Shift to get page */
1806 page = (int) (to >> this->page_shift);
1807 chipnr = (int) (to >> this->chip_shift);
1808
1809 /* Mask to get column */
1810 column = to & (mtd->oobsize - 1);
1811
1812 /* Initialize return length value */
1813 *retlen = 0;
1814
1815 /* Do not allow write past end of page */
1816 if ((column + len) > mtd->oobsize) {
Scott Wooddf83c472008-06-20 12:38:57 -05001817 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1818 "Attempt to write past end of page\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001819 return -EINVAL;
1820 }
1821
1822 /* Grab the lock and see if the device is available */
1823 nand_get_device (this, mtd, FL_WRITING);
1824
1825 /* Select the NAND device */
1826 this->select_chip(mtd, chipnr);
1827
1828 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1829 in one of my DiskOnChip 2000 test units) will clear the whole
1830 data page too if we don't do this. I have no clue why, but
1831 I seem to have 'fixed' it in the doc2000 driver in
1832 August 1999. dwmw2. */
1833 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1834
1835 /* Check, if it is write protected */
1836 if (nand_check_wp(mtd))
1837 goto out;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001838
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001839 /* Invalidate the page cache, if we write to the cached page */
1840 if (page == this->pagebuf)
1841 this->pagebuf = -1;
1842
1843 if (NAND_MUST_PAD(this)) {
1844 /* Write out desired data */
1845 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02001846 if (!ffchars) {
1847 if (!(ffchars = kmalloc (mtd->oobsize, GFP_KERNEL))) {
Scott Wooddf83c472008-06-20 12:38:57 -05001848 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1849 "No memory for padding array, "
1850 "need %d bytes", mtd->oobsize);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02001851 ret = -ENOMEM;
1852 goto out;
1853 }
1854 memset(ffchars, 0xff, mtd->oobsize);
1855 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001856 /* prepad 0xff for partial programming */
1857 this->write_buf(mtd, ffchars, column);
1858 /* write data */
1859 this->write_buf(mtd, buf, len);
1860 /* postpad 0xff for partial programming */
1861 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1862 } else {
1863 /* Write out desired data */
1864 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1865 /* write data */
1866 this->write_buf(mtd, buf, len);
1867 }
1868 /* Send command to program the OOB data */
1869 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1870
1871 status = this->waitfunc (mtd, this, FL_WRITING);
1872
1873 /* See if device thinks it succeeded */
1874 if (status & 0x01) {
Scott Wooddf83c472008-06-20 12:38:57 -05001875 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1876 "Failed write, page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001877 ret = -EIO;
1878 goto out;
1879 }
1880 /* Return happy */
1881 *retlen = len;
1882
1883#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1884 /* Send command to read back the data */
1885 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1886
1887 if (this->verify_buf(mtd, buf, len)) {
Scott Wooddf83c472008-06-20 12:38:57 -05001888 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1889 "Failed write verify, page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001890 ret = -EIO;
1891 goto out;
1892 }
1893#endif
1894 ret = 0;
1895out:
1896 /* Deselect and wake up anyone waiting on the device */
1897 nand_release_device(mtd);
1898
1899 return ret;
1900}
1901
1902/* XXX U-BOOT XXX */
1903#if 0
1904/**
1905 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1906 * @mtd: MTD device structure
1907 * @vecs: the iovectors to write
1908 * @count: number of vectors
1909 * @to: offset to write to
1910 * @retlen: pointer to variable to store the number of written bytes
1911 *
1912 * NAND write with kvec. This just calls the ecc function
1913 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001914static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001915 loff_t to, size_t * retlen)
1916{
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001917 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001918}
1919
1920/**
1921 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1922 * @mtd: MTD device structure
1923 * @vecs: the iovectors to write
1924 * @count: number of vectors
1925 * @to: offset to write to
1926 * @retlen: pointer to variable to store the number of written bytes
1927 * @eccbuf: filesystem supplied oob data buffer
1928 * @oobsel: oob selection structure
1929 *
1930 * NAND write with iovec with ecc
1931 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001932static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001933 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1934{
1935 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1936 int oob, numpages, autoplace = 0, startpage;
1937 struct nand_chip *this = mtd->priv;
1938 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1939 u_char *oobbuf, *bufstart;
1940
1941 /* Preset written len for early exit */
1942 *retlen = 0;
1943
1944 /* Calculate total length of data */
1945 total_len = 0;
1946 for (i = 0; i < count; i++)
1947 total_len += (int) vecs[i].iov_len;
1948
Scott Wooddf83c472008-06-20 12:38:57 -05001949 MTDDEBUG (MTD_DEBUG_LEVEL3,
1950 "nand_writev: to = 0x%08x, len = %i, count = %ld\n",
1951 (unsigned int) to, (unsigned int) total_len, count);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001952
1953 /* Do not allow write past end of page */
1954 if ((to + total_len) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05001955 MTDDEBUG (MTD_DEBUG_LEVEL0,
1956 "nand_writev: Attempted write past end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001957 return -EINVAL;
1958 }
1959
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001960 /* reject writes, which are not page aligned */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001961 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1962 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1963 return -EINVAL;
1964 }
1965
1966 /* Grab the lock and see if the device is available */
1967 nand_get_device (this, mtd, FL_WRITING);
1968
1969 /* Get the current chip-nr */
1970 chipnr = (int) (to >> this->chip_shift);
1971 /* Select the NAND device */
1972 this->select_chip(mtd, chipnr);
1973
1974 /* Check, if it is write protected */
1975 if (nand_check_wp(mtd))
1976 goto out;
1977
1978 /* if oobsel is NULL, use chip defaults */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001979 if (oobsel == NULL)
1980 oobsel = &mtd->oobinfo;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001981
1982 /* Autoplace of oob data ? Use the default placement scheme */
1983 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1984 oobsel = this->autooob;
1985 autoplace = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001986 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001987 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1988 autoplace = 1;
1989
1990 /* Setup start page */
1991 page = (int) (to >> this->page_shift);
1992 /* Invalidate the page cache, if we write to the cached page */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001993 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001994 this->pagebuf = -1;
1995
1996 startpage = page & this->pagemask;
1997
1998 /* Loop until all kvec' data has been written */
1999 len = 0;
2000 while (count) {
2001 /* If the given tuple is >= pagesize then
2002 * write it out from the iov
2003 */
2004 if ((vecs->iov_len - len) >= mtd->oobblock) {
2005 /* Calc number of pages we can write
2006 * out of this iov in one go */
2007 numpages = (vecs->iov_len - len) >> this->page_shift;
2008 /* Do not cross block boundaries */
2009 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
2010 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2011 bufstart = (u_char *)vecs->iov_base;
2012 bufstart += len;
2013 this->data_poi = bufstart;
2014 oob = 0;
2015 for (i = 1; i <= numpages; i++) {
2016 /* Write one page. If this is the last page to write
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002017 * then use the real pageprogram command, else select
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002018 * cached programming if supported by the chip.
2019 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002020 ret = nand_write_page (mtd, this, page & this->pagemask,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002021 &oobbuf[oob], oobsel, i != numpages);
2022 if (ret)
2023 goto out;
2024 this->data_poi += mtd->oobblock;
2025 len += mtd->oobblock;
2026 oob += mtd->oobsize;
2027 page++;
2028 }
2029 /* Check, if we have to switch to the next tuple */
2030 if (len >= (int) vecs->iov_len) {
2031 vecs++;
2032 len = 0;
2033 count--;
2034 }
2035 } else {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002036 /* We must use the internal buffer, read data out of each
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002037 * tuple until we have a full page to write
2038 */
2039 int cnt = 0;
2040 while (cnt < mtd->oobblock) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002041 if (vecs->iov_base != NULL && vecs->iov_len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002042 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
2043 /* Check, if we have to switch to the next tuple */
2044 if (len >= (int) vecs->iov_len) {
2045 vecs++;
2046 len = 0;
2047 count--;
2048 }
2049 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002050 this->pagebuf = page;
2051 this->data_poi = this->data_buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002052 bufstart = this->data_poi;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002053 numpages = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002054 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2055 ret = nand_write_page (mtd, this, page & this->pagemask,
2056 oobbuf, oobsel, 0);
2057 if (ret)
2058 goto out;
2059 page++;
2060 }
2061
2062 this->data_poi = bufstart;
2063 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2064 if (ret)
2065 goto out;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002066
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002067 written += mtd->oobblock * numpages;
2068 /* All done ? */
2069 if (!count)
2070 break;
2071
2072 startpage = page & this->pagemask;
2073 /* Check, if we cross a chip boundary */
2074 if (!startpage) {
2075 chipnr++;
2076 this->select_chip(mtd, -1);
2077 this->select_chip(mtd, chipnr);
2078 }
2079 }
2080 ret = 0;
2081out:
2082 /* Deselect and wake up anyone waiting on the device */
2083 nand_release_device(mtd);
2084
2085 *retlen = written;
2086 return ret;
2087}
2088#endif
2089
2090/**
2091 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2092 * @mtd: MTD device structure
2093 * @page: the page address of the block which will be erased
2094 *
2095 * Standard erase command for NAND chips
2096 */
2097static void single_erase_cmd (struct mtd_info *mtd, int page)
2098{
2099 struct nand_chip *this = mtd->priv;
2100 /* Send commands to erase a block */
2101 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2102 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2103}
2104
2105/**
2106 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2107 * @mtd: MTD device structure
2108 * @page: the page address of the block which will be erased
2109 *
2110 * AND multi block erase command function
2111 * Erase 4 consecutive blocks
2112 */
2113static void multi_erase_cmd (struct mtd_info *mtd, int page)
2114{
2115 struct nand_chip *this = mtd->priv;
2116 /* Send commands to erase a block */
2117 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2118 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2119 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2120 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2121 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2122}
2123
2124/**
2125 * nand_erase - [MTD Interface] erase block(s)
2126 * @mtd: MTD device structure
2127 * @instr: erase instruction
2128 *
2129 * Erase one ore more blocks
2130 */
2131static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2132{
2133 return nand_erase_nand (mtd, instr, 0);
2134}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002135
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002136/**
2137 * nand_erase_intern - [NAND Interface] erase block(s)
2138 * @mtd: MTD device structure
2139 * @instr: erase instruction
2140 * @allowbbt: allow erasing the bbt area
2141 *
2142 * Erase one ore more blocks
2143 */
2144int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2145{
2146 int page, len, status, pages_per_block, ret, chipnr;
2147 struct nand_chip *this = mtd->priv;
2148
Scott Wooddf83c472008-06-20 12:38:57 -05002149 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2150 (unsigned int) instr->addr, (unsigned int) instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002151
2152 /* Start address must align on block boundary */
2153 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002154 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002155 return -EINVAL;
2156 }
2157
2158 /* Length must align on block boundary */
2159 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002160 MTDDEBUG (MTD_DEBUG_LEVEL0,
2161 "nand_erase: Length not block aligned\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002162 return -EINVAL;
2163 }
2164
2165 /* Do not allow erase past end of device */
2166 if ((instr->len + instr->addr) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05002167 MTDDEBUG (MTD_DEBUG_LEVEL0,
2168 "nand_erase: Erase past end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002169 return -EINVAL;
2170 }
2171
2172 instr->fail_addr = 0xffffffff;
2173
2174 /* Grab the lock and see if the device is available */
2175 nand_get_device (this, mtd, FL_ERASING);
2176
2177 /* Shift to get first page */
2178 page = (int) (instr->addr >> this->page_shift);
2179 chipnr = (int) (instr->addr >> this->chip_shift);
2180
2181 /* Calculate pages in each block */
2182 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2183
2184 /* Select the NAND device */
2185 this->select_chip(mtd, chipnr);
2186
2187 /* Check the WP bit */
2188 /* Check, if it is write protected */
2189 if (nand_check_wp(mtd)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002190 MTDDEBUG (MTD_DEBUG_LEVEL0,
2191 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002192 instr->state = MTD_ERASE_FAILED;
2193 goto erase_exit;
2194 }
2195
2196 /* Loop through the pages */
2197 len = instr->len;
2198
2199 instr->state = MTD_ERASING;
2200
2201 while (len) {
Markus Klotzbücher85678e22006-03-06 13:45:42 +01002202#ifndef NAND_ALLOW_ERASE_ALL
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002203 /* Check if we have a bad block, we do not erase bad blocks ! */
2204 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2205 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2206 instr->state = MTD_ERASE_FAILED;
2207 goto erase_exit;
2208 }
Markus Klotzbücher85678e22006-03-06 13:45:42 +01002209#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002210 /* Invalidate the page cache, if we erase the block which contains
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002211 the current cached page */
2212 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2213 this->pagebuf = -1;
2214
2215 this->erase_cmd (mtd, page & this->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002216
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002217 status = this->waitfunc (mtd, this, FL_ERASING);
2218
2219 /* See if block erase succeeded */
2220 if (status & 0x01) {
Scott Wooddf83c472008-06-20 12:38:57 -05002221 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2222 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002223 instr->state = MTD_ERASE_FAILED;
2224 instr->fail_addr = (page << this->page_shift);
2225 goto erase_exit;
2226 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002227
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002228 /* Increment page address and decrement length */
2229 len -= (1 << this->phys_erase_shift);
2230 page += pages_per_block;
2231
2232 /* Check, if we cross a chip boundary */
2233 if (len && !(page & this->pagemask)) {
2234 chipnr++;
2235 this->select_chip(mtd, -1);
2236 this->select_chip(mtd, chipnr);
2237 }
2238 }
2239 instr->state = MTD_ERASE_DONE;
2240
2241erase_exit:
2242
2243 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2244 /* Do call back function */
2245 if (!ret)
2246 mtd_erase_callback(instr);
2247
2248 /* Deselect and wake up anyone waiting on the device */
2249 nand_release_device(mtd);
2250
2251 /* Return more or less happy */
2252 return ret;
2253}
2254
2255/**
2256 * nand_sync - [MTD Interface] sync
2257 * @mtd: MTD device structure
2258 *
2259 * Sync is actually a wait for chip ready function
2260 */
2261static void nand_sync (struct mtd_info *mtd)
2262{
2263 struct nand_chip *this = mtd->priv;
2264
Scott Wooddf83c472008-06-20 12:38:57 -05002265 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002266
2267 /* Grab the lock and see if the device is available */
2268 nand_get_device (this, mtd, FL_SYNCING);
2269 /* Release it and go back */
2270 nand_release_device (mtd);
2271}
2272
2273
2274/**
2275 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2276 * @mtd: MTD device structure
2277 * @ofs: offset relative to mtd start
2278 */
2279static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2280{
2281 /* Check for invalid offset */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002282 if (ofs > mtd->size)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002283 return -EINVAL;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002284
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002285 return nand_block_checkbad (mtd, ofs, 1, 0);
2286}
2287
2288/**
2289 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2290 * @mtd: MTD device structure
2291 * @ofs: offset relative to mtd start
2292 */
2293static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2294{
2295 struct nand_chip *this = mtd->priv;
2296 int ret;
2297
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002298 if ((ret = nand_block_isbad(mtd, ofs))) {
2299 /* If it was bad already, return success and do nothing. */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002300 if (ret > 0)
2301 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002302 return ret;
2303 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002304
2305 return this->block_markbad(mtd, ofs);
2306}
2307
2308/**
2309 * nand_scan - [NAND Interface] Scan for the NAND device
2310 * @mtd: MTD device structure
2311 * @maxchips: Number of chips to scan for
2312 *
2313 * This fills out all the not initialized function pointers
2314 * with the defaults.
2315 * The flash ID is read and the mtd/chip structures are
2316 * filled with the appropriate values. Buffers are allocated if
2317 * they are not provided by the board driver
2318 *
2319 */
2320int nand_scan (struct mtd_info *mtd, int maxchips)
2321{
2322 int i, j, nand_maf_id, nand_dev_id, busw;
2323 struct nand_chip *this = mtd->priv;
2324
2325 /* Get buswidth to select the correct functions*/
2326 busw = this->options & NAND_BUSWIDTH_16;
2327
2328 /* check for proper chip_delay setup, set 20us if not */
2329 if (!this->chip_delay)
2330 this->chip_delay = 20;
2331
2332 /* check, if a user supplied command function given */
2333 if (this->cmdfunc == NULL)
2334 this->cmdfunc = nand_command;
2335
2336 /* check, if a user supplied wait function given */
2337 if (this->waitfunc == NULL)
2338 this->waitfunc = nand_wait;
2339
2340 if (!this->select_chip)
2341 this->select_chip = nand_select_chip;
2342 if (!this->write_byte)
2343 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2344 if (!this->read_byte)
2345 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2346 if (!this->write_word)
2347 this->write_word = nand_write_word;
2348 if (!this->read_word)
2349 this->read_word = nand_read_word;
2350 if (!this->block_bad)
2351 this->block_bad = nand_block_bad;
2352 if (!this->block_markbad)
2353 this->block_markbad = nand_default_block_markbad;
2354 if (!this->write_buf)
2355 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2356 if (!this->read_buf)
2357 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2358 if (!this->verify_buf)
2359 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2360 if (!this->scan_bbt)
2361 this->scan_bbt = nand_default_bbt;
2362
2363 /* Select the device */
2364 this->select_chip(mtd, 0);
2365
2366 /* Send the command for reading device ID */
2367 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2368
2369 /* Read manufacturer and device IDs */
2370 nand_maf_id = this->read_byte(mtd);
2371 nand_dev_id = this->read_byte(mtd);
2372
2373 /* Print and store flash device information */
2374 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002375
2376 if (nand_dev_id != nand_flash_ids[i].id)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002377 continue;
2378
2379 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2380 this->chipsize = nand_flash_ids[i].chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002381
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002382 /* New devices have all the information in additional id bytes */
2383 if (!nand_flash_ids[i].pagesize) {
2384 int extid;
2385 /* The 3rd id byte contains non relevant data ATM */
2386 extid = this->read_byte(mtd);
2387 /* The 4th id byte is the important one */
2388 extid = this->read_byte(mtd);
2389 /* Calc pagesize */
2390 mtd->oobblock = 1024 << (extid & 0x3);
2391 extid >>= 2;
2392 /* Calc oobsize */
Stefan Roesee58a4af2007-01-05 11:46:05 +01002393 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->oobblock / 512);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002394 extid >>= 2;
2395 /* Calc blocksize. Blocksize is multiples of 64KiB */
2396 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2397 extid >>= 2;
2398 /* Get buswidth information */
2399 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002400
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002401 } else {
2402 /* Old devices have this data hardcoded in the
2403 * device id table */
2404 mtd->erasesize = nand_flash_ids[i].erasesize;
2405 mtd->oobblock = nand_flash_ids[i].pagesize;
2406 mtd->oobsize = mtd->oobblock / 32;
2407 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2408 }
2409
2410 /* Check, if buswidth is correct. Hardware drivers should set
2411 * this correct ! */
2412 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2413 printk (KERN_INFO "NAND device: Manufacturer ID:"
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002414 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002415 nand_manuf_ids[i].name , mtd->name);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002416 printk (KERN_WARNING
2417 "NAND bus width %d instead %d bit\n",
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002418 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2419 busw ? 16 : 8);
2420 this->select_chip(mtd, -1);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002421 return 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002422 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002423
2424 /* Calculate the address shift from the page size */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002425 this->page_shift = ffs(mtd->oobblock) - 1;
2426 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2427 this->chip_shift = ffs(this->chipsize) - 1;
2428
2429 /* Set the bad block position */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002430 this->badblockpos = mtd->oobblock > 512 ?
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002431 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2432
2433 /* Get chip options, preserve non chip based options */
2434 this->options &= ~NAND_CHIPOPTIONS_MSK;
2435 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2436 /* Set this as a default. Board drivers can override it, if neccecary */
2437 this->options |= NAND_NO_AUTOINCR;
2438 /* Check if this is a not a samsung device. Do not clear the options
2439 * for chips which are not having an extended id.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002440 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002441 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2442 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002443
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002444 /* Check for AND chips with 4 page planes */
2445 if (this->options & NAND_4PAGE_ARRAY)
2446 this->erase_cmd = multi_erase_cmd;
2447 else
2448 this->erase_cmd = single_erase_cmd;
2449
2450 /* Do not replace user supplied command function ! */
2451 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2452 this->cmdfunc = nand_command_lp;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002453
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002454 /* Try to identify manufacturer */
2455 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2456 if (nand_manuf_ids[j].id == nand_maf_id)
2457 break;
2458 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002459 break;
2460 }
2461
2462 if (!nand_flash_ids[i].name) {
Stefan Roese47cf6a52006-10-07 11:36:51 +02002463#ifndef CFG_NAND_QUIET_TEST
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002464 printk (KERN_WARNING "No NAND device found!!!\n");
Stefan Roese47cf6a52006-10-07 11:36:51 +02002465#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002466 this->select_chip(mtd, -1);
2467 return 1;
2468 }
2469
2470 for (i=1; i < maxchips; i++) {
2471 this->select_chip(mtd, i);
2472
2473 /* Send the command for reading device ID */
2474 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2475
2476 /* Read manufacturer and device IDs */
2477 if (nand_maf_id != this->read_byte(mtd) ||
2478 nand_dev_id != this->read_byte(mtd))
2479 break;
2480 }
2481 if (i > 1)
2482 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002483
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002484 /* Allocate buffers, if neccecary */
2485 if (!this->oob_buf) {
2486 size_t len;
2487 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2488 this->oob_buf = kmalloc (len, GFP_KERNEL);
2489 if (!this->oob_buf) {
2490 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2491 return -ENOMEM;
2492 }
2493 this->options |= NAND_OOBBUF_ALLOC;
2494 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002495
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002496 if (!this->data_buf) {
2497 size_t len;
2498 len = mtd->oobblock + mtd->oobsize;
2499 this->data_buf = kmalloc (len, GFP_KERNEL);
2500 if (!this->data_buf) {
2501 if (this->options & NAND_OOBBUF_ALLOC)
2502 kfree (this->oob_buf);
2503 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2504 return -ENOMEM;
2505 }
2506 this->options |= NAND_DATABUF_ALLOC;
2507 }
2508
2509 /* Store the number of chips and calc total size for mtd */
2510 this->numchips = i;
2511 mtd->size = i * this->chipsize;
2512 /* Convert chipsize to number of pages per chip -1. */
2513 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2514 /* Preset the internal oob buffer */
2515 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2516
2517 /* If no default placement scheme is given, select an
2518 * appropriate one */
2519 if (!this->autooob) {
2520 /* Select the appropriate default oob placement scheme for
2521 * placement agnostic filesystems */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002522 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002523 case 8:
2524 this->autooob = &nand_oob_8;
2525 break;
2526 case 16:
2527 this->autooob = &nand_oob_16;
2528 break;
2529 case 64:
2530 this->autooob = &nand_oob_64;
2531 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002532 case 128:
2533 this->autooob = &nand_oob_128;
2534 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002535 default:
2536 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2537 mtd->oobsize);
2538/* BUG(); */
2539 }
2540 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002541
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002542 /* The number of bytes available for the filesystem to place fs dependend
2543 * oob data */
Troy Kisky6dba1dd2007-09-24 16:41:43 -07002544 mtd->oobavail = 0;
2545 for (i=0; this->autooob->oobfree[i][1]; i++)
2546 mtd->oobavail += this->autooob->oobfree[i][1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002547
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002548 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002549 * check ECC mode, default to software
2550 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002551 * fallback to software ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002552 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002553 this->eccsize = 256; /* set default eccsize */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002554 this->eccbytes = 3;
2555
2556 switch (this->eccmode) {
2557 case NAND_ECC_HW12_2048:
2558 if (mtd->oobblock < 2048) {
2559 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2560 mtd->oobblock);
2561 this->eccmode = NAND_ECC_SOFT;
2562 this->calculate_ecc = nand_calculate_ecc;
2563 this->correct_data = nand_correct_data;
2564 } else
2565 this->eccsize = 2048;
2566 break;
2567
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002568 case NAND_ECC_HW3_512:
2569 case NAND_ECC_HW6_512:
2570 case NAND_ECC_HW8_512:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002571 if (mtd->oobblock == 256) {
2572 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2573 this->eccmode = NAND_ECC_SOFT;
2574 this->calculate_ecc = nand_calculate_ecc;
2575 this->correct_data = nand_correct_data;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002576 } else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002577 this->eccsize = 512; /* set eccsize to 512 */
2578 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002579
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002580 case NAND_ECC_HW3_256:
2581 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002582
2583 case NAND_ECC_NONE:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002584 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2585 this->eccmode = NAND_ECC_NONE;
2586 break;
2587
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002588 case NAND_ECC_SOFT:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002589 this->calculate_ecc = nand_calculate_ecc;
2590 this->correct_data = nand_correct_data;
2591 break;
2592
2593 default:
2594 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2595/* BUG(); */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002596 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002597
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002598 /* Check hardware ecc function availability and adjust number of ecc bytes per
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002599 * calculation step
2600 */
2601 switch (this->eccmode) {
2602 case NAND_ECC_HW12_2048:
2603 this->eccbytes += 4;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002604 case NAND_ECC_HW8_512:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002605 this->eccbytes += 2;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002606 case NAND_ECC_HW6_512:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002607 this->eccbytes += 3;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002608 case NAND_ECC_HW3_512:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002609 case NAND_ECC_HW3_256:
2610 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2611 break;
2612 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2613/* BUG(); */
2614 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002615
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002616 mtd->eccsize = this->eccsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002617
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002618 /* Set the number of read / write steps for one page to ensure ECC generation */
2619 switch (this->eccmode) {
2620 case NAND_ECC_HW12_2048:
2621 this->eccsteps = mtd->oobblock / 2048;
2622 break;
2623 case NAND_ECC_HW3_512:
2624 case NAND_ECC_HW6_512:
2625 case NAND_ECC_HW8_512:
2626 this->eccsteps = mtd->oobblock / 512;
2627 break;
2628 case NAND_ECC_HW3_256:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002629 case NAND_ECC_SOFT:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002630 this->eccsteps = mtd->oobblock / 256;
2631 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002632
2633 case NAND_ECC_NONE:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002634 this->eccsteps = 1;
2635 break;
2636 }
2637
2638/* XXX U-BOOT XXX */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002639#if 0
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002640 /* Initialize state, waitqueue and spinlock */
2641 this->state = FL_READY;
2642 init_waitqueue_head (&this->wq);
2643 spin_lock_init (&this->chip_lock);
2644#endif
2645
2646 /* De-select the device */
2647 this->select_chip(mtd, -1);
2648
2649 /* Invalidate the pagebuffer reference */
2650 this->pagebuf = -1;
2651
2652 /* Fill in remaining MTD driver data */
2653 mtd->type = MTD_NANDFLASH;
2654 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2655 mtd->ecctype = MTD_ECC_SW;
2656 mtd->erase = nand_erase;
2657 mtd->point = NULL;
2658 mtd->unpoint = NULL;
2659 mtd->read = nand_read;
2660 mtd->write = nand_write;
2661 mtd->read_ecc = nand_read_ecc;
2662 mtd->write_ecc = nand_write_ecc;
2663 mtd->read_oob = nand_read_oob;
2664 mtd->write_oob = nand_write_oob;
2665/* XXX U-BOOT XXX */
2666#if 0
2667 mtd->readv = NULL;
2668 mtd->writev = nand_writev;
2669 mtd->writev_ecc = nand_writev_ecc;
2670#endif
2671 mtd->sync = nand_sync;
2672/* XXX U-BOOT XXX */
2673#if 0
2674 mtd->lock = NULL;
2675 mtd->unlock = NULL;
2676 mtd->suspend = NULL;
2677 mtd->resume = NULL;
2678#endif
2679 mtd->block_isbad = nand_block_isbad;
2680 mtd->block_markbad = nand_block_markbad;
2681
2682 /* and make the autooob the default one */
2683 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2684/* XXX U-BOOT XXX */
2685#if 0
2686 mtd->owner = THIS_MODULE;
2687#endif
2688 /* Build bad block table */
2689 return this->scan_bbt (mtd);
2690}
2691
2692/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002693 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002694 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002695 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002696void nand_release (struct mtd_info *mtd)
2697{
2698 struct nand_chip *this = mtd->priv;
2699
2700#ifdef CONFIG_MTD_PARTITIONS
2701 /* Deregister partitions */
2702 del_mtd_partitions (mtd);
2703#endif
2704 /* Deregister the device */
2705/* XXX U-BOOT XXX */
2706#if 0
2707 del_mtd_device (mtd);
2708#endif
2709 /* Free bad block table memory, if allocated */
2710 if (this->bbt)
2711 kfree (this->bbt);
2712 /* Buffer allocated by nand_scan ? */
2713 if (this->options & NAND_OOBBUF_ALLOC)
2714 kfree (this->oob_buf);
2715 /* Buffer allocated by nand_scan ? */
2716 if (this->options & NAND_DATABUF_ALLOC)
2717 kfree (this->data_buf);
2718}
2719
2720#endif