blob: 7acb1a0e7409fbf950a06e97f8e58b3a1be38559 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +020010 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020015 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000016 * David Woodhouse for adding multichip support
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020021 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 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.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030027 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 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
David Woodhouse552d9202006-05-14 01:20:46 +010035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020038#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
45#include <linux/mtd/compatmac.h>
46#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50
51#ifdef CONFIG_MTD_PARTITIONS
52#include <linux/mtd/partitions.h>
53#endif
54
55/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020056static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .eccbytes = 3,
58 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020059 .oobfree = {
60 {.offset = 3,
61 .length = 2},
62 {.offset = 6,
63 .length = 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020066static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .eccbytes = 6,
68 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020069 .oobfree = {
70 {.offset = 8,
71 . length = 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020074static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .eccbytes = 24,
76 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010077 40, 41, 42, 43, 44, 45, 46, 47,
78 48, 49, 50, 51, 52, 53, 54, 55,
79 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020080 .oobfree = {
81 {.offset = 2,
82 .length = 38}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020085static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +020086 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020088static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
89 struct mtd_oob_ops *ops);
90
Thomas Gleixnerd470a972006-05-23 23:48:57 +020091/*
Joe Perches8e87d782008-02-03 17:22:34 +020092 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +020093 * compiled away when LED support is disabled.
94 */
95DEFINE_LED_TRIGGER(nand_led_trigger);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/**
98 * nand_release_device - [GENERIC] release chip
99 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000100 *
101 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100103static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200105 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200108 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100109
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200110 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200111 spin_lock(&chip->controller->lock);
112 chip->controller->active = NULL;
113 chip->state = FL_READY;
114 wake_up(&chip->controller->wq);
115 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/**
119 * nand_read_byte - [DEFAULT] read one byte from the chip
120 * @mtd: MTD device structure
121 *
122 * Default read function for 8bit buswith
123 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200124static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200126 struct nand_chip *chip = mtd->priv;
127 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
132 * @mtd: MTD device structure
133 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000134 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * endianess conversion
136 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200137static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200139 struct nand_chip *chip = mtd->priv;
140 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * nand_read_word - [DEFAULT] read one word from the chip
145 * @mtd: MTD device structure
146 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000147 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * endianess conversion
149 */
150static u16 nand_read_word(struct mtd_info *mtd)
151{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200152 struct nand_chip *chip = mtd->priv;
153 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * nand_select_chip - [DEFAULT] control CE line
158 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700159 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
161 * Default select function for 1 chip devices.
162 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200163static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200165 struct nand_chip *chip = mtd->priv;
166
167 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200169 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 break;
171 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
173
174 default:
175 BUG();
176 }
177}
178
179/**
180 * nand_write_buf - [DEFAULT] write buffer to chip
181 * @mtd: MTD device structure
182 * @buf: data buffer
183 * @len: number of bytes to write
184 *
185 * Default write function for 8bit buswith
186 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200187static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200190 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David Woodhousee0c7d762006-05-13 18:07:53 +0100192 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200193 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000197 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * @mtd: MTD device structure
199 * @buf: buffer to store date
200 * @len: number of bytes to read
201 *
202 * Default read function for 8bit buswith
203 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200204static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200207 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David Woodhousee0c7d762006-05-13 18:07:53 +0100209 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200210 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000214 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * @mtd: MTD device structure
216 * @buf: buffer containing the data to compare
217 * @len: number of bytes to compare
218 *
219 * Default verify function for 8bit buswith
220 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200221static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200224 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
David Woodhousee0c7d762006-05-13 18:07:53 +0100226 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200227 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230}
231
232/**
233 * nand_write_buf16 - [DEFAULT] write buffer to chip
234 * @mtd: MTD device structure
235 * @buf: data buffer
236 * @len: number of bytes to write
237 *
238 * Default write function for 16bit buswith
239 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200240static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200243 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 u16 *p = (u16 *) buf;
245 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000246
David Woodhousee0c7d762006-05-13 18:07:53 +0100247 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000253 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * @mtd: MTD device structure
255 * @buf: buffer to store date
256 * @len: number of bytes to read
257 *
258 * Default read function for 16bit buswith
259 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200260static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200263 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 u16 *p = (u16 *) buf;
265 len >>= 1;
266
David Woodhousee0c7d762006-05-13 18:07:53 +0100267 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200268 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000272 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * @mtd: MTD device structure
274 * @buf: buffer containing the data to compare
275 * @len: number of bytes to compare
276 *
277 * Default verify function for 16bit buswith
278 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200279static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200282 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u16 *p = (u16 *) buf;
284 len >>= 1;
285
David Woodhousee0c7d762006-05-13 18:07:53 +0100286 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200287 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -EFAULT;
289
290 return 0;
291}
292
293/**
294 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
295 * @mtd: MTD device structure
296 * @ofs: offset from device start
297 * @getchip: 0, if the chip is already selected
298 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000299 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
301static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
302{
303 int page, chipnr, res = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200304 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u16 bad;
306
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100307 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200310 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200312 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200315 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200318 if (chip->options & NAND_BUSWIDTH_16) {
319 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100320 page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200321 bad = cpu_to_le16(chip->read_word(mtd));
322 if (chip->badblockpos & 0x1)
Vitaly Wool49196f32005-11-02 16:54:46 +0000323 bad >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if ((bad & 0xFF) != 0xff)
325 res = 1;
326 } else {
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100327 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200328 if (chip->read_byte(mtd) != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 res = 1;
330 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000331
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200332 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return res;
336}
337
338/**
339 * nand_default_block_markbad - [DEFAULT] mark a block bad
340 * @mtd: MTD device structure
341 * @ofs: offset from device start
342 *
343 * This is the default implementation, which can be overridden by
344 * a hardware specific driver.
345*/
346static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
347{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200348 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200349 uint8_t buf[2] = { 0, 0 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200350 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400353 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200354 if (chip->bbt)
355 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200358 if (chip->options & NAND_USE_FLASH_BBT)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200359 ret = nand_update_bbt(mtd, ofs);
360 else {
361 /* We write two bytes, so we dont have to mess with 16 bit
362 * access
363 */
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300364 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200365 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200366 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200367 chip->ops.datbuf = NULL;
368 chip->ops.oobbuf = buf;
369 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000370
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200371 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300372 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200373 }
374 if (!ret)
375 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300376
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200377 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000380/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * nand_check_wp - [GENERIC] check if the chip is write protected
382 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000383 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100387static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200389 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200391 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
392 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/**
396 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
397 * @mtd: MTD device structure
398 * @ofs: offset from device start
399 * @getchip: 0, if the chip is already selected
400 * @allowbbt: 1, if its allowed to access the bbt area
401 *
402 * Check, if the block is bad. Either by reading the bad block table or
403 * calling of the scan function.
404 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200405static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
406 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200408 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000409
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200410 if (!chip->bbt)
411 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100414 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000417/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000418 * Wait for the ready pin, after a command
419 * The timeout is catched later.
420 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100421void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000422{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200423 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100424 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000425
Richard Purdie8fe833c2006-03-31 02:31:14 -0800426 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000427 /* wait until command is processed or timeout occures */
428 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200429 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800430 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700431 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000432 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800433 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000434}
David Woodhouse4b648b02006-09-25 17:05:24 +0100435EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/**
438 * nand_command - [DEFAULT] Send command to NAND device
439 * @mtd: MTD device structure
440 * @command: the command to be sent
441 * @column: the column address for this command, -1 if none
442 * @page_addr: the page address for this command, -1 if none
443 *
444 * Send command to NAND device. This function is used for small page
445 * devices (256/512 Bytes per page)
446 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200447static void nand_command(struct mtd_info *mtd, unsigned int command,
448 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200450 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200451 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
454 * Write out the command to the device.
455 */
456 if (command == NAND_CMD_SEQIN) {
457 int readcmd;
458
Joern Engel28318772006-05-22 23:18:05 +0200459 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200461 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 readcmd = NAND_CMD_READOOB;
463 } else if (column < 256) {
464 /* First 256 bytes --> READ0 */
465 readcmd = NAND_CMD_READ0;
466 } else {
467 column -= 256;
468 readcmd = NAND_CMD_READ1;
469 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200470 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200471 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200473 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200475 /*
476 * Address cycle, when necessary
477 */
478 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
479 /* Serially input address */
480 if (column != -1) {
481 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200482 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200483 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200484 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200485 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200487 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200488 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200489 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200490 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200491 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200492 if (chip->chipsize > (32 << 20))
493 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200494 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200495 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000496
497 /*
498 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 case NAND_CMD_PAGEPROG:
504 case NAND_CMD_ERASE1:
505 case NAND_CMD_ERASE2:
506 case NAND_CMD_SEQIN:
507 case NAND_CMD_STATUS:
508 return;
509
510 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200511 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200513 udelay(chip->chip_delay);
514 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200515 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200516 chip->cmd_ctrl(mtd,
517 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200518 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return;
520
David Woodhousee0c7d762006-05-13 18:07:53 +0100521 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000523 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * If we don't have access to the busy pin, we apply the given
525 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100526 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200527 if (!chip->dev_ready) {
528 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Apply this short delay always to ensure that we do wait tWB in
533 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100534 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000535
536 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/**
540 * nand_command_lp - [DEFAULT] Send command to NAND large page device
541 * @mtd: MTD device structure
542 * @command: the command to be sent
543 * @column: the column address for this command, -1 if none
544 * @page_addr: the page address for this command, -1 if none
545 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200546 * Send command to NAND device. This is the version for the new large page
547 * devices We dont have the separate regions as we have in the small page
548 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200550static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
551 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200553 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* Emulate NAND_CMD_READOOB */
556 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200557 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 command = NAND_CMD_READ0;
559 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000560
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200561 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200562 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200563 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200566 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* Serially input address */
569 if (column != -1) {
570 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200571 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200573 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200574 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200575 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200578 chip->cmd_ctrl(mtd, page_addr, ctrl);
579 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200580 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 if (chip->chipsize > (128 << 20))
583 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200584 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200587 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000588
589 /*
590 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000591 * status, sequential in, and deplete1 need no delay
592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 case NAND_CMD_CACHEDPROG:
596 case NAND_CMD_PAGEPROG:
597 case NAND_CMD_ERASE1:
598 case NAND_CMD_ERASE2:
599 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200600 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000602 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return;
604
David Woodhousee0c7d762006-05-13 18:07:53 +0100605 /*
606 * read error status commands require only a short delay
607 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000608 case NAND_CMD_STATUS_ERROR:
609 case NAND_CMD_STATUS_ERROR0:
610 case NAND_CMD_STATUS_ERROR1:
611 case NAND_CMD_STATUS_ERROR2:
612 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200613 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000614 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200617 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200619 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200620 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
621 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
622 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
623 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200624 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return;
626
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200627 case NAND_CMD_RNDOUT:
628 /* No ready / busy check necessary */
629 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
630 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
631 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
632 NAND_NCE | NAND_CTRL_CHANGE);
633 return;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200636 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
637 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
638 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
639 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000640
David Woodhousee0c7d762006-05-13 18:07:53 +0100641 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000643 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * If we don't have access to the busy pin, we apply the given
645 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100646 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200647 if (!chip->dev_ready) {
648 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* Apply this short delay always to ensure that we do wait tWB in
654 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100655 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000656
657 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/**
661 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700662 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000664 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 *
666 * Get the device and lock it for exclusive access
667 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200668static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200669nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200671 spinlock_t *lock = &chip->controller->lock;
672 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100673 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100674 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100675 spin_lock(lock);
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* Hardware controller shared among independend devices */
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200678 /* Hardware controller shared among independend devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200679 if (!chip->controller->active)
680 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200681
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200682 if (chip->controller->active == chip && chip->state == FL_READY) {
683 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100684 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100685 return 0;
686 }
687 if (new_state == FL_PM_SUSPENDED) {
688 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200689 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100690 }
691 set_current_state(TASK_UNINTERRUPTIBLE);
692 add_wait_queue(wq, &wait);
693 spin_unlock(lock);
694 schedule();
695 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto retry;
697}
698
699/**
700 * nand_wait - [DEFAULT] wait until the command is done
701 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700702 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *
704 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000705 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700707 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200708static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710
David Woodhousee0c7d762006-05-13 18:07:53 +0100711 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200712 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100715 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100717 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Richard Purdie8fe833c2006-03-31 02:31:14 -0800719 led_trigger_event(nand_led_trigger, LED_FULL);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* Apply this short delay always to ensure that we do wait tWB in
722 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100723 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200725 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
726 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000727 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200728 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000730 while (time_before(jiffies, timeo)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (chip->dev_ready) {
732 if (chip->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 } else {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200735 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
737 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000738 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800740 led_trigger_event(nand_led_trigger, LED_OFF);
741
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200742 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return status;
744}
745
746/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200747 * nand_read_page_raw - [Intern] read raw page data without ecc
748 * @mtd: mtd info structure
749 * @chip: nand chip info structure
750 * @buf: buffer to store read data
751 */
752static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
753 uint8_t *buf)
754{
755 chip->read_buf(mtd, buf, mtd->writesize);
756 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
757 return 0;
758}
759
760/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300761 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200762 * @mtd: mtd info structure
763 * @chip: nand chip info structure
764 * @buf: buffer to store read data
David A. Marlin068e3c02005-01-24 03:07:46 +0000765 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200766static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
767 uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200769 int i, eccsize = chip->ecc.size;
770 int eccbytes = chip->ecc.bytes;
771 int eccsteps = chip->ecc.steps;
772 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100773 uint8_t *ecc_calc = chip->buffers->ecccalc;
774 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100775 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200776
Thomas Gleixner90424de2007-04-05 11:44:05 +0200777 chip->ecc.read_page_raw(mtd, chip, buf);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200778
779 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
780 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
781
782 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200783 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200784
785 eccsteps = chip->ecc.steps;
786 p = buf;
787
788 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
789 int stat;
790
791 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700792 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200793 mtd->ecc_stats.failed++;
794 else
795 mtd->ecc_stats.corrected += stat;
796 }
797 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100798}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300801 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200802 * @mtd: mtd info structure
803 * @chip: nand chip info structure
804 * @buf: buffer to store read data
805 *
806 * Not for syndrome calculating ecc controllers which need a special oob layout
807 */
808static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
809 uint8_t *buf)
810{
811 int i, eccsize = chip->ecc.size;
812 int eccbytes = chip->ecc.bytes;
813 int eccsteps = chip->ecc.steps;
814 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100815 uint8_t *ecc_calc = chip->buffers->ecccalc;
816 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100817 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200818
819 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
820 chip->ecc.hwctl(mtd, NAND_ECC_READ);
821 chip->read_buf(mtd, p, eccsize);
822 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
823 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200824 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200825
826 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200827 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200828
829 eccsteps = chip->ecc.steps;
830 p = buf;
831
832 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
833 int stat;
834
835 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700836 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200837 mtd->ecc_stats.failed++;
838 else
839 mtd->ecc_stats.corrected += stat;
840 }
841 return 0;
842}
843
844/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300845 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200846 * @mtd: mtd info structure
847 * @chip: nand chip info structure
848 * @buf: buffer to store read data
849 *
850 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200851 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200852 */
853static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
854 uint8_t *buf)
855{
856 int i, eccsize = chip->ecc.size;
857 int eccbytes = chip->ecc.bytes;
858 int eccsteps = chip->ecc.steps;
859 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200860 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200861
862 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
863 int stat;
864
865 chip->ecc.hwctl(mtd, NAND_ECC_READ);
866 chip->read_buf(mtd, p, eccsize);
867
868 if (chip->ecc.prepad) {
869 chip->read_buf(mtd, oob, chip->ecc.prepad);
870 oob += chip->ecc.prepad;
871 }
872
873 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
874 chip->read_buf(mtd, oob, eccbytes);
875 stat = chip->ecc.correct(mtd, p, oob, NULL);
876
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700877 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200878 mtd->ecc_stats.failed++;
879 else
880 mtd->ecc_stats.corrected += stat;
881
882 oob += eccbytes;
883
884 if (chip->ecc.postpad) {
885 chip->read_buf(mtd, oob, chip->ecc.postpad);
886 oob += chip->ecc.postpad;
887 }
888 }
889
890 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +0400891 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200892 if (i)
893 chip->read_buf(mtd, oob, i);
894
895 return 0;
896}
897
898/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200899 * nand_transfer_oob - [Internal] Transfer oob to client buffer
900 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700901 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200902 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +0300903 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200904 */
905static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +0300906 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200907{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200908 switch(ops->mode) {
909
910 case MTD_OOB_PLACE:
911 case MTD_OOB_RAW:
912 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
913 return oob + len;
914
915 case MTD_OOB_AUTO: {
916 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200917 uint32_t boffs = 0, roffs = ops->ooboffs;
918 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200919
920 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200921 /* Read request not from offset 0 ? */
922 if (unlikely(roffs)) {
923 if (roffs >= free->length) {
924 roffs -= free->length;
925 continue;
926 }
927 boffs = free->offset + roffs;
928 bytes = min_t(size_t, len,
929 (free->length - roffs));
930 roffs = 0;
931 } else {
932 bytes = min_t(size_t, len, free->length);
933 boffs = free->offset;
934 }
935 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200936 oob += bytes;
937 }
938 return oob;
939 }
940 default:
941 BUG();
942 }
943 return NULL;
944}
945
946/**
947 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200948 *
David A. Marlin068e3c02005-01-24 03:07:46 +0000949 * @mtd: MTD device structure
950 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -0700951 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +0000952 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200953 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +0000954 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200955static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
956 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +0000957{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200958 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200959 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200960 struct mtd_ecc_stats stats;
961 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
962 int sndcmd = 1;
963 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200964 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +0300965 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200966 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200968 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200970 chipnr = (int)(from >> chip->chip_shift);
971 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200973 realpage = (int)(from >> chip->page_shift);
974 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200976 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200978 buf = ops->datbuf;
979 oob = ops->oobbuf;
980
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200981 while(1) {
982 bytes = min(mtd->writesize - col, readlen);
983 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000984
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200985 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200986 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100987 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200989 if (likely(sndcmd)) {
990 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
991 sndcmd = 0;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200994 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +0100995 if (unlikely(ops->mode == MTD_OOB_RAW))
996 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
997 else
998 ret = chip->ecc.read_page(mtd, chip, bufpoi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200999 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001000 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001001
1002 /* Transfer not aligned data */
1003 if (!aligned) {
1004 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001005 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001007
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001008 buf += bytes;
1009
1010 if (unlikely(oob)) {
1011 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001012 if (ops->mode != MTD_OOB_RAW) {
1013 int toread = min(oobreadlen,
1014 chip->ecc.layout->oobavail);
1015 if (toread) {
1016 oob = nand_transfer_oob(chip,
1017 oob, ops, toread);
1018 oobreadlen -= toread;
1019 }
1020 } else
1021 buf = nand_transfer_oob(chip,
1022 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001023 }
1024
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001025 if (!(chip->options & NAND_NO_READRDY)) {
1026 /*
1027 * Apply delay or wait for ready/busy pin. Do
1028 * this before the AUTOINCR check, so no
1029 * problems arise if a chip which does auto
1030 * increment is marked as NOAUTOINCR by the
1031 * board driver.
1032 */
1033 if (!chip->dev_ready)
1034 udelay(chip->chip_delay);
1035 else
1036 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001038 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001039 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001040 buf += bytes;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001043 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001044
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001045 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /* For subsequent reads align to page boundary. */
1049 col = 0;
1050 /* Increment page address */
1051 realpage++;
1052
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001053 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* Check, if we cross a chip boundary */
1055 if (!page) {
1056 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001057 chip->select_chip(mtd, -1);
1058 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001060
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001061 /* Check, if the chip supports auto page increment
1062 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001063 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001064 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001065 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001068 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001069 if (oob)
1070 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001072 if (ret)
1073 return ret;
1074
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001075 if (mtd->ecc_stats.failed - stats.failed)
1076 return -EBADMSG;
1077
1078 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001079}
1080
1081/**
1082 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1083 * @mtd: MTD device structure
1084 * @from: offset to read from
1085 * @len: number of bytes to read
1086 * @retlen: pointer to variable to store the number of read bytes
1087 * @buf: the databuffer to put data
1088 *
1089 * Get hold of the chip and call nand_do_read
1090 */
1091static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1092 size_t *retlen, uint8_t *buf)
1093{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001094 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001095 int ret;
1096
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001097 /* Do not allow reads past end of device */
1098 if ((from + len) > mtd->size)
1099 return -EINVAL;
1100 if (!len)
1101 return 0;
1102
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001103 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001104
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001105 chip->ops.len = len;
1106 chip->ops.datbuf = buf;
1107 chip->ops.oobbuf = NULL;
1108
1109 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001110
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001111 *retlen = chip->ops.retlen;
1112
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001113 nand_release_device(mtd);
1114
1115 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001119 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1120 * @mtd: mtd info structure
1121 * @chip: nand chip info structure
1122 * @page: page number to read
1123 * @sndcmd: flag whether to issue read command or not
1124 */
1125static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1126 int page, int sndcmd)
1127{
1128 if (sndcmd) {
1129 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1130 sndcmd = 0;
1131 }
1132 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1133 return sndcmd;
1134}
1135
1136/**
1137 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1138 * with syndromes
1139 * @mtd: mtd info structure
1140 * @chip: nand chip info structure
1141 * @page: page number to read
1142 * @sndcmd: flag whether to issue read command or not
1143 */
1144static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1145 int page, int sndcmd)
1146{
1147 uint8_t *buf = chip->oob_poi;
1148 int length = mtd->oobsize;
1149 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1150 int eccsize = chip->ecc.size;
1151 uint8_t *bufpoi = buf;
1152 int i, toread, sndrnd = 0, pos;
1153
1154 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1155 for (i = 0; i < chip->ecc.steps; i++) {
1156 if (sndrnd) {
1157 pos = eccsize + i * (eccsize + chunk);
1158 if (mtd->writesize > 512)
1159 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1160 else
1161 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1162 } else
1163 sndrnd = 1;
1164 toread = min_t(int, length, chunk);
1165 chip->read_buf(mtd, bufpoi, toread);
1166 bufpoi += toread;
1167 length -= toread;
1168 }
1169 if (length > 0)
1170 chip->read_buf(mtd, bufpoi, length);
1171
1172 return 1;
1173}
1174
1175/**
1176 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1177 * @mtd: mtd info structure
1178 * @chip: nand chip info structure
1179 * @page: page number to write
1180 */
1181static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1182 int page)
1183{
1184 int status = 0;
1185 const uint8_t *buf = chip->oob_poi;
1186 int length = mtd->oobsize;
1187
1188 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1189 chip->write_buf(mtd, buf, length);
1190 /* Send command to program the OOB data */
1191 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1192
1193 status = chip->waitfunc(mtd, chip);
1194
Savin Zlobec0d420f92006-06-21 11:51:20 +02001195 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001196}
1197
1198/**
1199 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1200 * with syndrome - only for large page flash !
1201 * @mtd: mtd info structure
1202 * @chip: nand chip info structure
1203 * @page: page number to write
1204 */
1205static int nand_write_oob_syndrome(struct mtd_info *mtd,
1206 struct nand_chip *chip, int page)
1207{
1208 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1209 int eccsize = chip->ecc.size, length = mtd->oobsize;
1210 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1211 const uint8_t *bufpoi = chip->oob_poi;
1212
1213 /*
1214 * data-ecc-data-ecc ... ecc-oob
1215 * or
1216 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1217 */
1218 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1219 pos = steps * (eccsize + chunk);
1220 steps = 0;
1221 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001222 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001223
1224 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1225 for (i = 0; i < steps; i++) {
1226 if (sndcmd) {
1227 if (mtd->writesize <= 512) {
1228 uint32_t fill = 0xFFFFFFFF;
1229
1230 len = eccsize;
1231 while (len > 0) {
1232 int num = min_t(int, len, 4);
1233 chip->write_buf(mtd, (uint8_t *)&fill,
1234 num);
1235 len -= num;
1236 }
1237 } else {
1238 pos = eccsize + i * (eccsize + chunk);
1239 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1240 }
1241 } else
1242 sndcmd = 1;
1243 len = min_t(int, length, chunk);
1244 chip->write_buf(mtd, bufpoi, len);
1245 bufpoi += len;
1246 length -= len;
1247 }
1248 if (length > 0)
1249 chip->write_buf(mtd, bufpoi, length);
1250
1251 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1252 status = chip->waitfunc(mtd, chip);
1253
1254 return status & NAND_STATUS_FAIL ? -EIO : 0;
1255}
1256
1257/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001258 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * @mtd: MTD device structure
1260 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001261 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 *
1263 * NAND read out-of-band data from the spare area
1264 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001265static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1266 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001268 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001269 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001270 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001271 int readlen = ops->ooblen;
1272 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001273 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Andrew Morton7e9a0bb2006-05-30 09:06:41 +01001275 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1276 (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Adrian Hunter03736152007-01-31 17:58:29 +02001278 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001279 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001280 else
1281 len = mtd->oobsize;
1282
1283 if (unlikely(ops->ooboffs >= len)) {
1284 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1285 "Attempt to start read outside oob\n");
1286 return -EINVAL;
1287 }
1288
1289 /* Do not allow reads past end of device */
1290 if (unlikely(from >= mtd->size ||
1291 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1292 (from >> chip->page_shift)) * len)) {
1293 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1294 "Attempt read beyond end of device\n");
1295 return -EINVAL;
1296 }
Vitaly Wool70145682006-11-03 18:20:38 +03001297
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001298 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001299 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001301 /* Shift to get page */
1302 realpage = (int)(from >> chip->page_shift);
1303 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001305 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001306 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001307
1308 len = min(len, readlen);
1309 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001310
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001311 if (!(chip->options & NAND_NO_READRDY)) {
1312 /*
1313 * Apply delay or wait for ready/busy pin. Do this
1314 * before the AUTOINCR check, so no problems arise if a
1315 * chip which does auto increment is marked as
1316 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001317 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001318 if (!chip->dev_ready)
1319 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001320 else
1321 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001323
Vitaly Wool70145682006-11-03 18:20:38 +03001324 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001325 if (!readlen)
1326 break;
1327
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001328 /* Increment page address */
1329 realpage++;
1330
1331 page = realpage & chip->pagemask;
1332 /* Check, if we cross a chip boundary */
1333 if (!page) {
1334 chipnr++;
1335 chip->select_chip(mtd, -1);
1336 chip->select_chip(mtd, chipnr);
1337 }
1338
1339 /* Check, if the chip supports auto page increment
1340 * or if we have hit a block boundary.
1341 */
1342 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1343 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345
Vitaly Wool70145682006-11-03 18:20:38 +03001346 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 0;
1348}
1349
1350/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001351 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001354 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001356 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001358static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1359 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001361 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001362 int ret = -ENOTSUPP;
1363
1364 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001367 if (ops->datbuf && (from + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001368 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001369 "Attempt read beyond end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EINVAL;
1371 }
1372
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001373 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001375 switch(ops->mode) {
1376 case MTD_OOB_PLACE:
1377 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001378 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001379 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001380
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001381 default:
1382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001385 if (!ops->datbuf)
1386 ret = nand_do_read_oob(mtd, from, ops);
1387 else
1388 ret = nand_do_read_ops(mtd, from, ops);
1389
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001390 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001392 return ret;
1393}
1394
1395
1396/**
1397 * nand_write_page_raw - [Intern] raw page write function
1398 * @mtd: mtd info structure
1399 * @chip: nand chip info structure
1400 * @buf: data buffer
1401 */
1402static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1403 const uint8_t *buf)
1404{
1405 chip->write_buf(mtd, buf, mtd->writesize);
1406 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001409/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001410 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001411 * @mtd: mtd info structure
1412 * @chip: nand chip info structure
1413 * @buf: data buffer
1414 */
1415static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1416 const uint8_t *buf)
1417{
1418 int i, eccsize = chip->ecc.size;
1419 int eccbytes = chip->ecc.bytes;
1420 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001421 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001422 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001423 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001424
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001425 /* Software ecc calculation */
1426 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1427 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001428
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001429 for (i = 0; i < chip->ecc.total; i++)
1430 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001431
Thomas Gleixner90424de2007-04-05 11:44:05 +02001432 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001433}
1434
1435/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001436 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001437 * @mtd: mtd info structure
1438 * @chip: nand chip info structure
1439 * @buf: data buffer
1440 */
1441static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1442 const uint8_t *buf)
1443{
1444 int i, eccsize = chip->ecc.size;
1445 int eccbytes = chip->ecc.bytes;
1446 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001447 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001448 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001449 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001450
1451 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1452 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001453 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001454 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1455 }
1456
1457 for (i = 0; i < chip->ecc.total; i++)
1458 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1459
1460 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1461}
1462
1463/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001464 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001465 * @mtd: mtd info structure
1466 * @chip: nand chip info structure
1467 * @buf: data buffer
1468 *
1469 * The hw generator calculates the error syndrome automatically. Therefor
1470 * we need a special oob layout and handling.
1471 */
1472static void nand_write_page_syndrome(struct mtd_info *mtd,
1473 struct nand_chip *chip, const uint8_t *buf)
1474{
1475 int i, eccsize = chip->ecc.size;
1476 int eccbytes = chip->ecc.bytes;
1477 int eccsteps = chip->ecc.steps;
1478 const uint8_t *p = buf;
1479 uint8_t *oob = chip->oob_poi;
1480
1481 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1482
1483 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1484 chip->write_buf(mtd, p, eccsize);
1485
1486 if (chip->ecc.prepad) {
1487 chip->write_buf(mtd, oob, chip->ecc.prepad);
1488 oob += chip->ecc.prepad;
1489 }
1490
1491 chip->ecc.calculate(mtd, p, oob);
1492 chip->write_buf(mtd, oob, eccbytes);
1493 oob += eccbytes;
1494
1495 if (chip->ecc.postpad) {
1496 chip->write_buf(mtd, oob, chip->ecc.postpad);
1497 oob += chip->ecc.postpad;
1498 }
1499 }
1500
1501 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001502 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001503 if (i)
1504 chip->write_buf(mtd, oob, i);
1505}
1506
1507/**
David Woodhouse956e9442006-09-25 17:12:39 +01001508 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001509 * @mtd: MTD device structure
1510 * @chip: NAND chip descriptor
1511 * @buf: the data to write
1512 * @page: page number to write
1513 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02001514 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001515 */
1516static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001517 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001518{
1519 int status;
1520
1521 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1522
David Woodhouse956e9442006-09-25 17:12:39 +01001523 if (unlikely(raw))
1524 chip->ecc.write_page_raw(mtd, chip, buf);
1525 else
1526 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001527
1528 /*
1529 * Cached progamming disabled for now, Not sure if its worth the
1530 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1531 */
1532 cached = 0;
1533
1534 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1535
1536 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001537 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001538 /*
1539 * See if operation failed and additional status checks are
1540 * available
1541 */
1542 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1543 status = chip->errstat(mtd, chip, FL_WRITING, status,
1544 page);
1545
1546 if (status & NAND_STATUS_FAIL)
1547 return -EIO;
1548 } else {
1549 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001550 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001551 }
1552
1553#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1554 /* Send command to read back the data */
1555 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1556
1557 if (chip->verify_buf(mtd, buf, mtd->writesize))
1558 return -EIO;
1559#endif
1560 return 0;
1561}
1562
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001563/**
1564 * nand_fill_oob - [Internal] Transfer client buffer to oob
1565 * @chip: nand chip structure
1566 * @oob: oob data buffer
1567 * @ops: oob ops structure
1568 */
1569static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1570 struct mtd_oob_ops *ops)
1571{
1572 size_t len = ops->ooblen;
1573
1574 switch(ops->mode) {
1575
1576 case MTD_OOB_PLACE:
1577 case MTD_OOB_RAW:
1578 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1579 return oob + len;
1580
1581 case MTD_OOB_AUTO: {
1582 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001583 uint32_t boffs = 0, woffs = ops->ooboffs;
1584 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001585
1586 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001587 /* Write request not from offset 0 ? */
1588 if (unlikely(woffs)) {
1589 if (woffs >= free->length) {
1590 woffs -= free->length;
1591 continue;
1592 }
1593 boffs = free->offset + woffs;
1594 bytes = min_t(size_t, len,
1595 (free->length - woffs));
1596 woffs = 0;
1597 } else {
1598 bytes = min_t(size_t, len, free->length);
1599 boffs = free->offset;
1600 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001601 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001602 oob += bytes;
1603 }
1604 return oob;
1605 }
1606 default:
1607 BUG();
1608 }
1609 return NULL;
1610}
1611
Thomas Gleixner29072b92006-09-28 15:38:36 +02001612#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001613
1614/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001615 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001616 * @mtd: MTD device structure
1617 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001618 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001619 *
1620 * NAND write with ECC
1621 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001622static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1623 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001624{
Thomas Gleixner29072b92006-09-28 15:38:36 +02001625 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001626 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001627 uint32_t writelen = ops->len;
1628 uint8_t *oob = ops->oobbuf;
1629 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001630 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001631
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001632 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001633 if (!writelen)
1634 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001635
1636 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001637 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001638 printk(KERN_NOTICE "nand_write: "
1639 "Attempt to write not page aligned data\n");
1640 return -EINVAL;
1641 }
1642
Thomas Gleixner29072b92006-09-28 15:38:36 +02001643 column = to & (mtd->writesize - 1);
1644 subpage = column || (writelen & (mtd->writesize - 1));
1645
1646 if (subpage && oob)
1647 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001648
Thomas Gleixner6a930962006-06-28 00:11:45 +02001649 chipnr = (int)(to >> chip->chip_shift);
1650 chip->select_chip(mtd, chipnr);
1651
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001652 /* Check, if it is write protected */
1653 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001654 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001655
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001656 realpage = (int)(to >> chip->page_shift);
1657 page = realpage & chip->pagemask;
1658 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1659
1660 /* Invalidate the page cache, when we write to the cached page */
1661 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001662 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001663 chip->pagebuf = -1;
1664
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001665 /* If we're not given explicit OOB data, let it be 0xFF */
1666 if (likely(!oob))
1667 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001668
1669 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02001670 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001671 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001672 uint8_t *wbuf = buf;
1673
1674 /* Partial page write ? */
1675 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1676 cached = 0;
1677 bytes = min_t(int, bytes - column, (int) writelen);
1678 chip->pagebuf = -1;
1679 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1680 memcpy(&chip->buffers->databuf[column], buf, bytes);
1681 wbuf = chip->buffers->databuf;
1682 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001683
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001684 if (unlikely(oob))
1685 oob = nand_fill_oob(chip, oob, ops);
1686
Thomas Gleixner29072b92006-09-28 15:38:36 +02001687 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01001688 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001689 if (ret)
1690 break;
1691
1692 writelen -= bytes;
1693 if (!writelen)
1694 break;
1695
Thomas Gleixner29072b92006-09-28 15:38:36 +02001696 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001697 buf += bytes;
1698 realpage++;
1699
1700 page = realpage & chip->pagemask;
1701 /* Check, if we cross a chip boundary */
1702 if (!page) {
1703 chipnr++;
1704 chip->select_chip(mtd, -1);
1705 chip->select_chip(mtd, chipnr);
1706 }
1707 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001708
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001709 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03001710 if (unlikely(oob))
1711 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001712 return ret;
1713}
1714
1715/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001716 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * @mtd: MTD device structure
1718 * @to: offset to write to
1719 * @len: number of bytes to write
1720 * @retlen: pointer to variable to store the number of written bytes
1721 * @buf: the data to write
1722 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001723 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001725static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001726 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001728 struct nand_chip *chip = mtd->priv;
1729 int ret;
1730
1731 /* Do not allow reads past end of device */
1732 if ((to + len) > mtd->size)
1733 return -EINVAL;
1734 if (!len)
1735 return 0;
1736
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001737 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001738
1739 chip->ops.len = len;
1740 chip->ops.datbuf = (uint8_t *)buf;
1741 chip->ops.oobbuf = NULL;
1742
1743 ret = nand_do_write_ops(mtd, to, &chip->ops);
1744
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001745 *retlen = chip->ops.retlen;
1746
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001747 nand_release_device(mtd);
1748
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001749 return ret;
1750}
1751
1752/**
1753 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1754 * @mtd: MTD device structure
1755 * @to: offset to write to
1756 * @ops: oob operation description structure
1757 *
1758 * NAND write out-of-band
1759 */
1760static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1761 struct mtd_oob_ops *ops)
1762{
Adrian Hunter03736152007-01-31 17:58:29 +02001763 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001764 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001766 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
Vitaly Wool70145682006-11-03 18:20:38 +03001767 (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Adrian Hunter03736152007-01-31 17:58:29 +02001769 if (ops->mode == MTD_OOB_AUTO)
1770 len = chip->ecc.layout->oobavail;
1771 else
1772 len = mtd->oobsize;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02001775 if ((ops->ooboffs + ops->ooblen) > len) {
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001776 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1777 "Attempt to write past end of page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return -EINVAL;
1779 }
1780
Adrian Hunter03736152007-01-31 17:58:29 +02001781 if (unlikely(ops->ooboffs >= len)) {
1782 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1783 "Attempt to start write outside oob\n");
1784 return -EINVAL;
1785 }
1786
1787 /* Do not allow reads past end of device */
1788 if (unlikely(to >= mtd->size ||
1789 ops->ooboffs + ops->ooblen >
1790 ((mtd->size >> chip->page_shift) -
1791 (to >> chip->page_shift)) * len)) {
1792 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1793 "Attempt write beyond end of device\n");
1794 return -EINVAL;
1795 }
1796
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001797 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001798 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001800 /* Shift to get page */
1801 page = (int)(to >> chip->page_shift);
1802
1803 /*
1804 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1805 * of my DiskOnChip 2000 test units) will clear the whole data page too
1806 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1807 * it in the doc2000 driver in August 1999. dwmw2.
1808 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001809 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /* Check, if it is write protected */
1812 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001813 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001816 if (page == chip->pagebuf)
1817 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001819 memset(chip->oob_poi, 0xff, mtd->oobsize);
1820 nand_fill_oob(chip, ops->oobbuf, ops);
1821 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1822 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001823
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001824 if (status)
1825 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Vitaly Wool70145682006-11-03 18:20:38 +03001827 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001829 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001832/**
1833 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1834 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001835 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001836 * @ops: oob operation description structure
1837 */
1838static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1839 struct mtd_oob_ops *ops)
1840{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001841 struct nand_chip *chip = mtd->priv;
1842 int ret = -ENOTSUPP;
1843
1844 ops->retlen = 0;
1845
1846 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001847 if (ops->datbuf && (to + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001848 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1849 "Attempt read beyond end of device\n");
1850 return -EINVAL;
1851 }
1852
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001853 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001854
1855 switch(ops->mode) {
1856 case MTD_OOB_PLACE:
1857 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001858 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001859 break;
1860
1861 default:
1862 goto out;
1863 }
1864
1865 if (!ops->datbuf)
1866 ret = nand_do_write_oob(mtd, to, ops);
1867 else
1868 ret = nand_do_write_ops(mtd, to, ops);
1869
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001870 out:
1871 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return ret;
1873}
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1877 * @mtd: MTD device structure
1878 * @page: the page address of the block which will be erased
1879 *
1880 * Standard erase command for NAND chips
1881 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001882static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001884 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001886 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1887 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
1890/**
1891 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1892 * @mtd: MTD device structure
1893 * @page: the page address of the block which will be erased
1894 *
1895 * AND multi block erase command function
1896 * Erase 4 consecutive blocks
1897 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001898static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001900 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001902 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1903 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1904 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1905 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1906 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
1909/**
1910 * nand_erase - [MTD Interface] erase block(s)
1911 * @mtd: MTD device structure
1912 * @instr: erase instruction
1913 *
1914 * Erase one ore more blocks
1915 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001916static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
David Woodhousee0c7d762006-05-13 18:07:53 +01001918 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001920
David A. Marlin30f464b2005-01-17 18:35:25 +00001921#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001923 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 * @mtd: MTD device structure
1925 * @instr: erase instruction
1926 * @allowbbt: allow erasing the bbt area
1927 *
1928 * Erase one ore more blocks
1929 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001930int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
1931 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 int page, len, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001934 struct nand_chip *chip = mtd->priv;
1935 int rewrite_bbt[NAND_MAX_CHIPS]={0};
1936 unsigned int bbt_masked_page = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001938 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
1939 (unsigned int)instr->addr, (unsigned int)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001942 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001943 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return -EINVAL;
1945 }
1946
1947 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001948 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
1949 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1950 "Length not block aligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return -EINVAL;
1952 }
1953
1954 /* Do not allow erase past end of device */
1955 if ((instr->len + instr->addr) > mtd->size) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001956 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1957 "Erase past end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return -EINVAL;
1959 }
1960
1961 instr->fail_addr = 0xffffffff;
1962
1963 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001964 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001967 page = (int)(instr->addr >> chip->page_shift);
1968 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001971 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001974 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* Check, if it is write protected */
1977 if (nand_check_wp(mtd)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001978 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1979 "Device is write protected!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 instr->state = MTD_ERASE_FAILED;
1981 goto erase_exit;
1982 }
1983
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001984 /*
1985 * If BBT requires refresh, set the BBT page mask to see if the BBT
1986 * should be rewritten. Otherwise the mask is set to 0xffffffff which
1987 * can not be matched. This is also done when the bbt is actually
1988 * erased to avoid recusrsive updates
1989 */
1990 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
1991 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* Loop through the pages */
1994 len = instr->len;
1995
1996 instr->state = MTD_ERASING;
1997
1998 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001999 /*
2000 * heck if we have a bad block, we do not erase bad blocks !
2001 */
2002 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2003 chip->page_shift, 0, allowbbt)) {
2004 printk(KERN_WARNING "nand_erase: attempt to erase a "
2005 "bad block at page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 instr->state = MTD_ERASE_FAILED;
2007 goto erase_exit;
2008 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002009
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002010 /*
2011 * Invalidate the page cache, if we erase the block which
2012 * contains the current cached page
2013 */
2014 if (page <= chip->pagebuf && chip->pagebuf <
2015 (page + pages_per_block))
2016 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002018 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002019
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002020 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002022 /*
2023 * See if operation failed and additional status checks are
2024 * available
2025 */
2026 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2027 status = chip->errstat(mtd, chip, FL_ERASING,
2028 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002031 if (status & NAND_STATUS_FAIL) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002032 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2033 "Failed erase, page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 instr->state = MTD_ERASE_FAILED;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002035 instr->fail_addr = (page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 goto erase_exit;
2037 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002038
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002039 /*
2040 * If BBT requires refresh, set the BBT rewrite flag to the
2041 * page being erased
2042 */
2043 if (bbt_masked_page != 0xffffffff &&
2044 (page & BBT_PAGE_MASK) == bbt_masked_page)
2045 rewrite_bbt[chipnr] = (page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002048 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 page += pages_per_block;
2050
2051 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002052 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002054 chip->select_chip(mtd, -1);
2055 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002056
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002057 /*
2058 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2059 * page mask to see if this BBT should be rewritten
2060 */
2061 if (bbt_masked_page != 0xffffffff &&
2062 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2063 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2064 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066 }
2067 instr->state = MTD_ERASE_DONE;
2068
David Woodhousee0c7d762006-05-13 18:07:53 +01002069 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* Deselect and wake up anyone waiting on the device */
2074 nand_release_device(mtd);
2075
David Woodhouse49defc02007-10-06 15:01:59 -04002076 /* Do call back function */
2077 if (!ret)
2078 mtd_erase_callback(instr);
2079
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002080 /*
2081 * If BBT requires refresh and erase was successful, rewrite any
2082 * selected bad block tables
2083 */
2084 if (bbt_masked_page == 0xffffffff || ret)
2085 return ret;
2086
2087 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2088 if (!rewrite_bbt[chipnr])
2089 continue;
2090 /* update the BBT for chip */
2091 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2092 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2093 chip->bbt_td->pages[chipnr]);
2094 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002095 }
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 /* Return more or less happy */
2098 return ret;
2099}
2100
2101/**
2102 * nand_sync - [MTD Interface] sync
2103 * @mtd: MTD device structure
2104 *
2105 * Sync is actually a wait for chip ready function
2106 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002107static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002109 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
David Woodhousee0c7d762006-05-13 18:07:53 +01002111 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002114 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002116 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002120 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002122 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002124static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002127 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002129
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002130 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002134 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 * @mtd: MTD device structure
2136 * @ofs: offset relative to mtd start
2137 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002138static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002140 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 int ret;
2142
David Woodhousee0c7d762006-05-13 18:07:53 +01002143 if ((ret = nand_block_isbad(mtd, ofs))) {
2144 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (ret > 0)
2146 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002147 return ret;
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002150 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002154 * nand_suspend - [MTD Interface] Suspend the NAND flash
2155 * @mtd: MTD device structure
2156 */
2157static int nand_suspend(struct mtd_info *mtd)
2158{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002159 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002160
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002161 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002162}
2163
2164/**
2165 * nand_resume - [MTD Interface] Resume the NAND flash
2166 * @mtd: MTD device structure
2167 */
2168static void nand_resume(struct mtd_info *mtd)
2169{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002170 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002171
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002172 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002173 nand_release_device(mtd);
2174 else
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02002175 printk(KERN_ERR "nand_resume() called for a chip which is not "
2176 "in suspended state\n");
Vitaly Wool962034f2005-09-15 14:58:53 +01002177}
2178
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002179/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002180 * Set default functions
2181 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002182static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002185 if (!chip->chip_delay)
2186 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002189 if (chip->cmdfunc == NULL)
2190 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002193 if (chip->waitfunc == NULL)
2194 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002196 if (!chip->select_chip)
2197 chip->select_chip = nand_select_chip;
2198 if (!chip->read_byte)
2199 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2200 if (!chip->read_word)
2201 chip->read_word = nand_read_word;
2202 if (!chip->block_bad)
2203 chip->block_bad = nand_block_bad;
2204 if (!chip->block_markbad)
2205 chip->block_markbad = nand_default_block_markbad;
2206 if (!chip->write_buf)
2207 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2208 if (!chip->read_buf)
2209 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2210 if (!chip->verify_buf)
2211 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2212 if (!chip->scan_bbt)
2213 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002214
2215 if (!chip->controller) {
2216 chip->controller = &chip->hwcontrol;
2217 spin_lock_init(&chip->controller->lock);
2218 init_waitqueue_head(&chip->controller->wq);
2219 }
2220
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002221}
2222
2223/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002224 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002225 */
2226static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002227 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002228 int busw, int *maf_id)
2229{
2230 struct nand_flash_dev *type = NULL;
2231 int i, dev_id, maf_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002234 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002237 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002240 *maf_id = chip->read_byte(mtd);
2241 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002243 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002245 if (dev_id == nand_flash_ids[i].id) {
2246 type = &nand_flash_ids[i];
2247 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002251 if (!type)
2252 return ERR_PTR(-ENODEV);
2253
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002254 if (!mtd->name)
2255 mtd->name = type->name;
2256
2257 chip->chipsize = type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002258
2259 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002260 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002261 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002262 /* The 3rd id byte holds MLC / multichip data */
2263 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002264 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002265 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002266 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002267 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002268 extid >>= 2;
2269 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002270 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002271 extid >>= 2;
2272 /* Calc blocksize. Blocksize is multiples of 64KiB */
2273 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2274 extid >>= 2;
2275 /* Get buswidth information */
2276 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2277
2278 } else {
2279 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002280 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002281 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002282 mtd->erasesize = type->erasesize;
2283 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002284 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002285 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002286 }
2287
2288 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002289 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002290 if (nand_manuf_ids[maf_idx].id == *maf_id)
2291 break;
2292 }
2293
2294 /*
2295 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002296 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002297 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002298 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002299 printk(KERN_INFO "NAND device: Manufacturer ID:"
2300 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2301 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2302 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002303 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002304 busw ? 16 : 8);
2305 return ERR_PTR(-EINVAL);
2306 }
2307
2308 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002309 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002310 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002311 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002312
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002313 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002314 ffs(mtd->erasesize) - 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002315 chip->chip_shift = ffs(chip->chipsize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002316
2317 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002318 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002319 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2320
2321 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002322 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002323 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002324
2325 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002326 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002327 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002328 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002329
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002330 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002331 * options for chips which are not having an extended id.
2332 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002333 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002334 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002335
2336 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002337 if (chip->options & NAND_4PAGE_ARRAY)
2338 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002339 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002340 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002341
2342 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002343 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2344 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002345
2346 printk(KERN_INFO "NAND device: Manufacturer ID:"
2347 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2348 nand_manuf_ids[maf_idx].name, type->name);
2349
2350 return type;
2351}
2352
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002353/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002354 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2355 * @mtd: MTD device structure
2356 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002357 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002358 * This is the first phase of the normal nand_scan() function. It
2359 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002360 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002361 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002362 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002363int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002364{
2365 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002366 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002367 struct nand_flash_dev *type;
2368
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002369 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002370 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002371 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002372 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002373
2374 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002375 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002376
2377 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002378 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002379 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002380 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002383 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002384 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002385 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002387 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002389 if (nand_maf_id != chip->read_byte(mtd) ||
2390 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 break;
2392 }
2393 if (i > 1)
2394 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002397 chip->numchips = i;
2398 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
David Woodhouse3b85c322006-09-25 17:06:53 +01002400 return 0;
2401}
2402
2403
2404/**
2405 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2406 * @mtd: MTD device structure
2407 * @maxchips: Number of chips to scan for
2408 *
2409 * This is the second phase of the normal nand_scan() function. It
2410 * fills out all the uninitialized function pointers with the defaults
2411 * and scans for a bad block table if appropriate.
2412 */
2413int nand_scan_tail(struct mtd_info *mtd)
2414{
2415 int i;
2416 struct nand_chip *chip = mtd->priv;
2417
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002418 if (!(chip->options & NAND_OWN_BUFFERS))
2419 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2420 if (!chip->buffers)
2421 return -ENOMEM;
2422
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002423 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002424 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002425
2426 /*
2427 * If no default placement scheme is given, select an appropriate one
2428 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002429 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002430 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002432 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 break;
2434 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002435 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 break;
2437 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002438 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
2440 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002441 printk(KERN_WARNING "No oob scheme defined for "
2442 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 BUG();
2444 }
2445 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002446
David Woodhouse956e9442006-09-25 17:12:39 +01002447 if (!chip->write_page)
2448 chip->write_page = nand_write_page;
2449
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002450 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002451 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2452 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002453 */
David Woodhouse956e9442006-09-25 17:12:39 +01002454 if (!chip->ecc.read_page_raw)
2455 chip->ecc.read_page_raw = nand_read_page_raw;
2456 if (!chip->ecc.write_page_raw)
2457 chip->ecc.write_page_raw = nand_write_page_raw;
2458
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002459 switch (chip->ecc.mode) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002460 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002461 /* Use standard hwecc read page function ? */
2462 if (!chip->ecc.read_page)
2463 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002464 if (!chip->ecc.write_page)
2465 chip->ecc.write_page = nand_write_page_hwecc;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002466 if (!chip->ecc.read_oob)
2467 chip->ecc.read_oob = nand_read_oob_std;
2468 if (!chip->ecc.write_oob)
2469 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002470
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002471 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06002472 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2473 !chip->ecc.hwctl) &&
2474 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002475 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06002476 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002477 chip->ecc.write_page == nand_write_page_hwecc)) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002478 printk(KERN_WARNING "No ECC functions supplied, "
2479 "Hardware ECC not possible\n");
2480 BUG();
2481 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002482 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002483 if (!chip->ecc.read_page)
2484 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002485 if (!chip->ecc.write_page)
2486 chip->ecc.write_page = nand_write_page_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002487 if (!chip->ecc.read_oob)
2488 chip->ecc.read_oob = nand_read_oob_syndrome;
2489 if (!chip->ecc.write_oob)
2490 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002491
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002492 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002493 break;
2494 printk(KERN_WARNING "%d byte HW ECC not possible on "
2495 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002496 chip->ecc.size, mtd->writesize);
2497 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002499 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002500 chip->ecc.calculate = nand_calculate_ecc;
2501 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002502 chip->ecc.read_page = nand_read_page_swecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002503 chip->ecc.write_page = nand_write_page_swecc;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002504 chip->ecc.read_oob = nand_read_oob_std;
2505 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002506 chip->ecc.size = 256;
2507 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002509
2510 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002511 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2512 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002513 chip->ecc.read_page = nand_read_page_raw;
2514 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002515 chip->ecc.read_oob = nand_read_oob_std;
2516 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002517 chip->ecc.size = mtd->writesize;
2518 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002522 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002523 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002524 BUG();
2525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002527 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002528 * The number of bytes available for a client to place data into
2529 * the out of band area
2530 */
2531 chip->ecc.layout->oobavail = 0;
2532 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2533 chip->ecc.layout->oobavail +=
2534 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002535 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002536
2537 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002538 * Set the number of read / write steps for one page depending on ECC
2539 * mode
2540 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002541 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2542 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002543 printk(KERN_WARNING "Invalid ecc parameters\n");
2544 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002546 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002547
Thomas Gleixner29072b92006-09-28 15:38:36 +02002548 /*
2549 * Allow subpage writes up to ecc.steps. Not possible for MLC
2550 * FLASH.
2551 */
2552 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2553 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2554 switch(chip->ecc.steps) {
2555 case 2:
2556 mtd->subpage_sft = 1;
2557 break;
2558 case 4:
2559 case 8:
2560 mtd->subpage_sft = 2;
2561 break;
2562 }
2563 }
2564 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2565
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002566 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002567 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002570 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002573 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 /* Fill in remaining MTD driver data */
2576 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002577 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 mtd->erase = nand_erase;
2579 mtd->point = NULL;
2580 mtd->unpoint = NULL;
2581 mtd->read = nand_read;
2582 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 mtd->read_oob = nand_read_oob;
2584 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 mtd->sync = nand_sync;
2586 mtd->lock = NULL;
2587 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002588 mtd->suspend = nand_suspend;
2589 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 mtd->block_isbad = nand_block_isbad;
2591 mtd->block_markbad = nand_block_markbad;
2592
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002593 /* propagate ecc.layout to mtd_info */
2594 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002596 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002597 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002601 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
David Woodhouse3b85c322006-09-25 17:06:53 +01002604/* module_text_address() isn't exported, and it's mostly a pointless
2605 test if this is a module _anyway_ -- they'd have to try _really_ hard
2606 to call us from in-kernel code if the core NAND support is modular. */
2607#ifdef MODULE
2608#define caller_is_module() (1)
2609#else
2610#define caller_is_module() \
2611 module_text_address((unsigned long)__builtin_return_address(0))
2612#endif
2613
2614/**
2615 * nand_scan - [NAND Interface] Scan for the NAND device
2616 * @mtd: MTD device structure
2617 * @maxchips: Number of chips to scan for
2618 *
2619 * This fills out all the uninitialized function pointers
2620 * with the defaults.
2621 * The flash ID is read and the mtd/chip structures are
2622 * filled with the appropriate values.
2623 * The mtd->owner field must be set to the module of the caller
2624 *
2625 */
2626int nand_scan(struct mtd_info *mtd, int maxchips)
2627{
2628 int ret;
2629
2630 /* Many callers got this wrong, so check for it for a while... */
2631 if (!mtd->owner && caller_is_module()) {
2632 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2633 BUG();
2634 }
2635
2636 ret = nand_scan_ident(mtd, maxchips);
2637 if (!ret)
2638 ret = nand_scan_tail(mtd);
2639 return ret;
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002643 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 * @mtd: MTD device structure
2645*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002646void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002648 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650#ifdef CONFIG_MTD_PARTITIONS
2651 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002652 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653#endif
2654 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002655 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Jesper Juhlfa671642005-11-07 01:01:27 -08002657 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002658 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002659 if (!(chip->options & NAND_OWN_BUFFERS))
2660 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
David Woodhousee0c7d762006-05-13 18:07:53 +01002663EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01002664EXPORT_SYMBOL_GPL(nand_scan_ident);
2665EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01002666EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002667
2668static int __init nand_base_init(void)
2669{
2670 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2671 return 0;
2672}
2673
2674static void __exit nand_base_exit(void)
2675{
2676 led_trigger_unregister_simple(nand_led_trigger);
2677}
2678
2679module_init(nand_base_init);
2680module_exit(nand_base_exit);
2681
David Woodhousee0c7d762006-05-13 18:07:53 +01002682MODULE_LICENSE("GPL");
2683MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2684MODULE_DESCRIPTION("Generic NAND flash driver code");