blob: 0913bb87418cb52bf43d8efbd4944c358811ec0c [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)
William Juul52c07962007-10-31 13:53:06 +010013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020014 *
William Juul52c07962007-10-31 13:53:06 +010015 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020016 * David Woodhouse for adding multichip support
17 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
William Juul52c07962007-10-31 13:53:06 +010021 * TODO:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
27 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020028 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License version 2 as
30 * published by the Free Software Foundation.
31 *
32 */
33
34/* XXX U-BOOT XXX */
35#if 0
William Juul52c07962007-10-31 13:53:06 +010036#include <linux/module.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020037#include <linux/delay.h>
38#include <linux/errno.h>
William Juul52c07962007-10-31 13:53:06 +010039#include <linux/err.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020040#include <linux/sched.h>
41#include <linux/slab.h>
42#include <linux/types.h>
43#include <linux/mtd/mtd.h>
44#include <linux/mtd/nand.h>
45#include <linux/mtd/nand_ecc.h>
46#include <linux/mtd/compatmac.h>
47#include <linux/interrupt.h>
48#include <linux/bitops.h>
William Juul52c07962007-10-31 13:53:06 +010049#include <linux/leds.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020050#include <asm/io.h>
51
52#ifdef CONFIG_MTD_PARTITIONS
53#include <linux/mtd/partitions.h>
54#endif
55
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020056#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020057
58#include <common.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010059
William Juul52c07962007-10-31 13:53:06 +010060#define ENOTSUPP 524 /* Operation is not supported */
61
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020062#include <malloc.h>
63#include <watchdog.h>
William Juul52c07962007-10-31 13:53:06 +010064#include <linux/err.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020065#include <linux/mtd/compat.h>
66#include <linux/mtd/mtd.h>
67#include <linux/mtd/nand.h>
68#include <linux/mtd/nand_ecc.h>
69
70#include <asm/io.h>
71#include <asm/errno.h>
72
73#ifdef CONFIG_JFFS2_NAND
74#include <jffs2/jffs2.h>
75#endif
76
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020077/* Define default oob placement schemes for large and small page devices */
William Juul52c07962007-10-31 13:53:06 +010078static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020079 .eccbytes = 3,
80 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010081 .oobfree = {
82 {.offset = 3,
83 .length = 2},
84 {.offset = 6,
85 .length = 2}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020086};
87
William Juul52c07962007-10-31 13:53:06 +010088static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020089 .eccbytes = 6,
90 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010091 .oobfree = {
92 {.offset = 8,
93 . length = 8}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094};
95
William Juul52c07962007-10-31 13:53:06 +010096static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020097 .eccbytes = 24,
98 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010099 40, 41, 42, 43, 44, 45, 46, 47,
100 48, 49, 50, 51, 52, 53, 54, 55,
101 56, 57, 58, 59, 60, 61, 62, 63},
102 .oobfree = {
103 {.offset = 2,
104 .length = 38}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200105};
106
William Juul52c07962007-10-31 13:53:06 +0100107static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200108 .eccbytes = 48,
109 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +0100110 80, 81, 82, 83, 84, 85, 86, 87,
111 88, 89, 90, 91, 92, 93, 94, 95,
112 96, 97, 98, 99, 100, 101, 102, 103,
113 104, 105, 106, 107, 108, 109, 110, 111,
114 112, 113, 114, 115, 116, 117, 118, 119,
115 120, 121, 122, 123, 124, 125, 126, 127},
116 .oobfree = {
117 {.offset = 2,
118 .length = 78}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200119};
120
William Juul52c07962007-10-31 13:53:06 +0100121
122static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
123 int new_state);
124
125static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
126 struct mtd_oob_ops *ops);
127
128static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200129
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200130/*
William Juul52c07962007-10-31 13:53:06 +0100131 * For devices which display every fart in the system on a seperate LED. Is
132 * compiled away when LED support is disabled.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200133 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200134/* XXX U-BOOT XXX */
135#if 0
William Juul52c07962007-10-31 13:53:06 +0100136DEFINE_LED_TRIGGER(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200137#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200138
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200139/**
140 * nand_release_device - [GENERIC] release chip
141 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200142 *
143 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200144 */
145/* XXX U-BOOT XXX */
146#if 0
William Juul52c07962007-10-31 13:53:06 +0100147static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200148{
William Juul52c07962007-10-31 13:53:06 +0100149 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200150
151 /* De-select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100152 chip->select_chip(mtd, -1);
153
154 /* Release the controller and the chip */
155 spin_lock(&chip->controller->lock);
156 chip->controller->active = NULL;
157 chip->state = FL_READY;
158 wake_up(&chip->controller->wq);
159 spin_unlock(&chip->controller->lock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160}
161#else
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100162static void nand_release_device (struct mtd_info *mtd)
163{
164 struct nand_chip *this = mtd->priv;
165 this->select_chip(mtd, -1); /* De-select the NAND device */
166}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200167#endif
168
169/**
170 * nand_read_byte - [DEFAULT] read one byte from the chip
171 * @mtd: MTD device structure
172 *
173 * Default read function for 8bit buswith
174 */
William Juul52c07962007-10-31 13:53:06 +0100175static uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200176{
William Juul52c07962007-10-31 13:53:06 +0100177 struct nand_chip *chip = mtd->priv;
178 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200179}
180
181/**
182 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
183 * @mtd: MTD device structure
184 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200185 * Default read function for 16bit buswith with
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200186 * endianess conversion
187 */
William Juul52c07962007-10-31 13:53:06 +0100188static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200189{
William Juul52c07962007-10-31 13:53:06 +0100190 struct nand_chip *chip = mtd->priv;
191 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192}
193
194/**
195 * nand_read_word - [DEFAULT] read one word from the chip
196 * @mtd: MTD device structure
197 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200198 * Default read function for 16bit buswith without
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200199 * endianess conversion
200 */
201static u16 nand_read_word(struct mtd_info *mtd)
202{
William Juul52c07962007-10-31 13:53:06 +0100203 struct nand_chip *chip = mtd->priv;
204 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200205}
206
207/**
208 * nand_select_chip - [DEFAULT] control CE line
209 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100210 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200211 *
212 * Default select function for 1 chip devices.
213 */
William Juul52c07962007-10-31 13:53:06 +0100214static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200215{
William Juul52c07962007-10-31 13:53:06 +0100216 struct nand_chip *chip = mtd->priv;
217
218 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200219 case -1:
William Juul52c07962007-10-31 13:53:06 +0100220 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200221 break;
222 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200223 break;
224
225 default:
226 BUG();
227 }
228}
229
230/**
231 * nand_write_buf - [DEFAULT] write buffer to chip
232 * @mtd: MTD device structure
233 * @buf: data buffer
234 * @len: number of bytes to write
235 *
236 * Default write function for 8bit buswith
237 */
William Juul52c07962007-10-31 13:53:06 +0100238static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200239{
240 int i;
William Juul52c07962007-10-31 13:53:06 +0100241 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200242
William Juul52c07962007-10-31 13:53:06 +0100243 for (i = 0; i < len; i++)
244 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200245}
246
247/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200248 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200249 * @mtd: MTD device structure
250 * @buf: buffer to store date
251 * @len: number of bytes to read
252 *
253 * Default read function for 8bit buswith
254 */
William Juul52c07962007-10-31 13:53:06 +0100255static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200256{
257 int i;
William Juul52c07962007-10-31 13:53:06 +0100258 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200259
William Juul52c07962007-10-31 13:53:06 +0100260 for (i = 0; i < len; i++)
261 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200262}
263
264/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200265 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200266 * @mtd: MTD device structure
267 * @buf: buffer containing the data to compare
268 * @len: number of bytes to compare
269 *
270 * Default verify function for 8bit buswith
271 */
William Juul52c07962007-10-31 13:53:06 +0100272static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200273{
274 int i;
William Juul52c07962007-10-31 13:53:06 +0100275 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200276
William Juul52c07962007-10-31 13:53:06 +0100277 for (i = 0; i < len; i++)
278 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200279 return -EFAULT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200280 return 0;
281}
282
283/**
284 * nand_write_buf16 - [DEFAULT] write buffer to chip
285 * @mtd: MTD device structure
286 * @buf: data buffer
287 * @len: number of bytes to write
288 *
289 * Default write function for 16bit buswith
290 */
William Juul52c07962007-10-31 13:53:06 +0100291static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200292{
293 int i;
William Juul52c07962007-10-31 13:53:06 +0100294 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200295 u16 *p = (u16 *) buf;
296 len >>= 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200297
William Juul52c07962007-10-31 13:53:06 +0100298 for (i = 0; i < len; i++)
299 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200300
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200301}
302
303/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200304 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305 * @mtd: MTD device structure
306 * @buf: buffer to store date
307 * @len: number of bytes to read
308 *
309 * Default read function for 16bit buswith
310 */
William Juul52c07962007-10-31 13:53:06 +0100311static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200312{
313 int i;
William Juul52c07962007-10-31 13:53:06 +0100314 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200315 u16 *p = (u16 *) buf;
316 len >>= 1;
317
William Juul52c07962007-10-31 13:53:06 +0100318 for (i = 0; i < len; i++)
319 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200320}
321
322/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200323 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200324 * @mtd: MTD device structure
325 * @buf: buffer containing the data to compare
326 * @len: number of bytes to compare
327 *
328 * Default verify function for 16bit buswith
329 */
William Juul52c07962007-10-31 13:53:06 +0100330static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200331{
332 int i;
William Juul52c07962007-10-31 13:53:06 +0100333 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200334 u16 *p = (u16 *) buf;
335 len >>= 1;
336
William Juul52c07962007-10-31 13:53:06 +0100337 for (i = 0; i < len; i++)
338 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200339 return -EFAULT;
340
341 return 0;
342}
343
344/**
345 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
346 * @mtd: MTD device structure
347 * @ofs: offset from device start
348 * @getchip: 0, if the chip is already selected
349 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200350 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200351 */
352static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
353{
354 int page, chipnr, res = 0;
William Juul52c07962007-10-31 13:53:06 +0100355 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200356 u16 bad;
357
William Juul52c07962007-10-31 13:53:06 +0100358 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200359
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200360 if (getchip) {
William Juul52c07962007-10-31 13:53:06 +0100361 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200362
William Juul52c07962007-10-31 13:53:06 +0100363 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200364
365 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100366 chip->select_chip(mtd, chipnr);
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200367 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200368
William Juul52c07962007-10-31 13:53:06 +0100369 if (chip->options & NAND_BUSWIDTH_16) {
370 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
371 page);
372 bad = cpu_to_le16(chip->read_word(mtd));
373 if (chip->badblockpos & 0x1)
374 bad >>= 8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200375 if ((bad & 0xFF) != 0xff)
376 res = 1;
377 } else {
William Juul52c07962007-10-31 13:53:06 +0100378 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
379 if (chip->read_byte(mtd) != 0xff)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200380 res = 1;
381 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200382
William Juul52c07962007-10-31 13:53:06 +0100383 if (getchip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200384 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200385
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200386 return res;
387}
388
389/**
390 * nand_default_block_markbad - [DEFAULT] mark a block bad
391 * @mtd: MTD device structure
392 * @ofs: offset from device start
393 *
394 * This is the default implementation, which can be overridden by
395 * a hardware specific driver.
396*/
397static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
398{
William Juul52c07962007-10-31 13:53:06 +0100399 struct nand_chip *chip = mtd->priv;
400 uint8_t buf[2] = { 0, 0 };
401 int block, ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200402
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200403 /* Get block number */
William Juul52c07962007-10-31 13:53:06 +0100404 block = (int)(ofs >> chip->bbt_erase_shift);
405 if (chip->bbt)
406 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200407
408 /* Do we have a flash based bad block table ? */
William Juul52c07962007-10-31 13:53:06 +0100409 if (chip->options & NAND_USE_FLASH_BBT)
410 ret = nand_update_bbt(mtd, ofs);
411 else {
412 /* We write two bytes, so we dont have to mess with 16 bit
413 * access
414 */
415 ofs += mtd->oobsize;
416 chip->ops.len = chip->ops.ooblen = 2;
417 chip->ops.datbuf = NULL;
418 chip->ops.oobbuf = buf;
419 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200420
William Juul52c07962007-10-31 13:53:06 +0100421 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
422 }
423 if (!ret)
424 mtd->ecc_stats.badblocks++;
425 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200426}
427
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200428/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200429 * nand_check_wp - [GENERIC] check if the chip is write protected
430 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200431 * Check, if the device is write protected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200432 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200433 * The function expects, that the device is already selected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200434 */
William Juul52c07962007-10-31 13:53:06 +0100435static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200436{
William Juul52c07962007-10-31 13:53:06 +0100437 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200438 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100439 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
440 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200441}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100442
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200443/**
444 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
445 * @mtd: MTD device structure
446 * @ofs: offset from device start
447 * @getchip: 0, if the chip is already selected
448 * @allowbbt: 1, if its allowed to access the bbt area
449 *
450 * Check, if the block is bad. Either by reading the bad block table or
451 * calling of the scan function.
452 */
William Juul52c07962007-10-31 13:53:06 +0100453static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
454 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200455{
William Juul52c07962007-10-31 13:53:06 +0100456 struct nand_chip *chip = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200457
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200458 if (!(chip->options & NAND_BBT_SCANNED)) {
459 chip->scan_bbt(mtd);
460 chip->options |= NAND_BBT_SCANNED;
461 }
462
William Juul52c07962007-10-31 13:53:06 +0100463 if (!chip->bbt)
464 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200465
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200466 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100467 return nand_isbad_bbt(mtd, ofs, allowbbt);
468}
469
470/*
471 * Wait for the ready pin, after a command
472 * The timeout is catched later.
473 */
474/* XXX U-BOOT XXX */
475#if 0
476void nand_wait_ready(struct mtd_info *mtd)
477{
478 struct nand_chip *chip = mtd->priv;
479 unsigned long timeo = jiffies + 2;
480
481 led_trigger_event(nand_led_trigger, LED_FULL);
482 /* wait until command is processed or timeout occures */
483 do {
484 if (chip->dev_ready(mtd))
485 break;
486 touch_softlockup_watchdog();
487 } while (time_before(jiffies, timeo));
488 led_trigger_event(nand_led_trigger, LED_OFF);
489}
490EXPORT_SYMBOL_GPL(nand_wait_ready);
491#else
492void nand_wait_ready(struct mtd_info *mtd)
493{
494 struct nand_chip *chip = mtd->priv;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100495 u32 timeo = (CFG_HZ * 20) / 1000;
496
497 reset_timer();
498
499 /* wait until command is processed or timeout occures */
500 while (get_timer(0) < timeo) {
501 if (chip->dev_ready)
502 if (chip->dev_ready(mtd))
503 break;
504 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200505}
William Juul52c07962007-10-31 13:53:06 +0100506#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200507
508/**
509 * nand_command - [DEFAULT] Send command to NAND device
510 * @mtd: MTD device structure
511 * @command: the command to be sent
512 * @column: the column address for this command, -1 if none
513 * @page_addr: the page address for this command, -1 if none
514 *
515 * Send command to NAND device. This function is used for small page
516 * devices (256/512 Bytes per page)
517 */
William Juul52c07962007-10-31 13:53:06 +0100518static void nand_command(struct mtd_info *mtd, unsigned int command,
519 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200520{
William Juul52c07962007-10-31 13:53:06 +0100521 register struct nand_chip *chip = mtd->priv;
522 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200523
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200524 /*
525 * Write out the command to the device.
526 */
527 if (command == NAND_CMD_SEQIN) {
528 int readcmd;
529
William Juul52c07962007-10-31 13:53:06 +0100530 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200531 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100532 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200533 readcmd = NAND_CMD_READOOB;
534 } else if (column < 256) {
535 /* First 256 bytes --> READ0 */
536 readcmd = NAND_CMD_READ0;
537 } else {
538 column -= 256;
539 readcmd = NAND_CMD_READ1;
540 }
William Juul52c07962007-10-31 13:53:06 +0100541 chip->cmd_ctrl(mtd, readcmd, ctrl);
542 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200543 }
William Juul52c07962007-10-31 13:53:06 +0100544 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200545
William Juul52c07962007-10-31 13:53:06 +0100546 /*
547 * Address cycle, when necessary
548 */
549 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
550 /* Serially input address */
551 if (column != -1) {
552 /* Adjust columns for 16 bit buswidth */
553 if (chip->options & NAND_BUSWIDTH_16)
554 column >>= 1;
555 chip->cmd_ctrl(mtd, column, ctrl);
556 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200557 }
William Juul52c07962007-10-31 13:53:06 +0100558 if (page_addr != -1) {
559 chip->cmd_ctrl(mtd, page_addr, ctrl);
560 ctrl &= ~NAND_CTRL_CHANGE;
561 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
562 /* One more address cycle for devices > 32MiB */
563 if (chip->chipsize > (32 << 20))
564 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
565 }
566 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200567
568 /*
569 * program and erase have their own busy handlers
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200570 * status and sequential in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100571 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200572 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200573
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200574 case NAND_CMD_PAGEPROG:
575 case NAND_CMD_ERASE1:
576 case NAND_CMD_ERASE2:
577 case NAND_CMD_SEQIN:
578 case NAND_CMD_STATUS:
579 return;
580
581 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100582 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200583 break;
William Juul52c07962007-10-31 13:53:06 +0100584 udelay(chip->chip_delay);
585 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
586 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
587 chip->cmd_ctrl(mtd,
588 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
589 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200590 return;
591
William Juul52c07962007-10-31 13:53:06 +0100592 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200593 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200594 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200595 * If we don't have access to the busy pin, we apply the given
596 * command delay
William Juul52c07962007-10-31 13:53:06 +0100597 */
598 if (!chip->dev_ready) {
599 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200600 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200601 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200602 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 /* Apply this short delay always to ensure that we do wait tWB in
604 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100605 ndelay(100);
606
607 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200608}
609
610/**
611 * nand_command_lp - [DEFAULT] Send command to NAND large page device
612 * @mtd: MTD device structure
613 * @command: the command to be sent
614 * @column: the column address for this command, -1 if none
615 * @page_addr: the page address for this command, -1 if none
616 *
William Juul52c07962007-10-31 13:53:06 +0100617 * Send command to NAND device. This is the version for the new large page
618 * devices We dont have the separate regions as we have in the small page
619 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200620 */
William Juul52c07962007-10-31 13:53:06 +0100621static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
622 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200623{
William Juul52c07962007-10-31 13:53:06 +0100624 register struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200625
626 /* Emulate NAND_CMD_READOOB */
627 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100628 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200629 command = NAND_CMD_READ0;
630 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200631
William Juul52c07962007-10-31 13:53:06 +0100632 /* Command latch cycle */
633 chip->cmd_ctrl(mtd, command & 0xff,
634 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200635
636 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100637 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200638
639 /* Serially input address */
640 if (column != -1) {
641 /* Adjust columns for 16 bit buswidth */
William Juul52c07962007-10-31 13:53:06 +0100642 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200643 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100644 chip->cmd_ctrl(mtd, column, ctrl);
645 ctrl &= ~NAND_CTRL_CHANGE;
646 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200647 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200648 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100649 chip->cmd_ctrl(mtd, page_addr, ctrl);
650 chip->cmd_ctrl(mtd, page_addr >> 8,
651 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200652 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-10-31 13:53:06 +0100653 if (chip->chipsize > (128 << 20))
654 chip->cmd_ctrl(mtd, page_addr >> 16,
655 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200656 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200657 }
William Juul52c07962007-10-31 13:53:06 +0100658 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200659
660 /*
661 * program and erase have their own busy handlers
William Juul52c07962007-10-31 13:53:06 +0100662 * status, sequential in, and deplete1 need no delay
663 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200664 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200665
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200666 case NAND_CMD_CACHEDPROG:
667 case NAND_CMD_PAGEPROG:
668 case NAND_CMD_ERASE1:
669 case NAND_CMD_ERASE2:
670 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100671 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200672 case NAND_CMD_STATUS:
William Juul52c07962007-10-31 13:53:06 +0100673 case NAND_CMD_DEPLETE1:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200674 return;
675
William Juul52c07962007-10-31 13:53:06 +0100676 /*
677 * read error status commands require only a short delay
678 */
679 case NAND_CMD_STATUS_ERROR:
680 case NAND_CMD_STATUS_ERROR0:
681 case NAND_CMD_STATUS_ERROR1:
682 case NAND_CMD_STATUS_ERROR2:
683 case NAND_CMD_STATUS_ERROR3:
684 udelay(chip->chip_delay);
685 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200686
687 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100688 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200689 break;
William Juul52c07962007-10-31 13:53:06 +0100690 udelay(chip->chip_delay);
691 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
692 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
693 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
694 NAND_NCE | NAND_CTRL_CHANGE);
695 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
696 return;
697
698 case NAND_CMD_RNDOUT:
699 /* No ready / busy check necessary */
700 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
701 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
702 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
703 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200704 return;
705
706 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100707 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
708 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
709 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
710 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200711
William Juul52c07962007-10-31 13:53:06 +0100712 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200713 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200714 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200715 * If we don't have access to the busy pin, we apply the given
716 * command delay
William Juul52c07962007-10-31 13:53:06 +0100717 */
718 if (!chip->dev_ready) {
719 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200720 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200721 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200722 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200723
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724 /* Apply this short delay always to ensure that we do wait tWB in
725 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100726 ndelay(100);
727
728 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200729}
730
731/**
732 * nand_get_device - [GENERIC] Get chip for selected access
William Juul52c07962007-10-31 13:53:06 +0100733 * @chip: the nand chip descriptor
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200734 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200735 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200736 *
737 * Get the device and lock it for exclusive access
738 */
739/* XXX U-BOOT XXX */
740#if 0
William Juul52c07962007-10-31 13:53:06 +0100741static int
742nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200743{
William Juul52c07962007-10-31 13:53:06 +0100744 spinlock_t *lock = &chip->controller->lock;
745 wait_queue_head_t *wq = &chip->controller->wq;
746 DECLARE_WAITQUEUE(wait, current);
747 retry:
748 spin_lock(lock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200749
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 /* Hardware controller shared among independend devices */
William Juul52c07962007-10-31 13:53:06 +0100751 /* Hardware controller shared among independend devices */
752 if (!chip->controller->active)
753 chip->controller->active = chip;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200754
William Juul52c07962007-10-31 13:53:06 +0100755 if (chip->controller->active == chip && chip->state == FL_READY) {
756 chip->state = new_state;
757 spin_unlock(lock);
758 return 0;
759 }
760 if (new_state == FL_PM_SUSPENDED) {
761 spin_unlock(lock);
762 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200763 }
William Juul52c07962007-10-31 13:53:06 +0100764 set_current_state(TASK_UNINTERRUPTIBLE);
765 add_wait_queue(wq, &wait);
766 spin_unlock(lock);
767 schedule();
768 remove_wait_queue(wq, &wait);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200769 goto retry;
770}
771#else
William Juul52c07962007-10-31 13:53:06 +0100772static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
773{
Marcel Ziswilerf30a2aa2008-06-22 16:30:06 +0200774 this->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100775 return 0;
776}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200777#endif
778
779/**
780 * nand_wait - [DEFAULT] wait until the command is done
781 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100782 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200783 *
784 * Wait for command done. This applies to erase and program only
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200785 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200786 * general NAND and SmartMedia specs
William Juul52c07962007-10-31 13:53:06 +0100787 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200788/* XXX U-BOOT XXX */
789#if 0
William Juul52c07962007-10-31 13:53:06 +0100790static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200791{
William Juul52c07962007-10-31 13:53:06 +0100792
793 unsigned long timeo = jiffies;
794 int status, state = chip->state;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200795
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200796 if (state == FL_ERASING)
William Juul52c07962007-10-31 13:53:06 +0100797 timeo += (HZ * 400) / 1000;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200798 else
William Juul52c07962007-10-31 13:53:06 +0100799 timeo += (HZ * 20) / 1000;
800
801 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200802
803 /* Apply this short delay always to ensure that we do wait tWB in
804 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100805 ndelay(100);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200806
William Juul52c07962007-10-31 13:53:06 +0100807 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
808 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200809 else
William Juul52c07962007-10-31 13:53:06 +0100810 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200811
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200812 while (time_before(jiffies, timeo)) {
William Juul52c07962007-10-31 13:53:06 +0100813 if (chip->dev_ready) {
814 if (chip->dev_ready(mtd))
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200815 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200816 } else {
William Juul52c07962007-10-31 13:53:06 +0100817 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200818 break;
819 }
William Juul52c07962007-10-31 13:53:06 +0100820 cond_resched();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200821 }
William Juul52c07962007-10-31 13:53:06 +0100822 led_trigger_event(nand_led_trigger, LED_OFF);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200823
William Juul52c07962007-10-31 13:53:06 +0100824 status = (int)chip->read_byte(mtd);
825 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200826}
827#else
William Juul52c07962007-10-31 13:53:06 +0100828static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200829{
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100830 unsigned long timeo;
William Juul52c07962007-10-31 13:53:06 +0100831 int state = this->state;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100832
833 if (state == FL_ERASING)
Wolfgang Denka1be4762008-05-20 16:00:29 +0200834 timeo = (CFG_HZ * 400) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100835 else
Stefan Roese3427cf62006-11-28 11:04:45 +0100836 timeo = (CFG_HZ * 20) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100837
838 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
839 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
840 else
841 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
842
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100843 reset_timer();
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100844
845 while (1) {
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100846 if (get_timer(0) > timeo) {
847 printf("Timeout!");
Stefan Roeseef26d242006-11-27 17:22:19 +0100848 return 0x01;
849 }
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100850
851 if (this->dev_ready) {
852 if (this->dev_ready(mtd))
853 break;
854 } else {
855 if (this->read_byte(mtd) & NAND_STATUS_READY)
856 break;
857 }
858 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100859#ifdef PPCHAMELON_NAND_TIMER_HACK
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100860 reset_timer();
861 while (get_timer(0) < 10);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100862#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100863
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100864 return this->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200865}
866#endif
867
868/**
William Juul52c07962007-10-31 13:53:06 +0100869 * nand_read_page_raw - [Intern] read raw page data without ecc
870 * @mtd: mtd info structure
871 * @chip: nand chip info structure
872 * @buf: buffer to store read data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200873 */
William Juul52c07962007-10-31 13:53:06 +0100874static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
875 uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200876{
William Juul52c07962007-10-31 13:53:06 +0100877 chip->read_buf(mtd, buf, mtd->writesize);
878 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
879 return 0;
880}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200881
William Juul52c07962007-10-31 13:53:06 +0100882/**
883 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
884 * @mtd: mtd info structure
885 * @chip: nand chip info structure
886 * @buf: buffer to store read data
887 */
888static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
889 uint8_t *buf)
890{
891 int i, eccsize = chip->ecc.size;
892 int eccbytes = chip->ecc.bytes;
893 int eccsteps = chip->ecc.steps;
894 uint8_t *p = buf;
895 uint8_t *ecc_calc = chip->buffers->ecccalc;
896 uint8_t *ecc_code = chip->buffers->ecccode;
897 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200898
William Juul52c07962007-10-31 13:53:06 +0100899 chip->ecc.read_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200900
William Juul52c07962007-10-31 13:53:06 +0100901 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
902 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200903
William Juul52c07962007-10-31 13:53:06 +0100904 for (i = 0; i < chip->ecc.total; i++)
905 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200906
William Juul52c07962007-10-31 13:53:06 +0100907 eccsteps = chip->ecc.steps;
908 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200909
William Juul52c07962007-10-31 13:53:06 +0100910 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
911 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200912
William Juul52c07962007-10-31 13:53:06 +0100913 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
914 if (stat == -1)
915 mtd->ecc_stats.failed++;
916 else
917 mtd->ecc_stats.corrected += stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200918 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200919 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200920}
921
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200922/**
William Juul52c07962007-10-31 13:53:06 +0100923 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
924 * @mtd: mtd info structure
925 * @chip: nand chip info structure
926 * @buf: buffer to store read data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200927 *
William Juul52c07962007-10-31 13:53:06 +0100928 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200929 */
William Juul52c07962007-10-31 13:53:06 +0100930static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
931 uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200932{
William Juul52c07962007-10-31 13:53:06 +0100933 int i, eccsize = chip->ecc.size;
934 int eccbytes = chip->ecc.bytes;
935 int eccsteps = chip->ecc.steps;
936 uint8_t *p = buf;
937 uint8_t *ecc_calc = chip->buffers->ecccalc;
938 uint8_t *ecc_code = chip->buffers->ecccode;
939 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200940
William Juul52c07962007-10-31 13:53:06 +0100941 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
942 chip->ecc.hwctl(mtd, NAND_ECC_READ);
943 chip->read_buf(mtd, p, eccsize);
944 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
945 }
946 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200947
William Juul52c07962007-10-31 13:53:06 +0100948 for (i = 0; i < chip->ecc.total; i++)
949 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200950
William Juul52c07962007-10-31 13:53:06 +0100951 eccsteps = chip->ecc.steps;
952 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200953
William Juul52c07962007-10-31 13:53:06 +0100954 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
955 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200956
William Juul52c07962007-10-31 13:53:06 +0100957 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
958 if (stat == -1)
959 mtd->ecc_stats.failed++;
960 else
961 mtd->ecc_stats.corrected += stat;
962 }
963 return 0;
964}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200965
William Juul52c07962007-10-31 13:53:06 +0100966/**
967 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
968 * @mtd: mtd info structure
969 * @chip: nand chip info structure
970 * @buf: buffer to store read data
971 *
972 * The hw generator calculates the error syndrome automatically. Therefor
973 * we need a special oob layout and handling.
974 */
975static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
976 uint8_t *buf)
977{
978 int i, eccsize = chip->ecc.size;
979 int eccbytes = chip->ecc.bytes;
980 int eccsteps = chip->ecc.steps;
981 uint8_t *p = buf;
982 uint8_t *oob = chip->oob_poi;
983
984 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
985 int stat;
986
987 chip->ecc.hwctl(mtd, NAND_ECC_READ);
988 chip->read_buf(mtd, p, eccsize);
989
990 if (chip->ecc.prepad) {
991 chip->read_buf(mtd, oob, chip->ecc.prepad);
992 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200993 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200994
William Juul52c07962007-10-31 13:53:06 +0100995 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
996 chip->read_buf(mtd, oob, eccbytes);
997 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200998
William Juul52c07962007-10-31 13:53:06 +0100999 if (stat == -1)
1000 mtd->ecc_stats.failed++;
1001 else
1002 mtd->ecc_stats.corrected += stat;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001003
William Juul52c07962007-10-31 13:53:06 +01001004 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001005
William Juul52c07962007-10-31 13:53:06 +01001006 if (chip->ecc.postpad) {
1007 chip->read_buf(mtd, oob, chip->ecc.postpad);
1008 oob += chip->ecc.postpad;
1009 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001010 }
William Juul52c07962007-10-31 13:53:06 +01001011
1012 /* Calculate remaining oob bytes */
1013 i = mtd->oobsize - (oob - chip->oob_poi);
1014 if (i)
1015 chip->read_buf(mtd, oob, i);
1016
1017 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001018}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001019
1020/**
William Juul52c07962007-10-31 13:53:06 +01001021 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1022 * @chip: nand chip structure
1023 * @oob: oob destination address
1024 * @ops: oob ops structure
1025 * @len: size of oob to transfer
1026 */
1027static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1028 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001029{
William Juul52c07962007-10-31 13:53:06 +01001030 switch(ops->mode) {
1031
1032 case MTD_OOB_PLACE:
1033 case MTD_OOB_RAW:
1034 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1035 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001036
William Juul52c07962007-10-31 13:53:06 +01001037 case MTD_OOB_AUTO: {
1038 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1039 uint32_t boffs = 0, roffs = ops->ooboffs;
1040 size_t bytes = 0;
1041
1042 for(; free->length && len; free++, len -= bytes) {
1043 /* Read request not from offset 0 ? */
1044 if (unlikely(roffs)) {
1045 if (roffs >= free->length) {
1046 roffs -= free->length;
1047 continue;
1048 }
1049 boffs = free->offset + roffs;
1050 bytes = min_t(size_t, len,
1051 (free->length - roffs));
1052 roffs = 0;
1053 } else {
1054 bytes = min_t(size_t, len, free->length);
1055 boffs = free->offset;
1056 }
1057 memcpy(oob, chip->oob_poi + boffs, bytes);
1058 oob += bytes;
1059 }
1060 return oob;
1061 }
1062 default:
1063 BUG();
1064 }
1065 return NULL;
1066}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001067
1068/**
William Juul52c07962007-10-31 13:53:06 +01001069 * nand_do_read_ops - [Internal] Read data with ECC
1070 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001071 * @mtd: MTD device structure
1072 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001073 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001074 *
William Juul52c07962007-10-31 13:53:06 +01001075 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001076 */
William Juul52c07962007-10-31 13:53:06 +01001077static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1078 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001079{
William Juul52c07962007-10-31 13:53:06 +01001080 int chipnr, page, realpage, col, bytes, aligned;
1081 struct nand_chip *chip = mtd->priv;
1082 struct mtd_ecc_stats stats;
1083 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1084 int sndcmd = 1;
1085 int ret = 0;
1086 uint32_t readlen = ops->len;
1087 uint32_t oobreadlen = ops->ooblen;
1088 uint8_t *bufpoi, *oob, *buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001089
William Juul52c07962007-10-31 13:53:06 +01001090 stats = mtd->ecc_stats;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001091
William Juul52c07962007-10-31 13:53:06 +01001092 chipnr = (int)(from >> chip->chip_shift);
1093 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001094
William Juul52c07962007-10-31 13:53:06 +01001095 realpage = (int)(from >> chip->page_shift);
1096 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001097
William Juul52c07962007-10-31 13:53:06 +01001098 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001099
William Juul52c07962007-10-31 13:53:06 +01001100 buf = ops->datbuf;
1101 oob = ops->oobbuf;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001102
William Juul52c07962007-10-31 13:53:06 +01001103 while(1) {
1104 bytes = min(mtd->writesize - col, readlen);
1105 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001106
William Juul52c07962007-10-31 13:53:06 +01001107 /* Is the current page in the buffer ? */
1108 if (realpage != chip->pagebuf || oob) {
1109 bufpoi = aligned ? buf : chip->buffers->databuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001110
William Juul52c07962007-10-31 13:53:06 +01001111 if (likely(sndcmd)) {
1112 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1113 sndcmd = 0;
1114 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001115
William Juul52c07962007-10-31 13:53:06 +01001116 /* Now read the page into the buffer */
1117 if (unlikely(ops->mode == MTD_OOB_RAW))
1118 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
1119 else
1120 ret = chip->ecc.read_page(mtd, chip, bufpoi);
1121 if (ret < 0)
1122 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001123
William Juul52c07962007-10-31 13:53:06 +01001124 /* Transfer not aligned data */
1125 if (!aligned) {
1126 chip->pagebuf = realpage;
1127 memcpy(buf, chip->buffers->databuf + col, bytes);
1128 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001129
William Juul52c07962007-10-31 13:53:06 +01001130 buf += bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001131
William Juul52c07962007-10-31 13:53:06 +01001132 if (unlikely(oob)) {
1133 /* Raw mode does data:oob:data:oob */
1134 if (ops->mode != MTD_OOB_RAW) {
1135 int toread = min(oobreadlen,
1136 chip->ecc.layout->oobavail);
1137 if (toread) {
1138 oob = nand_transfer_oob(chip,
1139 oob, ops, toread);
1140 oobreadlen -= toread;
1141 }
1142 } else
1143 buf = nand_transfer_oob(chip,
1144 buf, ops, mtd->oobsize);
1145 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001146
William Juul52c07962007-10-31 13:53:06 +01001147 if (!(chip->options & NAND_NO_READRDY)) {
1148 /*
1149 * Apply delay or wait for ready/busy pin. Do
1150 * this before the AUTOINCR check, so no
1151 * problems arise if a chip which does auto
1152 * increment is marked as NOAUTOINCR by the
1153 * board driver.
1154 */
1155 if (!chip->dev_ready)
1156 udelay(chip->chip_delay);
1157 else
1158 nand_wait_ready(mtd);
1159 }
1160 } else {
1161 memcpy(buf, chip->buffers->databuf + col, bytes);
1162 buf += bytes;
1163 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001164
William Juul52c07962007-10-31 13:53:06 +01001165 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001166
William Juul52c07962007-10-31 13:53:06 +01001167 if (!readlen)
1168 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001169
William Juul52c07962007-10-31 13:53:06 +01001170 /* For subsequent reads align to page boundary. */
1171 col = 0;
1172 /* Increment page address */
1173 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001174
William Juul52c07962007-10-31 13:53:06 +01001175 page = realpage & chip->pagemask;
1176 /* Check, if we cross a chip boundary */
1177 if (!page) {
1178 chipnr++;
1179 chip->select_chip(mtd, -1);
1180 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001181 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001182
William Juul52c07962007-10-31 13:53:06 +01001183 /* Check, if the chip supports auto page increment
1184 * or if we have hit a block boundary.
1185 */
1186 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1187 sndcmd = 1;
1188 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001189
William Juul52c07962007-10-31 13:53:06 +01001190 ops->retlen = ops->len - (size_t) readlen;
1191 if (oob)
1192 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001193
William Juul52c07962007-10-31 13:53:06 +01001194 if (ret)
1195 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001196
William Juul52c07962007-10-31 13:53:06 +01001197 if (mtd->ecc_stats.failed - stats.failed)
1198 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001199
William Juul52c07962007-10-31 13:53:06 +01001200 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1201}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001202
William Juul52c07962007-10-31 13:53:06 +01001203/**
1204 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1205 * @mtd: MTD device structure
1206 * @from: offset to read from
1207 * @len: number of bytes to read
1208 * @retlen: pointer to variable to store the number of read bytes
1209 * @buf: the databuffer to put data
1210 *
1211 * Get hold of the chip and call nand_do_read
1212 */
1213static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1214 size_t *retlen, uint8_t *buf)
1215{
1216 struct nand_chip *chip = mtd->priv;
1217 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001218
William Juul52c07962007-10-31 13:53:06 +01001219 /* Do not allow reads past end of device */
1220 if ((from + len) > mtd->size)
1221 return -EINVAL;
1222 if (!len)
1223 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001224
William Juul52c07962007-10-31 13:53:06 +01001225 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001226
William Juul52c07962007-10-31 13:53:06 +01001227 chip->ops.len = len;
1228 chip->ops.datbuf = buf;
1229 chip->ops.oobbuf = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001230
William Juul52c07962007-10-31 13:53:06 +01001231 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001232
William Juul52c07962007-10-31 13:53:06 +01001233 *retlen = chip->ops.retlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001234
William Juul52c07962007-10-31 13:53:06 +01001235 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001236
William Juul52c07962007-10-31 13:53:06 +01001237 return ret;
1238}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001239
William Juul52c07962007-10-31 13:53:06 +01001240/**
1241 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1242 * @mtd: mtd info structure
1243 * @chip: nand chip info structure
1244 * @page: page number to read
1245 * @sndcmd: flag whether to issue read command or not
1246 */
1247static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1248 int page, int sndcmd)
1249{
1250 if (sndcmd) {
1251 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1252 sndcmd = 0;
1253 }
1254 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1255 return sndcmd;
1256}
1257
1258/**
1259 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1260 * with syndromes
1261 * @mtd: mtd info structure
1262 * @chip: nand chip info structure
1263 * @page: page number to read
1264 * @sndcmd: flag whether to issue read command or not
1265 */
1266static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1267 int page, int sndcmd)
1268{
1269 uint8_t *buf = chip->oob_poi;
1270 int length = mtd->oobsize;
1271 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1272 int eccsize = chip->ecc.size;
1273 uint8_t *bufpoi = buf;
1274 int i, toread, sndrnd = 0, pos;
1275
1276 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1277 for (i = 0; i < chip->ecc.steps; i++) {
1278 if (sndrnd) {
1279 pos = eccsize + i * (eccsize + chunk);
1280 if (mtd->writesize > 512)
1281 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1282 else
1283 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001284 } else
William Juul52c07962007-10-31 13:53:06 +01001285 sndrnd = 1;
1286 toread = min_t(int, length, chunk);
1287 chip->read_buf(mtd, bufpoi, toread);
1288 bufpoi += toread;
1289 length -= toread;
1290 }
1291 if (length > 0)
1292 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001293
William Juul52c07962007-10-31 13:53:06 +01001294 return 1;
1295}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001296
William Juul52c07962007-10-31 13:53:06 +01001297/**
1298 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1299 * @mtd: mtd info structure
1300 * @chip: nand chip info structure
1301 * @page: page number to write
1302 */
1303static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1304 int page)
1305{
1306 int status = 0;
1307 const uint8_t *buf = chip->oob_poi;
1308 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001309
William Juul52c07962007-10-31 13:53:06 +01001310 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1311 chip->write_buf(mtd, buf, length);
1312 /* Send command to program the OOB data */
1313 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001314
William Juul52c07962007-10-31 13:53:06 +01001315 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001316
William Juul52c07962007-10-31 13:53:06 +01001317 return status & NAND_STATUS_FAIL ? -EIO : 0;
1318}
1319
1320/**
1321 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1322 * with syndrome - only for large page flash !
1323 * @mtd: mtd info structure
1324 * @chip: nand chip info structure
1325 * @page: page number to write
1326 */
1327static int nand_write_oob_syndrome(struct mtd_info *mtd,
1328 struct nand_chip *chip, int page)
1329{
1330 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1331 int eccsize = chip->ecc.size, length = mtd->oobsize;
1332 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1333 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001334
1335 /*
William Juul52c07962007-10-31 13:53:06 +01001336 * data-ecc-data-ecc ... ecc-oob
1337 * or
1338 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001339 */
William Juul52c07962007-10-31 13:53:06 +01001340 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1341 pos = steps * (eccsize + chunk);
1342 steps = 0;
1343 } else
1344 pos = eccsize;
1345
1346 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1347 for (i = 0; i < steps; i++) {
1348 if (sndcmd) {
1349 if (mtd->writesize <= 512) {
1350 uint32_t fill = 0xFFFFFFFF;
1351
1352 len = eccsize;
1353 while (len > 0) {
1354 int num = min_t(int, len, 4);
1355 chip->write_buf(mtd, (uint8_t *)&fill,
1356 num);
1357 len -= num;
1358 }
1359 } else {
1360 pos = eccsize + i * (eccsize + chunk);
1361 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1362 }
1363 } else
1364 sndcmd = 1;
1365 len = min_t(int, length, chunk);
1366 chip->write_buf(mtd, bufpoi, len);
1367 bufpoi += len;
1368 length -= len;
1369 }
1370 if (length > 0)
1371 chip->write_buf(mtd, bufpoi, length);
1372
1373 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1374 status = chip->waitfunc(mtd, chip);
1375
1376 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001377}
1378
1379/**
William Juul52c07962007-10-31 13:53:06 +01001380 * nand_do_read_oob - [Intern] NAND read out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001381 * @mtd: MTD device structure
1382 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001383 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001384 *
1385 * NAND read out-of-band data from the spare area
1386 */
William Juul52c07962007-10-31 13:53:06 +01001387static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1388 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001389{
William Juul52c07962007-10-31 13:53:06 +01001390 int page, realpage, chipnr, sndcmd = 1;
1391 struct nand_chip *chip = mtd->priv;
1392 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1393 int readlen = ops->ooblen;
1394 int len;
1395 uint8_t *buf = ops->oobbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001396
William Juul52c07962007-10-31 13:53:06 +01001397 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1398 (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001399
William Juul52c07962007-10-31 13:53:06 +01001400 if (ops->mode == MTD_OOB_AUTO)
1401 len = chip->ecc.layout->oobavail;
1402 else
1403 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001404
William Juul52c07962007-10-31 13:53:06 +01001405 if (unlikely(ops->ooboffs >= len)) {
1406 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1407 "Attempt to start read outside oob\n");
1408 return -EINVAL;
1409 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001410
1411 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001412 if (unlikely(from >= mtd->size ||
1413 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1414 (from >> chip->page_shift)) * len)) {
1415 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1416 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001417 return -EINVAL;
1418 }
1419
William Juul52c07962007-10-31 13:53:06 +01001420 chipnr = (int)(from >> chip->chip_shift);
1421 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001422
William Juul52c07962007-10-31 13:53:06 +01001423 /* Shift to get page */
1424 realpage = (int)(from >> chip->page_shift);
1425 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001426
William Juul52c07962007-10-31 13:53:06 +01001427 while(1) {
1428 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001429
William Juul52c07962007-10-31 13:53:06 +01001430 len = min(len, readlen);
1431 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001432
William Juul52c07962007-10-31 13:53:06 +01001433 if (!(chip->options & NAND_NO_READRDY)) {
1434 /*
1435 * Apply delay or wait for ready/busy pin. Do this
1436 * before the AUTOINCR check, so no problems arise if a
1437 * chip which does auto increment is marked as
1438 * NOAUTOINCR by the board driver.
1439 */
1440 if (!chip->dev_ready)
1441 udelay(chip->chip_delay);
1442 else
1443 nand_wait_ready(mtd);
1444 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001445
William Juul52c07962007-10-31 13:53:06 +01001446 readlen -= len;
1447 if (!readlen)
1448 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001449
William Juul52c07962007-10-31 13:53:06 +01001450 /* Increment page address */
1451 realpage++;
1452
1453 page = realpage & chip->pagemask;
1454 /* Check, if we cross a chip boundary */
1455 if (!page) {
1456 chipnr++;
1457 chip->select_chip(mtd, -1);
1458 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001459 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001460
William Juul52c07962007-10-31 13:53:06 +01001461 /* Check, if the chip supports auto page increment
1462 * or if we have hit a block boundary.
1463 */
1464 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1465 sndcmd = 1;
1466 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001467
William Juul52c07962007-10-31 13:53:06 +01001468 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001469 return 0;
1470}
1471
1472/**
William Juul52c07962007-10-31 13:53:06 +01001473 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001474 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001475 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001476 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001477 *
William Juul52c07962007-10-31 13:53:06 +01001478 * NAND read data and/or out-of-band data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001479 */
William Juul52c07962007-10-31 13:53:06 +01001480static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1481 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001482{
William Juul52c07962007-10-31 13:53:06 +01001483 struct nand_chip *chip = mtd->priv;
1484 int ret = -ENOTSUPP;
1485
1486 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001487
1488 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001489 if (ops->datbuf && (from + ops->len) > mtd->size) {
1490 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1491 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001492 return -EINVAL;
1493 }
1494
William Juul52c07962007-10-31 13:53:06 +01001495 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001496
William Juul52c07962007-10-31 13:53:06 +01001497 switch(ops->mode) {
1498 case MTD_OOB_PLACE:
1499 case MTD_OOB_AUTO:
1500 case MTD_OOB_RAW:
1501 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001502
William Juul52c07962007-10-31 13:53:06 +01001503 default:
1504 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001505 }
1506
William Juul52c07962007-10-31 13:53:06 +01001507 if (!ops->datbuf)
1508 ret = nand_do_read_oob(mtd, from, ops);
1509 else
1510 ret = nand_do_read_ops(mtd, from, ops);
1511
1512 out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001513 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001514 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001515}
1516
1517
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001518/**
William Juul52c07962007-10-31 13:53:06 +01001519 * nand_write_page_raw - [Intern] raw page write function
1520 * @mtd: mtd info structure
1521 * @chip: nand chip info structure
1522 * @buf: data buffer
1523 */
1524static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1525 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001526{
William Juul52c07962007-10-31 13:53:06 +01001527 chip->write_buf(mtd, buf, mtd->writesize);
1528 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1529}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001530
William Juul52c07962007-10-31 13:53:06 +01001531/**
1532 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1533 * @mtd: mtd info structure
1534 * @chip: nand chip info structure
1535 * @buf: data buffer
1536 */
1537static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1538 const uint8_t *buf)
1539{
1540 int i, eccsize = chip->ecc.size;
1541 int eccbytes = chip->ecc.bytes;
1542 int eccsteps = chip->ecc.steps;
1543 uint8_t *ecc_calc = chip->buffers->ecccalc;
1544 const uint8_t *p = buf;
1545 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001546
William Juul52c07962007-10-31 13:53:06 +01001547 /* Software ecc calculation */
1548 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1549 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001550
William Juul52c07962007-10-31 13:53:06 +01001551 for (i = 0; i < chip->ecc.total; i++)
1552 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001553
William Juul52c07962007-10-31 13:53:06 +01001554 chip->ecc.write_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001555}
1556
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001557/**
William Juul52c07962007-10-31 13:53:06 +01001558 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1559 * @mtd: mtd info structure
1560 * @chip: nand chip info structure
1561 * @buf: data buffer
1562 */
1563static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1564 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001565{
William Juul52c07962007-10-31 13:53:06 +01001566 int i, eccsize = chip->ecc.size;
1567 int eccbytes = chip->ecc.bytes;
1568 int eccsteps = chip->ecc.steps;
1569 uint8_t *ecc_calc = chip->buffers->ecccalc;
1570 const uint8_t *p = buf;
1571 uint32_t *eccpos = chip->ecc.layout->eccpos;
1572
1573 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1574 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1575 chip->write_buf(mtd, p, eccsize);
1576 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1577 }
1578
1579 for (i = 0; i < chip->ecc.total; i++)
1580 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1581
1582 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001583}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001584
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001585/**
William Juul52c07962007-10-31 13:53:06 +01001586 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1587 * @mtd: mtd info structure
1588 * @chip: nand chip info structure
1589 * @buf: data buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001590 *
William Juul52c07962007-10-31 13:53:06 +01001591 * The hw generator calculates the error syndrome automatically. Therefor
1592 * we need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001593 */
William Juul52c07962007-10-31 13:53:06 +01001594static void nand_write_page_syndrome(struct mtd_info *mtd,
1595 struct nand_chip *chip, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001596{
William Juul52c07962007-10-31 13:53:06 +01001597 int i, eccsize = chip->ecc.size;
1598 int eccbytes = chip->ecc.bytes;
1599 int eccsteps = chip->ecc.steps;
1600 const uint8_t *p = buf;
1601 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001602
William Juul52c07962007-10-31 13:53:06 +01001603 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001604
William Juul52c07962007-10-31 13:53:06 +01001605 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1606 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001607
William Juul52c07962007-10-31 13:53:06 +01001608 if (chip->ecc.prepad) {
1609 chip->write_buf(mtd, oob, chip->ecc.prepad);
1610 oob += chip->ecc.prepad;
1611 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001612
William Juul52c07962007-10-31 13:53:06 +01001613 chip->ecc.calculate(mtd, p, oob);
1614 chip->write_buf(mtd, oob, eccbytes);
1615 oob += eccbytes;
1616
1617 if (chip->ecc.postpad) {
1618 chip->write_buf(mtd, oob, chip->ecc.postpad);
1619 oob += chip->ecc.postpad;
1620 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001621 }
1622
William Juul52c07962007-10-31 13:53:06 +01001623 /* Calculate remaining oob bytes */
1624 i = mtd->oobsize - (oob - chip->oob_poi);
1625 if (i)
1626 chip->write_buf(mtd, oob, i);
1627}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001628
William Juul52c07962007-10-31 13:53:06 +01001629/**
1630 * nand_write_page - [REPLACEABLE] write one page
1631 * @mtd: MTD device structure
1632 * @chip: NAND chip descriptor
1633 * @buf: the data to write
1634 * @page: page number to write
1635 * @cached: cached programming
1636 * @raw: use _raw version of write_page
1637 */
1638static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1639 const uint8_t *buf, int page, int cached, int raw)
1640{
1641 int status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001642
William Juul52c07962007-10-31 13:53:06 +01001643 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1644
1645 if (unlikely(raw))
1646 chip->ecc.write_page_raw(mtd, chip, buf);
1647 else
1648 chip->ecc.write_page(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001649
William Juul52c07962007-10-31 13:53:06 +01001650 /*
1651 * Cached progamming disabled for now, Not sure if its worth the
1652 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1653 */
1654 cached = 0;
1655
1656 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1657
1658 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1659 status = chip->waitfunc(mtd, chip);
1660 /*
1661 * See if operation failed and additional status checks are
1662 * available
1663 */
1664 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1665 status = chip->errstat(mtd, chip, FL_WRITING, status,
1666 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001667
William Juul52c07962007-10-31 13:53:06 +01001668 if (status & NAND_STATUS_FAIL)
1669 return -EIO;
1670 } else {
1671 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1672 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001673 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001674
William Juul52c07962007-10-31 13:53:06 +01001675#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1676 /* Send command to read back the data */
1677 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001678
William Juul52c07962007-10-31 13:53:06 +01001679 if (chip->verify_buf(mtd, buf, mtd->writesize))
1680 return -EIO;
1681#endif
1682 return 0;
1683}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001684
William Juul52c07962007-10-31 13:53:06 +01001685/**
1686 * nand_fill_oob - [Internal] Transfer client buffer to oob
1687 * @chip: nand chip structure
1688 * @oob: oob data buffer
1689 * @ops: oob ops structure
1690 */
1691static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1692 struct mtd_oob_ops *ops)
1693{
1694 size_t len = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001695
William Juul52c07962007-10-31 13:53:06 +01001696 switch(ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001697
William Juul52c07962007-10-31 13:53:06 +01001698 case MTD_OOB_PLACE:
1699 case MTD_OOB_RAW:
1700 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1701 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001702
William Juul52c07962007-10-31 13:53:06 +01001703 case MTD_OOB_AUTO: {
1704 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1705 uint32_t boffs = 0, woffs = ops->ooboffs;
1706 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001707
William Juul52c07962007-10-31 13:53:06 +01001708 for(; free->length && len; free++, len -= bytes) {
1709 /* Write request not from offset 0 ? */
1710 if (unlikely(woffs)) {
1711 if (woffs >= free->length) {
1712 woffs -= free->length;
1713 continue;
1714 }
1715 boffs = free->offset + woffs;
1716 bytes = min_t(size_t, len,
1717 (free->length - woffs));
1718 woffs = 0;
1719 } else {
1720 bytes = min_t(size_t, len, free->length);
1721 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001722 }
William Juul52c07962007-10-31 13:53:06 +01001723 memcpy(chip->oob_poi + boffs, oob, bytes);
1724 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001725 }
William Juul52c07962007-10-31 13:53:06 +01001726 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001727 }
William Juul52c07962007-10-31 13:53:06 +01001728 default:
1729 BUG();
1730 }
1731 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001732}
1733
William Juul52c07962007-10-31 13:53:06 +01001734#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001735
1736/**
William Juul52c07962007-10-31 13:53:06 +01001737 * nand_do_write_ops - [Internal] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001738 * @mtd: MTD device structure
1739 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001740 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001741 *
William Juul52c07962007-10-31 13:53:06 +01001742 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001743 */
William Juul52c07962007-10-31 13:53:06 +01001744static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1745 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001746{
William Juul52c07962007-10-31 13:53:06 +01001747 int chipnr, realpage, page, blockmask, column;
1748 struct nand_chip *chip = mtd->priv;
1749 uint32_t writelen = ops->len;
1750 uint8_t *oob = ops->oobbuf;
1751 uint8_t *buf = ops->datbuf;
1752 int ret, subpage;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001753
William Juul52c07962007-10-31 13:53:06 +01001754 ops->retlen = 0;
1755 if (!writelen)
1756 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001757
William Juul52c07962007-10-31 13:53:06 +01001758 /* reject writes, which are not page aligned */
1759 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
1760 printk(KERN_NOTICE "nand_write: "
1761 "Attempt to write not page aligned data\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001762 return -EINVAL;
1763 }
1764
William Juul52c07962007-10-31 13:53:06 +01001765 column = to & (mtd->writesize - 1);
1766 subpage = column || (writelen & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001767
William Juul52c07962007-10-31 13:53:06 +01001768 if (subpage && oob)
1769 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001770
William Juul52c07962007-10-31 13:53:06 +01001771 chipnr = (int)(to >> chip->chip_shift);
1772 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001773
1774 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01001775 if (nand_check_wp(mtd)) {
1776 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1777 return -EIO;
1778 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001779
William Juul52c07962007-10-31 13:53:06 +01001780 realpage = (int)(to >> chip->page_shift);
1781 page = realpage & chip->pagemask;
1782 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001783
William Juul52c07962007-10-31 13:53:06 +01001784 /* Invalidate the page cache, when we write to the cached page */
1785 if (to <= (chip->pagebuf << chip->page_shift) &&
1786 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1787 chip->pagebuf = -1;
1788
1789 /* If we're not given explicit OOB data, let it be 0xFF */
1790 if (likely(!oob))
1791 memset(chip->oob_poi, 0xff, mtd->oobsize);
1792
1793 while(1) {
1794 int bytes = mtd->writesize;
1795 int cached = writelen > bytes && page != blockmask;
1796 uint8_t *wbuf = buf;
1797
1798 /* Partial page write ? */
1799 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1800 cached = 0;
1801 bytes = min_t(int, bytes - column, (int) writelen);
1802 chip->pagebuf = -1;
1803 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1804 memcpy(&chip->buffers->databuf[column], buf, bytes);
1805 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02001806 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001807
William Juul52c07962007-10-31 13:53:06 +01001808 if (unlikely(oob))
1809 oob = nand_fill_oob(chip, oob, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001810
William Juul52c07962007-10-31 13:53:06 +01001811 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1812 (ops->mode == MTD_OOB_RAW));
1813 if (ret)
1814 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001815
William Juul52c07962007-10-31 13:53:06 +01001816 writelen -= bytes;
1817 if (!writelen)
1818 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001819
William Juul52c07962007-10-31 13:53:06 +01001820 column = 0;
1821 buf += bytes;
1822 realpage++;
1823
1824 page = realpage & chip->pagemask;
1825 /* Check, if we cross a chip boundary */
1826 if (!page) {
1827 chipnr++;
1828 chip->select_chip(mtd, -1);
1829 chip->select_chip(mtd, chipnr);
1830 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001831 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001832
William Juul52c07962007-10-31 13:53:06 +01001833 ops->retlen = ops->len - writelen;
1834 if (unlikely(oob))
1835 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001836 return ret;
1837}
1838
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001839/**
William Juul52c07962007-10-31 13:53:06 +01001840 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001841 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001842 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001843 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001844 * @retlen: pointer to variable to store the number of written bytes
William Juul52c07962007-10-31 13:53:06 +01001845 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001846 *
William Juul52c07962007-10-31 13:53:06 +01001847 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001848 */
William Juul52c07962007-10-31 13:53:06 +01001849static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1850 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001851{
William Juul52c07962007-10-31 13:53:06 +01001852 struct nand_chip *chip = mtd->priv;
1853 int ret;
1854
1855 /* Do not allow reads past end of device */
1856 if ((to + len) > mtd->size)
1857 return -EINVAL;
1858 if (!len)
1859 return 0;
1860
1861 nand_get_device(chip, mtd, FL_WRITING);
1862
1863 chip->ops.len = len;
1864 chip->ops.datbuf = (uint8_t *)buf;
1865 chip->ops.oobbuf = NULL;
1866
1867 ret = nand_do_write_ops(mtd, to, &chip->ops);
1868
1869 *retlen = chip->ops.retlen;
1870
1871 nand_release_device(mtd);
1872
1873 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001874}
1875
1876/**
William Juul52c07962007-10-31 13:53:06 +01001877 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001878 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001879 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001880 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001881 *
William Juul52c07962007-10-31 13:53:06 +01001882 * NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001883 */
William Juul52c07962007-10-31 13:53:06 +01001884static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1885 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001886{
William Juul52c07962007-10-31 13:53:06 +01001887 int chipnr, page, status, len;
1888 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001889
William Juul52c07962007-10-31 13:53:06 +01001890 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1891 (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001892
William Juul52c07962007-10-31 13:53:06 +01001893 if (ops->mode == MTD_OOB_AUTO)
1894 len = chip->ecc.layout->oobavail;
1895 else
1896 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001897
1898 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01001899 if ((ops->ooboffs + ops->ooblen) > len) {
1900 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1901 "Attempt to write past end of page\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001902 return -EINVAL;
1903 }
1904
William Juul52c07962007-10-31 13:53:06 +01001905 if (unlikely(ops->ooboffs >= len)) {
1906 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1907 "Attempt to start write outside oob\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001908 return -EINVAL;
1909 }
1910
William Juul52c07962007-10-31 13:53:06 +01001911 /* Do not allow reads past end of device */
1912 if (unlikely(to >= mtd->size ||
1913 ops->ooboffs + ops->ooblen >
1914 ((mtd->size >> chip->page_shift) -
1915 (to >> chip->page_shift)) * len)) {
1916 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1917 "Attempt write beyond end of device\n");
1918 return -EINVAL;
1919 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001920
William Juul52c07962007-10-31 13:53:06 +01001921 chipnr = (int)(to >> chip->chip_shift);
1922 chip->select_chip(mtd, chipnr);
1923
1924 /* Shift to get page */
1925 page = (int)(to >> chip->page_shift);
1926
1927 /*
1928 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1929 * of my DiskOnChip 2000 test units) will clear the whole data page too
1930 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1931 * it in the doc2000 driver in August 1999. dwmw2.
1932 */
1933 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001934
1935 /* Check, if it is write protected */
1936 if (nand_check_wp(mtd))
William Juul52c07962007-10-31 13:53:06 +01001937 return -EROFS;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001938
William Juul52c07962007-10-31 13:53:06 +01001939 /* Invalidate the page cache, if we write to the cached page */
1940 if (page == chip->pagebuf)
1941 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001942
William Juul52c07962007-10-31 13:53:06 +01001943 memset(chip->oob_poi, 0xff, mtd->oobsize);
1944 nand_fill_oob(chip, ops->oobbuf, ops);
1945 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1946 memset(chip->oob_poi, 0xff, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001947
William Juul52c07962007-10-31 13:53:06 +01001948 if (status)
1949 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001950
William Juul52c07962007-10-31 13:53:06 +01001951 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001952
William Juul52c07962007-10-31 13:53:06 +01001953 return 0;
1954}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001955
William Juul52c07962007-10-31 13:53:06 +01001956/**
1957 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1958 * @mtd: MTD device structure
1959 * @to: offset to write to
1960 * @ops: oob operation description structure
1961 */
1962static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1963 struct mtd_oob_ops *ops)
1964{
1965 struct nand_chip *chip = mtd->priv;
1966 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001967
William Juul52c07962007-10-31 13:53:06 +01001968 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001969
William Juul52c07962007-10-31 13:53:06 +01001970 /* Do not allow writes past end of device */
1971 if (ops->datbuf && (to + ops->len) > mtd->size) {
1972 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1973 "Attempt read beyond end of device\n");
1974 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001975 }
William Juul52c07962007-10-31 13:53:06 +01001976
1977 nand_get_device(chip, mtd, FL_WRITING);
1978
1979 switch(ops->mode) {
1980 case MTD_OOB_PLACE:
1981 case MTD_OOB_AUTO:
1982 case MTD_OOB_RAW:
1983 break;
1984
1985 default:
1986 goto out;
1987 }
1988
1989 if (!ops->datbuf)
1990 ret = nand_do_write_oob(mtd, to, ops);
1991 else
1992 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001993
William Juul52c07962007-10-31 13:53:06 +01001994 out:
1995 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001996 return ret;
1997}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001998
1999/**
2000 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2001 * @mtd: MTD device structure
2002 * @page: the page address of the block which will be erased
2003 *
2004 * Standard erase command for NAND chips
2005 */
William Juul52c07962007-10-31 13:53:06 +01002006static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002007{
William Juul52c07962007-10-31 13:53:06 +01002008 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002009 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002010 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2011 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002012}
2013
2014/**
2015 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2016 * @mtd: MTD device structure
2017 * @page: the page address of the block which will be erased
2018 *
2019 * AND multi block erase command function
2020 * Erase 4 consecutive blocks
2021 */
William Juul52c07962007-10-31 13:53:06 +01002022static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002023{
William Juul52c07962007-10-31 13:53:06 +01002024 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002025 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002026 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2027 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2028 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2029 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2030 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002031}
2032
2033/**
2034 * nand_erase - [MTD Interface] erase block(s)
2035 * @mtd: MTD device structure
2036 * @instr: erase instruction
2037 *
2038 * Erase one ore more blocks
2039 */
William Juul52c07962007-10-31 13:53:06 +01002040static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002041{
William Juul52c07962007-10-31 13:53:06 +01002042 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002043}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002044
William Juul52c07962007-10-31 13:53:06 +01002045#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002046/**
William Juul52c07962007-10-31 13:53:06 +01002047 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002048 * @mtd: MTD device structure
2049 * @instr: erase instruction
2050 * @allowbbt: allow erasing the bbt area
2051 *
2052 * Erase one ore more blocks
2053 */
William Juul52c07962007-10-31 13:53:06 +01002054int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2055 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002056{
2057 int page, len, status, pages_per_block, ret, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002058 struct nand_chip *chip = mtd->priv;
2059 int rewrite_bbt[NAND_MAX_CHIPS]={0};
2060 unsigned int bbt_masked_page = 0xffffffff;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002061
Scott Wooddf83c472008-06-20 12:38:57 -05002062 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2063 (unsigned int) instr->addr, (unsigned int) instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002064
2065 /* Start address must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002066 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002067 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002068 return -EINVAL;
2069 }
2070
2071 /* Length must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002072 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002073 MTDDEBUG (MTD_DEBUG_LEVEL0,
2074 "nand_erase: Length not block aligned\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002075 return -EINVAL;
2076 }
2077
2078 /* Do not allow erase past end of device */
2079 if ((instr->len + instr->addr) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05002080 MTDDEBUG (MTD_DEBUG_LEVEL0,
2081 "nand_erase: Erase past end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002082 return -EINVAL;
2083 }
2084
2085 instr->fail_addr = 0xffffffff;
2086
2087 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002088 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002089
2090 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002091 page = (int)(instr->addr >> chip->page_shift);
2092 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002093
2094 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002095 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002096
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002097 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002098 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002099
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002100 /* Check, if it is write protected */
2101 if (nand_check_wp(mtd)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002102 MTDDEBUG (MTD_DEBUG_LEVEL0,
2103 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002104 instr->state = MTD_ERASE_FAILED;
2105 goto erase_exit;
2106 }
2107
William Juul52c07962007-10-31 13:53:06 +01002108 /*
2109 * If BBT requires refresh, set the BBT page mask to see if the BBT
2110 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2111 * can not be matched. This is also done when the bbt is actually
2112 * erased to avoid recusrsive updates
2113 */
2114 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2115 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2116
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002117 /* Loop through the pages */
2118 len = instr->len;
2119
2120 instr->state = MTD_ERASING;
2121
2122 while (len) {
William Juul52c07962007-10-31 13:53:06 +01002123 /*
2124 * heck if we have a bad block, we do not erase bad blocks !
2125 */
2126 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2127 chip->page_shift, 0, allowbbt)) {
2128 printk(KERN_WARNING "nand_erase: attempt to erase a "
2129 "bad block at page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002130 instr->state = MTD_ERASE_FAILED;
2131 goto erase_exit;
2132 }
William Juul52c07962007-10-31 13:53:06 +01002133
2134 /*
2135 * Invalidate the page cache, if we erase the block which
2136 * contains the current cached page
2137 */
2138 if (page <= chip->pagebuf && chip->pagebuf <
2139 (page + pages_per_block))
2140 chip->pagebuf = -1;
2141
2142 chip->erase_cmd(mtd, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002143
William Juul52c07962007-10-31 13:53:06 +01002144 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002145
William Juul52c07962007-10-31 13:53:06 +01002146 /*
2147 * See if operation failed and additional status checks are
2148 * available
2149 */
2150 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2151 status = chip->errstat(mtd, chip, FL_ERASING,
2152 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002153
2154 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002155 if (status & NAND_STATUS_FAIL) {
Scott Wooddf83c472008-06-20 12:38:57 -05002156 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2157 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002158 instr->state = MTD_ERASE_FAILED;
William Juul52c07962007-10-31 13:53:06 +01002159 instr->fail_addr = (page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002160 goto erase_exit;
2161 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002162
William Juul52c07962007-10-31 13:53:06 +01002163 /*
2164 * If BBT requires refresh, set the BBT rewrite flag to the
2165 * page being erased
2166 */
2167 if (bbt_masked_page != 0xffffffff &&
2168 (page & BBT_PAGE_MASK) == bbt_masked_page)
2169 rewrite_bbt[chipnr] = (page << chip->page_shift);
2170
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002171 /* Increment page address and decrement length */
William Juul52c07962007-10-31 13:53:06 +01002172 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002173 page += pages_per_block;
2174
2175 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002176 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002177 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002178 chip->select_chip(mtd, -1);
2179 chip->select_chip(mtd, chipnr);
2180
2181 /*
2182 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2183 * page mask to see if this BBT should be rewritten
2184 */
2185 if (bbt_masked_page != 0xffffffff &&
2186 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2187 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2188 BBT_PAGE_MASK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002189 }
2190 }
2191 instr->state = MTD_ERASE_DONE;
2192
William Juul52c07962007-10-31 13:53:06 +01002193 erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002194
2195 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2196 /* Do call back function */
2197 if (!ret)
2198 mtd_erase_callback(instr);
2199
2200 /* Deselect and wake up anyone waiting on the device */
2201 nand_release_device(mtd);
2202
William Juul52c07962007-10-31 13:53:06 +01002203 /*
2204 * If BBT requires refresh and erase was successful, rewrite any
2205 * selected bad block tables
2206 */
2207 if (bbt_masked_page == 0xffffffff || ret)
2208 return ret;
2209
2210 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2211 if (!rewrite_bbt[chipnr])
2212 continue;
2213 /* update the BBT for chip */
2214 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2215 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2216 chip->bbt_td->pages[chipnr]);
2217 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2218 }
2219
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002220 /* Return more or less happy */
2221 return ret;
2222}
2223
2224/**
2225 * nand_sync - [MTD Interface] sync
2226 * @mtd: MTD device structure
2227 *
2228 * Sync is actually a wait for chip ready function
2229 */
William Juul52c07962007-10-31 13:53:06 +01002230static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002231{
William Juul52c07962007-10-31 13:53:06 +01002232 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002233
Scott Wooddf83c472008-06-20 12:38:57 -05002234 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002235
2236 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002237 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002238 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002239 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002240}
2241
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002242/**
William Juul52c07962007-10-31 13:53:06 +01002243 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002244 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002245 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002246 */
William Juul52c07962007-10-31 13:53:06 +01002247static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002248{
2249 /* Check for invalid offset */
William Juul52c07962007-10-31 13:53:06 +01002250 if (offs > mtd->size)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002251 return -EINVAL;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002252
William Juul52c07962007-10-31 13:53:06 +01002253 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002254}
2255
2256/**
William Juul52c07962007-10-31 13:53:06 +01002257 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002258 * @mtd: MTD device structure
2259 * @ofs: offset relative to mtd start
2260 */
William Juul52c07962007-10-31 13:53:06 +01002261static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002262{
William Juul52c07962007-10-31 13:53:06 +01002263 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002264 int ret;
2265
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002266 if ((ret = nand_block_isbad(mtd, ofs))) {
2267 /* If it was bad already, return success and do nothing. */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002268 if (ret > 0)
2269 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002270 return ret;
2271 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002272
William Juul52c07962007-10-31 13:53:06 +01002273 return chip->block_markbad(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002274}
2275
2276/**
William Juul52c07962007-10-31 13:53:06 +01002277 * nand_suspend - [MTD Interface] Suspend the NAND flash
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002278 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002279 */
William Juul52c07962007-10-31 13:53:06 +01002280static int nand_suspend(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002281{
William Juul52c07962007-10-31 13:53:06 +01002282 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002283
William Juul52c07962007-10-31 13:53:06 +01002284 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2285}
2286
2287/**
2288 * nand_resume - [MTD Interface] Resume the NAND flash
2289 * @mtd: MTD device structure
2290 */
2291static void nand_resume(struct mtd_info *mtd)
2292{
2293 struct nand_chip *chip = mtd->priv;
2294
2295 if (chip->state == FL_PM_SUSPENDED)
2296 nand_release_device(mtd);
2297 else
2298 printk(KERN_ERR "nand_resume() called for a chip which is not "
2299 "in suspended state\n");
2300}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002301
William Juul52c07962007-10-31 13:53:06 +01002302/*
2303 * Set default functions
2304 */
2305static void nand_set_defaults(struct nand_chip *chip, int busw)
2306{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002307 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01002308 if (!chip->chip_delay)
2309 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002310
2311 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01002312 if (chip->cmdfunc == NULL)
2313 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002314
2315 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01002316 if (chip->waitfunc == NULL)
2317 chip->waitfunc = nand_wait;
2318
2319 if (!chip->select_chip)
2320 chip->select_chip = nand_select_chip;
2321 if (!chip->read_byte)
2322 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2323 if (!chip->read_word)
2324 chip->read_word = nand_read_word;
2325 if (!chip->block_bad)
2326 chip->block_bad = nand_block_bad;
2327 if (!chip->block_markbad)
2328 chip->block_markbad = nand_default_block_markbad;
2329 if (!chip->write_buf)
2330 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2331 if (!chip->read_buf)
2332 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2333 if (!chip->verify_buf)
2334 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2335 if (!chip->scan_bbt)
2336 chip->scan_bbt = nand_default_bbt;
2337
2338 if (!chip->controller) {
2339 chip->controller = &chip->hwcontrol;
2340
2341 /* XXX U-BOOT XXX */
2342#if 0
2343 spin_lock_init(&chip->controller->lock);
2344 init_waitqueue_head(&chip->controller->wq);
2345#endif
2346 }
2347
2348}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002349
William Juul52c07962007-10-31 13:53:06 +01002350/*
2351 * Get the flash and manufacturer id and lookup if the type is supported
2352 */
2353static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2354 struct nand_chip *chip,
2355 int busw, int *maf_id)
2356{
2357 struct nand_flash_dev *type = NULL;
2358 int i, dev_id, maf_idx;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002359
2360 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01002361 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002362
2363 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01002364 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002365
2366 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002367 *maf_id = chip->read_byte(mtd);
2368 dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002369
William Juul52c07962007-10-31 13:53:06 +01002370 /* Lookup the flash id */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002371 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
William Juul52c07962007-10-31 13:53:06 +01002372 if (dev_id == nand_flash_ids[i].id) {
2373 type = &nand_flash_ids[i];
2374 break;
2375 }
2376 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002377
William Juul52c07962007-10-31 13:53:06 +01002378 if (!type)
2379 return ERR_PTR(-ENODEV);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002380
William Juul52c07962007-10-31 13:53:06 +01002381 if (!mtd->name)
2382 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002383
William Juul52c07962007-10-31 13:53:06 +01002384 chip->chipsize = type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002385
William Juul52c07962007-10-31 13:53:06 +01002386 /* Newer devices have all the information in additional id bytes */
2387 if (!type->pagesize) {
2388 int extid;
2389 /* The 3rd id byte holds MLC / multichip data */
2390 chip->cellinfo = chip->read_byte(mtd);
2391 /* The 4th id byte is the important one */
2392 extid = chip->read_byte(mtd);
2393 /* Calc pagesize */
2394 mtd->writesize = 1024 << (extid & 0x3);
2395 extid >>= 2;
2396 /* Calc oobsize */
2397 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2398 extid >>= 2;
2399 /* Calc blocksize. Blocksize is multiples of 64KiB */
2400 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2401 extid >>= 2;
2402 /* Get buswidth information */
2403 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002404
William Juul52c07962007-10-31 13:53:06 +01002405 } else {
2406 /*
2407 * Old devices have chip data hardcoded in the device id table
2408 */
2409 mtd->erasesize = type->erasesize;
2410 mtd->writesize = type->pagesize;
2411 mtd->oobsize = mtd->writesize / 32;
2412 busw = type->options & NAND_BUSWIDTH_16;
2413 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002414
William Juul52c07962007-10-31 13:53:06 +01002415 /* Try to identify manufacturer */
2416 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2417 if (nand_manuf_ids[maf_idx].id == *maf_id)
2418 break;
2419 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002420
William Juul52c07962007-10-31 13:53:06 +01002421 /*
2422 * Check, if buswidth is correct. Hardware drivers should set
2423 * chip correct !
2424 */
2425 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2426 printk(KERN_INFO "NAND device: Manufacturer ID:"
2427 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2428 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2429 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2430 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2431 busw ? 16 : 8);
2432 return ERR_PTR(-EINVAL);
2433 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002434
William Juul52c07962007-10-31 13:53:06 +01002435 /* Calculate the address shift from the page size */
2436 chip->page_shift = ffs(mtd->writesize) - 1;
2437 /* Convert chipsize to number of pages per chip -1. */
2438 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002439
William Juul52c07962007-10-31 13:53:06 +01002440 chip->bbt_erase_shift = chip->phys_erase_shift =
2441 ffs(mtd->erasesize) - 1;
2442 chip->chip_shift = ffs(chip->chipsize) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002443
William Juul52c07962007-10-31 13:53:06 +01002444 /* Set the bad block position */
2445 chip->badblockpos = mtd->writesize > 512 ?
2446 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002447
William Juul52c07962007-10-31 13:53:06 +01002448 /* Get chip options, preserve non chip based options */
2449 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2450 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002451
William Juul52c07962007-10-31 13:53:06 +01002452 /*
2453 * Set chip as a default. Board drivers can override it, if necessary
2454 */
2455 chip->options |= NAND_NO_AUTOINCR;
2456
2457 /* Check if chip is a not a samsung device. Do not clear the
2458 * options for chips which are not having an extended id.
2459 */
2460 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2461 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2462
2463 /* Check for AND chips with 4 page planes */
2464 if (chip->options & NAND_4PAGE_ARRAY)
2465 chip->erase_cmd = multi_erase_cmd;
2466 else
2467 chip->erase_cmd = single_erase_cmd;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002468
William Juul52c07962007-10-31 13:53:06 +01002469 /* Do not replace user supplied command function ! */
2470 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2471 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002472
Stefan Roese4fe71432008-01-10 18:47:33 +01002473 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2474 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2475 nand_manuf_ids[maf_idx].name, type->name);
William Juul52c07962007-10-31 13:53:06 +01002476
2477 return type;
2478}
2479
2480/**
2481 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2482 * @mtd: MTD device structure
2483 * @maxchips: Number of chips to scan for
2484 *
2485 * This is the first phase of the normal nand_scan() function. It
2486 * reads the flash ID and sets up MTD fields accordingly.
2487 *
2488 * The mtd->owner field must be set to the module of the caller.
2489 */
2490int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2491{
2492 int i, busw, nand_maf_id;
2493 struct nand_chip *chip = mtd->priv;
2494 struct nand_flash_dev *type;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002495
William Juul52c07962007-10-31 13:53:06 +01002496 /* Get buswidth to select the correct functions */
2497 busw = chip->options & NAND_BUSWIDTH_16;
2498 /* Set the default functions */
2499 nand_set_defaults(chip, busw);
2500
2501 /* Read the flash type */
2502 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2503
2504 if (IS_ERR(type)) {
2505 printk(KERN_WARNING "No NAND device found!!!\n");
2506 chip->select_chip(mtd, -1);
2507 return PTR_ERR(type);
2508 }
2509
2510 /* Check for a chip array */
2511 for (i = 1; i < maxchips; i++) {
2512 chip->select_chip(mtd, i);
2513 /* Send the command for reading device ID */
2514 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002515 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002516 if (nand_maf_id != chip->read_byte(mtd) ||
2517 type->id != chip->read_byte(mtd))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002518 break;
2519 }
2520 if (i > 1)
2521 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002522
William Juul52c07962007-10-31 13:53:06 +01002523 /* Store the number of chips and calc total size for mtd */
2524 chip->numchips = i;
2525 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002526
William Juul52c07962007-10-31 13:53:06 +01002527 return 0;
2528}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002529
William Juul52c07962007-10-31 13:53:06 +01002530
2531/**
2532 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2533 * @mtd: MTD device structure
2534 * @maxchips: Number of chips to scan for
2535 *
2536 * This is the second phase of the normal nand_scan() function. It
2537 * fills out all the uninitialized function pointers with the defaults
2538 * and scans for a bad block table if appropriate.
2539 */
2540int nand_scan_tail(struct mtd_info *mtd)
2541{
2542 int i;
2543 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002544
William Juul52c07962007-10-31 13:53:06 +01002545 if (!(chip->options & NAND_OWN_BUFFERS))
2546 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2547 if (!chip->buffers)
2548 return -ENOMEM;
2549
2550 /* Set the internal oob buffer location, just after the page data */
2551 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2552
2553 /*
2554 * If no default placement scheme is given, select an appropriate one
2555 */
2556 if (!chip->ecc.layout) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002557 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002558 case 8:
William Juul52c07962007-10-31 13:53:06 +01002559 chip->ecc.layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002560 break;
2561 case 16:
William Juul52c07962007-10-31 13:53:06 +01002562 chip->ecc.layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002563 break;
2564 case 64:
William Juul52c07962007-10-31 13:53:06 +01002565 chip->ecc.layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002566 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002567 case 128:
William Juul52c07962007-10-31 13:53:06 +01002568 chip->ecc.layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002569 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002570 default:
William Juul52c07962007-10-31 13:53:06 +01002571 printk(KERN_WARNING "No oob scheme defined for "
2572 "oobsize %d\n", mtd->oobsize);
William Juul9e9c2c12007-11-09 13:32:30 +01002573/* BUG(); */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002574 }
2575 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002576
William Juul52c07962007-10-31 13:53:06 +01002577 if (!chip->write_page)
2578 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002579
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002580 /*
William Juul52c07962007-10-31 13:53:06 +01002581 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2582 * selected and we have 256 byte pagesize fallback to software ECC
2583 */
2584 if (!chip->ecc.read_page_raw)
2585 chip->ecc.read_page_raw = nand_read_page_raw;
2586 if (!chip->ecc.write_page_raw)
2587 chip->ecc.write_page_raw = nand_write_page_raw;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002588
William Juul52c07962007-10-31 13:53:06 +01002589 switch (chip->ecc.mode) {
2590 case NAND_ECC_HW:
2591 /* Use standard hwecc read page function ? */
2592 if (!chip->ecc.read_page)
2593 chip->ecc.read_page = nand_read_page_hwecc;
2594 if (!chip->ecc.write_page)
2595 chip->ecc.write_page = nand_write_page_hwecc;
2596 if (!chip->ecc.read_oob)
2597 chip->ecc.read_oob = nand_read_oob_std;
2598 if (!chip->ecc.write_oob)
2599 chip->ecc.write_oob = nand_write_oob_std;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002600
William Juul52c07962007-10-31 13:53:06 +01002601 case NAND_ECC_HW_SYNDROME:
Scott Wood5fd2c302008-03-18 15:29:14 -05002602 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2603 !chip->ecc.hwctl) &&
2604 (!chip->ecc.read_page ||
2605 chip->ecc.read_page == nand_read_page_hwecc ||
2606 !chip->ecc.write_page ||
2607 chip->ecc.write_page == nand_write_page_hwecc)) {
William Juul52c07962007-10-31 13:53:06 +01002608 printk(KERN_WARNING "No ECC functions supplied, "
2609 "Hardware ECC not possible\n");
2610 BUG();
2611 }
2612 /* Use standard syndrome read/write page function ? */
2613 if (!chip->ecc.read_page)
2614 chip->ecc.read_page = nand_read_page_syndrome;
2615 if (!chip->ecc.write_page)
2616 chip->ecc.write_page = nand_write_page_syndrome;
2617 if (!chip->ecc.read_oob)
2618 chip->ecc.read_oob = nand_read_oob_syndrome;
2619 if (!chip->ecc.write_oob)
2620 chip->ecc.write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002621
William Juul52c07962007-10-31 13:53:06 +01002622 if (mtd->writesize >= chip->ecc.size)
2623 break;
2624 printk(KERN_WARNING "%d byte HW ECC not possible on "
2625 "%d byte page size, fallback to SW ECC\n",
2626 chip->ecc.size, mtd->writesize);
2627 chip->ecc.mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002628
William Juul52c07962007-10-31 13:53:06 +01002629 case NAND_ECC_SOFT:
2630 chip->ecc.calculate = nand_calculate_ecc;
2631 chip->ecc.correct = nand_correct_data;
2632 chip->ecc.read_page = nand_read_page_swecc;
2633 chip->ecc.write_page = nand_write_page_swecc;
2634 chip->ecc.read_oob = nand_read_oob_std;
2635 chip->ecc.write_oob = nand_write_oob_std;
2636 chip->ecc.size = 256;
2637 chip->ecc.bytes = 3;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002638 break;
2639
William Juul52c07962007-10-31 13:53:06 +01002640 case NAND_ECC_NONE:
2641 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2642 "This is not recommended !!\n");
2643 chip->ecc.read_page = nand_read_page_raw;
2644 chip->ecc.write_page = nand_write_page_raw;
2645 chip->ecc.read_oob = nand_read_oob_std;
2646 chip->ecc.write_oob = nand_write_oob_std;
2647 chip->ecc.size = mtd->writesize;
2648 chip->ecc.bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002649 break;
2650
2651 default:
William Juul52c07962007-10-31 13:53:06 +01002652 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2653 chip->ecc.mode);
2654 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002655 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002656
William Juul52c07962007-10-31 13:53:06 +01002657 /*
2658 * The number of bytes available for a client to place data into
2659 * the out of band area
2660 */
2661 chip->ecc.layout->oobavail = 0;
2662 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2663 chip->ecc.layout->oobavail +=
2664 chip->ecc.layout->oobfree[i].length;
2665 mtd->oobavail = chip->ecc.layout->oobavail;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002666
William Juul52c07962007-10-31 13:53:06 +01002667 /*
2668 * Set the number of read / write steps for one page depending on ECC
2669 * mode
2670 */
2671 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2672 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2673 printk(KERN_WARNING "Invalid ecc parameters\n");
2674 BUG();
2675 }
2676 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002677
William Juul52c07962007-10-31 13:53:06 +01002678 /*
2679 * Allow subpage writes up to ecc.steps. Not possible for MLC
2680 * FLASH.
2681 */
2682 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2683 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2684 switch(chip->ecc.steps) {
2685 case 2:
2686 mtd->subpage_sft = 1;
2687 break;
2688 case 4:
2689 case 8:
2690 mtd->subpage_sft = 2;
2691 break;
2692 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002693 }
William Juul52c07962007-10-31 13:53:06 +01002694 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002695
William Juul52c07962007-10-31 13:53:06 +01002696 /* Initialize state */
2697 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002698
2699 /* De-select the device */
William Juul52c07962007-10-31 13:53:06 +01002700 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002701
2702 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01002703 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002704
2705 /* Fill in remaining MTD driver data */
2706 mtd->type = MTD_NANDFLASH;
William Juul52c07962007-10-31 13:53:06 +01002707 mtd->flags = MTD_CAP_NANDFLASH;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002708 mtd->erase = nand_erase;
2709 mtd->point = NULL;
2710 mtd->unpoint = NULL;
2711 mtd->read = nand_read;
2712 mtd->write = nand_write;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002713 mtd->read_oob = nand_read_oob;
2714 mtd->write_oob = nand_write_oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002715 mtd->sync = nand_sync;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002716 mtd->lock = NULL;
2717 mtd->unlock = NULL;
William Juul52c07962007-10-31 13:53:06 +01002718 mtd->suspend = nand_suspend;
2719 mtd->resume = nand_resume;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002720 mtd->block_isbad = nand_block_isbad;
2721 mtd->block_markbad = nand_block_markbad;
2722
William Juul52c07962007-10-31 13:53:06 +01002723 /* propagate ecc.layout to mtd_info */
2724 mtd->ecclayout = chip->ecc.layout;
2725
2726 /* Check, if we should skip the bad block table scan */
2727 if (chip->options & NAND_SKIP_BBTSCAN)
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02002728 chip->options |= NAND_BBT_SCANNED;
William Juul52c07962007-10-31 13:53:06 +01002729
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02002730 return 0;
William Juul52c07962007-10-31 13:53:06 +01002731}
2732
2733/* module_text_address() isn't exported, and it's mostly a pointless
2734 test if this is a module _anyway_ -- they'd have to try _really_ hard
2735 to call us from in-kernel code if the core NAND support is modular. */
2736#ifdef MODULE
2737#define caller_is_module() (1)
2738#else
2739#define caller_is_module() \
2740 module_text_address((unsigned long)__builtin_return_address(0))
2741#endif
2742
2743/**
2744 * nand_scan - [NAND Interface] Scan for the NAND device
2745 * @mtd: MTD device structure
2746 * @maxchips: Number of chips to scan for
2747 *
2748 * This fills out all the uninitialized function pointers
2749 * with the defaults.
2750 * The flash ID is read and the mtd/chip structures are
2751 * filled with the appropriate values.
2752 * The mtd->owner field must be set to the module of the caller
2753 *
2754 */
2755int nand_scan(struct mtd_info *mtd, int maxchips)
2756{
2757 int ret;
2758
2759 /* Many callers got this wrong, so check for it for a while... */
2760 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002761#if 0
William Juul52c07962007-10-31 13:53:06 +01002762 if (!mtd->owner && caller_is_module()) {
2763 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2764 BUG();
2765 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002766#endif
William Juulb76ec382007-11-08 10:39:53 +01002767
William Juul52c07962007-10-31 13:53:06 +01002768 ret = nand_scan_ident(mtd, maxchips);
2769 if (!ret)
2770 ret = nand_scan_tail(mtd);
2771 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002772}
2773
2774/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002775 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002776 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002777*/
2778void nand_release(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002779{
William Juul52c07962007-10-31 13:53:06 +01002780 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002781
2782#ifdef CONFIG_MTD_PARTITIONS
2783 /* Deregister partitions */
William Juul52c07962007-10-31 13:53:06 +01002784 del_mtd_partitions(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002785#endif
2786 /* Deregister the device */
William Juul52c07962007-10-31 13:53:06 +01002787 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002788#if 0
William Juul52c07962007-10-31 13:53:06 +01002789 del_mtd_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002790#endif
William Juul52c07962007-10-31 13:53:06 +01002791
2792 /* Free bad block table memory */
2793 kfree(chip->bbt);
2794 if (!(chip->options & NAND_OWN_BUFFERS))
2795 kfree(chip->buffers);
2796}
2797
2798/* XXX U-BOOT XXX */
2799#if 0
2800EXPORT_SYMBOL_GPL(nand_scan);
2801EXPORT_SYMBOL_GPL(nand_scan_ident);
2802EXPORT_SYMBOL_GPL(nand_scan_tail);
2803EXPORT_SYMBOL_GPL(nand_release);
2804
2805static int __init nand_base_init(void)
2806{
2807 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2808 return 0;
2809}
2810
2811static void __exit nand_base_exit(void)
2812{
2813 led_trigger_unregister_simple(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002814}
2815
William Juul52c07962007-10-31 13:53:06 +01002816module_init(nand_base_init);
2817module_exit(nand_base_exit);
2818
2819MODULE_LICENSE("GPL");
2820MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2821MODULE_DESCRIPTION("Generic NAND flash driver code");
2822#endif