blob: 7e68203fe1ba1c46c779d55a5ae2c9151c5659bf [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
10 * http://www.linux-mtd.infradead.org/tech/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.
27 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License version 2 as
30 * published by the Free Software Foundation.
31 *
32 */
33
David Woodhouse552d9202006-05-14 01:20:46 +010034#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/delay.h>
36#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020037#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sched.h>
39#include <linux/slab.h>
40#include <linux/types.h>
41#include <linux/mtd/mtd.h>
42#include <linux/mtd/nand.h>
43#include <linux/mtd/nand_ecc.h>
44#include <linux/mtd/compatmac.h>
45#include <linux/interrupt.h>
46#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080047#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/io.h>
49
50#ifdef CONFIG_MTD_PARTITIONS
51#include <linux/mtd/partitions.h>
52#endif
53
54/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020055static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .eccbytes = 3,
57 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020058 .oobfree = {
59 {.offset = 3,
60 .length = 2},
61 {.offset = 6,
62 .length = 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020065static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .eccbytes = 6,
67 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020068 .oobfree = {
69 {.offset = 8,
70 . length = 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020073static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .eccbytes = 24,
75 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010076 40, 41, 42, 43, 44, 45, 46, 47,
77 48, 49, 50, 51, 52, 53, 54, 55,
78 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020079 .oobfree = {
80 {.offset = 2,
81 .length = 38}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020084static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +020085 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020087static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
88 struct mtd_oob_ops *ops);
89
Thomas Gleixnerd470a972006-05-23 23:48:57 +020090/*
91 * For devices which display every fart in the system on a seperate LED. Is
92 * compiled away when LED support is disabled.
93 */
94DEFINE_LED_TRIGGER(nand_led_trigger);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/**
97 * nand_release_device - [GENERIC] release chip
98 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000099 *
100 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100102static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200104 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200107 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100108
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200109 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200110 spin_lock(&chip->controller->lock);
111 chip->controller->active = NULL;
112 chip->state = FL_READY;
113 wake_up(&chip->controller->wq);
114 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/**
118 * nand_read_byte - [DEFAULT] read one byte from the chip
119 * @mtd: MTD device structure
120 *
121 * Default read function for 8bit buswith
122 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200123static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200125 struct nand_chip *chip = mtd->priv;
126 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
131 * @mtd: MTD device structure
132 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000133 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * endianess conversion
135 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200136static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200138 struct nand_chip *chip = mtd->priv;
139 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * nand_read_word - [DEFAULT] read one word from the chip
144 * @mtd: MTD device structure
145 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000146 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * endianess conversion
148 */
149static u16 nand_read_word(struct mtd_info *mtd)
150{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200151 struct nand_chip *chip = mtd->priv;
152 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * nand_select_chip - [DEFAULT] control CE line
157 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700158 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * Default select function for 1 chip devices.
161 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200162static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200164 struct nand_chip *chip = mtd->priv;
165
166 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200168 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
172
173 default:
174 BUG();
175 }
176}
177
178/**
179 * nand_write_buf - [DEFAULT] write buffer to chip
180 * @mtd: MTD device structure
181 * @buf: data buffer
182 * @len: number of bytes to write
183 *
184 * Default write function for 8bit buswith
185 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200186static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200189 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David Woodhousee0c7d762006-05-13 18:07:53 +0100191 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200192 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000196 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * @mtd: MTD device structure
198 * @buf: buffer to store date
199 * @len: number of bytes to read
200 *
201 * Default read function for 8bit buswith
202 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200203static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200206 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
David Woodhousee0c7d762006-05-13 18:07:53 +0100208 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200209 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000213 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * @mtd: MTD device structure
215 * @buf: buffer containing the data to compare
216 * @len: number of bytes to compare
217 *
218 * Default verify function for 8bit buswith
219 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200220static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200223 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
David Woodhousee0c7d762006-05-13 18:07:53 +0100225 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200226 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
229}
230
231/**
232 * nand_write_buf16 - [DEFAULT] write buffer to chip
233 * @mtd: MTD device structure
234 * @buf: data buffer
235 * @len: number of bytes to write
236 *
237 * Default write function for 16bit buswith
238 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200239static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200242 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 u16 *p = (u16 *) buf;
244 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000245
David Woodhousee0c7d762006-05-13 18:07:53 +0100246 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200247 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000252 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * @mtd: MTD device structure
254 * @buf: buffer to store date
255 * @len: number of bytes to read
256 *
257 * Default read function for 16bit buswith
258 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200259static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 u16 *p = (u16 *) buf;
264 len >>= 1;
265
David Woodhousee0c7d762006-05-13 18:07:53 +0100266 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200267 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000271 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * @mtd: MTD device structure
273 * @buf: buffer containing the data to compare
274 * @len: number of bytes to compare
275 *
276 * Default verify function for 16bit buswith
277 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200278static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200281 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 u16 *p = (u16 *) buf;
283 len >>= 1;
284
David Woodhousee0c7d762006-05-13 18:07:53 +0100285 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200286 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EFAULT;
288
289 return 0;
290}
291
292/**
293 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
294 * @mtd: MTD device structure
295 * @ofs: offset from device start
296 * @getchip: 0, if the chip is already selected
297 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000298 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
300static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
301{
302 int page, chipnr, res = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200303 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 u16 bad;
305
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100306 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200309 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200311 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200314 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200317 if (chip->options & NAND_BUSWIDTH_16) {
318 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100319 page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200320 bad = cpu_to_le16(chip->read_word(mtd));
321 if (chip->badblockpos & 0x1)
Vitaly Wool49196f32005-11-02 16:54:46 +0000322 bad >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if ((bad & 0xFF) != 0xff)
324 res = 1;
325 } else {
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100326 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200327 if (chip->read_byte(mtd) != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 res = 1;
329 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000330
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200331 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return res;
335}
336
337/**
338 * nand_default_block_markbad - [DEFAULT] mark a block bad
339 * @mtd: MTD device structure
340 * @ofs: offset from device start
341 *
342 * This is the default implementation, which can be overridden by
343 * a hardware specific driver.
344*/
345static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
346{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200347 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200348 uint8_t buf[2] = { 0, 0 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200349 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400352 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200353 if (chip->bbt)
354 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200357 if (chip->options & NAND_USE_FLASH_BBT)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200358 ret = nand_update_bbt(mtd, ofs);
359 else {
360 /* We write two bytes, so we dont have to mess with 16 bit
361 * access
362 */
363 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200364 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200365 chip->ops.datbuf = NULL;
366 chip->ops.oobbuf = buf;
367 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000368
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200369 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
370 }
371 if (!ret)
372 mtd->ecc_stats.badblocks++;
373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000376/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * nand_check_wp - [GENERIC] check if the chip is write protected
378 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000381 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100383static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200385 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200387 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
388 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/**
392 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
393 * @mtd: MTD device structure
394 * @ofs: offset from device start
395 * @getchip: 0, if the chip is already selected
396 * @allowbbt: 1, if its allowed to access the bbt area
397 *
398 * Check, if the block is bad. Either by reading the bad block table or
399 * calling of the scan function.
400 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200401static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
402 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200404 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000405
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200406 if (!chip->bbt)
407 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100410 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000413/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000414 * Wait for the ready pin, after a command
415 * The timeout is catched later.
416 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100417void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000418{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200419 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100420 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000421
Richard Purdie8fe833c2006-03-31 02:31:14 -0800422 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000423 /* wait until command is processed or timeout occures */
424 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200425 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800426 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700427 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000428 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800429 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000430}
David Woodhouse4b648b02006-09-25 17:05:24 +0100431EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/**
434 * nand_command - [DEFAULT] Send command to NAND device
435 * @mtd: MTD device structure
436 * @command: the command to be sent
437 * @column: the column address for this command, -1 if none
438 * @page_addr: the page address for this command, -1 if none
439 *
440 * Send command to NAND device. This function is used for small page
441 * devices (256/512 Bytes per page)
442 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200443static void nand_command(struct mtd_info *mtd, unsigned int command,
444 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200446 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200447 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * Write out the command to the device.
451 */
452 if (command == NAND_CMD_SEQIN) {
453 int readcmd;
454
Joern Engel28318772006-05-22 23:18:05 +0200455 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200457 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 readcmd = NAND_CMD_READOOB;
459 } else if (column < 256) {
460 /* First 256 bytes --> READ0 */
461 readcmd = NAND_CMD_READ0;
462 } else {
463 column -= 256;
464 readcmd = NAND_CMD_READ1;
465 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200466 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200467 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200469 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200471 /*
472 * Address cycle, when necessary
473 */
474 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
475 /* Serially input address */
476 if (column != -1) {
477 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200478 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200479 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200480 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200481 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200483 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200484 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200485 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200486 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200487 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200488 if (chip->chipsize > (32 << 20))
489 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200490 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200491 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000492
493 /*
494 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case NAND_CMD_PAGEPROG:
500 case NAND_CMD_ERASE1:
501 case NAND_CMD_ERASE2:
502 case NAND_CMD_SEQIN:
503 case NAND_CMD_STATUS:
504 return;
505
506 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200507 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200509 udelay(chip->chip_delay);
510 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200511 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200512 chip->cmd_ctrl(mtd,
513 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200514 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
516
David Woodhousee0c7d762006-05-13 18:07:53 +0100517 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000519 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * If we don't have access to the busy pin, we apply the given
521 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100522 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200523 if (!chip->dev_ready) {
524 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* Apply this short delay always to ensure that we do wait tWB in
529 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100530 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000531
532 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/**
536 * nand_command_lp - [DEFAULT] Send command to NAND large page device
537 * @mtd: MTD device structure
538 * @command: the command to be sent
539 * @column: the column address for this command, -1 if none
540 * @page_addr: the page address for this command, -1 if none
541 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200542 * Send command to NAND device. This is the version for the new large page
543 * devices We dont have the separate regions as we have in the small page
544 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200546static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
547 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200549 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* Emulate NAND_CMD_READOOB */
552 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200553 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 command = NAND_CMD_READ0;
555 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000556
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200557 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200558 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200559 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200562 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Serially input address */
565 if (column != -1) {
566 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200567 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200569 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200570 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200571 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200574 chip->cmd_ctrl(mtd, page_addr, ctrl);
575 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200576 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200578 if (chip->chipsize > (128 << 20))
579 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200580 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200583 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584
585 /*
586 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000587 * status, sequential in, and deplete1 need no delay
588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 case NAND_CMD_CACHEDPROG:
592 case NAND_CMD_PAGEPROG:
593 case NAND_CMD_ERASE1:
594 case NAND_CMD_ERASE2:
595 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200596 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000598 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return;
600
David Woodhousee0c7d762006-05-13 18:07:53 +0100601 /*
602 * read error status commands require only a short delay
603 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000604 case NAND_CMD_STATUS_ERROR:
605 case NAND_CMD_STATUS_ERROR0:
606 case NAND_CMD_STATUS_ERROR1:
607 case NAND_CMD_STATUS_ERROR2:
608 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200613 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200615 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200616 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
617 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
618 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
619 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200620 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return;
622
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200623 case NAND_CMD_RNDOUT:
624 /* No ready / busy check necessary */
625 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
626 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
627 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
628 NAND_NCE | NAND_CTRL_CHANGE);
629 return;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200632 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
633 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
634 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
635 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000636
David Woodhousee0c7d762006-05-13 18:07:53 +0100637 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000639 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * If we don't have access to the busy pin, we apply the given
641 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100642 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200643 if (!chip->dev_ready) {
644 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Apply this short delay always to ensure that we do wait tWB in
650 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100651 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000652
653 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/**
657 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700658 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000660 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 *
662 * Get the device and lock it for exclusive access
663 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200664static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200665nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200667 spinlock_t *lock = &chip->controller->lock;
668 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100669 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100670 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100671 spin_lock(lock);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* Hardware controller shared among independend devices */
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200674 /* Hardware controller shared among independend devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200675 if (!chip->controller->active)
676 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200677
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200678 if (chip->controller->active == chip && chip->state == FL_READY) {
679 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100680 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100681 return 0;
682 }
683 if (new_state == FL_PM_SUSPENDED) {
684 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200685 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100686 }
687 set_current_state(TASK_UNINTERRUPTIBLE);
688 add_wait_queue(wq, &wait);
689 spin_unlock(lock);
690 schedule();
691 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 goto retry;
693}
694
695/**
696 * nand_wait - [DEFAULT] wait until the command is done
697 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700698 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *
700 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000701 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700703 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200704static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200708 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100711 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100713 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Richard Purdie8fe833c2006-03-31 02:31:14 -0800715 led_trigger_event(nand_led_trigger, LED_FULL);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Apply this short delay always to ensure that we do wait tWB in
718 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100719 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200721 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
722 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000723 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200724 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000726 while (time_before(jiffies, timeo)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 if (chip->dev_ready) {
728 if (chip->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000734 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800736 led_trigger_event(nand_led_trigger, LED_OFF);
737
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200738 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return status;
740}
741
742/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200743 * nand_read_page_raw - [Intern] read raw page data without ecc
744 * @mtd: mtd info structure
745 * @chip: nand chip info structure
746 * @buf: buffer to store read data
747 */
748static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
749 uint8_t *buf)
750{
751 chip->read_buf(mtd, buf, mtd->writesize);
752 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
753 return 0;
754}
755
756/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300757 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200758 * @mtd: mtd info structure
759 * @chip: nand chip info structure
760 * @buf: buffer to store read data
David A. Marlin068e3c02005-01-24 03:07:46 +0000761 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200762static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
763 uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200765 int i, eccsize = chip->ecc.size;
766 int eccbytes = chip->ecc.bytes;
767 int eccsteps = chip->ecc.steps;
768 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100769 uint8_t *ecc_calc = chip->buffers->ecccalc;
770 uint8_t *ecc_code = chip->buffers->ecccode;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200771 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200772
Thomas Gleixner90424de2007-04-05 11:44:05 +0200773 chip->ecc.read_page_raw(mtd, chip, buf);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200774
775 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
776 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
777
778 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200779 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200780
781 eccsteps = chip->ecc.steps;
782 p = buf;
783
784 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
785 int stat;
786
787 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
788 if (stat == -1)
789 mtd->ecc_stats.failed++;
790 else
791 mtd->ecc_stats.corrected += stat;
792 }
793 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300797 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200798 * @mtd: mtd info structure
799 * @chip: nand chip info structure
800 * @buf: buffer to store read data
801 *
802 * Not for syndrome calculating ecc controllers which need a special oob layout
803 */
804static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
805 uint8_t *buf)
806{
807 int i, eccsize = chip->ecc.size;
808 int eccbytes = chip->ecc.bytes;
809 int eccsteps = chip->ecc.steps;
810 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100811 uint8_t *ecc_calc = chip->buffers->ecccalc;
812 uint8_t *ecc_code = chip->buffers->ecccode;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200813 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200814
815 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
816 chip->ecc.hwctl(mtd, NAND_ECC_READ);
817 chip->read_buf(mtd, p, eccsize);
818 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
819 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200820 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200821
822 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200823 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200824
825 eccsteps = chip->ecc.steps;
826 p = buf;
827
828 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
829 int stat;
830
831 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
832 if (stat == -1)
833 mtd->ecc_stats.failed++;
834 else
835 mtd->ecc_stats.corrected += stat;
836 }
837 return 0;
838}
839
840/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300841 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200842 * @mtd: mtd info structure
843 * @chip: nand chip info structure
844 * @buf: buffer to store read data
845 *
846 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200847 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200848 */
849static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
850 uint8_t *buf)
851{
852 int i, eccsize = chip->ecc.size;
853 int eccbytes = chip->ecc.bytes;
854 int eccsteps = chip->ecc.steps;
855 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200856 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200857
858 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
859 int stat;
860
861 chip->ecc.hwctl(mtd, NAND_ECC_READ);
862 chip->read_buf(mtd, p, eccsize);
863
864 if (chip->ecc.prepad) {
865 chip->read_buf(mtd, oob, chip->ecc.prepad);
866 oob += chip->ecc.prepad;
867 }
868
869 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
870 chip->read_buf(mtd, oob, eccbytes);
871 stat = chip->ecc.correct(mtd, p, oob, NULL);
872
873 if (stat == -1)
874 mtd->ecc_stats.failed++;
875 else
876 mtd->ecc_stats.corrected += stat;
877
878 oob += eccbytes;
879
880 if (chip->ecc.postpad) {
881 chip->read_buf(mtd, oob, chip->ecc.postpad);
882 oob += chip->ecc.postpad;
883 }
884 }
885
886 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +0400887 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200888 if (i)
889 chip->read_buf(mtd, oob, i);
890
891 return 0;
892}
893
894/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200895 * nand_transfer_oob - [Internal] Transfer oob to client buffer
896 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700897 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200898 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +0300899 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200900 */
901static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +0300902 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200903{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200904 switch(ops->mode) {
905
906 case MTD_OOB_PLACE:
907 case MTD_OOB_RAW:
908 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
909 return oob + len;
910
911 case MTD_OOB_AUTO: {
912 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200913 uint32_t boffs = 0, roffs = ops->ooboffs;
914 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200915
916 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200917 /* Read request not from offset 0 ? */
918 if (unlikely(roffs)) {
919 if (roffs >= free->length) {
920 roffs -= free->length;
921 continue;
922 }
923 boffs = free->offset + roffs;
924 bytes = min_t(size_t, len,
925 (free->length - roffs));
926 roffs = 0;
927 } else {
928 bytes = min_t(size_t, len, free->length);
929 boffs = free->offset;
930 }
931 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200932 oob += bytes;
933 }
934 return oob;
935 }
936 default:
937 BUG();
938 }
939 return NULL;
940}
941
942/**
943 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200944 *
David A. Marlin068e3c02005-01-24 03:07:46 +0000945 * @mtd: MTD device structure
946 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -0700947 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +0000948 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200949 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +0000950 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200951static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
952 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +0000953{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200954 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200955 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200956 struct mtd_ecc_stats stats;
957 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
958 int sndcmd = 1;
959 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200960 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +0300961 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200962 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200964 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200966 chipnr = (int)(from >> chip->chip_shift);
967 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200969 realpage = (int)(from >> chip->page_shift);
970 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200972 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200974 buf = ops->datbuf;
975 oob = ops->oobbuf;
976
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200977 while(1) {
978 bytes = min(mtd->writesize - col, readlen);
979 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000980
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200981 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200982 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100983 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200985 if (likely(sndcmd)) {
986 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
987 sndcmd = 0;
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200990 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +0100991 if (unlikely(ops->mode == MTD_OOB_RAW))
992 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
993 else
994 ret = chip->ecc.read_page(mtd, chip, bufpoi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200995 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +0100996 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200997
998 /* Transfer not aligned data */
999 if (!aligned) {
1000 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001001 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001003
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001004 buf += bytes;
1005
1006 if (unlikely(oob)) {
1007 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001008 if (ops->mode != MTD_OOB_RAW) {
1009 int toread = min(oobreadlen,
1010 chip->ecc.layout->oobavail);
1011 if (toread) {
1012 oob = nand_transfer_oob(chip,
1013 oob, ops, toread);
1014 oobreadlen -= toread;
1015 }
1016 } else
1017 buf = nand_transfer_oob(chip,
1018 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001019 }
1020
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001021 if (!(chip->options & NAND_NO_READRDY)) {
1022 /*
1023 * Apply delay or wait for ready/busy pin. Do
1024 * this before the AUTOINCR check, so no
1025 * problems arise if a chip which does auto
1026 * increment is marked as NOAUTOINCR by the
1027 * board driver.
1028 */
1029 if (!chip->dev_ready)
1030 udelay(chip->chip_delay);
1031 else
1032 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001034 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001035 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001036 buf += bytes;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001039 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001040
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001041 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /* For subsequent reads align to page boundary. */
1045 col = 0;
1046 /* Increment page address */
1047 realpage++;
1048
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001049 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* Check, if we cross a chip boundary */
1051 if (!page) {
1052 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001053 chip->select_chip(mtd, -1);
1054 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001056
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001057 /* Check, if the chip supports auto page increment
1058 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001059 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001060 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001061 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001064 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001065 if (oob)
1066 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001068 if (ret)
1069 return ret;
1070
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001071 if (mtd->ecc_stats.failed - stats.failed)
1072 return -EBADMSG;
1073
1074 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001075}
1076
1077/**
1078 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1079 * @mtd: MTD device structure
1080 * @from: offset to read from
1081 * @len: number of bytes to read
1082 * @retlen: pointer to variable to store the number of read bytes
1083 * @buf: the databuffer to put data
1084 *
1085 * Get hold of the chip and call nand_do_read
1086 */
1087static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1088 size_t *retlen, uint8_t *buf)
1089{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001090 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001091 int ret;
1092
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001093 /* Do not allow reads past end of device */
1094 if ((from + len) > mtd->size)
1095 return -EINVAL;
1096 if (!len)
1097 return 0;
1098
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001099 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001100
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001101 chip->ops.len = len;
1102 chip->ops.datbuf = buf;
1103 chip->ops.oobbuf = NULL;
1104
1105 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001106
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001107 *retlen = chip->ops.retlen;
1108
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001109 nand_release_device(mtd);
1110
1111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001115 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1116 * @mtd: mtd info structure
1117 * @chip: nand chip info structure
1118 * @page: page number to read
1119 * @sndcmd: flag whether to issue read command or not
1120 */
1121static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1122 int page, int sndcmd)
1123{
1124 if (sndcmd) {
1125 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1126 sndcmd = 0;
1127 }
1128 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1129 return sndcmd;
1130}
1131
1132/**
1133 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1134 * with syndromes
1135 * @mtd: mtd info structure
1136 * @chip: nand chip info structure
1137 * @page: page number to read
1138 * @sndcmd: flag whether to issue read command or not
1139 */
1140static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1141 int page, int sndcmd)
1142{
1143 uint8_t *buf = chip->oob_poi;
1144 int length = mtd->oobsize;
1145 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1146 int eccsize = chip->ecc.size;
1147 uint8_t *bufpoi = buf;
1148 int i, toread, sndrnd = 0, pos;
1149
1150 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1151 for (i = 0; i < chip->ecc.steps; i++) {
1152 if (sndrnd) {
1153 pos = eccsize + i * (eccsize + chunk);
1154 if (mtd->writesize > 512)
1155 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1156 else
1157 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1158 } else
1159 sndrnd = 1;
1160 toread = min_t(int, length, chunk);
1161 chip->read_buf(mtd, bufpoi, toread);
1162 bufpoi += toread;
1163 length -= toread;
1164 }
1165 if (length > 0)
1166 chip->read_buf(mtd, bufpoi, length);
1167
1168 return 1;
1169}
1170
1171/**
1172 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1173 * @mtd: mtd info structure
1174 * @chip: nand chip info structure
1175 * @page: page number to write
1176 */
1177static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1178 int page)
1179{
1180 int status = 0;
1181 const uint8_t *buf = chip->oob_poi;
1182 int length = mtd->oobsize;
1183
1184 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1185 chip->write_buf(mtd, buf, length);
1186 /* Send command to program the OOB data */
1187 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1188
1189 status = chip->waitfunc(mtd, chip);
1190
Savin Zlobec0d420f92006-06-21 11:51:20 +02001191 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001192}
1193
1194/**
1195 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1196 * with syndrome - only for large page flash !
1197 * @mtd: mtd info structure
1198 * @chip: nand chip info structure
1199 * @page: page number to write
1200 */
1201static int nand_write_oob_syndrome(struct mtd_info *mtd,
1202 struct nand_chip *chip, int page)
1203{
1204 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1205 int eccsize = chip->ecc.size, length = mtd->oobsize;
1206 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1207 const uint8_t *bufpoi = chip->oob_poi;
1208
1209 /*
1210 * data-ecc-data-ecc ... ecc-oob
1211 * or
1212 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1213 */
1214 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1215 pos = steps * (eccsize + chunk);
1216 steps = 0;
1217 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001218 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001219
1220 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1221 for (i = 0; i < steps; i++) {
1222 if (sndcmd) {
1223 if (mtd->writesize <= 512) {
1224 uint32_t fill = 0xFFFFFFFF;
1225
1226 len = eccsize;
1227 while (len > 0) {
1228 int num = min_t(int, len, 4);
1229 chip->write_buf(mtd, (uint8_t *)&fill,
1230 num);
1231 len -= num;
1232 }
1233 } else {
1234 pos = eccsize + i * (eccsize + chunk);
1235 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1236 }
1237 } else
1238 sndcmd = 1;
1239 len = min_t(int, length, chunk);
1240 chip->write_buf(mtd, bufpoi, len);
1241 bufpoi += len;
1242 length -= len;
1243 }
1244 if (length > 0)
1245 chip->write_buf(mtd, bufpoi, length);
1246
1247 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1248 status = chip->waitfunc(mtd, chip);
1249
1250 return status & NAND_STATUS_FAIL ? -EIO : 0;
1251}
1252
1253/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001254 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 * @mtd: MTD device structure
1256 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001257 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 *
1259 * NAND read out-of-band data from the spare area
1260 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001261static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1262 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001264 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001265 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001266 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001267 int readlen = ops->ooblen;
1268 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001269 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Andrew Morton7e9a0bb2006-05-30 09:06:41 +01001271 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1272 (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Adrian Hunter03736152007-01-31 17:58:29 +02001274 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001275 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001276 else
1277 len = mtd->oobsize;
1278
1279 if (unlikely(ops->ooboffs >= len)) {
1280 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1281 "Attempt to start read outside oob\n");
1282 return -EINVAL;
1283 }
1284
1285 /* Do not allow reads past end of device */
1286 if (unlikely(from >= mtd->size ||
1287 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1288 (from >> chip->page_shift)) * len)) {
1289 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1290 "Attempt read beyond end of device\n");
1291 return -EINVAL;
1292 }
Vitaly Wool70145682006-11-03 18:20:38 +03001293
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001294 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001295 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001297 /* Shift to get page */
1298 realpage = (int)(from >> chip->page_shift);
1299 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001301 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001302 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001303
1304 len = min(len, readlen);
1305 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001306
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001307 if (!(chip->options & NAND_NO_READRDY)) {
1308 /*
1309 * Apply delay or wait for ready/busy pin. Do this
1310 * before the AUTOINCR check, so no problems arise if a
1311 * chip which does auto increment is marked as
1312 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001313 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001314 if (!chip->dev_ready)
1315 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001316 else
1317 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001319
Vitaly Wool70145682006-11-03 18:20:38 +03001320 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001321 if (!readlen)
1322 break;
1323
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001324 /* Increment page address */
1325 realpage++;
1326
1327 page = realpage & chip->pagemask;
1328 /* Check, if we cross a chip boundary */
1329 if (!page) {
1330 chipnr++;
1331 chip->select_chip(mtd, -1);
1332 chip->select_chip(mtd, chipnr);
1333 }
1334
1335 /* Check, if the chip supports auto page increment
1336 * or if we have hit a block boundary.
1337 */
1338 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1339 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
Vitaly Wool70145682006-11-03 18:20:38 +03001342 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return 0;
1344}
1345
1346/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001347 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001350 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001352 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001354static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1355 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001357 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001358 int ret = -ENOTSUPP;
1359
1360 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001363 if (ops->datbuf && (from + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001364 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001365 "Attempt read beyond end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EINVAL;
1367 }
1368
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001369 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001371 switch(ops->mode) {
1372 case MTD_OOB_PLACE:
1373 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001374 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001375 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001376
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001377 default:
1378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001381 if (!ops->datbuf)
1382 ret = nand_do_read_oob(mtd, from, ops);
1383 else
1384 ret = nand_do_read_ops(mtd, from, ops);
1385
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001386 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001388 return ret;
1389}
1390
1391
1392/**
1393 * nand_write_page_raw - [Intern] raw page write function
1394 * @mtd: mtd info structure
1395 * @chip: nand chip info structure
1396 * @buf: data buffer
1397 */
1398static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1399 const uint8_t *buf)
1400{
1401 chip->write_buf(mtd, buf, mtd->writesize);
1402 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001405/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001406 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001407 * @mtd: mtd info structure
1408 * @chip: nand chip info structure
1409 * @buf: data buffer
1410 */
1411static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1412 const uint8_t *buf)
1413{
1414 int i, eccsize = chip->ecc.size;
1415 int eccbytes = chip->ecc.bytes;
1416 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001417 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001418 const uint8_t *p = buf;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001419 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001420
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001421 /* Software ecc calculation */
1422 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1423 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001424
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001425 for (i = 0; i < chip->ecc.total; i++)
1426 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001427
Thomas Gleixner90424de2007-04-05 11:44:05 +02001428 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001429}
1430
1431/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001432 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001433 * @mtd: mtd info structure
1434 * @chip: nand chip info structure
1435 * @buf: data buffer
1436 */
1437static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1438 const uint8_t *buf)
1439{
1440 int i, eccsize = chip->ecc.size;
1441 int eccbytes = chip->ecc.bytes;
1442 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001443 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001444 const uint8_t *p = buf;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001445 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001446
1447 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1448 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001449 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001450 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1451 }
1452
1453 for (i = 0; i < chip->ecc.total; i++)
1454 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1455
1456 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1457}
1458
1459/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001460 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001461 * @mtd: mtd info structure
1462 * @chip: nand chip info structure
1463 * @buf: data buffer
1464 *
1465 * The hw generator calculates the error syndrome automatically. Therefor
1466 * we need a special oob layout and handling.
1467 */
1468static void nand_write_page_syndrome(struct mtd_info *mtd,
1469 struct nand_chip *chip, const uint8_t *buf)
1470{
1471 int i, eccsize = chip->ecc.size;
1472 int eccbytes = chip->ecc.bytes;
1473 int eccsteps = chip->ecc.steps;
1474 const uint8_t *p = buf;
1475 uint8_t *oob = chip->oob_poi;
1476
1477 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1478
1479 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1480 chip->write_buf(mtd, p, eccsize);
1481
1482 if (chip->ecc.prepad) {
1483 chip->write_buf(mtd, oob, chip->ecc.prepad);
1484 oob += chip->ecc.prepad;
1485 }
1486
1487 chip->ecc.calculate(mtd, p, oob);
1488 chip->write_buf(mtd, oob, eccbytes);
1489 oob += eccbytes;
1490
1491 if (chip->ecc.postpad) {
1492 chip->write_buf(mtd, oob, chip->ecc.postpad);
1493 oob += chip->ecc.postpad;
1494 }
1495 }
1496
1497 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001498 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001499 if (i)
1500 chip->write_buf(mtd, oob, i);
1501}
1502
1503/**
David Woodhouse956e9442006-09-25 17:12:39 +01001504 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001505 * @mtd: MTD device structure
1506 * @chip: NAND chip descriptor
1507 * @buf: the data to write
1508 * @page: page number to write
1509 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02001510 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001511 */
1512static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001513 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001514{
1515 int status;
1516
1517 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1518
David Woodhouse956e9442006-09-25 17:12:39 +01001519 if (unlikely(raw))
1520 chip->ecc.write_page_raw(mtd, chip, buf);
1521 else
1522 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001523
1524 /*
1525 * Cached progamming disabled for now, Not sure if its worth the
1526 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1527 */
1528 cached = 0;
1529
1530 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1531
1532 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001533 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001534 /*
1535 * See if operation failed and additional status checks are
1536 * available
1537 */
1538 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1539 status = chip->errstat(mtd, chip, FL_WRITING, status,
1540 page);
1541
1542 if (status & NAND_STATUS_FAIL)
1543 return -EIO;
1544 } else {
1545 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001546 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001547 }
1548
1549#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1550 /* Send command to read back the data */
1551 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1552
1553 if (chip->verify_buf(mtd, buf, mtd->writesize))
1554 return -EIO;
1555#endif
1556 return 0;
1557}
1558
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001559/**
1560 * nand_fill_oob - [Internal] Transfer client buffer to oob
1561 * @chip: nand chip structure
1562 * @oob: oob data buffer
1563 * @ops: oob ops structure
1564 */
1565static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1566 struct mtd_oob_ops *ops)
1567{
1568 size_t len = ops->ooblen;
1569
1570 switch(ops->mode) {
1571
1572 case MTD_OOB_PLACE:
1573 case MTD_OOB_RAW:
1574 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1575 return oob + len;
1576
1577 case MTD_OOB_AUTO: {
1578 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001579 uint32_t boffs = 0, woffs = ops->ooboffs;
1580 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001581
1582 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001583 /* Write request not from offset 0 ? */
1584 if (unlikely(woffs)) {
1585 if (woffs >= free->length) {
1586 woffs -= free->length;
1587 continue;
1588 }
1589 boffs = free->offset + woffs;
1590 bytes = min_t(size_t, len,
1591 (free->length - woffs));
1592 woffs = 0;
1593 } else {
1594 bytes = min_t(size_t, len, free->length);
1595 boffs = free->offset;
1596 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001597 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001598 oob += bytes;
1599 }
1600 return oob;
1601 }
1602 default:
1603 BUG();
1604 }
1605 return NULL;
1606}
1607
Thomas Gleixner29072b92006-09-28 15:38:36 +02001608#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001609
1610/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001611 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001612 * @mtd: MTD device structure
1613 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001614 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001615 *
1616 * NAND write with ECC
1617 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001618static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1619 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001620{
Thomas Gleixner29072b92006-09-28 15:38:36 +02001621 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001622 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001623 uint32_t writelen = ops->len;
1624 uint8_t *oob = ops->oobbuf;
1625 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001626 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001627
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001628 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001629 if (!writelen)
1630 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001631
1632 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001633 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001634 printk(KERN_NOTICE "nand_write: "
1635 "Attempt to write not page aligned data\n");
1636 return -EINVAL;
1637 }
1638
Thomas Gleixner29072b92006-09-28 15:38:36 +02001639 column = to & (mtd->writesize - 1);
1640 subpage = column || (writelen & (mtd->writesize - 1));
1641
1642 if (subpage && oob)
1643 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001644
Thomas Gleixner6a930962006-06-28 00:11:45 +02001645 chipnr = (int)(to >> chip->chip_shift);
1646 chip->select_chip(mtd, chipnr);
1647
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001648 /* Check, if it is write protected */
1649 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001650 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001651
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001652 realpage = (int)(to >> chip->page_shift);
1653 page = realpage & chip->pagemask;
1654 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1655
1656 /* Invalidate the page cache, when we write to the cached page */
1657 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001658 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001659 chip->pagebuf = -1;
1660
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001661 /* If we're not given explicit OOB data, let it be 0xFF */
1662 if (likely(!oob))
1663 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001664
1665 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02001666 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001667 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001668 uint8_t *wbuf = buf;
1669
1670 /* Partial page write ? */
1671 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1672 cached = 0;
1673 bytes = min_t(int, bytes - column, (int) writelen);
1674 chip->pagebuf = -1;
1675 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1676 memcpy(&chip->buffers->databuf[column], buf, bytes);
1677 wbuf = chip->buffers->databuf;
1678 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001679
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001680 if (unlikely(oob))
1681 oob = nand_fill_oob(chip, oob, ops);
1682
Thomas Gleixner29072b92006-09-28 15:38:36 +02001683 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01001684 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001685 if (ret)
1686 break;
1687
1688 writelen -= bytes;
1689 if (!writelen)
1690 break;
1691
Thomas Gleixner29072b92006-09-28 15:38:36 +02001692 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001693 buf += bytes;
1694 realpage++;
1695
1696 page = realpage & chip->pagemask;
1697 /* Check, if we cross a chip boundary */
1698 if (!page) {
1699 chipnr++;
1700 chip->select_chip(mtd, -1);
1701 chip->select_chip(mtd, chipnr);
1702 }
1703 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001704
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001705 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03001706 if (unlikely(oob))
1707 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001708 return ret;
1709}
1710
1711/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001712 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * @mtd: MTD device structure
1714 * @to: offset to write to
1715 * @len: number of bytes to write
1716 * @retlen: pointer to variable to store the number of written bytes
1717 * @buf: the data to write
1718 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001719 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001721static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001722 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001724 struct nand_chip *chip = mtd->priv;
1725 int ret;
1726
1727 /* Do not allow reads past end of device */
1728 if ((to + len) > mtd->size)
1729 return -EINVAL;
1730 if (!len)
1731 return 0;
1732
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001733 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001734
1735 chip->ops.len = len;
1736 chip->ops.datbuf = (uint8_t *)buf;
1737 chip->ops.oobbuf = NULL;
1738
1739 ret = nand_do_write_ops(mtd, to, &chip->ops);
1740
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001741 *retlen = chip->ops.retlen;
1742
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001743 nand_release_device(mtd);
1744
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001745 return ret;
1746}
1747
1748/**
1749 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1750 * @mtd: MTD device structure
1751 * @to: offset to write to
1752 * @ops: oob operation description structure
1753 *
1754 * NAND write out-of-band
1755 */
1756static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1757 struct mtd_oob_ops *ops)
1758{
Adrian Hunter03736152007-01-31 17:58:29 +02001759 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001760 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001762 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
Vitaly Wool70145682006-11-03 18:20:38 +03001763 (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Adrian Hunter03736152007-01-31 17:58:29 +02001765 if (ops->mode == MTD_OOB_AUTO)
1766 len = chip->ecc.layout->oobavail;
1767 else
1768 len = mtd->oobsize;
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02001771 if ((ops->ooboffs + ops->ooblen) > len) {
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001772 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1773 "Attempt to write past end of page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return -EINVAL;
1775 }
1776
Adrian Hunter03736152007-01-31 17:58:29 +02001777 if (unlikely(ops->ooboffs >= len)) {
1778 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1779 "Attempt to start write outside oob\n");
1780 return -EINVAL;
1781 }
1782
1783 /* Do not allow reads past end of device */
1784 if (unlikely(to >= mtd->size ||
1785 ops->ooboffs + ops->ooblen >
1786 ((mtd->size >> chip->page_shift) -
1787 (to >> chip->page_shift)) * len)) {
1788 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1789 "Attempt write beyond end of device\n");
1790 return -EINVAL;
1791 }
1792
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001793 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001794 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001796 /* Shift to get page */
1797 page = (int)(to >> chip->page_shift);
1798
1799 /*
1800 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1801 * of my DiskOnChip 2000 test units) will clear the whole data page too
1802 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1803 * it in the doc2000 driver in August 1999. dwmw2.
1804 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001805 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 /* Check, if it is write protected */
1808 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001809 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001812 if (page == chip->pagebuf)
1813 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001815 memset(chip->oob_poi, 0xff, mtd->oobsize);
1816 nand_fill_oob(chip, ops->oobbuf, ops);
1817 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1818 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001819
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001820 if (status)
1821 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Vitaly Wool70145682006-11-03 18:20:38 +03001823 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001825 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001828/**
1829 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1830 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001831 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001832 * @ops: oob operation description structure
1833 */
1834static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1835 struct mtd_oob_ops *ops)
1836{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001837 struct nand_chip *chip = mtd->priv;
1838 int ret = -ENOTSUPP;
1839
1840 ops->retlen = 0;
1841
1842 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001843 if (ops->datbuf && (to + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001844 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1845 "Attempt read beyond end of device\n");
1846 return -EINVAL;
1847 }
1848
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001849 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001850
1851 switch(ops->mode) {
1852 case MTD_OOB_PLACE:
1853 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001854 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001855 break;
1856
1857 default:
1858 goto out;
1859 }
1860
1861 if (!ops->datbuf)
1862 ret = nand_do_write_oob(mtd, to, ops);
1863 else
1864 ret = nand_do_write_ops(mtd, to, ops);
1865
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001866 out:
1867 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return ret;
1869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1873 * @mtd: MTD device structure
1874 * @page: the page address of the block which will be erased
1875 *
1876 * Standard erase command for NAND chips
1877 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001878static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001880 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001882 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1883 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
1886/**
1887 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1888 * @mtd: MTD device structure
1889 * @page: the page address of the block which will be erased
1890 *
1891 * AND multi block erase command function
1892 * Erase 4 consecutive blocks
1893 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001894static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001896 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001898 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1899 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1900 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1901 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1902 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
1905/**
1906 * nand_erase - [MTD Interface] erase block(s)
1907 * @mtd: MTD device structure
1908 * @instr: erase instruction
1909 *
1910 * Erase one ore more blocks
1911 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001912static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
David Woodhousee0c7d762006-05-13 18:07:53 +01001914 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001916
David A. Marlin30f464b2005-01-17 18:35:25 +00001917#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001919 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 * @mtd: MTD device structure
1921 * @instr: erase instruction
1922 * @allowbbt: allow erasing the bbt area
1923 *
1924 * Erase one ore more blocks
1925 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001926int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
1927 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 int page, len, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001930 struct nand_chip *chip = mtd->priv;
1931 int rewrite_bbt[NAND_MAX_CHIPS]={0};
1932 unsigned int bbt_masked_page = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001934 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
1935 (unsigned int)instr->addr, (unsigned int)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001938 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001939 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return -EINVAL;
1941 }
1942
1943 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001944 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
1945 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1946 "Length not block aligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return -EINVAL;
1948 }
1949
1950 /* Do not allow erase past end of device */
1951 if ((instr->len + instr->addr) > mtd->size) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001952 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1953 "Erase past end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return -EINVAL;
1955 }
1956
1957 instr->fail_addr = 0xffffffff;
1958
1959 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001960 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001963 page = (int)(instr->addr >> chip->page_shift);
1964 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001967 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001970 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /* Check, if it is write protected */
1973 if (nand_check_wp(mtd)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001974 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1975 "Device is write protected!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 instr->state = MTD_ERASE_FAILED;
1977 goto erase_exit;
1978 }
1979
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001980 /*
1981 * If BBT requires refresh, set the BBT page mask to see if the BBT
1982 * should be rewritten. Otherwise the mask is set to 0xffffffff which
1983 * can not be matched. This is also done when the bbt is actually
1984 * erased to avoid recusrsive updates
1985 */
1986 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
1987 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /* Loop through the pages */
1990 len = instr->len;
1991
1992 instr->state = MTD_ERASING;
1993
1994 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001995 /*
1996 * heck if we have a bad block, we do not erase bad blocks !
1997 */
1998 if (nand_block_checkbad(mtd, ((loff_t) page) <<
1999 chip->page_shift, 0, allowbbt)) {
2000 printk(KERN_WARNING "nand_erase: attempt to erase a "
2001 "bad block at page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 instr->state = MTD_ERASE_FAILED;
2003 goto erase_exit;
2004 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002005
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002006 /*
2007 * Invalidate the page cache, if we erase the block which
2008 * contains the current cached page
2009 */
2010 if (page <= chip->pagebuf && chip->pagebuf <
2011 (page + pages_per_block))
2012 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002014 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002015
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002016 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002018 /*
2019 * See if operation failed and additional status checks are
2020 * available
2021 */
2022 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2023 status = chip->errstat(mtd, chip, FL_ERASING,
2024 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002027 if (status & NAND_STATUS_FAIL) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002028 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2029 "Failed erase, page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 instr->state = MTD_ERASE_FAILED;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002031 instr->fail_addr = (page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 goto erase_exit;
2033 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002034
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002035 /*
2036 * If BBT requires refresh, set the BBT rewrite flag to the
2037 * page being erased
2038 */
2039 if (bbt_masked_page != 0xffffffff &&
2040 (page & BBT_PAGE_MASK) == bbt_masked_page)
2041 rewrite_bbt[chipnr] = (page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002044 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 page += pages_per_block;
2046
2047 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002048 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002050 chip->select_chip(mtd, -1);
2051 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002052
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002053 /*
2054 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2055 * page mask to see if this BBT should be rewritten
2056 */
2057 if (bbt_masked_page != 0xffffffff &&
2058 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2059 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2060 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062 }
2063 instr->state = MTD_ERASE_DONE;
2064
David Woodhousee0c7d762006-05-13 18:07:53 +01002065 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2068 /* Do call back function */
2069 if (!ret)
2070 mtd_erase_callback(instr);
2071
2072 /* Deselect and wake up anyone waiting on the device */
2073 nand_release_device(mtd);
2074
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002075 /*
2076 * If BBT requires refresh and erase was successful, rewrite any
2077 * selected bad block tables
2078 */
2079 if (bbt_masked_page == 0xffffffff || ret)
2080 return ret;
2081
2082 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2083 if (!rewrite_bbt[chipnr])
2084 continue;
2085 /* update the BBT for chip */
2086 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2087 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2088 chip->bbt_td->pages[chipnr]);
2089 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002090 }
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* Return more or less happy */
2093 return ret;
2094}
2095
2096/**
2097 * nand_sync - [MTD Interface] sync
2098 * @mtd: MTD device structure
2099 *
2100 * Sync is actually a wait for chip ready function
2101 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002102static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002104 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
David Woodhousee0c7d762006-05-13 18:07:53 +01002106 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002109 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002111 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002115 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002117 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002119static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002122 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002124
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002125 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002129 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 * @mtd: MTD device structure
2131 * @ofs: offset relative to mtd start
2132 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002133static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002135 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 int ret;
2137
David Woodhousee0c7d762006-05-13 18:07:53 +01002138 if ((ret = nand_block_isbad(mtd, ofs))) {
2139 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (ret > 0)
2141 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002142 return ret;
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002145 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002149 * nand_suspend - [MTD Interface] Suspend the NAND flash
2150 * @mtd: MTD device structure
2151 */
2152static int nand_suspend(struct mtd_info *mtd)
2153{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002154 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002155
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002156 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002157}
2158
2159/**
2160 * nand_resume - [MTD Interface] Resume the NAND flash
2161 * @mtd: MTD device structure
2162 */
2163static void nand_resume(struct mtd_info *mtd)
2164{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002165 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002166
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002167 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002168 nand_release_device(mtd);
2169 else
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02002170 printk(KERN_ERR "nand_resume() called for a chip which is not "
2171 "in suspended state\n");
Vitaly Wool962034f2005-09-15 14:58:53 +01002172}
2173
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002174/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002175 * Set default functions
2176 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002177static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002178{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002180 if (!chip->chip_delay)
2181 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002184 if (chip->cmdfunc == NULL)
2185 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002188 if (chip->waitfunc == NULL)
2189 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002191 if (!chip->select_chip)
2192 chip->select_chip = nand_select_chip;
2193 if (!chip->read_byte)
2194 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2195 if (!chip->read_word)
2196 chip->read_word = nand_read_word;
2197 if (!chip->block_bad)
2198 chip->block_bad = nand_block_bad;
2199 if (!chip->block_markbad)
2200 chip->block_markbad = nand_default_block_markbad;
2201 if (!chip->write_buf)
2202 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2203 if (!chip->read_buf)
2204 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2205 if (!chip->verify_buf)
2206 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2207 if (!chip->scan_bbt)
2208 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002209
2210 if (!chip->controller) {
2211 chip->controller = &chip->hwcontrol;
2212 spin_lock_init(&chip->controller->lock);
2213 init_waitqueue_head(&chip->controller->wq);
2214 }
2215
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002216}
2217
2218/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002219 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002220 */
2221static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002222 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002223 int busw, int *maf_id)
2224{
2225 struct nand_flash_dev *type = NULL;
2226 int i, dev_id, maf_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002229 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002232 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002235 *maf_id = chip->read_byte(mtd);
2236 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002238 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002240 if (dev_id == nand_flash_ids[i].id) {
2241 type = &nand_flash_ids[i];
2242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
2245
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002246 if (!type)
2247 return ERR_PTR(-ENODEV);
2248
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002249 if (!mtd->name)
2250 mtd->name = type->name;
2251
2252 chip->chipsize = type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002253
2254 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002255 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002256 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002257 /* The 3rd id byte holds MLC / multichip data */
2258 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002259 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002260 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002261 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002262 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002263 extid >>= 2;
2264 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002265 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002266 extid >>= 2;
2267 /* Calc blocksize. Blocksize is multiples of 64KiB */
2268 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2269 extid >>= 2;
2270 /* Get buswidth information */
2271 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2272
2273 } else {
2274 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002275 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002276 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002277 mtd->erasesize = type->erasesize;
2278 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002279 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002280 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002281 }
2282
2283 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002284 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002285 if (nand_manuf_ids[maf_idx].id == *maf_id)
2286 break;
2287 }
2288
2289 /*
2290 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002291 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002292 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002293 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002294 printk(KERN_INFO "NAND device: Manufacturer ID:"
2295 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2296 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2297 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002298 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002299 busw ? 16 : 8);
2300 return ERR_PTR(-EINVAL);
2301 }
2302
2303 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002304 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002305 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002306 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002307
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002308 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002309 ffs(mtd->erasesize) - 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002310 chip->chip_shift = ffs(chip->chipsize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002311
2312 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002313 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002314 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2315
2316 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002317 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002318 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002319
2320 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002321 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002322 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002323 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002324
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002325 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002326 * options for chips which are not having an extended id.
2327 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002328 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002329 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002330
2331 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002332 if (chip->options & NAND_4PAGE_ARRAY)
2333 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002334 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002335 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002336
2337 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002338 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2339 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002340
2341 printk(KERN_INFO "NAND device: Manufacturer ID:"
2342 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2343 nand_manuf_ids[maf_idx].name, type->name);
2344
2345 return type;
2346}
2347
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002348/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002349 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2350 * @mtd: MTD device structure
2351 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002352 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002353 * This is the first phase of the normal nand_scan() function. It
2354 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002355 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002356 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002357 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002358int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002359{
2360 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002361 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002362 struct nand_flash_dev *type;
2363
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002364 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002365 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002366 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002367 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002368
2369 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002370 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002371
2372 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002373 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002374 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002375 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002378 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002379 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002380 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002382 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002384 if (nand_maf_id != chip->read_byte(mtd) ||
2385 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 break;
2387 }
2388 if (i > 1)
2389 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002392 chip->numchips = i;
2393 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
David Woodhouse3b85c322006-09-25 17:06:53 +01002395 return 0;
2396}
2397
2398
2399/**
2400 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2401 * @mtd: MTD device structure
2402 * @maxchips: Number of chips to scan for
2403 *
2404 * This is the second phase of the normal nand_scan() function. It
2405 * fills out all the uninitialized function pointers with the defaults
2406 * and scans for a bad block table if appropriate.
2407 */
2408int nand_scan_tail(struct mtd_info *mtd)
2409{
2410 int i;
2411 struct nand_chip *chip = mtd->priv;
2412
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002413 if (!(chip->options & NAND_OWN_BUFFERS))
2414 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2415 if (!chip->buffers)
2416 return -ENOMEM;
2417
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002418 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002419 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002420
2421 /*
2422 * If no default placement scheme is given, select an appropriate one
2423 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002424 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002425 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002427 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 break;
2429 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002430 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 break;
2432 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002433 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 break;
2435 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002436 printk(KERN_WARNING "No oob scheme defined for "
2437 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 BUG();
2439 }
2440 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002441
David Woodhouse956e9442006-09-25 17:12:39 +01002442 if (!chip->write_page)
2443 chip->write_page = nand_write_page;
2444
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002445 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002446 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2447 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002448 */
David Woodhouse956e9442006-09-25 17:12:39 +01002449 if (!chip->ecc.read_page_raw)
2450 chip->ecc.read_page_raw = nand_read_page_raw;
2451 if (!chip->ecc.write_page_raw)
2452 chip->ecc.write_page_raw = nand_write_page_raw;
2453
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002454 switch (chip->ecc.mode) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002455 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002456 /* Use standard hwecc read page function ? */
2457 if (!chip->ecc.read_page)
2458 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002459 if (!chip->ecc.write_page)
2460 chip->ecc.write_page = nand_write_page_hwecc;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002461 if (!chip->ecc.read_oob)
2462 chip->ecc.read_oob = nand_read_oob_std;
2463 if (!chip->ecc.write_oob)
2464 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002465
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002466 case NAND_ECC_HW_SYNDROME:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002467 if (!chip->ecc.calculate || !chip->ecc.correct ||
2468 !chip->ecc.hwctl) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002469 printk(KERN_WARNING "No ECC functions supplied, "
2470 "Hardware ECC not possible\n");
2471 BUG();
2472 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002473 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002474 if (!chip->ecc.read_page)
2475 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002476 if (!chip->ecc.write_page)
2477 chip->ecc.write_page = nand_write_page_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002478 if (!chip->ecc.read_oob)
2479 chip->ecc.read_oob = nand_read_oob_syndrome;
2480 if (!chip->ecc.write_oob)
2481 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002482
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002483 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002484 break;
2485 printk(KERN_WARNING "%d byte HW ECC not possible on "
2486 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002487 chip->ecc.size, mtd->writesize);
2488 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002490 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002491 chip->ecc.calculate = nand_calculate_ecc;
2492 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002493 chip->ecc.read_page = nand_read_page_swecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002494 chip->ecc.write_page = nand_write_page_swecc;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002495 chip->ecc.read_oob = nand_read_oob_std;
2496 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002497 chip->ecc.size = 256;
2498 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002500
2501 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002502 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2503 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002504 chip->ecc.read_page = nand_read_page_raw;
2505 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002506 chip->ecc.read_oob = nand_read_oob_std;
2507 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002508 chip->ecc.size = mtd->writesize;
2509 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002513 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002514 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002515 BUG();
2516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002518 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002519 * The number of bytes available for a client to place data into
2520 * the out of band area
2521 */
2522 chip->ecc.layout->oobavail = 0;
2523 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2524 chip->ecc.layout->oobavail +=
2525 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002526 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002527
2528 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002529 * Set the number of read / write steps for one page depending on ECC
2530 * mode
2531 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002532 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2533 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002534 printk(KERN_WARNING "Invalid ecc parameters\n");
2535 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002537 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002538
Thomas Gleixner29072b92006-09-28 15:38:36 +02002539 /*
2540 * Allow subpage writes up to ecc.steps. Not possible for MLC
2541 * FLASH.
2542 */
2543 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2544 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2545 switch(chip->ecc.steps) {
2546 case 2:
2547 mtd->subpage_sft = 1;
2548 break;
2549 case 4:
2550 case 8:
2551 mtd->subpage_sft = 2;
2552 break;
2553 }
2554 }
2555 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2556
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002557 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002558 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002561 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002564 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 /* Fill in remaining MTD driver data */
2567 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002568 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 mtd->erase = nand_erase;
2570 mtd->point = NULL;
2571 mtd->unpoint = NULL;
2572 mtd->read = nand_read;
2573 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 mtd->read_oob = nand_read_oob;
2575 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 mtd->sync = nand_sync;
2577 mtd->lock = NULL;
2578 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002579 mtd->suspend = nand_suspend;
2580 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 mtd->block_isbad = nand_block_isbad;
2582 mtd->block_markbad = nand_block_markbad;
2583
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002584 /* propagate ecc.layout to mtd_info */
2585 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002587 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002588 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002592 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
David Woodhouse3b85c322006-09-25 17:06:53 +01002595/* module_text_address() isn't exported, and it's mostly a pointless
2596 test if this is a module _anyway_ -- they'd have to try _really_ hard
2597 to call us from in-kernel code if the core NAND support is modular. */
2598#ifdef MODULE
2599#define caller_is_module() (1)
2600#else
2601#define caller_is_module() \
2602 module_text_address((unsigned long)__builtin_return_address(0))
2603#endif
2604
2605/**
2606 * nand_scan - [NAND Interface] Scan for the NAND device
2607 * @mtd: MTD device structure
2608 * @maxchips: Number of chips to scan for
2609 *
2610 * This fills out all the uninitialized function pointers
2611 * with the defaults.
2612 * The flash ID is read and the mtd/chip structures are
2613 * filled with the appropriate values.
2614 * The mtd->owner field must be set to the module of the caller
2615 *
2616 */
2617int nand_scan(struct mtd_info *mtd, int maxchips)
2618{
2619 int ret;
2620
2621 /* Many callers got this wrong, so check for it for a while... */
2622 if (!mtd->owner && caller_is_module()) {
2623 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2624 BUG();
2625 }
2626
2627 ret = nand_scan_ident(mtd, maxchips);
2628 if (!ret)
2629 ret = nand_scan_tail(mtd);
2630 return ret;
2631}
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002634 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 * @mtd: MTD device structure
2636*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002637void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002639 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641#ifdef CONFIG_MTD_PARTITIONS
2642 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002643 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644#endif
2645 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002646 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Jesper Juhlfa671642005-11-07 01:01:27 -08002648 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002649 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002650 if (!(chip->options & NAND_OWN_BUFFERS))
2651 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
David Woodhousee0c7d762006-05-13 18:07:53 +01002654EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01002655EXPORT_SYMBOL_GPL(nand_scan_ident);
2656EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01002657EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002658
2659static int __init nand_base_init(void)
2660{
2661 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2662 return 0;
2663}
2664
2665static void __exit nand_base_exit(void)
2666{
2667 led_trigger_unregister_simple(nand_led_trigger);
2668}
2669
2670module_init(nand_base_init);
2671module_exit(nand_base_exit);
2672
David Woodhousee0c7d762006-05-13 18:07:53 +01002673MODULE_LICENSE("GPL");
2674MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2675MODULE_DESCRIPTION("Generic NAND flash driver code");