blob: 4069ab1278d4428928715fbb0b3bfbac498c4f40 [file] [log] [blame]
Wolfgang Denk932394a2005-08-17 12:55:25 +02001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02008 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02009 * Additional technical information is available on
Scott Woodc45912d2008-10-24 16:20:43 -050010 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020011 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juulcfa460a2007-10-31 13:53:06 +010013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk932394a2005-08-17 12:55:25 +020014 *
William Juulcfa460a2007-10-31 13:53:06 +010015 * Credits:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020016 * David Woodhouse for adding multichip support
17 *
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +010021 * TODO:
Wolfgang Denk932394a2005-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 Woodc45912d2008-10-24 16:20:43 -050027 * BBT table is not serialized, has to be fixed
Wolfgang Denk932394a2005-08-17 12:55:25 +020028 *
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +010037#include <linux/module.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020038#include <linux/delay.h>
39#include <linux/errno.h>
William Juulcfa460a2007-10-31 13:53:06 +010040#include <linux/err.h>
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +010050#include <linux/leds.h>
Wolfgang Denk932394a2005-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 Denkac7eb8a2005-09-14 23:53:32 +020057#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020058
59#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010060
William Juulcfa460a2007-10-31 13:53:06 +010061#define ENOTSUPP 524 /* Operation is not supported */
62
Wolfgang Denk932394a2005-08-17 12:55:25 +020063#include <malloc.h>
64#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010065#include <linux/err.h>
Wolfgang Denk932394a2005-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 Denk932394a2005-08-17 12:55:25 +020078/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010079static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020080 .eccbytes = 3,
81 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010082 .oobfree = {
83 {.offset = 3,
84 .length = 2},
85 {.offset = 6,
86 .length = 2}}
Wolfgang Denk932394a2005-08-17 12:55:25 +020087};
88
William Juulcfa460a2007-10-31 13:53:06 +010089static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020090 .eccbytes = 6,
91 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +010092 .oobfree = {
93 {.offset = 8,
94 . length = 8}}
Wolfgang Denk932394a2005-08-17 12:55:25 +020095};
96
William Juulcfa460a2007-10-31 13:53:06 +010097static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020098 .eccbytes = 24,
99 .eccpos = {
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200106};
107
William Juulcfa460a2007-10-31 13:53:06 +0100108static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200109 .eccbytes = 48,
110 .eccpos = {
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200120};
121
William Juulcfa460a2007-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 Poselenov248ae5c2008-06-06 15:42:43 +0200130
Wolfgang Denk932394a2005-08-17 12:55:25 +0200131/*
Scott Woodc45912d2008-10-24 16:20:43 -0500132 * For devices which display every fart in the system on a separate LED. Is
William Juulcfa460a2007-10-31 13:53:06 +0100133 * compiled away when LED support is disabled.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200134 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200135/* XXX U-BOOT XXX */
136#if 0
William Juulcfa460a2007-10-31 13:53:06 +0100137DEFINE_LED_TRIGGER(nand_led_trigger);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200138#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200139
140/**
141 * nand_release_device - [GENERIC] release chip
142 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200143 *
144 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk932394a2005-08-17 12:55:25 +0200145 */
146/* XXX U-BOOT XXX */
147#if 0
William Juulcfa460a2007-10-31 13:53:06 +0100148static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200149{
William Juulcfa460a2007-10-31 13:53:06 +0100150 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200151
152 /* De-select the NAND device */
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200161}
162#else
Wolfgang Denk8e9655f2005-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 Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100176static uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200177{
William Juulcfa460a2007-10-31 13:53:06 +0100178 struct nand_chip *chip = mtd->priv;
179 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-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 Denkac7eb8a2005-09-14 23:53:32 +0200186 * Default read function for 16bit buswith with
Wolfgang Denk932394a2005-08-17 12:55:25 +0200187 * endianess conversion
188 */
William Juulcfa460a2007-10-31 13:53:06 +0100189static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200190{
William Juulcfa460a2007-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 Denk932394a2005-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 Denkac7eb8a2005-09-14 23:53:32 +0200199 * Default read function for 16bit buswith without
Wolfgang Denk932394a2005-08-17 12:55:25 +0200200 * endianess conversion
201 */
202static u16 nand_read_word(struct mtd_info *mtd)
203{
William Juulcfa460a2007-10-31 13:53:06 +0100204 struct nand_chip *chip = mtd->priv;
205 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200206}
207
208/**
209 * nand_select_chip - [DEFAULT] control CE line
210 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +0100211 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200212 *
213 * Default select function for 1 chip devices.
214 */
William Juulcfa460a2007-10-31 13:53:06 +0100215static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200216{
William Juulcfa460a2007-10-31 13:53:06 +0100217 struct nand_chip *chip = mtd->priv;
218
219 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200220 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100221 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200222 break;
223 case 0:
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100239static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200240{
241 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100242 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200243
William Juulcfa460a2007-10-31 13:53:06 +0100244 for (i = 0; i < len; i++)
245 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200246}
247
248/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200249 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100256static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200257{
258 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100259 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200260
William Juulcfa460a2007-10-31 13:53:06 +0100261 for (i = 0; i < len; i++)
262 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200263}
264
265/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200266 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100273static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200274{
275 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100276 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200277
William Juulcfa460a2007-10-31 13:53:06 +0100278 for (i = 0; i < len; i++)
279 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200280 return -EFAULT;
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100292static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200293{
294 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100295 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200296 u16 *p = (u16 *) buf;
297 len >>= 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200298
William Juulcfa460a2007-10-31 13:53:06 +0100299 for (i = 0; i < len; i++)
300 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200301
Wolfgang Denk932394a2005-08-17 12:55:25 +0200302}
303
304/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200305 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100312static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200313{
314 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100315 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200316 u16 *p = (u16 *) buf;
317 len >>= 1;
318
William Juulcfa460a2007-10-31 13:53:06 +0100319 for (i = 0; i < len; i++)
320 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200321}
322
323/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200324 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100331static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200332{
333 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100334 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200335 u16 *p = (u16 *) buf;
336 len >>= 1;
337
William Juulcfa460a2007-10-31 13:53:06 +0100338 for (i = 0; i < len; i++)
339 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-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 Denkac7eb8a2005-09-14 23:53:32 +0200351 * Check, if the block is bad.
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100356 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200357 u16 bad;
358
William Juulcfa460a2007-10-31 13:53:06 +0100359 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200360
Wolfgang Denk932394a2005-08-17 12:55:25 +0200361 if (getchip) {
William Juulcfa460a2007-10-31 13:53:06 +0100362 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200363
William Juulcfa460a2007-10-31 13:53:06 +0100364 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200365
366 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100367 chip->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200368 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200369
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200376 if ((bad & 0xFF) != 0xff)
377 res = 1;
378 } else {
William Juulcfa460a2007-10-31 13:53:06 +0100379 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
380 if (chip->read_byte(mtd) != 0xff)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200381 res = 1;
382 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200383
William Juulcfa460a2007-10-31 13:53:06 +0100384 if (getchip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200385 nand_release_device(mtd);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200386
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100400 struct nand_chip *chip = mtd->priv;
401 uint8_t buf[2] = { 0, 0 };
402 int block, ret;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200403
Wolfgang Denk932394a2005-08-17 12:55:25 +0200404 /* Get block number */
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200408
409 /* Do we have a flash based bad block table ? */
William Juulcfa460a2007-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 Woodc45912d2008-10-24 16:20:43 -0500416 nand_get_device(chip, mtd, FL_WRITING);
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200422
William Juulcfa460a2007-10-31 13:53:06 +0100423 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Scott Woodc45912d2008-10-24 16:20:43 -0500424 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100425 }
426 if (!ret)
427 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500428
William Juulcfa460a2007-10-31 13:53:06 +0100429 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200430}
431
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200432/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200433 * nand_check_wp - [GENERIC] check if the chip is write protected
434 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200435 * Check, if the device is write protected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200436 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200437 * The function expects, that the device is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200438 */
William Juulcfa460a2007-10-31 13:53:06 +0100439static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200440{
William Juulcfa460a2007-10-31 13:53:06 +0100441 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200442 /* Check the WP bit */
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200445}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100446
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100457static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
458 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200459{
William Juulcfa460a2007-10-31 13:53:06 +0100460 struct nand_chip *chip = mtd->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200461
Ilya Yanok13f0fd92008-06-30 15:34:40 +0200462 if (!(chip->options & NAND_BBT_SCANNED)) {
463 chip->scan_bbt(mtd);
464 chip->options |= NAND_BBT_SCANNED;
465 }
466
William Juulcfa460a2007-10-31 13:53:06 +0100467 if (!chip->bbt)
468 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200469
Wolfgang Denk932394a2005-08-17 12:55:25 +0200470 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100471 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200472}
473
William Juulcfa460a2007-10-31 13:53:06 +0100474/*
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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200499 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Stefan Roese12072262008-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 }
William Juulcfa460a2007-10-31 13:53:06 +0100509}
510#endif
511
Wolfgang Denk932394a2005-08-17 12:55:25 +0200512/**
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 Juulcfa460a2007-10-31 13:53:06 +0100522static void nand_command(struct mtd_info *mtd, unsigned int command,
523 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200524{
William Juulcfa460a2007-10-31 13:53:06 +0100525 register struct nand_chip *chip = mtd->priv;
526 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200527
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100534 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200535 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100536 column -= mtd->writesize;
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100545 chip->cmd_ctrl(mtd, readcmd, ctrl);
546 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200547 }
William Juulcfa460a2007-10-31 13:53:06 +0100548 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200549
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200561 }
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200571
572 /*
573 * program and erase have their own busy handlers
Wolfgang Denk932394a2005-08-17 12:55:25 +0200574 * status and sequential in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100575 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200576 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200577
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100586 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200587 break;
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200594 return;
595
William Juulcfa460a2007-10-31 13:53:06 +0100596 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200597 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200598 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200599 * If we don't have access to the busy pin, we apply the given
600 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100601 */
602 if (!chip->dev_ready) {
603 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200604 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200605 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200606 }
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100609 ndelay(100);
610
611 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-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 Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200624 */
William Juulcfa460a2007-10-31 13:53:06 +0100625static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
626 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200627{
William Juulcfa460a2007-10-31 13:53:06 +0100628 register struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200629
630 /* Emulate NAND_CMD_READOOB */
631 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100632 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200633 command = NAND_CMD_READ0;
634 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200635
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200639
640 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100641 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200642
643 /* Serially input address */
644 if (column != -1) {
645 /* Adjust columns for 16 bit buswidth */
William Juulcfa460a2007-10-31 13:53:06 +0100646 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200647 column >>= 1;
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200651 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200652 if (page_addr != -1) {
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200656 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200660 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200661 }
William Juulcfa460a2007-10-31 13:53:06 +0100662 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200663
664 /*
665 * program and erase have their own busy handlers
William Juulcfa460a2007-10-31 13:53:06 +0100666 * status, sequential in, and deplete1 need no delay
667 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200668 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200669
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100675 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200676 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100677 case NAND_CMD_DEPLETE1:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200678 return;
679
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200690
691 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100692 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200693 break;
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200708 return;
709
710 case NAND_CMD_READ0:
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200715
William Juulcfa460a2007-10-31 13:53:06 +0100716 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200717 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200718 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200719 * If we don't have access to the busy pin, we apply the given
720 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100721 */
722 if (!chip->dev_ready) {
723 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200724 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200725 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200726 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200727
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100730 ndelay(100);
731
732 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200733}
734
735/**
736 * nand_get_device - [GENERIC] Get chip for selected access
William Juulcfa460a2007-10-31 13:53:06 +0100737 * @chip: the nand chip descriptor
Wolfgang Denk932394a2005-08-17 12:55:25 +0200738 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200739 * @new_state: the state which is requested
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100745static int
746nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200747{
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200753
Wolfgang Denk932394a2005-08-17 12:55:25 +0200754 /* Hardware controller shared among independend devices */
William Juulcfa460a2007-10-31 13:53:06 +0100755 /* Hardware controller shared among independend devices */
756 if (!chip->controller->active)
757 chip->controller->active = chip;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200758
William Juulcfa460a2007-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;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200763 }
William Juulcfa460a2007-10-31 13:53:06 +0100764 if (new_state == FL_PM_SUSPENDED) {
765 spin_unlock(lock);
766 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
767 }
768 set_current_state(TASK_UNINTERRUPTIBLE);
769 add_wait_queue(wq, &wait);
770 spin_unlock(lock);
771 schedule();
772 remove_wait_queue(wq, &wait);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200773 goto retry;
774}
775#else
William Juulcfa460a2007-10-31 13:53:06 +0100776static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
777{
Marcel Ziswilereafcabd2008-06-22 16:30:06 +0200778 this->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100779 return 0;
780}
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100786 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200787 *
788 * Wait for command done. This applies to erase and program only
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200789 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk932394a2005-08-17 12:55:25 +0200790 * general NAND and SmartMedia specs
William Juulcfa460a2007-10-31 13:53:06 +0100791 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200792/* XXX U-BOOT XXX */
793#if 0
William Juulcfa460a2007-10-31 13:53:06 +0100794static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200795{
William Juulcfa460a2007-10-31 13:53:06 +0100796
797 unsigned long timeo = jiffies;
798 int status, state = chip->state;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200799
Wolfgang Denk932394a2005-08-17 12:55:25 +0200800 if (state == FL_ERASING)
William Juulcfa460a2007-10-31 13:53:06 +0100801 timeo += (HZ * 400) / 1000;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200802 else
William Juulcfa460a2007-10-31 13:53:06 +0100803 timeo += (HZ * 20) / 1000;
804
805 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100809 ndelay(100);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200810
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200813 else
William Juulcfa460a2007-10-31 13:53:06 +0100814 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200815
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200816 while (time_before(jiffies, timeo)) {
William Juulcfa460a2007-10-31 13:53:06 +0100817 if (chip->dev_ready) {
818 if (chip->dev_ready(mtd))
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200819 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200820 } else {
William Juulcfa460a2007-10-31 13:53:06 +0100821 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200822 break;
823 }
William Juulcfa460a2007-10-31 13:53:06 +0100824 cond_resched();
Wolfgang Denk932394a2005-08-17 12:55:25 +0200825 }
William Juulcfa460a2007-10-31 13:53:06 +0100826 led_trigger_event(nand_led_trigger, LED_OFF);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200827
William Juulcfa460a2007-10-31 13:53:06 +0100828 status = (int)chip->read_byte(mtd);
829 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200830}
831#else
William Juulcfa460a2007-10-31 13:53:06 +0100832static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200833{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100834 unsigned long timeo;
William Juulcfa460a2007-10-31 13:53:06 +0100835 int state = this->state;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100836
837 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200838 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100839 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200840 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk8e9655f2005-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 Sieka038ccac2006-02-24 09:37:22 +0100847 reset_timer();
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100848
849 while (1) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100850 if (get_timer(0) > timeo) {
851 printf("Timeout!");
Stefan Roese15784862006-11-27 17:22:19 +0100852 return 0x01;
853 }
Wolfgang Denk8e9655f2005-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 Siekaaddb2e12006-03-05 18:57:33 +0100863#ifdef PPCHAMELON_NAND_TIMER_HACK
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100864 reset_timer();
865 while (get_timer(0) < 10);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100866#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100867
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100868 return this->read_byte(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200869}
870#endif
871
872/**
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200877 */
William Juulcfa460a2007-10-31 13:53:06 +0100878static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
879 uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200880{
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200885
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200902
William Juulcfa460a2007-10-31 13:53:06 +0100903 chip->ecc.read_page_raw(mtd, chip, buf);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200904
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200907
William Juulcfa460a2007-10-31 13:53:06 +0100908 for (i = 0; i < chip->ecc.total; i++)
909 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200910
William Juulcfa460a2007-10-31 13:53:06 +0100911 eccsteps = chip->ecc.steps;
912 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200913
William Juulcfa460a2007-10-31 13:53:06 +0100914 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
915 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200916
William Juulcfa460a2007-10-31 13:53:06 +0100917 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Woodc45912d2008-10-24 16:20:43 -0500918 if (stat < 0)
919 mtd->ecc_stats.failed++;
920 else
921 mtd->ecc_stats.corrected += stat;
922 }
923 return 0;
924}
925
926/**
927 * 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)
William Juulcfa460a2007-10-31 13:53:06 +01001000 mtd->ecc_stats.failed++;
1001 else
1002 mtd->ecc_stats.corrected += stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001003 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001004 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001005}
1006
Wolfgang Denk932394a2005-08-17 12:55:25 +02001007/**
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02001012 *
William Juulcfa460a2007-10-31 13:53:06 +01001013 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk932394a2005-08-17 12:55:25 +02001014 */
William Juulcfa460a2007-10-31 13:53:06 +01001015static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1016 uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001017{
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02001025
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02001032
William Juulcfa460a2007-10-31 13:53:06 +01001033 for (i = 0; i < chip->ecc.total; i++)
1034 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001035
William Juulcfa460a2007-10-31 13:53:06 +01001036 eccsteps = chip->ecc.steps;
1037 p = buf;
1038
1039 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1040 int stat;
1041
1042 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}
1050
1051/**
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 Denk932394a2005-08-17 12:55:25 +02001078 }
1079
William Juulcfa460a2007-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);
1083
Scott Woodc45912d2008-10-24 16:20:43 -05001084 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001085 mtd->ecc_stats.failed++;
1086 else
1087 mtd->ecc_stats.corrected += stat;
1088
1089 oob += eccbytes;
1090
1091 if (chip->ecc.postpad) {
1092 chip->read_buf(mtd, oob, chip->ecc.postpad);
1093 oob += chip->ecc.postpad;
1094 }
1095 }
1096
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;
1103}
1104
1105/**
1106 * 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)
1114{
1115 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;
1121
1122 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}
1152
1153/**
1154 * nand_do_read_ops - [Internal] Read data with ECC
1155 *
1156 * @mtd: MTD device structure
1157 * @from: offset to read from
1158 * @ops: oob ops structure
1159 *
1160 * Internal function. Called with chip held.
1161 */
1162static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1163 struct mtd_oob_ops *ops)
1164{
1165 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;
1174
1175 stats = mtd->ecc_stats;
1176
1177 chipnr = (int)(from >> chip->chip_shift);
1178 chip->select_chip(mtd, chipnr);
1179
1180 realpage = (int)(from >> chip->page_shift);
1181 page = realpage & chip->pagemask;
1182
1183 col = (int)(from & (mtd->writesize - 1));
1184
1185 buf = ops->datbuf;
1186 oob = ops->oobbuf;
1187
1188 while(1) {
1189 bytes = min(mtd->writesize - col, readlen);
1190 aligned = (bytes == mtd->writesize);
1191
1192 /* Is the current page in the buffer ? */
1193 if (realpage != chip->pagebuf || oob) {
1194 bufpoi = aligned ? buf : chip->buffers->databuf;
1195
1196 if (likely(sndcmd)) {
1197 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1198 sndcmd = 0;
1199 }
1200
1201 /* 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 Woodc45912d2008-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 Juulcfa460a2007-10-31 13:53:06 +01001206 else
1207 ret = chip->ecc.read_page(mtd, chip, bufpoi);
1208 if (ret < 0)
1209 break;
1210
1211 /* Transfer not aligned data */
1212 if (!aligned) {
Scott Woodc45912d2008-10-24 16:20:43 -05001213 if (!NAND_SUBPAGE_READ(chip) && !oob)
1214 chip->pagebuf = realpage;
William Juulcfa460a2007-10-31 13:53:06 +01001215 memcpy(buf, chip->buffers->databuf + col, bytes);
1216 }
1217
1218 buf += bytes;
1219
1220 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 }
1234
1235 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);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001247 }
1248 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001249 memcpy(buf, chip->buffers->databuf + col, bytes);
1250 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001251 }
1252
William Juulcfa460a2007-10-31 13:53:06 +01001253 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001254
William Juulcfa460a2007-10-31 13:53:06 +01001255 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001256 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001257
1258 /* For subsequent reads align to page boundary. */
1259 col = 0;
1260 /* Increment page address */
1261 realpage++;
1262
William Juulcfa460a2007-10-31 13:53:06 +01001263 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001264 /* Check, if we cross a chip boundary */
1265 if (!page) {
1266 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001267 chip->select_chip(mtd, -1);
1268 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001269 }
William Juulcfa460a2007-10-31 13:53:06 +01001270
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001271 /* Check, if the chip supports auto page increment
1272 * or if we have hit a block boundary.
William Juulcfa460a2007-10-31 13:53:06 +01001273 */
1274 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001275 sndcmd = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001276 }
1277
William Juulcfa460a2007-10-31 13:53:06 +01001278 ops->retlen = ops->len - (size_t) readlen;
1279 if (oob)
1280 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001281
William Juulcfa460a2007-10-31 13:53:06 +01001282 if (ret)
1283 return ret;
1284
1285 if (mtd->ecc_stats.failed - stats.failed)
1286 return -EBADMSG;
1287
1288 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001289}
1290
1291/**
William Juulcfa460a2007-10-31 13:53:06 +01001292 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
Wolfgang Denk932394a2005-08-17 12:55:25 +02001293 * @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 *
William Juulcfa460a2007-10-31 13:53:06 +01001299 * Get hold of the chip and call nand_do_read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001300 */
William Juulcfa460a2007-10-31 13:53:06 +01001301static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1302 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001303{
William Juulcfa460a2007-10-31 13:53:06 +01001304 struct nand_chip *chip = mtd->priv;
1305 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001306
1307 /* Do not allow reads past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01001308 if ((from + len) > mtd->size)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001309 return -EINVAL;
William Juulcfa460a2007-10-31 13:53:06 +01001310 if (!len)
1311 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001312
William Juulcfa460a2007-10-31 13:53:06 +01001313 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001314
William Juulcfa460a2007-10-31 13:53:06 +01001315 chip->ops.len = len;
1316 chip->ops.datbuf = buf;
1317 chip->ops.oobbuf = NULL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001318
William Juulcfa460a2007-10-31 13:53:06 +01001319 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001320
William Juulcfa460a2007-10-31 13:53:06 +01001321 *retlen = chip->ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001322
Wolfgang Denk932394a2005-08-17 12:55:25 +02001323 nand_release_device(mtd);
1324
1325 return ret;
1326}
1327
William Juulcfa460a2007-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}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001345
1346/**
William Juulcfa460a2007-10-31 13:53:06 +01001347 * 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);
1372 } else
1373 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);
1381
1382 return 1;
1383}
1384
1385/**
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;
1397
1398 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);
1402
1403 status = chip->waitfunc(mtd, chip);
1404
1405 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;
1422
1423 /*
1424 * data-ecc-data-ecc ... ecc-oob
1425 * or
1426 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1427 */
1428 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;
1465}
1466
1467/**
1468 * nand_do_read_oob - [Intern] NAND read out-of-band
1469 * @mtd: MTD device structure
1470 * @from: offset to read from
1471 * @ops: oob operations description structure
1472 *
1473 * NAND read out-of-band data from the spare area
1474 */
1475static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1476 struct mtd_oob_ops *ops)
1477{
1478 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;
1484
1485 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1486 (unsigned long long)from, readlen);
1487
1488 if (ops->mode == MTD_OOB_AUTO)
1489 len = chip->ecc.layout->oobavail;
1490 else
1491 len = mtd->oobsize;
1492
1493 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 }
1498
1499 /* Do not allow reads past end of device */
1500 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");
1505 return -EINVAL;
1506 }
1507
1508 chipnr = (int)(from >> chip->chip_shift);
1509 chip->select_chip(mtd, chipnr);
1510
1511 /* Shift to get page */
1512 realpage = (int)(from >> chip->page_shift);
1513 page = realpage & chip->pagemask;
1514
1515 while(1) {
1516 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1517
1518 len = min(len, readlen);
1519 buf = nand_transfer_oob(chip, buf, ops, len);
1520
1521 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 }
1533
1534 readlen -= len;
1535 if (!readlen)
1536 break;
1537
1538 /* 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);
1547 }
1548
1549 /* 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 }
1555
1556 ops->oobretlen = ops->ooblen;
1557 return 0;
1558}
1559
1560/**
1561 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1562 * @mtd: MTD device structure
1563 * @from: offset to read from
1564 * @ops: oob operation description structure
1565 *
1566 * NAND read data and/or out-of-band data
1567 */
1568static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1569 struct mtd_oob_ops *ops)
1570{
1571 struct nand_chip *chip = mtd->priv;
1572 int ret = -ENOTSUPP;
1573
1574 ops->retlen = 0;
1575
1576 /* Do not allow reads past end of device */
1577 if (ops->datbuf && (from + ops->len) > mtd->size) {
1578 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1579 "Attempt read beyond end of device\n");
1580 return -EINVAL;
1581 }
1582
1583 nand_get_device(chip, mtd, FL_READING);
1584
1585 switch(ops->mode) {
1586 case MTD_OOB_PLACE:
1587 case MTD_OOB_AUTO:
1588 case MTD_OOB_RAW:
1589 break;
1590
1591 default:
1592 goto out;
1593 }
1594
1595 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:
1601 nand_release_device(mtd);
1602 return ret;
1603}
1604
1605
1606/**
1607 * 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)
1614{
1615 chip->write_buf(mtd, buf, mtd->writesize);
1616 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1617}
1618
1619/**
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;
1634
1635 /* Software ecc calculation */
1636 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1637 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1638
1639 for (i = 0; i < chip->ecc.total; i++)
1640 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1641
1642 chip->ecc.write_page_raw(mtd, chip, buf);
1643}
1644
1645/**
1646 * 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)
1653{
1654 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);
1671}
1672
1673/**
1674 * 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
1678 *
1679 * The hw generator calculates the error syndrome automatically. Therefor
1680 * we need a special oob layout and handling.
1681 */
1682static void nand_write_page_syndrome(struct mtd_info *mtd,
1683 struct nand_chip *chip, const uint8_t *buf)
1684{
1685 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;
1690
1691 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1692
1693 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1694 chip->write_buf(mtd, p, eccsize);
1695
1696 if (chip->ecc.prepad) {
1697 chip->write_buf(mtd, oob, chip->ecc.prepad);
1698 oob += chip->ecc.prepad;
1699 }
1700
1701 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 }
1709 }
1710
1711 /* Calculate remaining oob bytes */
1712 i = mtd->oobsize - (oob - chip->oob_poi);
1713 if (i)
1714 chip->write_buf(mtd, oob, i);
1715}
1716
1717/**
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;
1730
1731 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);
1737
1738 /*
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);
1755
1756 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);
1761 }
1762
1763#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1764 /* Send command to read back the data */
1765 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1766
1767 if (chip->verify_buf(mtd, buf, mtd->writesize))
1768 return -EIO;
1769#endif
1770 return 0;
1771}
1772
1773/**
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;
1783
1784 switch(ops->mode) {
1785
1786 case MTD_OOB_PLACE:
1787 case MTD_OOB_RAW:
1788 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1789 return oob + len;
1790
1791 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;
1795
1796 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;
1810 }
1811 memcpy(chip->oob_poi + boffs, oob, bytes);
1812 oob += bytes;
1813 }
1814 return oob;
1815 }
1816 default:
1817 BUG();
1818 }
1819 return NULL;
1820}
1821
1822#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
1823
1824/**
1825 * nand_do_write_ops - [Internal] NAND write with ECC
1826 * @mtd: MTD device structure
1827 * @to: offset to write to
1828 * @ops: oob operations description structure
1829 *
1830 * NAND write with ECC
1831 */
1832static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1833 struct mtd_oob_ops *ops)
1834{
1835 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;
1841
1842 ops->retlen = 0;
1843 if (!writelen)
1844 return 0;
1845
1846 /* 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");
1850 return -EINVAL;
1851 }
1852
1853 column = to & (mtd->writesize - 1);
1854 subpage = column || (writelen & (mtd->writesize - 1));
1855
1856 if (subpage && oob)
1857 return -EINVAL;
1858
1859 chipnr = (int)(to >> chip->chip_shift);
1860 chip->select_chip(mtd, chipnr);
1861
1862 /* Check, if it is write protected */
1863 if (nand_check_wp(mtd)) {
1864 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1865 return -EIO;
1866 }
1867
1868 realpage = (int)(to >> chip->page_shift);
1869 page = realpage & chip->pagemask;
1870 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1871
1872 /* 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;
1894 }
1895
1896 if (unlikely(oob))
1897 oob = nand_fill_oob(chip, oob, ops);
1898
1899 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1900 (ops->mode == MTD_OOB_RAW));
1901 if (ret)
1902 break;
1903
1904 writelen -= bytes;
1905 if (!writelen)
1906 break;
1907
1908 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 }
1919 }
1920
1921 ops->retlen = ops->len - writelen;
1922 if (unlikely(oob))
1923 ops->oobretlen = ops->ooblen;
1924 return ret;
1925}
1926
1927/**
1928 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk932394a2005-08-17 12:55:25 +02001929 * @mtd: MTD device structure
1930 * @to: offset to write to
1931 * @len: number of bytes to write
1932 * @retlen: pointer to variable to store the number of written bytes
1933 * @buf: the data to write
1934 *
William Juulcfa460a2007-10-31 13:53:06 +01001935 * NAND write with ECC
1936 */
1937static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1938 size_t *retlen, const uint8_t *buf)
1939{
1940 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;
1962}
1963
1964/**
1965 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1966 * @mtd: MTD device structure
1967 * @to: offset to write to
1968 * @ops: oob operation description structure
1969 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02001970 * NAND write out-of-band
1971 */
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02001974{
William Juulcfa460a2007-10-31 13:53:06 +01001975 int chipnr, page, status, len;
1976 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001977
Scott Wood3167c532008-06-20 12:38:57 -05001978 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
William Juulcfa460a2007-10-31 13:53:06 +01001979 (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001980
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02001985
1986 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01001987 if ((ops->ooboffs + ops->ooblen) > len) {
Scott Wood3167c532008-06-20 12:38:57 -05001988 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1989 "Attempt to write past end of page\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02001990 return -EINVAL;
1991 }
1992
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02001996 return -EINVAL;
1997 }
1998
William Juulcfa460a2007-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");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002006 return -EINVAL;
2007 }
2008
William Juulcfa460a2007-10-31 13:53:06 +01002009 chipnr = (int)(to >> chip->chip_shift);
2010 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002011
William Juulcfa460a2007-10-31 13:53:06 +01002012 /* 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 Denk932394a2005-08-17 12:55:25 +02002022
2023 /* Check, if it is write protected */
2024 if (nand_check_wp(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01002025 return -EROFS;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002026
Wolfgang Denk932394a2005-08-17 12:55:25 +02002027 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002028 if (page == chip->pagebuf)
2029 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002030
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02002035
William Juulcfa460a2007-10-31 13:53:06 +01002036 if (status)
2037 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002038
William Juulcfa460a2007-10-31 13:53:06 +01002039 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002040
William Juulcfa460a2007-10-31 13:53:06 +01002041 return 0;
2042}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002043
William Juulcfa460a2007-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;
2055
2056 ops->retlen = 0;
2057
2058 /* 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 Denk932394a2005-08-17 12:55:25 +02002063 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002064
William Juulcfa460a2007-10-31 13:53:06 +01002065 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);
2081
2082 out:
2083 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002084 return ret;
2085}
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +01002094static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002095{
William Juulcfa460a2007-10-31 13:53:06 +01002096 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002097 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002098 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2099 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +01002110static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002111{
William Juulcfa460a2007-10-31 13:53:06 +01002112 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002113 /* Send commands to erase a block */
William Juulcfa460a2007-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 Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +01002128static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002129{
William Juulcfa460a2007-10-31 13:53:06 +01002130 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002131}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002132
William Juulcfa460a2007-10-31 13:53:06 +01002133#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk932394a2005-08-17 12:55:25 +02002134/**
William Juulcfa460a2007-10-31 13:53:06 +01002135 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +01002142int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2143 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002144{
2145 int page, len, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002146 struct nand_chip *chip = mtd->priv;
2147 int rewrite_bbt[NAND_MAX_CHIPS]={0};
2148 unsigned int bbt_masked_page = 0xffffffff;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002149
Scott Wood3167c532008-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 Denk932394a2005-08-17 12:55:25 +02002152
2153 /* Start address must align on block boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002154 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wood3167c532008-06-20 12:38:57 -05002155 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002156 return -EINVAL;
2157 }
2158
2159 /* Length must align on block boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002160 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wood3167c532008-06-20 12:38:57 -05002161 MTDDEBUG (MTD_DEBUG_LEVEL0,
2162 "nand_erase: Length not block aligned\n");
Wolfgang Denk932394a2005-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 Wood3167c532008-06-20 12:38:57 -05002168 MTDDEBUG (MTD_DEBUG_LEVEL0,
2169 "nand_erase: Erase past end of device\n");
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +01002176 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002177
2178 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002179 page = (int)(instr->addr >> chip->page_shift);
2180 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002181
2182 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002183 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002184
Wolfgang Denk932394a2005-08-17 12:55:25 +02002185 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002186 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002187
Wolfgang Denk932394a2005-08-17 12:55:25 +02002188 /* Check, if it is write protected */
2189 if (nand_check_wp(mtd)) {
Scott Wood3167c532008-06-20 12:38:57 -05002190 MTDDEBUG (MTD_DEBUG_LEVEL0,
2191 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002192 instr->state = MTD_ERASE_FAILED;
2193 goto erase_exit;
2194 }
2195
William Juulcfa460a2007-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 Denk932394a2005-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 Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02002218 instr->state = MTD_ERASE_FAILED;
2219 goto erase_exit;
2220 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002221
William Juulcfa460a2007-10-31 13:53:06 +01002222 /*
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;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002229
William Juulcfa460a2007-10-31 13:53:06 +01002230 chip->erase_cmd(mtd, page & chip->pagemask);
2231
2232 status = chip->waitfunc(mtd, chip);
2233
2234 /*
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 Denk932394a2005-08-17 12:55:25 +02002241
2242 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002243 if (status & NAND_STATUS_FAIL) {
Scott Wood3167c532008-06-20 12:38:57 -05002244 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2245 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002246 instr->state = MTD_ERASE_FAILED;
William Juulcfa460a2007-10-31 13:53:06 +01002247 instr->fail_addr = (page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002248 goto erase_exit;
2249 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002250
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02002259 /* Increment page address and decrement length */
William Juulcfa460a2007-10-31 13:53:06 +01002260 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002261 page += pages_per_block;
2262
2263 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002264 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002265 chipnr++;
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +02002277 }
2278 }
2279 instr->state = MTD_ERASE_DONE;
2280
William Juulcfa460a2007-10-31 13:53:06 +01002281 erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002282
2283 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002284
2285 /* Deselect and wake up anyone waiting on the device */
2286 nand_release_device(mtd);
2287
Scott Woodc45912d2008-10-24 16:20:43 -05002288 /* Do call back function */
2289 if (!ret)
2290 mtd_erase_callback(instr);
2291
William Juulcfa460a2007-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 Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +01002319static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002320{
William Juulcfa460a2007-10-31 13:53:06 +01002321 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002322
Scott Wood3167c532008-06-20 12:38:57 -05002323 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002324
2325 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002326 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002327 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002328 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002329}
2330
Wolfgang Denk932394a2005-08-17 12:55:25 +02002331/**
William Juulcfa460a2007-10-31 13:53:06 +01002332 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk932394a2005-08-17 12:55:25 +02002333 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01002334 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002335 */
William Juulcfa460a2007-10-31 13:53:06 +01002336static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002337{
2338 /* Check for invalid offset */
William Juulcfa460a2007-10-31 13:53:06 +01002339 if (offs > mtd->size)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002340 return -EINVAL;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002341
William Juulcfa460a2007-10-31 13:53:06 +01002342 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002343}
2344
2345/**
William Juulcfa460a2007-10-31 13:53:06 +01002346 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk932394a2005-08-17 12:55:25 +02002347 * @mtd: MTD device structure
2348 * @ofs: offset relative to mtd start
2349 */
William Juulcfa460a2007-10-31 13:53:06 +01002350static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002351{
William Juulcfa460a2007-10-31 13:53:06 +01002352 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002353 int ret;
2354
Wolfgang Denkac7eb8a2005-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 Denk932394a2005-08-17 12:55:25 +02002357 if (ret > 0)
2358 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002359 return ret;
2360 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002361
William Juulcfa460a2007-10-31 13:53:06 +01002362 return chip->block_markbad(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002363}
2364
2365/**
William Juulcfa460a2007-10-31 13:53:06 +01002366 * nand_suspend - [MTD Interface] Suspend the NAND flash
2367 * @mtd: MTD device structure
2368 */
2369static int nand_suspend(struct mtd_info *mtd)
2370{
2371 struct nand_chip *chip = mtd->priv;
2372
2373 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}
2390
2391/*
2392 * Set default functions
2393 */
2394static void nand_set_defaults(struct nand_chip *chip, int busw)
2395{
2396 /* check for proper chip_delay setup, set 20us if not */
2397 if (!chip->chip_delay)
2398 chip->chip_delay = 20;
2399
2400 /* check, if a user supplied command function given */
2401 if (chip->cmdfunc == NULL)
2402 chip->cmdfunc = nand_command;
2403
2404 /* check, if a user supplied wait function given */
2405 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}
2438
2439/*
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 Woodc45912d2008-10-24 16:20:43 -05002448 int tmp_id, tmp_manf;
William Juulcfa460a2007-10-31 13:53:06 +01002449
2450 /* Select the device */
2451 chip->select_chip(mtd, 0);
2452
2453 /* Send the command for reading device ID */
2454 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2455
2456 /* Read manufacturer and device IDs */
2457 *maf_id = chip->read_byte(mtd);
2458 dev_id = chip->read_byte(mtd);
2459
Scott Woodc45912d2008-10-24 16:20:43 -05002460 /* Try again to make sure, as some systems the bus-hold or other
2461 * interface concerns can cause random data which looks like a
2462 * possibly credible NAND flash to appear. If the two results do
2463 * not match, ignore the device completely.
2464 */
2465
2466 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2467
2468 /* Read manufacturer and device IDs */
2469
2470 tmp_manf = chip->read_byte(mtd);
2471 tmp_id = chip->read_byte(mtd);
2472
2473 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2474 printk(KERN_INFO "%s: second ID read did not match "
2475 "%02x,%02x against %02x,%02x\n", __func__,
2476 *maf_id, dev_id, tmp_manf, tmp_id);
2477 return ERR_PTR(-ENODEV);
2478 }
2479
William Juulcfa460a2007-10-31 13:53:06 +01002480 /* Lookup the flash id */
2481 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2482 if (dev_id == nand_flash_ids[i].id) {
2483 type = &nand_flash_ids[i];
2484 break;
2485 }
2486 }
2487
2488 if (!type)
2489 return ERR_PTR(-ENODEV);
2490
2491 if (!mtd->name)
2492 mtd->name = type->name;
2493
2494 chip->chipsize = type->chipsize << 20;
2495
2496 /* Newer devices have all the information in additional id bytes */
2497 if (!type->pagesize) {
2498 int extid;
2499 /* The 3rd id byte holds MLC / multichip data */
2500 chip->cellinfo = chip->read_byte(mtd);
2501 /* The 4th id byte is the important one */
2502 extid = chip->read_byte(mtd);
2503 /* Calc pagesize */
2504 mtd->writesize = 1024 << (extid & 0x3);
2505 extid >>= 2;
2506 /* Calc oobsize */
2507 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2508 extid >>= 2;
2509 /* Calc blocksize. Blocksize is multiples of 64KiB */
2510 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2511 extid >>= 2;
2512 /* Get buswidth information */
2513 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2514
2515 } else {
2516 /*
2517 * Old devices have chip data hardcoded in the device id table
2518 */
2519 mtd->erasesize = type->erasesize;
2520 mtd->writesize = type->pagesize;
2521 mtd->oobsize = mtd->writesize / 32;
2522 busw = type->options & NAND_BUSWIDTH_16;
2523 }
2524
2525 /* Try to identify manufacturer */
2526 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2527 if (nand_manuf_ids[maf_idx].id == *maf_id)
2528 break;
2529 }
2530
2531 /*
2532 * Check, if buswidth is correct. Hardware drivers should set
2533 * chip correct !
2534 */
2535 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2536 printk(KERN_INFO "NAND device: Manufacturer ID:"
2537 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2538 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2539 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2540 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2541 busw ? 16 : 8);
2542 return ERR_PTR(-EINVAL);
2543 }
2544
2545 /* Calculate the address shift from the page size */
2546 chip->page_shift = ffs(mtd->writesize) - 1;
2547 /* Convert chipsize to number of pages per chip -1. */
2548 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2549
2550 chip->bbt_erase_shift = chip->phys_erase_shift =
2551 ffs(mtd->erasesize) - 1;
2552 chip->chip_shift = ffs(chip->chipsize) - 1;
2553
2554 /* Set the bad block position */
2555 chip->badblockpos = mtd->writesize > 512 ?
2556 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2557
2558 /* Get chip options, preserve non chip based options */
2559 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2560 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
2561
2562 /*
2563 * Set chip as a default. Board drivers can override it, if necessary
2564 */
2565 chip->options |= NAND_NO_AUTOINCR;
2566
2567 /* Check if chip is a not a samsung device. Do not clear the
2568 * options for chips which are not having an extended id.
2569 */
2570 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2571 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2572
2573 /* Check for AND chips with 4 page planes */
2574 if (chip->options & NAND_4PAGE_ARRAY)
2575 chip->erase_cmd = multi_erase_cmd;
2576 else
2577 chip->erase_cmd = single_erase_cmd;
2578
2579 /* Do not replace user supplied command function ! */
2580 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2581 chip->cmdfunc = nand_command_lp;
2582
Stefan Roesee52b34d2008-01-10 18:47:33 +01002583 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2584 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2585 nand_manuf_ids[maf_idx].name, type->name);
William Juulcfa460a2007-10-31 13:53:06 +01002586
2587 return type;
2588}
2589
2590/**
2591 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2592 * @mtd: MTD device structure
2593 * @maxchips: Number of chips to scan for
2594 *
2595 * This is the first phase of the normal nand_scan() function. It
2596 * reads the flash ID and sets up MTD fields accordingly.
2597 *
2598 * The mtd->owner field must be set to the module of the caller.
2599 */
2600int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2601{
2602 int i, busw, nand_maf_id;
2603 struct nand_chip *chip = mtd->priv;
2604 struct nand_flash_dev *type;
2605
2606 /* Get buswidth to select the correct functions */
2607 busw = chip->options & NAND_BUSWIDTH_16;
2608 /* Set the default functions */
2609 nand_set_defaults(chip, busw);
2610
2611 /* Read the flash type */
2612 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2613
2614 if (IS_ERR(type)) {
2615 printk(KERN_WARNING "No NAND device found!!!\n");
2616 chip->select_chip(mtd, -1);
2617 return PTR_ERR(type);
2618 }
2619
2620 /* Check for a chip array */
2621 for (i = 1; i < maxchips; i++) {
2622 chip->select_chip(mtd, i);
2623 /* Send the command for reading device ID */
2624 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2625 /* Read manufacturer and device IDs */
2626 if (nand_maf_id != chip->read_byte(mtd) ||
2627 type->id != chip->read_byte(mtd))
2628 break;
2629 }
2630 if (i > 1)
2631 printk(KERN_INFO "%d NAND chips detected\n", i);
2632
2633 /* Store the number of chips and calc total size for mtd */
2634 chip->numchips = i;
2635 mtd->size = i * chip->chipsize;
2636
2637 return 0;
2638}
2639
2640
2641/**
2642 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2643 * @mtd: MTD device structure
2644 * @maxchips: Number of chips to scan for
2645 *
2646 * This is the second phase of the normal nand_scan() function. It
2647 * fills out all the uninitialized function pointers with the defaults
2648 * and scans for a bad block table if appropriate.
2649 */
2650int nand_scan_tail(struct mtd_info *mtd)
2651{
2652 int i;
2653 struct nand_chip *chip = mtd->priv;
2654
2655 if (!(chip->options & NAND_OWN_BUFFERS))
2656 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2657 if (!chip->buffers)
2658 return -ENOMEM;
2659
2660 /* Set the internal oob buffer location, just after the page data */
2661 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2662
2663 /*
2664 * If no default placement scheme is given, select an appropriate one
2665 */
2666 if (!chip->ecc.layout) {
2667 switch (mtd->oobsize) {
2668 case 8:
2669 chip->ecc.layout = &nand_oob_8;
2670 break;
2671 case 16:
2672 chip->ecc.layout = &nand_oob_16;
2673 break;
2674 case 64:
2675 chip->ecc.layout = &nand_oob_64;
2676 break;
2677 case 128:
2678 chip->ecc.layout = &nand_oob_128;
2679 break;
2680 default:
2681 printk(KERN_WARNING "No oob scheme defined for "
2682 "oobsize %d\n", mtd->oobsize);
William Juul5e1dae52007-11-09 13:32:30 +01002683/* BUG(); */
William Juulcfa460a2007-10-31 13:53:06 +01002684 }
2685 }
2686
2687 if (!chip->write_page)
2688 chip->write_page = nand_write_page;
2689
2690 /*
2691 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2692 * selected and we have 256 byte pagesize fallback to software ECC
2693 */
2694 if (!chip->ecc.read_page_raw)
2695 chip->ecc.read_page_raw = nand_read_page_raw;
2696 if (!chip->ecc.write_page_raw)
2697 chip->ecc.write_page_raw = nand_write_page_raw;
2698
2699 switch (chip->ecc.mode) {
2700 case NAND_ECC_HW:
2701 /* Use standard hwecc read page function ? */
2702 if (!chip->ecc.read_page)
2703 chip->ecc.read_page = nand_read_page_hwecc;
2704 if (!chip->ecc.write_page)
2705 chip->ecc.write_page = nand_write_page_hwecc;
2706 if (!chip->ecc.read_oob)
2707 chip->ecc.read_oob = nand_read_oob_std;
2708 if (!chip->ecc.write_oob)
2709 chip->ecc.write_oob = nand_write_oob_std;
2710
2711 case NAND_ECC_HW_SYNDROME:
Scott Wood41ef8c72008-03-18 15:29:14 -05002712 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2713 !chip->ecc.hwctl) &&
2714 (!chip->ecc.read_page ||
2715 chip->ecc.read_page == nand_read_page_hwecc ||
2716 !chip->ecc.write_page ||
2717 chip->ecc.write_page == nand_write_page_hwecc)) {
William Juulcfa460a2007-10-31 13:53:06 +01002718 printk(KERN_WARNING "No ECC functions supplied, "
2719 "Hardware ECC not possible\n");
2720 BUG();
2721 }
2722 /* Use standard syndrome read/write page function ? */
2723 if (!chip->ecc.read_page)
2724 chip->ecc.read_page = nand_read_page_syndrome;
2725 if (!chip->ecc.write_page)
2726 chip->ecc.write_page = nand_write_page_syndrome;
2727 if (!chip->ecc.read_oob)
2728 chip->ecc.read_oob = nand_read_oob_syndrome;
2729 if (!chip->ecc.write_oob)
2730 chip->ecc.write_oob = nand_write_oob_syndrome;
2731
2732 if (mtd->writesize >= chip->ecc.size)
2733 break;
2734 printk(KERN_WARNING "%d byte HW ECC not possible on "
2735 "%d byte page size, fallback to SW ECC\n",
2736 chip->ecc.size, mtd->writesize);
2737 chip->ecc.mode = NAND_ECC_SOFT;
2738
2739 case NAND_ECC_SOFT:
2740 chip->ecc.calculate = nand_calculate_ecc;
2741 chip->ecc.correct = nand_correct_data;
2742 chip->ecc.read_page = nand_read_page_swecc;
Scott Woodc45912d2008-10-24 16:20:43 -05002743 chip->ecc.read_subpage = nand_read_subpage;
William Juulcfa460a2007-10-31 13:53:06 +01002744 chip->ecc.write_page = nand_write_page_swecc;
2745 chip->ecc.read_oob = nand_read_oob_std;
2746 chip->ecc.write_oob = nand_write_oob_std;
2747 chip->ecc.size = 256;
2748 chip->ecc.bytes = 3;
2749 break;
2750
2751 case NAND_ECC_NONE:
2752 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2753 "This is not recommended !!\n");
2754 chip->ecc.read_page = nand_read_page_raw;
2755 chip->ecc.write_page = nand_write_page_raw;
2756 chip->ecc.read_oob = nand_read_oob_std;
2757 chip->ecc.write_oob = nand_write_oob_std;
2758 chip->ecc.size = mtd->writesize;
2759 chip->ecc.bytes = 0;
2760 break;
2761
2762 default:
2763 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2764 chip->ecc.mode);
2765 BUG();
2766 }
2767
2768 /*
2769 * The number of bytes available for a client to place data into
2770 * the out of band area
2771 */
2772 chip->ecc.layout->oobavail = 0;
2773 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2774 chip->ecc.layout->oobavail +=
2775 chip->ecc.layout->oobfree[i].length;
2776 mtd->oobavail = chip->ecc.layout->oobavail;
2777
2778 /*
2779 * Set the number of read / write steps for one page depending on ECC
2780 * mode
2781 */
2782 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2783 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2784 printk(KERN_WARNING "Invalid ecc parameters\n");
2785 BUG();
2786 }
2787 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
2788
2789 /*
2790 * Allow subpage writes up to ecc.steps. Not possible for MLC
2791 * FLASH.
2792 */
2793 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2794 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2795 switch(chip->ecc.steps) {
2796 case 2:
2797 mtd->subpage_sft = 1;
2798 break;
2799 case 4:
2800 case 8:
2801 mtd->subpage_sft = 2;
2802 break;
2803 }
2804 }
2805 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2806
2807 /* Initialize state */
2808 chip->state = FL_READY;
2809
2810 /* De-select the device */
2811 chip->select_chip(mtd, -1);
2812
2813 /* Invalidate the pagebuffer reference */
2814 chip->pagebuf = -1;
2815
2816 /* Fill in remaining MTD driver data */
2817 mtd->type = MTD_NANDFLASH;
2818 mtd->flags = MTD_CAP_NANDFLASH;
2819 mtd->erase = nand_erase;
2820 mtd->point = NULL;
2821 mtd->unpoint = NULL;
2822 mtd->read = nand_read;
2823 mtd->write = nand_write;
2824 mtd->read_oob = nand_read_oob;
2825 mtd->write_oob = nand_write_oob;
2826 mtd->sync = nand_sync;
2827 mtd->lock = NULL;
2828 mtd->unlock = NULL;
2829 mtd->suspend = nand_suspend;
2830 mtd->resume = nand_resume;
2831 mtd->block_isbad = nand_block_isbad;
2832 mtd->block_markbad = nand_block_markbad;
2833
2834 /* propagate ecc.layout to mtd_info */
2835 mtd->ecclayout = chip->ecc.layout;
2836
2837 /* Check, if we should skip the bad block table scan */
2838 if (chip->options & NAND_SKIP_BBTSCAN)
Ilya Yanok13f0fd92008-06-30 15:34:40 +02002839 chip->options |= NAND_BBT_SCANNED;
William Juulcfa460a2007-10-31 13:53:06 +01002840
Ilya Yanok13f0fd92008-06-30 15:34:40 +02002841 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002842}
2843
2844/* module_text_address() isn't exported, and it's mostly a pointless
2845 test if this is a module _anyway_ -- they'd have to try _really_ hard
2846 to call us from in-kernel code if the core NAND support is modular. */
2847#ifdef MODULE
2848#define caller_is_module() (1)
2849#else
2850#define caller_is_module() \
2851 module_text_address((unsigned long)__builtin_return_address(0))
2852#endif
2853
2854/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02002855 * nand_scan - [NAND Interface] Scan for the NAND device
2856 * @mtd: MTD device structure
2857 * @maxchips: Number of chips to scan for
2858 *
William Juulcfa460a2007-10-31 13:53:06 +01002859 * This fills out all the uninitialized function pointers
Wolfgang Denk932394a2005-08-17 12:55:25 +02002860 * with the defaults.
2861 * The flash ID is read and the mtd/chip structures are
William Juulcfa460a2007-10-31 13:53:06 +01002862 * filled with the appropriate values.
2863 * The mtd->owner field must be set to the module of the caller
Wolfgang Denk932394a2005-08-17 12:55:25 +02002864 *
2865 */
William Juulcfa460a2007-10-31 13:53:06 +01002866int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002867{
William Juulcfa460a2007-10-31 13:53:06 +01002868 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002869
William Juulcfa460a2007-10-31 13:53:06 +01002870 /* Many callers got this wrong, so check for it for a while... */
2871 /* XXX U-BOOT XXX */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002872#if 0
William Juulcfa460a2007-10-31 13:53:06 +01002873 if (!mtd->owner && caller_is_module()) {
2874 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2875 BUG();
2876 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002877#endif
William Juul4cbb6512007-11-08 10:39:53 +01002878
William Juulcfa460a2007-10-31 13:53:06 +01002879 ret = nand_scan_ident(mtd, maxchips);
2880 if (!ret)
2881 ret = nand_scan_tail(mtd);
2882 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002883}
2884
2885/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002886 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk932394a2005-08-17 12:55:25 +02002887 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01002888*/
2889void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002890{
William Juulcfa460a2007-10-31 13:53:06 +01002891 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002892
2893#ifdef CONFIG_MTD_PARTITIONS
2894 /* Deregister partitions */
William Juulcfa460a2007-10-31 13:53:06 +01002895 del_mtd_partitions(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002896#endif
2897 /* Deregister the device */
William Juulcfa460a2007-10-31 13:53:06 +01002898 /* XXX U-BOOT XXX */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002899#if 0
William Juulcfa460a2007-10-31 13:53:06 +01002900 del_mtd_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002901#endif
William Juulcfa460a2007-10-31 13:53:06 +01002902
2903 /* Free bad block table memory */
2904 kfree(chip->bbt);
2905 if (!(chip->options & NAND_OWN_BUFFERS))
2906 kfree(chip->buffers);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002907}
2908
William Juulcfa460a2007-10-31 13:53:06 +01002909/* XXX U-BOOT XXX */
2910#if 0
2911EXPORT_SYMBOL_GPL(nand_scan);
2912EXPORT_SYMBOL_GPL(nand_scan_ident);
2913EXPORT_SYMBOL_GPL(nand_scan_tail);
2914EXPORT_SYMBOL_GPL(nand_release);
2915
2916static int __init nand_base_init(void)
2917{
2918 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2919 return 0;
2920}
2921
2922static void __exit nand_base_exit(void)
2923{
2924 led_trigger_unregister_simple(nand_led_trigger);
2925}
2926
2927module_init(nand_base_init);
2928module_exit(nand_base_exit);
2929
2930MODULE_LICENSE("GPL");
2931MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2932MODULE_DESCRIPTION("Generic NAND flash driver code");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002933#endif