blob: ef37f97b3394d85b695c1c74e905f1b328f87219 [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
Scott Wood3628f002008-10-24 16:20:43 -050010 * http://www.linux-mtd.infradead.org/doc/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.
Scott Wood3628f002008-10-24 16:20:43 -050027 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020028 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
32 *
33 */
34
35/* XXX U-BOOT XXX */
36#if 0
William Juul52c07962007-10-31 13:53:06 +010037#include <linux/module.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020038#include <linux/delay.h>
39#include <linux/errno.h>
William Juul52c07962007-10-31 13:53:06 +010040#include <linux/err.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020041#include <linux/sched.h>
42#include <linux/slab.h>
43#include <linux/types.h>
44#include <linux/mtd/mtd.h>
45#include <linux/mtd/nand.h>
46#include <linux/mtd/nand_ecc.h>
47#include <linux/mtd/compatmac.h>
48#include <linux/interrupt.h>
49#include <linux/bitops.h>
William Juul52c07962007-10-31 13:53:06 +010050#include <linux/leds.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020051#include <asm/io.h>
52
53#ifdef CONFIG_MTD_PARTITIONS
54#include <linux/mtd/partitions.h>
55#endif
56
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020057#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020058
59#include <common.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010060
William Juul52c07962007-10-31 13:53:06 +010061#define ENOTSUPP 524 /* Operation is not supported */
62
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020063#include <malloc.h>
64#include <watchdog.h>
William Juul52c07962007-10-31 13:53:06 +010065#include <linux/err.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020066#include <linux/mtd/compat.h>
67#include <linux/mtd/mtd.h>
68#include <linux/mtd/nand.h>
69#include <linux/mtd/nand_ecc.h>
70
71#include <asm/io.h>
72#include <asm/errno.h>
73
74#ifdef CONFIG_JFFS2_NAND
75#include <jffs2/jffs2.h>
76#endif
77
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020078/* Define default oob placement schemes for large and small page devices */
William Juul52c07962007-10-31 13:53:06 +010079static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020080 .eccbytes = 3,
81 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010082 .oobfree = {
83 {.offset = 3,
84 .length = 2},
85 {.offset = 6,
86 .length = 2}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020087};
88
William Juul52c07962007-10-31 13:53:06 +010089static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020090 .eccbytes = 6,
91 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010092 .oobfree = {
93 {.offset = 8,
94 . length = 8}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020095};
96
William Juul52c07962007-10-31 13:53:06 +010097static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020098 .eccbytes = 24,
99 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +0100100 40, 41, 42, 43, 44, 45, 46, 47,
101 48, 49, 50, 51, 52, 53, 54, 55,
102 56, 57, 58, 59, 60, 61, 62, 63},
103 .oobfree = {
104 {.offset = 2,
105 .length = 38}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200106};
107
William Juul52c07962007-10-31 13:53:06 +0100108static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200109 .eccbytes = 48,
110 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +0100111 80, 81, 82, 83, 84, 85, 86, 87,
112 88, 89, 90, 91, 92, 93, 94, 95,
113 96, 97, 98, 99, 100, 101, 102, 103,
114 104, 105, 106, 107, 108, 109, 110, 111,
115 112, 113, 114, 115, 116, 117, 118, 119,
116 120, 121, 122, 123, 124, 125, 126, 127},
117 .oobfree = {
118 {.offset = 2,
119 .length = 78}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200120};
121
William Juul52c07962007-10-31 13:53:06 +0100122
123static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
124 int new_state);
125
126static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
127 struct mtd_oob_ops *ops);
128
129static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200130
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200131/*
Scott Wood3628f002008-10-24 16:20:43 -0500132 * For devices which display every fart in the system on a separate LED. Is
William Juul52c07962007-10-31 13:53:06 +0100133 * compiled away when LED support is disabled.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200134 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200135/* XXX U-BOOT XXX */
136#if 0
William Juul52c07962007-10-31 13:53:06 +0100137DEFINE_LED_TRIGGER(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200138#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200139
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200140/**
141 * nand_release_device - [GENERIC] release chip
142 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200143 *
144 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200145 */
146/* XXX U-BOOT XXX */
147#if 0
William Juul52c07962007-10-31 13:53:06 +0100148static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200149{
William Juul52c07962007-10-31 13:53:06 +0100150 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200151
152 /* De-select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100153 chip->select_chip(mtd, -1);
154
155 /* Release the controller and the chip */
156 spin_lock(&chip->controller->lock);
157 chip->controller->active = NULL;
158 chip->state = FL_READY;
159 wake_up(&chip->controller->wq);
160 spin_unlock(&chip->controller->lock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200161}
162#else
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100163static void nand_release_device (struct mtd_info *mtd)
164{
165 struct nand_chip *this = mtd->priv;
166 this->select_chip(mtd, -1); /* De-select the NAND device */
167}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200168#endif
169
170/**
171 * nand_read_byte - [DEFAULT] read one byte from the chip
172 * @mtd: MTD device structure
173 *
174 * Default read function for 8bit buswith
175 */
William Juul52c07962007-10-31 13:53:06 +0100176static uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200177{
William Juul52c07962007-10-31 13:53:06 +0100178 struct nand_chip *chip = mtd->priv;
179 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200180}
181
182/**
183 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
184 * @mtd: MTD device structure
185 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200186 * Default read function for 16bit buswith with
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200187 * endianess conversion
188 */
William Juul52c07962007-10-31 13:53:06 +0100189static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200190{
William Juul52c07962007-10-31 13:53:06 +0100191 struct nand_chip *chip = mtd->priv;
192 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200193}
194
195/**
196 * nand_read_word - [DEFAULT] read one word from the chip
197 * @mtd: MTD device structure
198 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200199 * Default read function for 16bit buswith without
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200200 * endianess conversion
201 */
202static u16 nand_read_word(struct mtd_info *mtd)
203{
William Juul52c07962007-10-31 13:53:06 +0100204 struct nand_chip *chip = mtd->priv;
205 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200206}
207
208/**
209 * nand_select_chip - [DEFAULT] control CE line
210 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100211 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200212 *
213 * Default select function for 1 chip devices.
214 */
William Juul52c07962007-10-31 13:53:06 +0100215static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200216{
William Juul52c07962007-10-31 13:53:06 +0100217 struct nand_chip *chip = mtd->priv;
218
219 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200220 case -1:
William Juul52c07962007-10-31 13:53:06 +0100221 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200222 break;
223 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200224 break;
225
226 default:
227 BUG();
228 }
229}
230
231/**
232 * nand_write_buf - [DEFAULT] write buffer to chip
233 * @mtd: MTD device structure
234 * @buf: data buffer
235 * @len: number of bytes to write
236 *
237 * Default write function for 8bit buswith
238 */
William Juul52c07962007-10-31 13:53:06 +0100239static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200240{
241 int i;
William Juul52c07962007-10-31 13:53:06 +0100242 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200243
William Juul52c07962007-10-31 13:53:06 +0100244 for (i = 0; i < len; i++)
245 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200246}
247
248/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200249 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200250 * @mtd: MTD device structure
251 * @buf: buffer to store date
252 * @len: number of bytes to read
253 *
254 * Default read function for 8bit buswith
255 */
William Juul52c07962007-10-31 13:53:06 +0100256static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200257{
258 int i;
William Juul52c07962007-10-31 13:53:06 +0100259 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200260
William Juul52c07962007-10-31 13:53:06 +0100261 for (i = 0; i < len; i++)
262 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200263}
264
265/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200266 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200267 * @mtd: MTD device structure
268 * @buf: buffer containing the data to compare
269 * @len: number of bytes to compare
270 *
271 * Default verify function for 8bit buswith
272 */
William Juul52c07962007-10-31 13:53:06 +0100273static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200274{
275 int i;
William Juul52c07962007-10-31 13:53:06 +0100276 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200277
William Juul52c07962007-10-31 13:53:06 +0100278 for (i = 0; i < len; i++)
279 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200280 return -EFAULT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200281 return 0;
282}
283
284/**
285 * nand_write_buf16 - [DEFAULT] write buffer to chip
286 * @mtd: MTD device structure
287 * @buf: data buffer
288 * @len: number of bytes to write
289 *
290 * Default write function for 16bit buswith
291 */
William Juul52c07962007-10-31 13:53:06 +0100292static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200293{
294 int i;
William Juul52c07962007-10-31 13:53:06 +0100295 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200296 u16 *p = (u16 *) buf;
297 len >>= 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200298
William Juul52c07962007-10-31 13:53:06 +0100299 for (i = 0; i < len; i++)
300 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200301
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200302}
303
304/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200305 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200306 * @mtd: MTD device structure
307 * @buf: buffer to store date
308 * @len: number of bytes to read
309 *
310 * Default read function for 16bit buswith
311 */
William Juul52c07962007-10-31 13:53:06 +0100312static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200313{
314 int i;
William Juul52c07962007-10-31 13:53:06 +0100315 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316 u16 *p = (u16 *) buf;
317 len >>= 1;
318
William Juul52c07962007-10-31 13:53:06 +0100319 for (i = 0; i < len; i++)
320 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200321}
322
323/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200324 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200325 * @mtd: MTD device structure
326 * @buf: buffer containing the data to compare
327 * @len: number of bytes to compare
328 *
329 * Default verify function for 16bit buswith
330 */
William Juul52c07962007-10-31 13:53:06 +0100331static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200332{
333 int i;
William Juul52c07962007-10-31 13:53:06 +0100334 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200335 u16 *p = (u16 *) buf;
336 len >>= 1;
337
William Juul52c07962007-10-31 13:53:06 +0100338 for (i = 0; i < len; i++)
339 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200340 return -EFAULT;
341
342 return 0;
343}
344
345/**
346 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
347 * @mtd: MTD device structure
348 * @ofs: offset from device start
349 * @getchip: 0, if the chip is already selected
350 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200351 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200352 */
353static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
354{
355 int page, chipnr, res = 0;
William Juul52c07962007-10-31 13:53:06 +0100356 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200357 u16 bad;
358
William Juul52c07962007-10-31 13:53:06 +0100359 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200360
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200361 if (getchip) {
William Juul52c07962007-10-31 13:53:06 +0100362 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200363
William Juul52c07962007-10-31 13:53:06 +0100364 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200365
366 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100367 chip->select_chip(mtd, chipnr);
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200368 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200369
William Juul52c07962007-10-31 13:53:06 +0100370 if (chip->options & NAND_BUSWIDTH_16) {
371 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
372 page);
373 bad = cpu_to_le16(chip->read_word(mtd));
374 if (chip->badblockpos & 0x1)
375 bad >>= 8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200376 if ((bad & 0xFF) != 0xff)
377 res = 1;
378 } else {
William Juul52c07962007-10-31 13:53:06 +0100379 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
380 if (chip->read_byte(mtd) != 0xff)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200381 res = 1;
382 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200383
William Juul52c07962007-10-31 13:53:06 +0100384 if (getchip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200385 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200386
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200387 return res;
388}
389
390/**
391 * nand_default_block_markbad - [DEFAULT] mark a block bad
392 * @mtd: MTD device structure
393 * @ofs: offset from device start
394 *
395 * This is the default implementation, which can be overridden by
396 * a hardware specific driver.
397*/
398static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
399{
William Juul52c07962007-10-31 13:53:06 +0100400 struct nand_chip *chip = mtd->priv;
401 uint8_t buf[2] = { 0, 0 };
402 int block, ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200403
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200404 /* Get block number */
William Juul52c07962007-10-31 13:53:06 +0100405 block = (int)(ofs >> chip->bbt_erase_shift);
406 if (chip->bbt)
407 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200408
409 /* Do we have a flash based bad block table ? */
William Juul52c07962007-10-31 13:53:06 +0100410 if (chip->options & NAND_USE_FLASH_BBT)
411 ret = nand_update_bbt(mtd, ofs);
412 else {
413 /* We write two bytes, so we dont have to mess with 16 bit
414 * access
415 */
Scott Wood3628f002008-10-24 16:20:43 -0500416 nand_get_device(chip, mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +0100417 ofs += mtd->oobsize;
418 chip->ops.len = chip->ops.ooblen = 2;
419 chip->ops.datbuf = NULL;
420 chip->ops.oobbuf = buf;
421 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200422
William Juul52c07962007-10-31 13:53:06 +0100423 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Scott Wood3628f002008-10-24 16:20:43 -0500424 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100425 }
426 if (!ret)
427 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500428
William Juul52c07962007-10-31 13:53:06 +0100429 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200430}
431
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200432/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200433 * nand_check_wp - [GENERIC] check if the chip is write protected
434 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200435 * Check, if the device is write protected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200436 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200437 * The function expects, that the device is already selected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200438 */
William Juul52c07962007-10-31 13:53:06 +0100439static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200440{
William Juul52c07962007-10-31 13:53:06 +0100441 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200442 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100443 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
444 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200445}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100446
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200447/**
448 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
449 * @mtd: MTD device structure
450 * @ofs: offset from device start
451 * @getchip: 0, if the chip is already selected
452 * @allowbbt: 1, if its allowed to access the bbt area
453 *
454 * Check, if the block is bad. Either by reading the bad block table or
455 * calling of the scan function.
456 */
William Juul52c07962007-10-31 13:53:06 +0100457static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
458 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200459{
William Juul52c07962007-10-31 13:53:06 +0100460 struct nand_chip *chip = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200461
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200462 if (!(chip->options & NAND_BBT_SCANNED)) {
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200463 chip->options |= NAND_BBT_SCANNED;
Scott Wood11b12a32008-12-16 14:24:16 -0600464 chip->scan_bbt(mtd);
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200465 }
466
William Juul52c07962007-10-31 13:53:06 +0100467 if (!chip->bbt)
468 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200469
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200470 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100471 return nand_isbad_bbt(mtd, ofs, allowbbt);
472}
473
474/*
475 * Wait for the ready pin, after a command
476 * The timeout is catched later.
477 */
478/* XXX U-BOOT XXX */
479#if 0
480void nand_wait_ready(struct mtd_info *mtd)
481{
482 struct nand_chip *chip = mtd->priv;
483 unsigned long timeo = jiffies + 2;
484
485 led_trigger_event(nand_led_trigger, LED_FULL);
486 /* wait until command is processed or timeout occures */
487 do {
488 if (chip->dev_ready(mtd))
489 break;
490 touch_softlockup_watchdog();
491 } while (time_before(jiffies, timeo));
492 led_trigger_event(nand_led_trigger, LED_OFF);
493}
494EXPORT_SYMBOL_GPL(nand_wait_ready);
495#else
496void nand_wait_ready(struct mtd_info *mtd)
497{
498 struct nand_chip *chip = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200499 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100500
501 reset_timer();
502
503 /* wait until command is processed or timeout occures */
504 while (get_timer(0) < timeo) {
505 if (chip->dev_ready)
506 if (chip->dev_ready(mtd))
507 break;
508 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200509}
William Juul52c07962007-10-31 13:53:06 +0100510#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200511
512/**
513 * nand_command - [DEFAULT] Send command to NAND device
514 * @mtd: MTD device structure
515 * @command: the command to be sent
516 * @column: the column address for this command, -1 if none
517 * @page_addr: the page address for this command, -1 if none
518 *
519 * Send command to NAND device. This function is used for small page
520 * devices (256/512 Bytes per page)
521 */
William Juul52c07962007-10-31 13:53:06 +0100522static void nand_command(struct mtd_info *mtd, unsigned int command,
523 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200524{
William Juul52c07962007-10-31 13:53:06 +0100525 register struct nand_chip *chip = mtd->priv;
526 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200527
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200528 /*
529 * Write out the command to the device.
530 */
531 if (command == NAND_CMD_SEQIN) {
532 int readcmd;
533
William Juul52c07962007-10-31 13:53:06 +0100534 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200535 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100536 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200537 readcmd = NAND_CMD_READOOB;
538 } else if (column < 256) {
539 /* First 256 bytes --> READ0 */
540 readcmd = NAND_CMD_READ0;
541 } else {
542 column -= 256;
543 readcmd = NAND_CMD_READ1;
544 }
William Juul52c07962007-10-31 13:53:06 +0100545 chip->cmd_ctrl(mtd, readcmd, ctrl);
546 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200547 }
William Juul52c07962007-10-31 13:53:06 +0100548 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200549
William Juul52c07962007-10-31 13:53:06 +0100550 /*
551 * Address cycle, when necessary
552 */
553 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
554 /* Serially input address */
555 if (column != -1) {
556 /* Adjust columns for 16 bit buswidth */
557 if (chip->options & NAND_BUSWIDTH_16)
558 column >>= 1;
559 chip->cmd_ctrl(mtd, column, ctrl);
560 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200561 }
William Juul52c07962007-10-31 13:53:06 +0100562 if (page_addr != -1) {
563 chip->cmd_ctrl(mtd, page_addr, ctrl);
564 ctrl &= ~NAND_CTRL_CHANGE;
565 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
566 /* One more address cycle for devices > 32MiB */
567 if (chip->chipsize > (32 << 20))
568 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
569 }
570 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200571
572 /*
573 * program and erase have their own busy handlers
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200574 * status and sequential in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100575 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200576 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200577
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200578 case NAND_CMD_PAGEPROG:
579 case NAND_CMD_ERASE1:
580 case NAND_CMD_ERASE2:
581 case NAND_CMD_SEQIN:
582 case NAND_CMD_STATUS:
583 return;
584
585 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100586 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200587 break;
William Juul52c07962007-10-31 13:53:06 +0100588 udelay(chip->chip_delay);
589 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
590 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
591 chip->cmd_ctrl(mtd,
592 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
593 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200594 return;
595
William Juul52c07962007-10-31 13:53:06 +0100596 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200597 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200598 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200599 * If we don't have access to the busy pin, we apply the given
600 * command delay
William Juul52c07962007-10-31 13:53:06 +0100601 */
602 if (!chip->dev_ready) {
603 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200604 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200605 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200606 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200607 /* Apply this short delay always to ensure that we do wait tWB in
608 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100609 ndelay(100);
610
611 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200612}
613
614/**
615 * nand_command_lp - [DEFAULT] Send command to NAND large page device
616 * @mtd: MTD device structure
617 * @command: the command to be sent
618 * @column: the column address for this command, -1 if none
619 * @page_addr: the page address for this command, -1 if none
620 *
William Juul52c07962007-10-31 13:53:06 +0100621 * Send command to NAND device. This is the version for the new large page
622 * devices We dont have the separate regions as we have in the small page
623 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200624 */
William Juul52c07962007-10-31 13:53:06 +0100625static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
626 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200627{
William Juul52c07962007-10-31 13:53:06 +0100628 register struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200629
630 /* Emulate NAND_CMD_READOOB */
631 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100632 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200633 command = NAND_CMD_READ0;
634 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200635
William Juul52c07962007-10-31 13:53:06 +0100636 /* Command latch cycle */
637 chip->cmd_ctrl(mtd, command & 0xff,
638 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200639
640 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100641 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200642
643 /* Serially input address */
644 if (column != -1) {
645 /* Adjust columns for 16 bit buswidth */
William Juul52c07962007-10-31 13:53:06 +0100646 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200647 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100648 chip->cmd_ctrl(mtd, column, ctrl);
649 ctrl &= ~NAND_CTRL_CHANGE;
650 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200651 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200652 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100653 chip->cmd_ctrl(mtd, page_addr, ctrl);
654 chip->cmd_ctrl(mtd, page_addr >> 8,
655 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200656 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-10-31 13:53:06 +0100657 if (chip->chipsize > (128 << 20))
658 chip->cmd_ctrl(mtd, page_addr >> 16,
659 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200660 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200661 }
William Juul52c07962007-10-31 13:53:06 +0100662 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200663
664 /*
665 * program and erase have their own busy handlers
William Juul52c07962007-10-31 13:53:06 +0100666 * status, sequential in, and deplete1 need no delay
667 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200668 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200669
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200670 case NAND_CMD_CACHEDPROG:
671 case NAND_CMD_PAGEPROG:
672 case NAND_CMD_ERASE1:
673 case NAND_CMD_ERASE2:
674 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100675 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200676 case NAND_CMD_STATUS:
William Juul52c07962007-10-31 13:53:06 +0100677 case NAND_CMD_DEPLETE1:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200678 return;
679
William Juul52c07962007-10-31 13:53:06 +0100680 /*
681 * read error status commands require only a short delay
682 */
683 case NAND_CMD_STATUS_ERROR:
684 case NAND_CMD_STATUS_ERROR0:
685 case NAND_CMD_STATUS_ERROR1:
686 case NAND_CMD_STATUS_ERROR2:
687 case NAND_CMD_STATUS_ERROR3:
688 udelay(chip->chip_delay);
689 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200690
691 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100692 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200693 break;
William Juul52c07962007-10-31 13:53:06 +0100694 udelay(chip->chip_delay);
695 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
696 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
697 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
698 NAND_NCE | NAND_CTRL_CHANGE);
699 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
700 return;
701
702 case NAND_CMD_RNDOUT:
703 /* No ready / busy check necessary */
704 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
705 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
706 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
707 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200708 return;
709
710 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100711 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
712 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
713 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
714 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200715
William Juul52c07962007-10-31 13:53:06 +0100716 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200717 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200718 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200719 * If we don't have access to the busy pin, we apply the given
720 * command delay
William Juul52c07962007-10-31 13:53:06 +0100721 */
722 if (!chip->dev_ready) {
723 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200725 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200726 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200727
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200728 /* Apply this short delay always to ensure that we do wait tWB in
729 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100730 ndelay(100);
731
732 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200733}
734
735/**
736 * nand_get_device - [GENERIC] Get chip for selected access
William Juul52c07962007-10-31 13:53:06 +0100737 * @chip: the nand chip descriptor
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200738 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200739 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200740 *
741 * Get the device and lock it for exclusive access
742 */
743/* XXX U-BOOT XXX */
744#if 0
William Juul52c07962007-10-31 13:53:06 +0100745static int
746nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200747{
William Juul52c07962007-10-31 13:53:06 +0100748 spinlock_t *lock = &chip->controller->lock;
749 wait_queue_head_t *wq = &chip->controller->wq;
750 DECLARE_WAITQUEUE(wait, current);
751 retry:
752 spin_lock(lock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200753
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200754 /* Hardware controller shared among independend devices */
William Juul52c07962007-10-31 13:53:06 +0100755 /* Hardware controller shared among independend devices */
756 if (!chip->controller->active)
757 chip->controller->active = chip;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200758
William Juul52c07962007-10-31 13:53:06 +0100759 if (chip->controller->active == chip && chip->state == FL_READY) {
760 chip->state = new_state;
761 spin_unlock(lock);
762 return 0;
763 }
764 if (new_state == FL_PM_SUSPENDED) {
765 spin_unlock(lock);
766 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200767 }
William Juul52c07962007-10-31 13:53:06 +0100768 set_current_state(TASK_UNINTERRUPTIBLE);
769 add_wait_queue(wq, &wait);
770 spin_unlock(lock);
771 schedule();
772 remove_wait_queue(wq, &wait);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200773 goto retry;
774}
775#else
William Juul52c07962007-10-31 13:53:06 +0100776static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
777{
Marcel Ziswilerf30a2aa2008-06-22 16:30:06 +0200778 this->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100779 return 0;
780}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200781#endif
782
783/**
784 * nand_wait - [DEFAULT] wait until the command is done
785 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100786 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200787 *
788 * Wait for command done. This applies to erase and program only
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200789 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200790 * general NAND and SmartMedia specs
William Juul52c07962007-10-31 13:53:06 +0100791 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200792/* XXX U-BOOT XXX */
793#if 0
William Juul52c07962007-10-31 13:53:06 +0100794static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200795{
William Juul52c07962007-10-31 13:53:06 +0100796
797 unsigned long timeo = jiffies;
798 int status, state = chip->state;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200799
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200800 if (state == FL_ERASING)
William Juul52c07962007-10-31 13:53:06 +0100801 timeo += (HZ * 400) / 1000;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200802 else
William Juul52c07962007-10-31 13:53:06 +0100803 timeo += (HZ * 20) / 1000;
804
805 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200806
807 /* Apply this short delay always to ensure that we do wait tWB in
808 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100809 ndelay(100);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200810
William Juul52c07962007-10-31 13:53:06 +0100811 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
812 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200813 else
William Juul52c07962007-10-31 13:53:06 +0100814 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200815
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200816 while (time_before(jiffies, timeo)) {
William Juul52c07962007-10-31 13:53:06 +0100817 if (chip->dev_ready) {
818 if (chip->dev_ready(mtd))
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200819 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200820 } else {
William Juul52c07962007-10-31 13:53:06 +0100821 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200822 break;
823 }
William Juul52c07962007-10-31 13:53:06 +0100824 cond_resched();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200825 }
William Juul52c07962007-10-31 13:53:06 +0100826 led_trigger_event(nand_led_trigger, LED_OFF);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200827
William Juul52c07962007-10-31 13:53:06 +0100828 status = (int)chip->read_byte(mtd);
829 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200830}
831#else
William Juul52c07962007-10-31 13:53:06 +0100832static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200833{
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100834 unsigned long timeo;
William Juul52c07962007-10-31 13:53:06 +0100835 int state = this->state;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100836
837 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200838 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100839 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200840 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100841
842 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
843 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
844 else
845 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
846
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100847 reset_timer();
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100848
849 while (1) {
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100850 if (get_timer(0) > timeo) {
851 printf("Timeout!");
Stefan Roeseef26d242006-11-27 17:22:19 +0100852 return 0x01;
853 }
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100854
855 if (this->dev_ready) {
856 if (this->dev_ready(mtd))
857 break;
858 } else {
859 if (this->read_byte(mtd) & NAND_STATUS_READY)
860 break;
861 }
862 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100863#ifdef PPCHAMELON_NAND_TIMER_HACK
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100864 reset_timer();
865 while (get_timer(0) < 10);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100866#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100867
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100868 return this->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200869}
870#endif
871
872/**
William Juul52c07962007-10-31 13:53:06 +0100873 * nand_read_page_raw - [Intern] read raw page data without ecc
874 * @mtd: mtd info structure
875 * @chip: nand chip info structure
876 * @buf: buffer to store read data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200877 */
William Juul52c07962007-10-31 13:53:06 +0100878static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
879 uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200880{
William Juul52c07962007-10-31 13:53:06 +0100881 chip->read_buf(mtd, buf, mtd->writesize);
882 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
883 return 0;
884}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200885
William Juul52c07962007-10-31 13:53:06 +0100886/**
887 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
888 * @mtd: mtd info structure
889 * @chip: nand chip info structure
890 * @buf: buffer to store read data
891 */
892static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
893 uint8_t *buf)
894{
895 int i, eccsize = chip->ecc.size;
896 int eccbytes = chip->ecc.bytes;
897 int eccsteps = chip->ecc.steps;
898 uint8_t *p = buf;
899 uint8_t *ecc_calc = chip->buffers->ecccalc;
900 uint8_t *ecc_code = chip->buffers->ecccode;
901 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200902
William Juul52c07962007-10-31 13:53:06 +0100903 chip->ecc.read_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200904
William Juul52c07962007-10-31 13:53:06 +0100905 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
906 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200907
William Juul52c07962007-10-31 13:53:06 +0100908 for (i = 0; i < chip->ecc.total; i++)
909 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200910
William Juul52c07962007-10-31 13:53:06 +0100911 eccsteps = chip->ecc.steps;
912 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200913
William Juul52c07962007-10-31 13:53:06 +0100914 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
915 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200916
William Juul52c07962007-10-31 13:53:06 +0100917 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood3628f002008-10-24 16:20:43 -0500918 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +0100919 mtd->ecc_stats.failed++;
920 else
921 mtd->ecc_stats.corrected += stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200922 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200923 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200924}
925
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200926/**
Scott Wood3628f002008-10-24 16:20:43 -0500927 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
928 * @mtd: mtd info structure
929 * @chip: nand chip info structure
930 * @dataofs offset of requested data within the page
931 * @readlen data length
932 * @buf: buffer to store read data
933 */
934static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
935{
936 int start_step, end_step, num_steps;
937 uint32_t *eccpos = chip->ecc.layout->eccpos;
938 uint8_t *p;
939 int data_col_addr, i, gaps = 0;
940 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
941 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
942
943 /* Column address wihin the page aligned to ECC size (256bytes). */
944 start_step = data_offs / chip->ecc.size;
945 end_step = (data_offs + readlen - 1) / chip->ecc.size;
946 num_steps = end_step - start_step + 1;
947
948 /* Data size aligned to ECC ecc.size*/
949 datafrag_len = num_steps * chip->ecc.size;
950 eccfrag_len = num_steps * chip->ecc.bytes;
951
952 data_col_addr = start_step * chip->ecc.size;
953 /* If we read not a page aligned data */
954 if (data_col_addr != 0)
955 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
956
957 p = bufpoi + data_col_addr;
958 chip->read_buf(mtd, p, datafrag_len);
959
960 /* Calculate ECC */
961 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
962 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
963
964 /* The performance is faster if to position offsets
965 according to ecc.pos. Let make sure here that
966 there are no gaps in ecc positions */
967 for (i = 0; i < eccfrag_len - 1; i++) {
968 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
969 eccpos[i + start_step * chip->ecc.bytes + 1]) {
970 gaps = 1;
971 break;
972 }
973 }
974 if (gaps) {
975 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
976 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
977 } else {
978 /* send the command to read the particular ecc bytes */
979 /* take care about buswidth alignment in read_buf */
980 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
981 aligned_len = eccfrag_len;
982 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
983 aligned_len++;
984 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
985 aligned_len++;
986
987 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
988 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
989 }
990
991 for (i = 0; i < eccfrag_len; i++)
992 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
993
994 p = bufpoi + data_col_addr;
995 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
996 int stat;
997
998 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
999 if (stat < 0)
1000 mtd->ecc_stats.failed++;
1001 else
1002 mtd->ecc_stats.corrected += stat;
1003 }
1004 return 0;
1005}
1006
1007/**
William Juul52c07962007-10-31 13:53:06 +01001008 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
1009 * @mtd: mtd info structure
1010 * @chip: nand chip info structure
1011 * @buf: buffer to store read data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001012 *
William Juul52c07962007-10-31 13:53:06 +01001013 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001014 */
William Juul52c07962007-10-31 13:53:06 +01001015static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1016 uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001017{
William Juul52c07962007-10-31 13:53:06 +01001018 int i, eccsize = chip->ecc.size;
1019 int eccbytes = chip->ecc.bytes;
1020 int eccsteps = chip->ecc.steps;
1021 uint8_t *p = buf;
1022 uint8_t *ecc_calc = chip->buffers->ecccalc;
1023 uint8_t *ecc_code = chip->buffers->ecccode;
1024 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001025
William Juul52c07962007-10-31 13:53:06 +01001026 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1027 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1028 chip->read_buf(mtd, p, eccsize);
1029 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1030 }
1031 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001032
William Juul52c07962007-10-31 13:53:06 +01001033 for (i = 0; i < chip->ecc.total; i++)
1034 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001035
William Juul52c07962007-10-31 13:53:06 +01001036 eccsteps = chip->ecc.steps;
1037 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001038
William Juul52c07962007-10-31 13:53:06 +01001039 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1040 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001041
William Juul52c07962007-10-31 13:53:06 +01001042 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1043 if (stat == -1)
1044 mtd->ecc_stats.failed++;
1045 else
1046 mtd->ecc_stats.corrected += stat;
1047 }
1048 return 0;
1049}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001050
William Juul52c07962007-10-31 13:53:06 +01001051/**
1052 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1053 * @mtd: mtd info structure
1054 * @chip: nand chip info structure
1055 * @buf: buffer to store read data
1056 *
1057 * The hw generator calculates the error syndrome automatically. Therefor
1058 * we need a special oob layout and handling.
1059 */
1060static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1061 uint8_t *buf)
1062{
1063 int i, eccsize = chip->ecc.size;
1064 int eccbytes = chip->ecc.bytes;
1065 int eccsteps = chip->ecc.steps;
1066 uint8_t *p = buf;
1067 uint8_t *oob = chip->oob_poi;
1068
1069 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1070 int stat;
1071
1072 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1073 chip->read_buf(mtd, p, eccsize);
1074
1075 if (chip->ecc.prepad) {
1076 chip->read_buf(mtd, oob, chip->ecc.prepad);
1077 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001078 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001079
William Juul52c07962007-10-31 13:53:06 +01001080 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1081 chip->read_buf(mtd, oob, eccbytes);
1082 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001083
Scott Wood3628f002008-10-24 16:20:43 -05001084 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001085 mtd->ecc_stats.failed++;
1086 else
1087 mtd->ecc_stats.corrected += stat;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001088
William Juul52c07962007-10-31 13:53:06 +01001089 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001090
William Juul52c07962007-10-31 13:53:06 +01001091 if (chip->ecc.postpad) {
1092 chip->read_buf(mtd, oob, chip->ecc.postpad);
1093 oob += chip->ecc.postpad;
1094 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001095 }
William Juul52c07962007-10-31 13:53:06 +01001096
1097 /* Calculate remaining oob bytes */
1098 i = mtd->oobsize - (oob - chip->oob_poi);
1099 if (i)
1100 chip->read_buf(mtd, oob, i);
1101
1102 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001103}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001104
1105/**
William Juul52c07962007-10-31 13:53:06 +01001106 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1107 * @chip: nand chip structure
1108 * @oob: oob destination address
1109 * @ops: oob ops structure
1110 * @len: size of oob to transfer
1111 */
1112static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1113 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001114{
William Juul52c07962007-10-31 13:53:06 +01001115 switch(ops->mode) {
1116
1117 case MTD_OOB_PLACE:
1118 case MTD_OOB_RAW:
1119 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1120 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001121
William Juul52c07962007-10-31 13:53:06 +01001122 case MTD_OOB_AUTO: {
1123 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1124 uint32_t boffs = 0, roffs = ops->ooboffs;
1125 size_t bytes = 0;
1126
1127 for(; free->length && len; free++, len -= bytes) {
1128 /* Read request not from offset 0 ? */
1129 if (unlikely(roffs)) {
1130 if (roffs >= free->length) {
1131 roffs -= free->length;
1132 continue;
1133 }
1134 boffs = free->offset + roffs;
1135 bytes = min_t(size_t, len,
1136 (free->length - roffs));
1137 roffs = 0;
1138 } else {
1139 bytes = min_t(size_t, len, free->length);
1140 boffs = free->offset;
1141 }
1142 memcpy(oob, chip->oob_poi + boffs, bytes);
1143 oob += bytes;
1144 }
1145 return oob;
1146 }
1147 default:
1148 BUG();
1149 }
1150 return NULL;
1151}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001152
1153/**
William Juul52c07962007-10-31 13:53:06 +01001154 * nand_do_read_ops - [Internal] Read data with ECC
1155 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001156 * @mtd: MTD device structure
1157 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001158 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001159 *
William Juul52c07962007-10-31 13:53:06 +01001160 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001161 */
William Juul52c07962007-10-31 13:53:06 +01001162static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1163 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001164{
William Juul52c07962007-10-31 13:53:06 +01001165 int chipnr, page, realpage, col, bytes, aligned;
1166 struct nand_chip *chip = mtd->priv;
1167 struct mtd_ecc_stats stats;
1168 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1169 int sndcmd = 1;
1170 int ret = 0;
1171 uint32_t readlen = ops->len;
1172 uint32_t oobreadlen = ops->ooblen;
1173 uint8_t *bufpoi, *oob, *buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001174
William Juul52c07962007-10-31 13:53:06 +01001175 stats = mtd->ecc_stats;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001176
William Juul52c07962007-10-31 13:53:06 +01001177 chipnr = (int)(from >> chip->chip_shift);
1178 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001179
William Juul52c07962007-10-31 13:53:06 +01001180 realpage = (int)(from >> chip->page_shift);
1181 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001182
William Juul52c07962007-10-31 13:53:06 +01001183 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001184
William Juul52c07962007-10-31 13:53:06 +01001185 buf = ops->datbuf;
1186 oob = ops->oobbuf;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001187
William Juul52c07962007-10-31 13:53:06 +01001188 while(1) {
1189 bytes = min(mtd->writesize - col, readlen);
1190 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001191
William Juul52c07962007-10-31 13:53:06 +01001192 /* Is the current page in the buffer ? */
1193 if (realpage != chip->pagebuf || oob) {
1194 bufpoi = aligned ? buf : chip->buffers->databuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001195
William Juul52c07962007-10-31 13:53:06 +01001196 if (likely(sndcmd)) {
1197 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1198 sndcmd = 0;
1199 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001200
William Juul52c07962007-10-31 13:53:06 +01001201 /* Now read the page into the buffer */
1202 if (unlikely(ops->mode == MTD_OOB_RAW))
1203 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
Scott Wood3628f002008-10-24 16:20:43 -05001204 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1205 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
William Juul52c07962007-10-31 13:53:06 +01001206 else
1207 ret = chip->ecc.read_page(mtd, chip, bufpoi);
1208 if (ret < 0)
1209 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001210
William Juul52c07962007-10-31 13:53:06 +01001211 /* Transfer not aligned data */
1212 if (!aligned) {
Scott Wood3628f002008-10-24 16:20:43 -05001213 if (!NAND_SUBPAGE_READ(chip) && !oob)
1214 chip->pagebuf = realpage;
William Juul52c07962007-10-31 13:53:06 +01001215 memcpy(buf, chip->buffers->databuf + col, bytes);
1216 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001217
William Juul52c07962007-10-31 13:53:06 +01001218 buf += bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001219
William Juul52c07962007-10-31 13:53:06 +01001220 if (unlikely(oob)) {
1221 /* Raw mode does data:oob:data:oob */
1222 if (ops->mode != MTD_OOB_RAW) {
1223 int toread = min(oobreadlen,
1224 chip->ecc.layout->oobavail);
1225 if (toread) {
1226 oob = nand_transfer_oob(chip,
1227 oob, ops, toread);
1228 oobreadlen -= toread;
1229 }
1230 } else
1231 buf = nand_transfer_oob(chip,
1232 buf, ops, mtd->oobsize);
1233 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001234
William Juul52c07962007-10-31 13:53:06 +01001235 if (!(chip->options & NAND_NO_READRDY)) {
1236 /*
1237 * Apply delay or wait for ready/busy pin. Do
1238 * this before the AUTOINCR check, so no
1239 * problems arise if a chip which does auto
1240 * increment is marked as NOAUTOINCR by the
1241 * board driver.
1242 */
1243 if (!chip->dev_ready)
1244 udelay(chip->chip_delay);
1245 else
1246 nand_wait_ready(mtd);
1247 }
1248 } else {
1249 memcpy(buf, chip->buffers->databuf + col, bytes);
1250 buf += bytes;
1251 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001252
William Juul52c07962007-10-31 13:53:06 +01001253 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001254
William Juul52c07962007-10-31 13:53:06 +01001255 if (!readlen)
1256 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001257
William Juul52c07962007-10-31 13:53:06 +01001258 /* For subsequent reads align to page boundary. */
1259 col = 0;
1260 /* Increment page address */
1261 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001262
William Juul52c07962007-10-31 13:53:06 +01001263 page = realpage & chip->pagemask;
1264 /* Check, if we cross a chip boundary */
1265 if (!page) {
1266 chipnr++;
1267 chip->select_chip(mtd, -1);
1268 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001269 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001270
William Juul52c07962007-10-31 13:53:06 +01001271 /* Check, if the chip supports auto page increment
1272 * or if we have hit a block boundary.
1273 */
1274 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1275 sndcmd = 1;
1276 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001277
William Juul52c07962007-10-31 13:53:06 +01001278 ops->retlen = ops->len - (size_t) readlen;
1279 if (oob)
1280 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001281
William Juul52c07962007-10-31 13:53:06 +01001282 if (ret)
1283 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001284
William Juul52c07962007-10-31 13:53:06 +01001285 if (mtd->ecc_stats.failed - stats.failed)
1286 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001287
William Juul52c07962007-10-31 13:53:06 +01001288 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1289}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001290
William Juul52c07962007-10-31 13:53:06 +01001291/**
1292 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1293 * @mtd: MTD device structure
1294 * @from: offset to read from
1295 * @len: number of bytes to read
1296 * @retlen: pointer to variable to store the number of read bytes
1297 * @buf: the databuffer to put data
1298 *
1299 * Get hold of the chip and call nand_do_read
1300 */
1301static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1302 size_t *retlen, uint8_t *buf)
1303{
1304 struct nand_chip *chip = mtd->priv;
1305 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001306
William Juul52c07962007-10-31 13:53:06 +01001307 /* Do not allow reads past end of device */
1308 if ((from + len) > mtd->size)
1309 return -EINVAL;
1310 if (!len)
1311 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001312
William Juul52c07962007-10-31 13:53:06 +01001313 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001314
William Juul52c07962007-10-31 13:53:06 +01001315 chip->ops.len = len;
1316 chip->ops.datbuf = buf;
1317 chip->ops.oobbuf = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001318
William Juul52c07962007-10-31 13:53:06 +01001319 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001320
William Juul52c07962007-10-31 13:53:06 +01001321 *retlen = chip->ops.retlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001322
William Juul52c07962007-10-31 13:53:06 +01001323 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001324
William Juul52c07962007-10-31 13:53:06 +01001325 return ret;
1326}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001327
William Juul52c07962007-10-31 13:53:06 +01001328/**
1329 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1330 * @mtd: mtd info structure
1331 * @chip: nand chip info structure
1332 * @page: page number to read
1333 * @sndcmd: flag whether to issue read command or not
1334 */
1335static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1336 int page, int sndcmd)
1337{
1338 if (sndcmd) {
1339 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1340 sndcmd = 0;
1341 }
1342 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1343 return sndcmd;
1344}
1345
1346/**
1347 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1348 * with syndromes
1349 * @mtd: mtd info structure
1350 * @chip: nand chip info structure
1351 * @page: page number to read
1352 * @sndcmd: flag whether to issue read command or not
1353 */
1354static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1355 int page, int sndcmd)
1356{
1357 uint8_t *buf = chip->oob_poi;
1358 int length = mtd->oobsize;
1359 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1360 int eccsize = chip->ecc.size;
1361 uint8_t *bufpoi = buf;
1362 int i, toread, sndrnd = 0, pos;
1363
1364 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1365 for (i = 0; i < chip->ecc.steps; i++) {
1366 if (sndrnd) {
1367 pos = eccsize + i * (eccsize + chunk);
1368 if (mtd->writesize > 512)
1369 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1370 else
1371 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001372 } else
William Juul52c07962007-10-31 13:53:06 +01001373 sndrnd = 1;
1374 toread = min_t(int, length, chunk);
1375 chip->read_buf(mtd, bufpoi, toread);
1376 bufpoi += toread;
1377 length -= toread;
1378 }
1379 if (length > 0)
1380 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001381
William Juul52c07962007-10-31 13:53:06 +01001382 return 1;
1383}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001384
William Juul52c07962007-10-31 13:53:06 +01001385/**
1386 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1387 * @mtd: mtd info structure
1388 * @chip: nand chip info structure
1389 * @page: page number to write
1390 */
1391static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1392 int page)
1393{
1394 int status = 0;
1395 const uint8_t *buf = chip->oob_poi;
1396 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001397
William Juul52c07962007-10-31 13:53:06 +01001398 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1399 chip->write_buf(mtd, buf, length);
1400 /* Send command to program the OOB data */
1401 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001402
William Juul52c07962007-10-31 13:53:06 +01001403 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001404
William Juul52c07962007-10-31 13:53:06 +01001405 return status & NAND_STATUS_FAIL ? -EIO : 0;
1406}
1407
1408/**
1409 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1410 * with syndrome - only for large page flash !
1411 * @mtd: mtd info structure
1412 * @chip: nand chip info structure
1413 * @page: page number to write
1414 */
1415static int nand_write_oob_syndrome(struct mtd_info *mtd,
1416 struct nand_chip *chip, int page)
1417{
1418 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1419 int eccsize = chip->ecc.size, length = mtd->oobsize;
1420 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1421 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001422
1423 /*
William Juul52c07962007-10-31 13:53:06 +01001424 * data-ecc-data-ecc ... ecc-oob
1425 * or
1426 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001427 */
William Juul52c07962007-10-31 13:53:06 +01001428 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1429 pos = steps * (eccsize + chunk);
1430 steps = 0;
1431 } else
1432 pos = eccsize;
1433
1434 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1435 for (i = 0; i < steps; i++) {
1436 if (sndcmd) {
1437 if (mtd->writesize <= 512) {
1438 uint32_t fill = 0xFFFFFFFF;
1439
1440 len = eccsize;
1441 while (len > 0) {
1442 int num = min_t(int, len, 4);
1443 chip->write_buf(mtd, (uint8_t *)&fill,
1444 num);
1445 len -= num;
1446 }
1447 } else {
1448 pos = eccsize + i * (eccsize + chunk);
1449 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1450 }
1451 } else
1452 sndcmd = 1;
1453 len = min_t(int, length, chunk);
1454 chip->write_buf(mtd, bufpoi, len);
1455 bufpoi += len;
1456 length -= len;
1457 }
1458 if (length > 0)
1459 chip->write_buf(mtd, bufpoi, length);
1460
1461 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1462 status = chip->waitfunc(mtd, chip);
1463
1464 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001465}
1466
1467/**
William Juul52c07962007-10-31 13:53:06 +01001468 * nand_do_read_oob - [Intern] NAND read out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001469 * @mtd: MTD device structure
1470 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001471 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001472 *
1473 * NAND read out-of-band data from the spare area
1474 */
William Juul52c07962007-10-31 13:53:06 +01001475static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1476 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001477{
William Juul52c07962007-10-31 13:53:06 +01001478 int page, realpage, chipnr, sndcmd = 1;
1479 struct nand_chip *chip = mtd->priv;
1480 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1481 int readlen = ops->ooblen;
1482 int len;
1483 uint8_t *buf = ops->oobbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001484
William Juul52c07962007-10-31 13:53:06 +01001485 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1486 (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001487
William Juul52c07962007-10-31 13:53:06 +01001488 if (ops->mode == MTD_OOB_AUTO)
1489 len = chip->ecc.layout->oobavail;
1490 else
1491 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001492
William Juul52c07962007-10-31 13:53:06 +01001493 if (unlikely(ops->ooboffs >= len)) {
1494 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1495 "Attempt to start read outside oob\n");
1496 return -EINVAL;
1497 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001498
1499 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001500 if (unlikely(from >= mtd->size ||
1501 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1502 (from >> chip->page_shift)) * len)) {
1503 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1504 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001505 return -EINVAL;
1506 }
1507
William Juul52c07962007-10-31 13:53:06 +01001508 chipnr = (int)(from >> chip->chip_shift);
1509 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001510
William Juul52c07962007-10-31 13:53:06 +01001511 /* Shift to get page */
1512 realpage = (int)(from >> chip->page_shift);
1513 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001514
William Juul52c07962007-10-31 13:53:06 +01001515 while(1) {
1516 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001517
William Juul52c07962007-10-31 13:53:06 +01001518 len = min(len, readlen);
1519 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001520
William Juul52c07962007-10-31 13:53:06 +01001521 if (!(chip->options & NAND_NO_READRDY)) {
1522 /*
1523 * Apply delay or wait for ready/busy pin. Do this
1524 * before the AUTOINCR check, so no problems arise if a
1525 * chip which does auto increment is marked as
1526 * NOAUTOINCR by the board driver.
1527 */
1528 if (!chip->dev_ready)
1529 udelay(chip->chip_delay);
1530 else
1531 nand_wait_ready(mtd);
1532 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001533
William Juul52c07962007-10-31 13:53:06 +01001534 readlen -= len;
1535 if (!readlen)
1536 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001537
William Juul52c07962007-10-31 13:53:06 +01001538 /* Increment page address */
1539 realpage++;
1540
1541 page = realpage & chip->pagemask;
1542 /* Check, if we cross a chip boundary */
1543 if (!page) {
1544 chipnr++;
1545 chip->select_chip(mtd, -1);
1546 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001547 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001548
William Juul52c07962007-10-31 13:53:06 +01001549 /* Check, if the chip supports auto page increment
1550 * or if we have hit a block boundary.
1551 */
1552 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1553 sndcmd = 1;
1554 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001555
William Juul52c07962007-10-31 13:53:06 +01001556 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001557 return 0;
1558}
1559
1560/**
William Juul52c07962007-10-31 13:53:06 +01001561 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001562 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001563 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001564 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001565 *
William Juul52c07962007-10-31 13:53:06 +01001566 * NAND read data and/or out-of-band data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001567 */
William Juul52c07962007-10-31 13:53:06 +01001568static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1569 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001570{
William Juul52c07962007-10-31 13:53:06 +01001571 struct nand_chip *chip = mtd->priv;
1572 int ret = -ENOTSUPP;
1573
1574 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001575
1576 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001577 if (ops->datbuf && (from + ops->len) > mtd->size) {
1578 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1579 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001580 return -EINVAL;
1581 }
1582
William Juul52c07962007-10-31 13:53:06 +01001583 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001584
William Juul52c07962007-10-31 13:53:06 +01001585 switch(ops->mode) {
1586 case MTD_OOB_PLACE:
1587 case MTD_OOB_AUTO:
1588 case MTD_OOB_RAW:
1589 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001590
William Juul52c07962007-10-31 13:53:06 +01001591 default:
1592 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001593 }
1594
William Juul52c07962007-10-31 13:53:06 +01001595 if (!ops->datbuf)
1596 ret = nand_do_read_oob(mtd, from, ops);
1597 else
1598 ret = nand_do_read_ops(mtd, from, ops);
1599
1600 out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001601 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001602 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001603}
1604
1605
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001606/**
William Juul52c07962007-10-31 13:53:06 +01001607 * nand_write_page_raw - [Intern] raw page write function
1608 * @mtd: mtd info structure
1609 * @chip: nand chip info structure
1610 * @buf: data buffer
1611 */
1612static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1613 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001614{
William Juul52c07962007-10-31 13:53:06 +01001615 chip->write_buf(mtd, buf, mtd->writesize);
1616 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1617}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001618
William Juul52c07962007-10-31 13:53:06 +01001619/**
1620 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1621 * @mtd: mtd info structure
1622 * @chip: nand chip info structure
1623 * @buf: data buffer
1624 */
1625static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1626 const uint8_t *buf)
1627{
1628 int i, eccsize = chip->ecc.size;
1629 int eccbytes = chip->ecc.bytes;
1630 int eccsteps = chip->ecc.steps;
1631 uint8_t *ecc_calc = chip->buffers->ecccalc;
1632 const uint8_t *p = buf;
1633 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001634
William Juul52c07962007-10-31 13:53:06 +01001635 /* Software ecc calculation */
1636 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1637 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001638
William Juul52c07962007-10-31 13:53:06 +01001639 for (i = 0; i < chip->ecc.total; i++)
1640 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001641
William Juul52c07962007-10-31 13:53:06 +01001642 chip->ecc.write_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001643}
1644
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001645/**
William Juul52c07962007-10-31 13:53:06 +01001646 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1647 * @mtd: mtd info structure
1648 * @chip: nand chip info structure
1649 * @buf: data buffer
1650 */
1651static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1652 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001653{
William Juul52c07962007-10-31 13:53:06 +01001654 int i, eccsize = chip->ecc.size;
1655 int eccbytes = chip->ecc.bytes;
1656 int eccsteps = chip->ecc.steps;
1657 uint8_t *ecc_calc = chip->buffers->ecccalc;
1658 const uint8_t *p = buf;
1659 uint32_t *eccpos = chip->ecc.layout->eccpos;
1660
1661 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1662 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1663 chip->write_buf(mtd, p, eccsize);
1664 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1665 }
1666
1667 for (i = 0; i < chip->ecc.total; i++)
1668 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1669
1670 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001671}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001672
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001673/**
William Juul52c07962007-10-31 13:53:06 +01001674 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1675 * @mtd: mtd info structure
1676 * @chip: nand chip info structure
1677 * @buf: data buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001678 *
William Juul52c07962007-10-31 13:53:06 +01001679 * The hw generator calculates the error syndrome automatically. Therefor
1680 * we need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001681 */
William Juul52c07962007-10-31 13:53:06 +01001682static void nand_write_page_syndrome(struct mtd_info *mtd,
1683 struct nand_chip *chip, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001684{
William Juul52c07962007-10-31 13:53:06 +01001685 int i, eccsize = chip->ecc.size;
1686 int eccbytes = chip->ecc.bytes;
1687 int eccsteps = chip->ecc.steps;
1688 const uint8_t *p = buf;
1689 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001690
William Juul52c07962007-10-31 13:53:06 +01001691 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001692
William Juul52c07962007-10-31 13:53:06 +01001693 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1694 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001695
William Juul52c07962007-10-31 13:53:06 +01001696 if (chip->ecc.prepad) {
1697 chip->write_buf(mtd, oob, chip->ecc.prepad);
1698 oob += chip->ecc.prepad;
1699 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001700
William Juul52c07962007-10-31 13:53:06 +01001701 chip->ecc.calculate(mtd, p, oob);
1702 chip->write_buf(mtd, oob, eccbytes);
1703 oob += eccbytes;
1704
1705 if (chip->ecc.postpad) {
1706 chip->write_buf(mtd, oob, chip->ecc.postpad);
1707 oob += chip->ecc.postpad;
1708 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001709 }
1710
William Juul52c07962007-10-31 13:53:06 +01001711 /* Calculate remaining oob bytes */
1712 i = mtd->oobsize - (oob - chip->oob_poi);
1713 if (i)
1714 chip->write_buf(mtd, oob, i);
1715}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001716
William Juul52c07962007-10-31 13:53:06 +01001717/**
1718 * nand_write_page - [REPLACEABLE] write one page
1719 * @mtd: MTD device structure
1720 * @chip: NAND chip descriptor
1721 * @buf: the data to write
1722 * @page: page number to write
1723 * @cached: cached programming
1724 * @raw: use _raw version of write_page
1725 */
1726static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1727 const uint8_t *buf, int page, int cached, int raw)
1728{
1729 int status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001730
William Juul52c07962007-10-31 13:53:06 +01001731 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1732
1733 if (unlikely(raw))
1734 chip->ecc.write_page_raw(mtd, chip, buf);
1735 else
1736 chip->ecc.write_page(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001737
William Juul52c07962007-10-31 13:53:06 +01001738 /*
1739 * Cached progamming disabled for now, Not sure if its worth the
1740 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1741 */
1742 cached = 0;
1743
1744 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1745
1746 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1747 status = chip->waitfunc(mtd, chip);
1748 /*
1749 * See if operation failed and additional status checks are
1750 * available
1751 */
1752 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1753 status = chip->errstat(mtd, chip, FL_WRITING, status,
1754 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001755
William Juul52c07962007-10-31 13:53:06 +01001756 if (status & NAND_STATUS_FAIL)
1757 return -EIO;
1758 } else {
1759 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1760 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001761 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001762
William Juul52c07962007-10-31 13:53:06 +01001763#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1764 /* Send command to read back the data */
1765 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001766
William Juul52c07962007-10-31 13:53:06 +01001767 if (chip->verify_buf(mtd, buf, mtd->writesize))
1768 return -EIO;
1769#endif
1770 return 0;
1771}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001772
William Juul52c07962007-10-31 13:53:06 +01001773/**
1774 * nand_fill_oob - [Internal] Transfer client buffer to oob
1775 * @chip: nand chip structure
1776 * @oob: oob data buffer
1777 * @ops: oob ops structure
1778 */
1779static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1780 struct mtd_oob_ops *ops)
1781{
1782 size_t len = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001783
William Juul52c07962007-10-31 13:53:06 +01001784 switch(ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001785
William Juul52c07962007-10-31 13:53:06 +01001786 case MTD_OOB_PLACE:
1787 case MTD_OOB_RAW:
1788 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1789 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001790
William Juul52c07962007-10-31 13:53:06 +01001791 case MTD_OOB_AUTO: {
1792 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1793 uint32_t boffs = 0, woffs = ops->ooboffs;
1794 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001795
William Juul52c07962007-10-31 13:53:06 +01001796 for(; free->length && len; free++, len -= bytes) {
1797 /* Write request not from offset 0 ? */
1798 if (unlikely(woffs)) {
1799 if (woffs >= free->length) {
1800 woffs -= free->length;
1801 continue;
1802 }
1803 boffs = free->offset + woffs;
1804 bytes = min_t(size_t, len,
1805 (free->length - woffs));
1806 woffs = 0;
1807 } else {
1808 bytes = min_t(size_t, len, free->length);
1809 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001810 }
William Juul52c07962007-10-31 13:53:06 +01001811 memcpy(chip->oob_poi + boffs, oob, bytes);
1812 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001813 }
William Juul52c07962007-10-31 13:53:06 +01001814 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001815 }
William Juul52c07962007-10-31 13:53:06 +01001816 default:
1817 BUG();
1818 }
1819 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001820}
1821
William Juul52c07962007-10-31 13:53:06 +01001822#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001823
1824/**
William Juul52c07962007-10-31 13:53:06 +01001825 * nand_do_write_ops - [Internal] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001826 * @mtd: MTD device structure
1827 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001828 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001829 *
William Juul52c07962007-10-31 13:53:06 +01001830 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001831 */
William Juul52c07962007-10-31 13:53:06 +01001832static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1833 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001834{
William Juul52c07962007-10-31 13:53:06 +01001835 int chipnr, realpage, page, blockmask, column;
1836 struct nand_chip *chip = mtd->priv;
1837 uint32_t writelen = ops->len;
1838 uint8_t *oob = ops->oobbuf;
1839 uint8_t *buf = ops->datbuf;
1840 int ret, subpage;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001841
William Juul52c07962007-10-31 13:53:06 +01001842 ops->retlen = 0;
1843 if (!writelen)
1844 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001845
William Juul52c07962007-10-31 13:53:06 +01001846 /* reject writes, which are not page aligned */
1847 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
1848 printk(KERN_NOTICE "nand_write: "
1849 "Attempt to write not page aligned data\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001850 return -EINVAL;
1851 }
1852
William Juul52c07962007-10-31 13:53:06 +01001853 column = to & (mtd->writesize - 1);
1854 subpage = column || (writelen & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001855
William Juul52c07962007-10-31 13:53:06 +01001856 if (subpage && oob)
1857 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001858
William Juul52c07962007-10-31 13:53:06 +01001859 chipnr = (int)(to >> chip->chip_shift);
1860 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001861
1862 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01001863 if (nand_check_wp(mtd)) {
1864 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1865 return -EIO;
1866 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001867
William Juul52c07962007-10-31 13:53:06 +01001868 realpage = (int)(to >> chip->page_shift);
1869 page = realpage & chip->pagemask;
1870 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001871
William Juul52c07962007-10-31 13:53:06 +01001872 /* Invalidate the page cache, when we write to the cached page */
1873 if (to <= (chip->pagebuf << chip->page_shift) &&
1874 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1875 chip->pagebuf = -1;
1876
1877 /* If we're not given explicit OOB data, let it be 0xFF */
1878 if (likely(!oob))
1879 memset(chip->oob_poi, 0xff, mtd->oobsize);
1880
1881 while(1) {
1882 int bytes = mtd->writesize;
1883 int cached = writelen > bytes && page != blockmask;
1884 uint8_t *wbuf = buf;
1885
1886 /* Partial page write ? */
1887 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1888 cached = 0;
1889 bytes = min_t(int, bytes - column, (int) writelen);
1890 chip->pagebuf = -1;
1891 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1892 memcpy(&chip->buffers->databuf[column], buf, bytes);
1893 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02001894 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001895
William Juul52c07962007-10-31 13:53:06 +01001896 if (unlikely(oob))
1897 oob = nand_fill_oob(chip, oob, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001898
William Juul52c07962007-10-31 13:53:06 +01001899 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1900 (ops->mode == MTD_OOB_RAW));
1901 if (ret)
1902 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001903
William Juul52c07962007-10-31 13:53:06 +01001904 writelen -= bytes;
1905 if (!writelen)
1906 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001907
William Juul52c07962007-10-31 13:53:06 +01001908 column = 0;
1909 buf += bytes;
1910 realpage++;
1911
1912 page = realpage & chip->pagemask;
1913 /* Check, if we cross a chip boundary */
1914 if (!page) {
1915 chipnr++;
1916 chip->select_chip(mtd, -1);
1917 chip->select_chip(mtd, chipnr);
1918 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001919 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001920
William Juul52c07962007-10-31 13:53:06 +01001921 ops->retlen = ops->len - writelen;
1922 if (unlikely(oob))
1923 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001924 return ret;
1925}
1926
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001927/**
William Juul52c07962007-10-31 13:53:06 +01001928 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001929 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001930 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001931 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001932 * @retlen: pointer to variable to store the number of written bytes
William Juul52c07962007-10-31 13:53:06 +01001933 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001934 *
William Juul52c07962007-10-31 13:53:06 +01001935 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001936 */
William Juul52c07962007-10-31 13:53:06 +01001937static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1938 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001939{
William Juul52c07962007-10-31 13:53:06 +01001940 struct nand_chip *chip = mtd->priv;
1941 int ret;
1942
1943 /* Do not allow reads past end of device */
1944 if ((to + len) > mtd->size)
1945 return -EINVAL;
1946 if (!len)
1947 return 0;
1948
1949 nand_get_device(chip, mtd, FL_WRITING);
1950
1951 chip->ops.len = len;
1952 chip->ops.datbuf = (uint8_t *)buf;
1953 chip->ops.oobbuf = NULL;
1954
1955 ret = nand_do_write_ops(mtd, to, &chip->ops);
1956
1957 *retlen = chip->ops.retlen;
1958
1959 nand_release_device(mtd);
1960
1961 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001962}
1963
1964/**
William Juul52c07962007-10-31 13:53:06 +01001965 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001966 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001967 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001968 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001969 *
William Juul52c07962007-10-31 13:53:06 +01001970 * NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001971 */
William Juul52c07962007-10-31 13:53:06 +01001972static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1973 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001974{
William Juul52c07962007-10-31 13:53:06 +01001975 int chipnr, page, status, len;
1976 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001977
William Juul52c07962007-10-31 13:53:06 +01001978 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1979 (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001980
William Juul52c07962007-10-31 13:53:06 +01001981 if (ops->mode == MTD_OOB_AUTO)
1982 len = chip->ecc.layout->oobavail;
1983 else
1984 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001985
1986 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01001987 if ((ops->ooboffs + ops->ooblen) > len) {
1988 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1989 "Attempt to write past end of page\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001990 return -EINVAL;
1991 }
1992
William Juul52c07962007-10-31 13:53:06 +01001993 if (unlikely(ops->ooboffs >= len)) {
1994 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1995 "Attempt to start write outside oob\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001996 return -EINVAL;
1997 }
1998
William Juul52c07962007-10-31 13:53:06 +01001999 /* Do not allow reads past end of device */
2000 if (unlikely(to >= mtd->size ||
2001 ops->ooboffs + ops->ooblen >
2002 ((mtd->size >> chip->page_shift) -
2003 (to >> chip->page_shift)) * len)) {
2004 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2005 "Attempt write beyond end of device\n");
2006 return -EINVAL;
2007 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002008
William Juul52c07962007-10-31 13:53:06 +01002009 chipnr = (int)(to >> chip->chip_shift);
2010 chip->select_chip(mtd, chipnr);
2011
2012 /* Shift to get page */
2013 page = (int)(to >> chip->page_shift);
2014
2015 /*
2016 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2017 * of my DiskOnChip 2000 test units) will clear the whole data page too
2018 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2019 * it in the doc2000 driver in August 1999. dwmw2.
2020 */
2021 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002022
2023 /* Check, if it is write protected */
2024 if (nand_check_wp(mtd))
William Juul52c07962007-10-31 13:53:06 +01002025 return -EROFS;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002026
William Juul52c07962007-10-31 13:53:06 +01002027 /* Invalidate the page cache, if we write to the cached page */
2028 if (page == chip->pagebuf)
2029 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002030
William Juul52c07962007-10-31 13:53:06 +01002031 memset(chip->oob_poi, 0xff, mtd->oobsize);
2032 nand_fill_oob(chip, ops->oobbuf, ops);
2033 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2034 memset(chip->oob_poi, 0xff, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002035
William Juul52c07962007-10-31 13:53:06 +01002036 if (status)
2037 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002038
William Juul52c07962007-10-31 13:53:06 +01002039 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002040
William Juul52c07962007-10-31 13:53:06 +01002041 return 0;
2042}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002043
William Juul52c07962007-10-31 13:53:06 +01002044/**
2045 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2046 * @mtd: MTD device structure
2047 * @to: offset to write to
2048 * @ops: oob operation description structure
2049 */
2050static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2051 struct mtd_oob_ops *ops)
2052{
2053 struct nand_chip *chip = mtd->priv;
2054 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002055
William Juul52c07962007-10-31 13:53:06 +01002056 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002057
William Juul52c07962007-10-31 13:53:06 +01002058 /* Do not allow writes past end of device */
2059 if (ops->datbuf && (to + ops->len) > mtd->size) {
2060 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2061 "Attempt read beyond end of device\n");
2062 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002063 }
William Juul52c07962007-10-31 13:53:06 +01002064
2065 nand_get_device(chip, mtd, FL_WRITING);
2066
2067 switch(ops->mode) {
2068 case MTD_OOB_PLACE:
2069 case MTD_OOB_AUTO:
2070 case MTD_OOB_RAW:
2071 break;
2072
2073 default:
2074 goto out;
2075 }
2076
2077 if (!ops->datbuf)
2078 ret = nand_do_write_oob(mtd, to, ops);
2079 else
2080 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002081
William Juul52c07962007-10-31 13:53:06 +01002082 out:
2083 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002084 return ret;
2085}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002086
2087/**
2088 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2089 * @mtd: MTD device structure
2090 * @page: the page address of the block which will be erased
2091 *
2092 * Standard erase command for NAND chips
2093 */
William Juul52c07962007-10-31 13:53:06 +01002094static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002095{
William Juul52c07962007-10-31 13:53:06 +01002096 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002097 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002098 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2099 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002100}
2101
2102/**
2103 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2104 * @mtd: MTD device structure
2105 * @page: the page address of the block which will be erased
2106 *
2107 * AND multi block erase command function
2108 * Erase 4 consecutive blocks
2109 */
William Juul52c07962007-10-31 13:53:06 +01002110static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002111{
William Juul52c07962007-10-31 13:53:06 +01002112 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002113 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002114 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2115 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2116 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2117 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2118 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002119}
2120
2121/**
2122 * nand_erase - [MTD Interface] erase block(s)
2123 * @mtd: MTD device structure
2124 * @instr: erase instruction
2125 *
2126 * Erase one ore more blocks
2127 */
William Juul52c07962007-10-31 13:53:06 +01002128static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002129{
William Juul52c07962007-10-31 13:53:06 +01002130 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002131}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002132
William Juul52c07962007-10-31 13:53:06 +01002133#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002134/**
William Juul52c07962007-10-31 13:53:06 +01002135 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002136 * @mtd: MTD device structure
2137 * @instr: erase instruction
2138 * @allowbbt: allow erasing the bbt area
2139 *
2140 * Erase one ore more blocks
2141 */
William Juul52c07962007-10-31 13:53:06 +01002142int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2143 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002144{
2145 int page, len, status, pages_per_block, ret, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002146 struct nand_chip *chip = mtd->priv;
Wolfgang Grandegger03025502009-01-16 18:55:54 +01002147 int rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS]={0};
William Juul52c07962007-10-31 13:53:06 +01002148 unsigned int bbt_masked_page = 0xffffffff;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002149
Scott Wooddf83c472008-06-20 12:38:57 -05002150 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2151 (unsigned int) instr->addr, (unsigned int) instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002152
2153 /* Start address must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002154 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002155 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002156 return -EINVAL;
2157 }
2158
2159 /* Length must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002160 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002161 MTDDEBUG (MTD_DEBUG_LEVEL0,
2162 "nand_erase: Length not block aligned\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002163 return -EINVAL;
2164 }
2165
2166 /* Do not allow erase past end of device */
2167 if ((instr->len + instr->addr) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05002168 MTDDEBUG (MTD_DEBUG_LEVEL0,
2169 "nand_erase: Erase past end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002170 return -EINVAL;
2171 }
2172
2173 instr->fail_addr = 0xffffffff;
2174
2175 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002176 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002177
2178 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002179 page = (int)(instr->addr >> chip->page_shift);
2180 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002181
2182 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002183 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002184
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002185 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002186 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002187
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002188 /* Check, if it is write protected */
2189 if (nand_check_wp(mtd)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002190 MTDDEBUG (MTD_DEBUG_LEVEL0,
2191 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002192 instr->state = MTD_ERASE_FAILED;
2193 goto erase_exit;
2194 }
2195
William Juul52c07962007-10-31 13:53:06 +01002196 /*
2197 * If BBT requires refresh, set the BBT page mask to see if the BBT
2198 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2199 * can not be matched. This is also done when the bbt is actually
2200 * erased to avoid recusrsive updates
2201 */
2202 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2203 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2204
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002205 /* Loop through the pages */
2206 len = instr->len;
2207
2208 instr->state = MTD_ERASING;
2209
2210 while (len) {
William Juul52c07962007-10-31 13:53:06 +01002211 /*
2212 * heck if we have a bad block, we do not erase bad blocks !
2213 */
2214 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2215 chip->page_shift, 0, allowbbt)) {
2216 printk(KERN_WARNING "nand_erase: attempt to erase a "
2217 "bad block at page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002218 instr->state = MTD_ERASE_FAILED;
2219 goto erase_exit;
2220 }
William Juul52c07962007-10-31 13:53:06 +01002221
2222 /*
2223 * Invalidate the page cache, if we erase the block which
2224 * contains the current cached page
2225 */
2226 if (page <= chip->pagebuf && chip->pagebuf <
2227 (page + pages_per_block))
2228 chip->pagebuf = -1;
2229
2230 chip->erase_cmd(mtd, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002231
William Juul52c07962007-10-31 13:53:06 +01002232 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002233
William Juul52c07962007-10-31 13:53:06 +01002234 /*
2235 * See if operation failed and additional status checks are
2236 * available
2237 */
2238 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2239 status = chip->errstat(mtd, chip, FL_ERASING,
2240 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002241
2242 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002243 if (status & NAND_STATUS_FAIL) {
Scott Wooddf83c472008-06-20 12:38:57 -05002244 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2245 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002246 instr->state = MTD_ERASE_FAILED;
William Juul52c07962007-10-31 13:53:06 +01002247 instr->fail_addr = (page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002248 goto erase_exit;
2249 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002250
William Juul52c07962007-10-31 13:53:06 +01002251 /*
2252 * If BBT requires refresh, set the BBT rewrite flag to the
2253 * page being erased
2254 */
2255 if (bbt_masked_page != 0xffffffff &&
2256 (page & BBT_PAGE_MASK) == bbt_masked_page)
2257 rewrite_bbt[chipnr] = (page << chip->page_shift);
2258
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002259 /* Increment page address and decrement length */
William Juul52c07962007-10-31 13:53:06 +01002260 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002261 page += pages_per_block;
2262
2263 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002264 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002265 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002266 chip->select_chip(mtd, -1);
2267 chip->select_chip(mtd, chipnr);
2268
2269 /*
2270 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2271 * page mask to see if this BBT should be rewritten
2272 */
2273 if (bbt_masked_page != 0xffffffff &&
2274 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2275 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2276 BBT_PAGE_MASK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002277 }
2278 }
2279 instr->state = MTD_ERASE_DONE;
2280
William Juul52c07962007-10-31 13:53:06 +01002281 erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002282
2283 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002284
2285 /* Deselect and wake up anyone waiting on the device */
2286 nand_release_device(mtd);
2287
Scott Wood3628f002008-10-24 16:20:43 -05002288 /* Do call back function */
2289 if (!ret)
2290 mtd_erase_callback(instr);
2291
William Juul52c07962007-10-31 13:53:06 +01002292 /*
2293 * If BBT requires refresh and erase was successful, rewrite any
2294 * selected bad block tables
2295 */
2296 if (bbt_masked_page == 0xffffffff || ret)
2297 return ret;
2298
2299 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2300 if (!rewrite_bbt[chipnr])
2301 continue;
2302 /* update the BBT for chip */
2303 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2304 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2305 chip->bbt_td->pages[chipnr]);
2306 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2307 }
2308
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002309 /* Return more or less happy */
2310 return ret;
2311}
2312
2313/**
2314 * nand_sync - [MTD Interface] sync
2315 * @mtd: MTD device structure
2316 *
2317 * Sync is actually a wait for chip ready function
2318 */
William Juul52c07962007-10-31 13:53:06 +01002319static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002320{
William Juul52c07962007-10-31 13:53:06 +01002321 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002322
Scott Wooddf83c472008-06-20 12:38:57 -05002323 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002324
2325 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002326 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002327 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002328 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002329}
2330
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002331/**
William Juul52c07962007-10-31 13:53:06 +01002332 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002333 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002334 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002335 */
William Juul52c07962007-10-31 13:53:06 +01002336static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002337{
2338 /* Check for invalid offset */
William Juul52c07962007-10-31 13:53:06 +01002339 if (offs > mtd->size)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002340 return -EINVAL;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002341
William Juul52c07962007-10-31 13:53:06 +01002342 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002343}
2344
2345/**
William Juul52c07962007-10-31 13:53:06 +01002346 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002347 * @mtd: MTD device structure
2348 * @ofs: offset relative to mtd start
2349 */
William Juul52c07962007-10-31 13:53:06 +01002350static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002351{
William Juul52c07962007-10-31 13:53:06 +01002352 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002353 int ret;
2354
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002355 if ((ret = nand_block_isbad(mtd, ofs))) {
2356 /* If it was bad already, return success and do nothing. */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002357 if (ret > 0)
2358 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002359 return ret;
2360 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002361
William Juul52c07962007-10-31 13:53:06 +01002362 return chip->block_markbad(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002363}
2364
2365/**
William Juul52c07962007-10-31 13:53:06 +01002366 * nand_suspend - [MTD Interface] Suspend the NAND flash
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002367 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002368 */
William Juul52c07962007-10-31 13:53:06 +01002369static int nand_suspend(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002370{
William Juul52c07962007-10-31 13:53:06 +01002371 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002372
William Juul52c07962007-10-31 13:53:06 +01002373 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2374}
2375
2376/**
2377 * nand_resume - [MTD Interface] Resume the NAND flash
2378 * @mtd: MTD device structure
2379 */
2380static void nand_resume(struct mtd_info *mtd)
2381{
2382 struct nand_chip *chip = mtd->priv;
2383
2384 if (chip->state == FL_PM_SUSPENDED)
2385 nand_release_device(mtd);
2386 else
2387 printk(KERN_ERR "nand_resume() called for a chip which is not "
2388 "in suspended state\n");
2389}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002390
William Juul52c07962007-10-31 13:53:06 +01002391/*
2392 * Set default functions
2393 */
2394static void nand_set_defaults(struct nand_chip *chip, int busw)
2395{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002396 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01002397 if (!chip->chip_delay)
2398 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002399
2400 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01002401 if (chip->cmdfunc == NULL)
2402 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002403
2404 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01002405 if (chip->waitfunc == NULL)
2406 chip->waitfunc = nand_wait;
2407
2408 if (!chip->select_chip)
2409 chip->select_chip = nand_select_chip;
2410 if (!chip->read_byte)
2411 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2412 if (!chip->read_word)
2413 chip->read_word = nand_read_word;
2414 if (!chip->block_bad)
2415 chip->block_bad = nand_block_bad;
2416 if (!chip->block_markbad)
2417 chip->block_markbad = nand_default_block_markbad;
2418 if (!chip->write_buf)
2419 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2420 if (!chip->read_buf)
2421 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2422 if (!chip->verify_buf)
2423 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2424 if (!chip->scan_bbt)
2425 chip->scan_bbt = nand_default_bbt;
2426
2427 if (!chip->controller) {
2428 chip->controller = &chip->hwcontrol;
2429
2430 /* XXX U-BOOT XXX */
2431#if 0
2432 spin_lock_init(&chip->controller->lock);
2433 init_waitqueue_head(&chip->controller->wq);
2434#endif
2435 }
2436
2437}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002438
William Juul52c07962007-10-31 13:53:06 +01002439/*
2440 * Get the flash and manufacturer id and lookup if the type is supported
2441 */
2442static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2443 struct nand_chip *chip,
2444 int busw, int *maf_id)
2445{
2446 struct nand_flash_dev *type = NULL;
2447 int i, dev_id, maf_idx;
Scott Wood3628f002008-10-24 16:20:43 -05002448 int tmp_id, tmp_manf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002449
2450 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01002451 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002452
Karl Beldanb6322fc2008-09-15 16:08:03 +02002453 /*
2454 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2455 * after power-up
2456 */
2457 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2458
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002459 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01002460 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002461
2462 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002463 *maf_id = chip->read_byte(mtd);
2464 dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002465
Scott Wood3628f002008-10-24 16:20:43 -05002466 /* Try again to make sure, as some systems the bus-hold or other
2467 * interface concerns can cause random data which looks like a
2468 * possibly credible NAND flash to appear. If the two results do
2469 * not match, ignore the device completely.
2470 */
2471
2472 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2473
2474 /* Read manufacturer and device IDs */
2475
2476 tmp_manf = chip->read_byte(mtd);
2477 tmp_id = chip->read_byte(mtd);
2478
2479 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2480 printk(KERN_INFO "%s: second ID read did not match "
2481 "%02x,%02x against %02x,%02x\n", __func__,
2482 *maf_id, dev_id, tmp_manf, tmp_id);
2483 return ERR_PTR(-ENODEV);
2484 }
2485
William Juul52c07962007-10-31 13:53:06 +01002486 /* Lookup the flash id */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002487 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
William Juul52c07962007-10-31 13:53:06 +01002488 if (dev_id == nand_flash_ids[i].id) {
2489 type = &nand_flash_ids[i];
2490 break;
2491 }
2492 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002493
William Juul52c07962007-10-31 13:53:06 +01002494 if (!type)
2495 return ERR_PTR(-ENODEV);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002496
William Juul52c07962007-10-31 13:53:06 +01002497 if (!mtd->name)
2498 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002499
William Juul52c07962007-10-31 13:53:06 +01002500 chip->chipsize = type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002501
William Juul52c07962007-10-31 13:53:06 +01002502 /* Newer devices have all the information in additional id bytes */
2503 if (!type->pagesize) {
2504 int extid;
2505 /* The 3rd id byte holds MLC / multichip data */
2506 chip->cellinfo = chip->read_byte(mtd);
2507 /* The 4th id byte is the important one */
2508 extid = chip->read_byte(mtd);
2509 /* Calc pagesize */
2510 mtd->writesize = 1024 << (extid & 0x3);
2511 extid >>= 2;
2512 /* Calc oobsize */
2513 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2514 extid >>= 2;
2515 /* Calc blocksize. Blocksize is multiples of 64KiB */
2516 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2517 extid >>= 2;
2518 /* Get buswidth information */
2519 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002520
William Juul52c07962007-10-31 13:53:06 +01002521 } else {
2522 /*
2523 * Old devices have chip data hardcoded in the device id table
2524 */
2525 mtd->erasesize = type->erasesize;
2526 mtd->writesize = type->pagesize;
2527 mtd->oobsize = mtd->writesize / 32;
2528 busw = type->options & NAND_BUSWIDTH_16;
2529 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002530
William Juul52c07962007-10-31 13:53:06 +01002531 /* Try to identify manufacturer */
2532 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2533 if (nand_manuf_ids[maf_idx].id == *maf_id)
2534 break;
2535 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002536
William Juul52c07962007-10-31 13:53:06 +01002537 /*
2538 * Check, if buswidth is correct. Hardware drivers should set
2539 * chip correct !
2540 */
2541 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2542 printk(KERN_INFO "NAND device: Manufacturer ID:"
2543 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2544 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2545 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2546 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2547 busw ? 16 : 8);
2548 return ERR_PTR(-EINVAL);
2549 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002550
William Juul52c07962007-10-31 13:53:06 +01002551 /* Calculate the address shift from the page size */
2552 chip->page_shift = ffs(mtd->writesize) - 1;
2553 /* Convert chipsize to number of pages per chip -1. */
2554 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002555
William Juul52c07962007-10-31 13:53:06 +01002556 chip->bbt_erase_shift = chip->phys_erase_shift =
2557 ffs(mtd->erasesize) - 1;
2558 chip->chip_shift = ffs(chip->chipsize) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002559
William Juul52c07962007-10-31 13:53:06 +01002560 /* Set the bad block position */
2561 chip->badblockpos = mtd->writesize > 512 ?
2562 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002563
William Juul52c07962007-10-31 13:53:06 +01002564 /* Get chip options, preserve non chip based options */
2565 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2566 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002567
William Juul52c07962007-10-31 13:53:06 +01002568 /*
2569 * Set chip as a default. Board drivers can override it, if necessary
2570 */
2571 chip->options |= NAND_NO_AUTOINCR;
2572
2573 /* Check if chip is a not a samsung device. Do not clear the
2574 * options for chips which are not having an extended id.
2575 */
2576 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2577 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2578
2579 /* Check for AND chips with 4 page planes */
2580 if (chip->options & NAND_4PAGE_ARRAY)
2581 chip->erase_cmd = multi_erase_cmd;
2582 else
2583 chip->erase_cmd = single_erase_cmd;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002584
William Juul52c07962007-10-31 13:53:06 +01002585 /* Do not replace user supplied command function ! */
2586 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2587 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002588
Stefan Roese4fe71432008-01-10 18:47:33 +01002589 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2590 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2591 nand_manuf_ids[maf_idx].name, type->name);
William Juul52c07962007-10-31 13:53:06 +01002592
2593 return type;
2594}
2595
2596/**
2597 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2598 * @mtd: MTD device structure
2599 * @maxchips: Number of chips to scan for
2600 *
2601 * This is the first phase of the normal nand_scan() function. It
2602 * reads the flash ID and sets up MTD fields accordingly.
2603 *
2604 * The mtd->owner field must be set to the module of the caller.
2605 */
2606int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2607{
2608 int i, busw, nand_maf_id;
2609 struct nand_chip *chip = mtd->priv;
2610 struct nand_flash_dev *type;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002611
William Juul52c07962007-10-31 13:53:06 +01002612 /* Get buswidth to select the correct functions */
2613 busw = chip->options & NAND_BUSWIDTH_16;
2614 /* Set the default functions */
2615 nand_set_defaults(chip, busw);
2616
2617 /* Read the flash type */
2618 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2619
2620 if (IS_ERR(type)) {
2621 printk(KERN_WARNING "No NAND device found!!!\n");
2622 chip->select_chip(mtd, -1);
2623 return PTR_ERR(type);
2624 }
2625
2626 /* Check for a chip array */
2627 for (i = 1; i < maxchips; i++) {
2628 chip->select_chip(mtd, i);
Karl Beldanb6322fc2008-09-15 16:08:03 +02002629 /* See comment in nand_get_flash_type for reset */
2630 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002631 /* Send the command for reading device ID */
2632 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002633 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002634 if (nand_maf_id != chip->read_byte(mtd) ||
2635 type->id != chip->read_byte(mtd))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002636 break;
2637 }
2638 if (i > 1)
2639 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002640
William Juul52c07962007-10-31 13:53:06 +01002641 /* Store the number of chips and calc total size for mtd */
2642 chip->numchips = i;
2643 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002644
William Juul52c07962007-10-31 13:53:06 +01002645 return 0;
2646}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002647
William Juul52c07962007-10-31 13:53:06 +01002648
2649/**
2650 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2651 * @mtd: MTD device structure
2652 * @maxchips: Number of chips to scan for
2653 *
2654 * This is the second phase of the normal nand_scan() function. It
2655 * fills out all the uninitialized function pointers with the defaults
2656 * and scans for a bad block table if appropriate.
2657 */
2658int nand_scan_tail(struct mtd_info *mtd)
2659{
2660 int i;
2661 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002662
William Juul52c07962007-10-31 13:53:06 +01002663 if (!(chip->options & NAND_OWN_BUFFERS))
2664 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2665 if (!chip->buffers)
2666 return -ENOMEM;
2667
2668 /* Set the internal oob buffer location, just after the page data */
2669 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2670
2671 /*
2672 * If no default placement scheme is given, select an appropriate one
2673 */
2674 if (!chip->ecc.layout) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002675 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002676 case 8:
William Juul52c07962007-10-31 13:53:06 +01002677 chip->ecc.layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002678 break;
2679 case 16:
William Juul52c07962007-10-31 13:53:06 +01002680 chip->ecc.layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002681 break;
2682 case 64:
William Juul52c07962007-10-31 13:53:06 +01002683 chip->ecc.layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002684 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002685 case 128:
William Juul52c07962007-10-31 13:53:06 +01002686 chip->ecc.layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002687 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002688 default:
William Juul52c07962007-10-31 13:53:06 +01002689 printk(KERN_WARNING "No oob scheme defined for "
2690 "oobsize %d\n", mtd->oobsize);
William Juul9e9c2c12007-11-09 13:32:30 +01002691/* BUG(); */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002692 }
2693 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002694
William Juul52c07962007-10-31 13:53:06 +01002695 if (!chip->write_page)
2696 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002697
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002698 /*
William Juul52c07962007-10-31 13:53:06 +01002699 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2700 * selected and we have 256 byte pagesize fallback to software ECC
2701 */
2702 if (!chip->ecc.read_page_raw)
2703 chip->ecc.read_page_raw = nand_read_page_raw;
2704 if (!chip->ecc.write_page_raw)
2705 chip->ecc.write_page_raw = nand_write_page_raw;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002706
William Juul52c07962007-10-31 13:53:06 +01002707 switch (chip->ecc.mode) {
2708 case NAND_ECC_HW:
2709 /* Use standard hwecc read page function ? */
2710 if (!chip->ecc.read_page)
2711 chip->ecc.read_page = nand_read_page_hwecc;
2712 if (!chip->ecc.write_page)
2713 chip->ecc.write_page = nand_write_page_hwecc;
2714 if (!chip->ecc.read_oob)
2715 chip->ecc.read_oob = nand_read_oob_std;
2716 if (!chip->ecc.write_oob)
2717 chip->ecc.write_oob = nand_write_oob_std;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002718
William Juul52c07962007-10-31 13:53:06 +01002719 case NAND_ECC_HW_SYNDROME:
Scott Wood5fd2c302008-03-18 15:29:14 -05002720 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2721 !chip->ecc.hwctl) &&
2722 (!chip->ecc.read_page ||
2723 chip->ecc.read_page == nand_read_page_hwecc ||
2724 !chip->ecc.write_page ||
2725 chip->ecc.write_page == nand_write_page_hwecc)) {
William Juul52c07962007-10-31 13:53:06 +01002726 printk(KERN_WARNING "No ECC functions supplied, "
2727 "Hardware ECC not possible\n");
2728 BUG();
2729 }
2730 /* Use standard syndrome read/write page function ? */
2731 if (!chip->ecc.read_page)
2732 chip->ecc.read_page = nand_read_page_syndrome;
2733 if (!chip->ecc.write_page)
2734 chip->ecc.write_page = nand_write_page_syndrome;
2735 if (!chip->ecc.read_oob)
2736 chip->ecc.read_oob = nand_read_oob_syndrome;
2737 if (!chip->ecc.write_oob)
2738 chip->ecc.write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002739
William Juul52c07962007-10-31 13:53:06 +01002740 if (mtd->writesize >= chip->ecc.size)
2741 break;
2742 printk(KERN_WARNING "%d byte HW ECC not possible on "
2743 "%d byte page size, fallback to SW ECC\n",
2744 chip->ecc.size, mtd->writesize);
2745 chip->ecc.mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002746
William Juul52c07962007-10-31 13:53:06 +01002747 case NAND_ECC_SOFT:
2748 chip->ecc.calculate = nand_calculate_ecc;
2749 chip->ecc.correct = nand_correct_data;
2750 chip->ecc.read_page = nand_read_page_swecc;
Scott Wood3628f002008-10-24 16:20:43 -05002751 chip->ecc.read_subpage = nand_read_subpage;
William Juul52c07962007-10-31 13:53:06 +01002752 chip->ecc.write_page = nand_write_page_swecc;
2753 chip->ecc.read_oob = nand_read_oob_std;
2754 chip->ecc.write_oob = nand_write_oob_std;
2755 chip->ecc.size = 256;
2756 chip->ecc.bytes = 3;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002757 break;
2758
William Juul52c07962007-10-31 13:53:06 +01002759 case NAND_ECC_NONE:
2760 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2761 "This is not recommended !!\n");
2762 chip->ecc.read_page = nand_read_page_raw;
2763 chip->ecc.write_page = nand_write_page_raw;
2764 chip->ecc.read_oob = nand_read_oob_std;
2765 chip->ecc.write_oob = nand_write_oob_std;
2766 chip->ecc.size = mtd->writesize;
2767 chip->ecc.bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002768 break;
2769
2770 default:
William Juul52c07962007-10-31 13:53:06 +01002771 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2772 chip->ecc.mode);
2773 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002774 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002775
William Juul52c07962007-10-31 13:53:06 +01002776 /*
2777 * The number of bytes available for a client to place data into
2778 * the out of band area
2779 */
2780 chip->ecc.layout->oobavail = 0;
2781 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2782 chip->ecc.layout->oobavail +=
2783 chip->ecc.layout->oobfree[i].length;
2784 mtd->oobavail = chip->ecc.layout->oobavail;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002785
William Juul52c07962007-10-31 13:53:06 +01002786 /*
2787 * Set the number of read / write steps for one page depending on ECC
2788 * mode
2789 */
2790 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2791 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2792 printk(KERN_WARNING "Invalid ecc parameters\n");
2793 BUG();
2794 }
2795 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002796
William Juul52c07962007-10-31 13:53:06 +01002797 /*
2798 * Allow subpage writes up to ecc.steps. Not possible for MLC
2799 * FLASH.
2800 */
2801 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2802 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2803 switch(chip->ecc.steps) {
2804 case 2:
2805 mtd->subpage_sft = 1;
2806 break;
2807 case 4:
2808 case 8:
2809 mtd->subpage_sft = 2;
2810 break;
2811 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002812 }
William Juul52c07962007-10-31 13:53:06 +01002813 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002814
William Juul52c07962007-10-31 13:53:06 +01002815 /* Initialize state */
2816 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002817
2818 /* De-select the device */
William Juul52c07962007-10-31 13:53:06 +01002819 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002820
2821 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01002822 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002823
2824 /* Fill in remaining MTD driver data */
2825 mtd->type = MTD_NANDFLASH;
William Juul52c07962007-10-31 13:53:06 +01002826 mtd->flags = MTD_CAP_NANDFLASH;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002827 mtd->erase = nand_erase;
2828 mtd->point = NULL;
2829 mtd->unpoint = NULL;
2830 mtd->read = nand_read;
2831 mtd->write = nand_write;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002832 mtd->read_oob = nand_read_oob;
2833 mtd->write_oob = nand_write_oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002834 mtd->sync = nand_sync;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002835 mtd->lock = NULL;
2836 mtd->unlock = NULL;
William Juul52c07962007-10-31 13:53:06 +01002837 mtd->suspend = nand_suspend;
2838 mtd->resume = nand_resume;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002839 mtd->block_isbad = nand_block_isbad;
2840 mtd->block_markbad = nand_block_markbad;
2841
William Juul52c07962007-10-31 13:53:06 +01002842 /* propagate ecc.layout to mtd_info */
2843 mtd->ecclayout = chip->ecc.layout;
2844
2845 /* Check, if we should skip the bad block table scan */
2846 if (chip->options & NAND_SKIP_BBTSCAN)
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02002847 chip->options |= NAND_BBT_SCANNED;
William Juul52c07962007-10-31 13:53:06 +01002848
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02002849 return 0;
William Juul52c07962007-10-31 13:53:06 +01002850}
2851
2852/* module_text_address() isn't exported, and it's mostly a pointless
2853 test if this is a module _anyway_ -- they'd have to try _really_ hard
2854 to call us from in-kernel code if the core NAND support is modular. */
2855#ifdef MODULE
2856#define caller_is_module() (1)
2857#else
2858#define caller_is_module() \
2859 module_text_address((unsigned long)__builtin_return_address(0))
2860#endif
2861
2862/**
2863 * nand_scan - [NAND Interface] Scan for the NAND device
2864 * @mtd: MTD device structure
2865 * @maxchips: Number of chips to scan for
2866 *
2867 * This fills out all the uninitialized function pointers
2868 * with the defaults.
2869 * The flash ID is read and the mtd/chip structures are
2870 * filled with the appropriate values.
2871 * The mtd->owner field must be set to the module of the caller
2872 *
2873 */
2874int nand_scan(struct mtd_info *mtd, int maxchips)
2875{
2876 int ret;
2877
2878 /* Many callers got this wrong, so check for it for a while... */
2879 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002880#if 0
William Juul52c07962007-10-31 13:53:06 +01002881 if (!mtd->owner && caller_is_module()) {
2882 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2883 BUG();
2884 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002885#endif
William Juulb76ec382007-11-08 10:39:53 +01002886
William Juul52c07962007-10-31 13:53:06 +01002887 ret = nand_scan_ident(mtd, maxchips);
2888 if (!ret)
2889 ret = nand_scan_tail(mtd);
2890 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002891}
2892
2893/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002894 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002895 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002896*/
2897void nand_release(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002898{
William Juul52c07962007-10-31 13:53:06 +01002899 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002900
2901#ifdef CONFIG_MTD_PARTITIONS
2902 /* Deregister partitions */
William Juul52c07962007-10-31 13:53:06 +01002903 del_mtd_partitions(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002904#endif
2905 /* Deregister the device */
William Juul52c07962007-10-31 13:53:06 +01002906 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002907#if 0
William Juul52c07962007-10-31 13:53:06 +01002908 del_mtd_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002909#endif
William Juul52c07962007-10-31 13:53:06 +01002910
2911 /* Free bad block table memory */
2912 kfree(chip->bbt);
2913 if (!(chip->options & NAND_OWN_BUFFERS))
2914 kfree(chip->buffers);
2915}
2916
2917/* XXX U-BOOT XXX */
2918#if 0
2919EXPORT_SYMBOL_GPL(nand_scan);
2920EXPORT_SYMBOL_GPL(nand_scan_ident);
2921EXPORT_SYMBOL_GPL(nand_scan_tail);
2922EXPORT_SYMBOL_GPL(nand_release);
2923
2924static int __init nand_base_init(void)
2925{
2926 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2927 return 0;
2928}
2929
2930static void __exit nand_base_exit(void)
2931{
2932 led_trigger_unregister_simple(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002933}
2934
William Juul52c07962007-10-31 13:53:06 +01002935module_init(nand_base_init);
2936module_exit(nand_base_exit);
2937
2938MODULE_LICENSE("GPL");
2939MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2940MODULE_DESCRIPTION("Generic NAND flash driver code");
2941#endif