blob: 7a3a44907715c7facb17a2f7f58973134bea9cc5 [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
158 * @chip: chipnumber to select, -1 for deselect
159 *
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
306 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200307 page = (int)(ofs >> chip->page_shift);
308 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200310 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200313 chip->select_chip(mtd, chipnr);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000314 } else
David Woodhousee0c7d762006-05-13 18:07:53 +0100315 page = (int)ofs;
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,
319 page & chip->pagemask);
320 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 Gleixnerace4dfe2006-05-24 12:07:37 +0200326 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
327 page & chip->pagemask);
328 if (chip->read_byte(mtd) != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 res = 1;
330 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000331
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200332 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return res;
336}
337
338/**
339 * nand_default_block_markbad - [DEFAULT] mark a block bad
340 * @mtd: MTD device structure
341 * @ofs: offset from device start
342 *
343 * This is the default implementation, which can be overridden by
344 * a hardware specific driver.
345*/
346static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
347{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200348 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200349 uint8_t buf[2] = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int block;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* Get block number */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200353 block = ((int)ofs) >> chip->bbt_erase_shift;
354 if (chip->bbt)
355 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200358 if (chip->options & NAND_USE_FLASH_BBT)
David Woodhousee0c7d762006-05-13 18:07:53 +0100359 return nand_update_bbt(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* We write two bytes, so we dont have to mess with 16 bit access */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200362 ofs += mtd->oobsize;
363 chip->ops.len = 2;
364 chip->ops.datbuf = NULL;
365 chip->ops.oobbuf = buf;
366 chip->ops.ooboffs = chip->badblockpos & ~0x01;
367
368 return nand_do_write_oob(mtd, ofs, &chip->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000371/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * nand_check_wp - [GENERIC] check if the chip is write protected
373 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000374 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000376 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100378static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200380 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200382 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
383 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386/**
387 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
388 * @mtd: MTD device structure
389 * @ofs: offset from device start
390 * @getchip: 0, if the chip is already selected
391 * @allowbbt: 1, if its allowed to access the bbt area
392 *
393 * Check, if the block is bad. Either by reading the bad block table or
394 * calling of the scan function.
395 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200396static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
397 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200399 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000400
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200401 if (!chip->bbt)
402 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100405 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000408/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000409 * Wait for the ready pin, after a command
410 * The timeout is catched later.
411 */
412static void nand_wait_ready(struct mtd_info *mtd)
413{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200414 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100415 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000416
Richard Purdie8fe833c2006-03-31 02:31:14 -0800417 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000418 /* wait until command is processed or timeout occures */
419 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200420 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800421 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700422 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000423 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800424 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
428 * nand_command - [DEFAULT] Send command to NAND device
429 * @mtd: MTD device structure
430 * @command: the command to be sent
431 * @column: the column address for this command, -1 if none
432 * @page_addr: the page address for this command, -1 if none
433 *
434 * Send command to NAND device. This function is used for small page
435 * devices (256/512 Bytes per page)
436 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200437static void nand_command(struct mtd_info *mtd, unsigned int command,
438 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200440 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200441 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * Write out the command to the device.
445 */
446 if (command == NAND_CMD_SEQIN) {
447 int readcmd;
448
Joern Engel28318772006-05-22 23:18:05 +0200449 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200451 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 readcmd = NAND_CMD_READOOB;
453 } else if (column < 256) {
454 /* First 256 bytes --> READ0 */
455 readcmd = NAND_CMD_READ0;
456 } else {
457 column -= 256;
458 readcmd = NAND_CMD_READ1;
459 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200460 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200461 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200463 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200465 /*
466 * Address cycle, when necessary
467 */
468 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
469 /* Serially input address */
470 if (column != -1) {
471 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200472 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200473 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200474 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200475 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200477 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200478 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200479 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200480 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200481 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200482 if (chip->chipsize > (32 << 20))
483 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200484 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200485 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000486
487 /*
488 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 case NAND_CMD_PAGEPROG:
494 case NAND_CMD_ERASE1:
495 case NAND_CMD_ERASE2:
496 case NAND_CMD_SEQIN:
497 case NAND_CMD_STATUS:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200498 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return;
500
501 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200502 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200504 udelay(chip->chip_delay);
505 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200506 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200507 chip->cmd_ctrl(mtd,
508 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200509 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return;
511
David Woodhousee0c7d762006-05-13 18:07:53 +0100512 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000514 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * If we don't have access to the busy pin, we apply the given
516 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100517 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200518 if (!chip->dev_ready) {
519 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Apply this short delay always to ensure that we do wait tWB in
524 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100525 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000526
527 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/**
531 * nand_command_lp - [DEFAULT] Send command to NAND large page device
532 * @mtd: MTD device structure
533 * @command: the command to be sent
534 * @column: the column address for this command, -1 if none
535 * @page_addr: the page address for this command, -1 if none
536 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200537 * Send command to NAND device. This is the version for the new large page
538 * devices We dont have the separate regions as we have in the small page
539 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *
541 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200542static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
543 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200545 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* Emulate NAND_CMD_READOOB */
548 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200549 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 command = NAND_CMD_READ0;
551 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000552
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200553 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200554 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200555 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200558 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* Serially input address */
561 if (column != -1) {
562 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200563 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200565 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200566 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200567 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200570 chip->cmd_ctrl(mtd, page_addr, ctrl);
571 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200572 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200574 if (chip->chipsize > (128 << 20))
575 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200576 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200579 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000580
581 /*
582 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000583 * status, sequential in, and deplete1 need no delay
584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 case NAND_CMD_CACHEDPROG:
588 case NAND_CMD_PAGEPROG:
589 case NAND_CMD_ERASE1:
590 case NAND_CMD_ERASE2:
591 case NAND_CMD_SEQIN:
592 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000593 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return;
595
David Woodhousee0c7d762006-05-13 18:07:53 +0100596 /*
597 * read error status commands require only a short delay
598 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000599 case NAND_CMD_STATUS_ERROR:
600 case NAND_CMD_STATUS_ERROR0:
601 case NAND_CMD_STATUS_ERROR1:
602 case NAND_CMD_STATUS_ERROR2:
603 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200604 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000605 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200608 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200610 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200611 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
612 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
613 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
614 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200615 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return;
617
618 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200619 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
620 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
621 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
622 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000623
David Woodhousee0c7d762006-05-13 18:07:53 +0100624 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000626 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * If we don't have access to the busy pin, we apply the given
628 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100629 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200630 if (!chip->dev_ready) {
631 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* Apply this short delay always to ensure that we do wait tWB in
637 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100638 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000639
640 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/**
644 * nand_get_device - [GENERIC] Get chip for selected access
645 * @this: the nand chip descriptor
646 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000647 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 *
649 * Get the device and lock it for exclusive access
650 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200651static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200652nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 spinlock_t *lock = &chip->controller->lock;
655 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100656 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100657 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100658 spin_lock(lock);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* Hardware controller shared among independend devices */
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200661 /* Hardware controller shared among independend devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200662 if (!chip->controller->active)
663 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200664
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200665 if (chip->controller->active == chip && chip->state == FL_READY) {
666 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100667 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100668 return 0;
669 }
670 if (new_state == FL_PM_SUSPENDED) {
671 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200672 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100673 }
674 set_current_state(TASK_UNINTERRUPTIBLE);
675 add_wait_queue(wq, &wait);
676 spin_unlock(lock);
677 schedule();
678 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto retry;
680}
681
682/**
683 * nand_wait - [DEFAULT] wait until the command is done
684 * @mtd: MTD device structure
685 * @this: NAND chip structure
686 * @state: state to select the max. timeout value
687 *
688 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000689 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * general NAND and SmartMedia specs
691 *
692*/
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200693static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695
David Woodhousee0c7d762006-05-13 18:07:53 +0100696 unsigned long timeo = jiffies;
697 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100700 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100702 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Richard Purdie8fe833c2006-03-31 02:31:14 -0800704 led_trigger_event(nand_led_trigger, LED_FULL);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* Apply this short delay always to ensure that we do wait tWB in
707 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100708 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200710 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
711 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000712 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200713 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000715 while (time_before(jiffies, timeo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* Check, if we were interrupted */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200717 if (chip->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200720 if (chip->dev_ready) {
721 if (chip->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 } else {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200724 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
726 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000727 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800729 led_trigger_event(nand_led_trigger, LED_OFF);
730
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return status;
733}
734
735/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200736 * nand_read_page_raw - [Intern] read raw page data without ecc
737 * @mtd: mtd info structure
738 * @chip: nand chip info structure
739 * @buf: buffer to store read data
740 */
741static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
742 uint8_t *buf)
743{
744 chip->read_buf(mtd, buf, mtd->writesize);
745 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
746 return 0;
747}
748
749/**
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200750 * nand_read_page_swecc - {REPLACABLE] software ecc based page read function
751 * @mtd: mtd info structure
752 * @chip: nand chip info structure
753 * @buf: buffer to store read data
David A. Marlin068e3c02005-01-24 03:07:46 +0000754 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200755static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
756 uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200758 int i, eccsize = chip->ecc.size;
759 int eccbytes = chip->ecc.bytes;
760 int eccsteps = chip->ecc.steps;
761 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200762 uint8_t *ecc_calc = chip->buffers.ecccalc;
763 uint8_t *ecc_code = chip->buffers.ecccode;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200764 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200765
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200766 nand_read_page_raw(mtd, chip, buf);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200767
768 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
769 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
770
771 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200772 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200773
774 eccsteps = chip->ecc.steps;
775 p = buf;
776
777 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
778 int stat;
779
780 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
781 if (stat == -1)
782 mtd->ecc_stats.failed++;
783 else
784 mtd->ecc_stats.corrected += stat;
785 }
786 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100787}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/**
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200790 * nand_read_page_hwecc - {REPLACABLE] hardware ecc based page read function
791 * @mtd: mtd info structure
792 * @chip: nand chip info structure
793 * @buf: buffer to store read data
794 *
795 * Not for syndrome calculating ecc controllers which need a special oob layout
796 */
797static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
798 uint8_t *buf)
799{
800 int i, eccsize = chip->ecc.size;
801 int eccbytes = chip->ecc.bytes;
802 int eccsteps = chip->ecc.steps;
803 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200804 uint8_t *ecc_calc = chip->buffers.ecccalc;
805 uint8_t *ecc_code = chip->buffers.ecccode;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200806 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200807
808 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
809 chip->ecc.hwctl(mtd, NAND_ECC_READ);
810 chip->read_buf(mtd, p, eccsize);
811 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
812 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200813 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200814
815 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200816 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200817
818 eccsteps = chip->ecc.steps;
819 p = buf;
820
821 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
822 int stat;
823
824 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
825 if (stat == -1)
826 mtd->ecc_stats.failed++;
827 else
828 mtd->ecc_stats.corrected += stat;
829 }
830 return 0;
831}
832
833/**
834 * nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
835 * @mtd: mtd info structure
836 * @chip: nand chip info structure
837 * @buf: buffer to store read data
838 *
839 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200840 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200841 */
842static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
843 uint8_t *buf)
844{
845 int i, eccsize = chip->ecc.size;
846 int eccbytes = chip->ecc.bytes;
847 int eccsteps = chip->ecc.steps;
848 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200849 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200850
851 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
852 int stat;
853
854 chip->ecc.hwctl(mtd, NAND_ECC_READ);
855 chip->read_buf(mtd, p, eccsize);
856
857 if (chip->ecc.prepad) {
858 chip->read_buf(mtd, oob, chip->ecc.prepad);
859 oob += chip->ecc.prepad;
860 }
861
862 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
863 chip->read_buf(mtd, oob, eccbytes);
864 stat = chip->ecc.correct(mtd, p, oob, NULL);
865
866 if (stat == -1)
867 mtd->ecc_stats.failed++;
868 else
869 mtd->ecc_stats.corrected += stat;
870
871 oob += eccbytes;
872
873 if (chip->ecc.postpad) {
874 chip->read_buf(mtd, oob, chip->ecc.postpad);
875 oob += chip->ecc.postpad;
876 }
877 }
878
879 /* Calculate remaining oob bytes */
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200880 i = oob - chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200881 if (i)
882 chip->read_buf(mtd, oob, i);
883
884 return 0;
885}
886
887/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200888 * nand_transfer_oob - [Internal] Transfer oob to client buffer
889 * @chip: nand chip structure
890 * @ops: oob ops structure
891 */
892static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
893 struct mtd_oob_ops *ops)
894{
895 size_t len = ops->ooblen;
896
897 switch(ops->mode) {
898
899 case MTD_OOB_PLACE:
900 case MTD_OOB_RAW:
901 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
902 return oob + len;
903
904 case MTD_OOB_AUTO: {
905 struct nand_oobfree *free = chip->ecc.layout->oobfree;
906 size_t bytes;
907
908 for(; free->length && len; free++, len -= bytes) {
909 bytes = min(len, free->length);
910
911 memcpy(oob, chip->oob_poi + free->offset, bytes);
912 oob += bytes;
913 }
914 return oob;
915 }
916 default:
917 BUG();
918 }
919 return NULL;
920}
921
922/**
923 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200924 *
David A. Marlin068e3c02005-01-24 03:07:46 +0000925 * @mtd: MTD device structure
926 * @from: offset to read from
David A. Marlin068e3c02005-01-24 03:07:46 +0000927 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200928 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +0000929 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200930static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
931 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +0000932{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200933 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200934 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200935 struct mtd_ecc_stats stats;
936 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
937 int sndcmd = 1;
938 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200939 uint32_t readlen = ops->len;
940 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200942 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200944 chipnr = (int)(from >> chip->chip_shift);
945 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200947 realpage = (int)(from >> chip->page_shift);
948 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200950 col = (int)(from & (mtd->writesize - 1));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200951 chip->oob_poi = chip->buffers.oobrbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200953 buf = ops->datbuf;
954 oob = ops->oobbuf;
955
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200956 while(1) {
957 bytes = min(mtd->writesize - col, readlen);
958 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000959
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200960 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200961 if (realpage != chip->pagebuf || oob) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200962 bufpoi = aligned ? buf : chip->buffers.databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200964 if (likely(sndcmd)) {
965 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
966 sndcmd = 0;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200969 /* Now read the page into the buffer */
970 ret = chip->ecc.read_page(mtd, chip, bufpoi);
971 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +0100972 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200973
974 /* Transfer not aligned data */
975 if (!aligned) {
976 chip->pagebuf = realpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200977 memcpy(buf, chip->buffers.databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000979
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200980 buf += bytes;
981
982 if (unlikely(oob)) {
983 /* Raw mode does data:oob:data:oob */
984 if (ops->mode != MTD_OOB_RAW)
985 oob = nand_transfer_oob(chip, oob, ops);
986 else
987 buf = nand_transfer_oob(chip, buf, ops);
988 }
989
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200990 if (!(chip->options & NAND_NO_READRDY)) {
991 /*
992 * Apply delay or wait for ready/busy pin. Do
993 * this before the AUTOINCR check, so no
994 * problems arise if a chip which does auto
995 * increment is marked as NOAUTOINCR by the
996 * board driver.
997 */
998 if (!chip->dev_ready)
999 udelay(chip->chip_delay);
1000 else
1001 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001003 } else {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001004 memcpy(buf, chip->buffers.databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001005 buf += bytes;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001008 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001009
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001010 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001011 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 /* For subsequent reads align to page boundary. */
1014 col = 0;
1015 /* Increment page address */
1016 realpage++;
1017
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001018 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /* Check, if we cross a chip boundary */
1020 if (!page) {
1021 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001022 chip->select_chip(mtd, -1);
1023 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001025
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001026 /* Check, if the chip supports auto page increment
1027 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001028 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001029 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001030 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001033 ops->retlen = ops->len - (size_t) readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001035 if (ret)
1036 return ret;
1037
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001038 if (mtd->ecc_stats.failed - stats.failed)
1039 return -EBADMSG;
1040
1041 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001042}
1043
1044/**
1045 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1046 * @mtd: MTD device structure
1047 * @from: offset to read from
1048 * @len: number of bytes to read
1049 * @retlen: pointer to variable to store the number of read bytes
1050 * @buf: the databuffer to put data
1051 *
1052 * Get hold of the chip and call nand_do_read
1053 */
1054static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1055 size_t *retlen, uint8_t *buf)
1056{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001057 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001058 int ret;
1059
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001060 /* Do not allow reads past end of device */
1061 if ((from + len) > mtd->size)
1062 return -EINVAL;
1063 if (!len)
1064 return 0;
1065
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001066 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001067
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001068 chip->ops.len = len;
1069 chip->ops.datbuf = buf;
1070 chip->ops.oobbuf = NULL;
1071
1072 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001073
1074 nand_release_device(mtd);
1075
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001076 *retlen = chip->ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001077 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001081 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 * @mtd: MTD device structure
1083 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001084 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *
1086 * NAND read out-of-band data from the spare area
1087 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001088static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1089 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001091 int col, page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001092 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001093 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001094 int direct, bytes, readlen = ops->len;
1095 uint8_t *bufpoi, *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001097 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n",
1098 (unsigned int)from, (int)len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001100 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001101 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001103 /* Shift to get page */
1104 realpage = (int)(from >> chip->page_shift);
1105 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001107 if (ops->mode != MTD_OOB_AUTO) {
1108 col = ops->ooboffs;
1109 direct = 1;
1110 } else {
1111 col = 0;
1112 direct = 0;
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001115 while(1) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001116 bytes = direct ? ops->ooblen : mtd->oobsize;
1117 bufpoi = direct ? buf : chip->buffers.oobrbuf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001118
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001119 if (likely(sndcmd)) {
1120 chip->cmdfunc(mtd, NAND_CMD_READOOB, col, page);
1121 sndcmd = 0;
1122 }
1123
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001124 chip->read_buf(mtd, bufpoi, bytes);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001125
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001126 if (unlikely(!direct))
1127 buf = nand_transfer_oob(chip, buf, ops);
1128 else
1129 buf += ops->ooblen;
1130
1131 readlen -= ops->ooblen;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001132 if (!readlen)
1133 break;
1134
1135 if (!(chip->options & NAND_NO_READRDY)) {
1136 /*
1137 * Apply delay or wait for ready/busy pin. Do this
1138 * before the AUTOINCR check, so no problems arise if a
1139 * chip which does auto increment is marked as
1140 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001141 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001142 if (!chip->dev_ready)
1143 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001144 else
1145 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001147
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001148 /* Increment page address */
1149 realpage++;
1150
1151 page = realpage & chip->pagemask;
1152 /* Check, if we cross a chip boundary */
1153 if (!page) {
1154 chipnr++;
1155 chip->select_chip(mtd, -1);
1156 chip->select_chip(mtd, chipnr);
1157 }
1158
1159 /* Check, if the chip supports auto page increment
1160 * or if we have hit a block boundary.
1161 */
1162 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1163 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001166 ops->retlen = ops->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return 0;
1168}
1169
1170/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001171 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001174 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001176 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001178static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1179 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001181 int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
1182 uint8_t *buf) = NULL;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001183 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001184 int ret = -ENOTSUPP;
1185
1186 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* Do not allow reads past end of device */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001189 if ((from + ops->len) > mtd->size) {
1190 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001191 "Attempt read beyond end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return -EINVAL;
1193 }
1194
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001195 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001197 switch(ops->mode) {
1198 case MTD_OOB_PLACE:
1199 case MTD_OOB_AUTO:
1200 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001201
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001202 case MTD_OOB_RAW:
1203 /* Replace the read_page algorithm temporary */
1204 read_page = chip->ecc.read_page;
1205 chip->ecc.read_page = nand_read_page_raw;
1206 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001207
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001208 default:
1209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001212 if (!ops->datbuf)
1213 ret = nand_do_read_oob(mtd, from, ops);
1214 else
1215 ret = nand_do_read_ops(mtd, from, ops);
1216
1217 if (unlikely(ops->mode == MTD_OOB_RAW))
1218 chip->ecc.read_page = read_page;
1219 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001221 return ret;
1222}
1223
1224
1225/**
1226 * nand_write_page_raw - [Intern] raw page write function
1227 * @mtd: mtd info structure
1228 * @chip: nand chip info structure
1229 * @buf: data buffer
1230 */
1231static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1232 const uint8_t *buf)
1233{
1234 chip->write_buf(mtd, buf, mtd->writesize);
1235 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001238/**
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001239 * nand_write_page_swecc - {REPLACABLE] software ecc based page write function
1240 * @mtd: mtd info structure
1241 * @chip: nand chip info structure
1242 * @buf: data buffer
1243 */
1244static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1245 const uint8_t *buf)
1246{
1247 int i, eccsize = chip->ecc.size;
1248 int eccbytes = chip->ecc.bytes;
1249 int eccsteps = chip->ecc.steps;
1250 uint8_t *ecc_calc = chip->buffers.ecccalc;
1251 const uint8_t *p = buf;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001252 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001253
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001254 /* Software ecc calculation */
1255 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1256 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001257
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001258 for (i = 0; i < chip->ecc.total; i++)
1259 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001260
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001261 nand_write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001262}
1263
1264/**
1265 * nand_write_page_hwecc - {REPLACABLE] hardware ecc based page write function
1266 * @mtd: mtd info structure
1267 * @chip: nand chip info structure
1268 * @buf: data buffer
1269 */
1270static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1271 const uint8_t *buf)
1272{
1273 int i, eccsize = chip->ecc.size;
1274 int eccbytes = chip->ecc.bytes;
1275 int eccsteps = chip->ecc.steps;
1276 uint8_t *ecc_calc = chip->buffers.ecccalc;
1277 const uint8_t *p = buf;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001278 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001279
1280 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1281 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001282 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001283 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1284 }
1285
1286 for (i = 0; i < chip->ecc.total; i++)
1287 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1288
1289 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1290}
1291
1292/**
1293 * nand_write_page_syndrome - {REPLACABLE] hardware ecc syndrom based page write
1294 * @mtd: mtd info structure
1295 * @chip: nand chip info structure
1296 * @buf: data buffer
1297 *
1298 * The hw generator calculates the error syndrome automatically. Therefor
1299 * we need a special oob layout and handling.
1300 */
1301static void nand_write_page_syndrome(struct mtd_info *mtd,
1302 struct nand_chip *chip, const uint8_t *buf)
1303{
1304 int i, eccsize = chip->ecc.size;
1305 int eccbytes = chip->ecc.bytes;
1306 int eccsteps = chip->ecc.steps;
1307 const uint8_t *p = buf;
1308 uint8_t *oob = chip->oob_poi;
1309
1310 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1311
1312 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1313 chip->write_buf(mtd, p, eccsize);
1314
1315 if (chip->ecc.prepad) {
1316 chip->write_buf(mtd, oob, chip->ecc.prepad);
1317 oob += chip->ecc.prepad;
1318 }
1319
1320 chip->ecc.calculate(mtd, p, oob);
1321 chip->write_buf(mtd, oob, eccbytes);
1322 oob += eccbytes;
1323
1324 if (chip->ecc.postpad) {
1325 chip->write_buf(mtd, oob, chip->ecc.postpad);
1326 oob += chip->ecc.postpad;
1327 }
1328 }
1329
1330 /* Calculate remaining oob bytes */
1331 i = oob - chip->oob_poi;
1332 if (i)
1333 chip->write_buf(mtd, oob, i);
1334}
1335
1336/**
1337 * nand_write_page - [INTERNAL] write one page
1338 * @mtd: MTD device structure
1339 * @chip: NAND chip descriptor
1340 * @buf: the data to write
1341 * @page: page number to write
1342 * @cached: cached programming
1343 */
1344static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1345 const uint8_t *buf, int page, int cached)
1346{
1347 int status;
1348
1349 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1350
1351 chip->ecc.write_page(mtd, chip, buf);
1352
1353 /*
1354 * Cached progamming disabled for now, Not sure if its worth the
1355 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1356 */
1357 cached = 0;
1358
1359 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1360
1361 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1362 status = chip->waitfunc(mtd, chip, FL_WRITING);
1363 /*
1364 * See if operation failed and additional status checks are
1365 * available
1366 */
1367 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1368 status = chip->errstat(mtd, chip, FL_WRITING, status,
1369 page);
1370
1371 if (status & NAND_STATUS_FAIL)
1372 return -EIO;
1373 } else {
1374 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1375 status = chip->waitfunc(mtd, chip, FL_WRITING);
1376 }
1377
1378#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1379 /* Send command to read back the data */
1380 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1381
1382 if (chip->verify_buf(mtd, buf, mtd->writesize))
1383 return -EIO;
1384#endif
1385 return 0;
1386}
1387
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001388/**
1389 * nand_fill_oob - [Internal] Transfer client buffer to oob
1390 * @chip: nand chip structure
1391 * @oob: oob data buffer
1392 * @ops: oob ops structure
1393 */
1394static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1395 struct mtd_oob_ops *ops)
1396{
1397 size_t len = ops->ooblen;
1398
1399 switch(ops->mode) {
1400
1401 case MTD_OOB_PLACE:
1402 case MTD_OOB_RAW:
1403 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1404 return oob + len;
1405
1406 case MTD_OOB_AUTO: {
1407 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1408 size_t bytes;
1409
1410 for(; free->length && len; free++, len -= bytes) {
1411 bytes = min(len, free->length);
1412 memcpy(chip->oob_poi + free->offset, oob, bytes);
1413 oob += bytes;
1414 }
1415 return oob;
1416 }
1417 default:
1418 BUG();
1419 }
1420 return NULL;
1421}
1422
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001423#define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0
1424
1425/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001426 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001427 * @mtd: MTD device structure
1428 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001429 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001430 *
1431 * NAND write with ECC
1432 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001433static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1434 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001435{
1436 int chipnr, realpage, page, blockmask;
1437 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001438 uint32_t writelen = ops->len;
1439 uint8_t *oob = ops->oobbuf;
1440 uint8_t *buf = ops->datbuf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001441 int bytes = mtd->writesize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001442 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001443
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001444 ops->retlen = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001445
1446 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001447 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001448 printk(KERN_NOTICE "nand_write: "
1449 "Attempt to write not page aligned data\n");
1450 return -EINVAL;
1451 }
1452
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001453 if (!writelen)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001454 return 0;
1455
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001456 /* Check, if it is write protected */
1457 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001458 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001459
1460 chipnr = (int)(to >> chip->chip_shift);
1461 chip->select_chip(mtd, chipnr);
1462
1463 realpage = (int)(to >> chip->page_shift);
1464 page = realpage & chip->pagemask;
1465 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1466
1467 /* Invalidate the page cache, when we write to the cached page */
1468 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001469 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001470 chip->pagebuf = -1;
1471
1472 chip->oob_poi = chip->buffers.oobwbuf;
1473
1474 while(1) {
1475 int cached = writelen > bytes && page != blockmask;
1476
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001477 if (unlikely(oob))
1478 oob = nand_fill_oob(chip, oob, ops);
1479
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001480 ret = nand_write_page(mtd, chip, buf, page, cached);
1481 if (ret)
1482 break;
1483
1484 writelen -= bytes;
1485 if (!writelen)
1486 break;
1487
1488 buf += bytes;
1489 realpage++;
1490
1491 page = realpage & chip->pagemask;
1492 /* Check, if we cross a chip boundary */
1493 if (!page) {
1494 chipnr++;
1495 chip->select_chip(mtd, -1);
1496 chip->select_chip(mtd, chipnr);
1497 }
1498 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001499
1500 if (unlikely(oob))
1501 memset(chip->oob_poi, 0xff, mtd->oobsize);
1502
1503 ops->retlen = ops->len - writelen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001504 return ret;
1505}
1506
1507/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001508 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 * @mtd: MTD device structure
1510 * @to: offset to write to
1511 * @len: number of bytes to write
1512 * @retlen: pointer to variable to store the number of written bytes
1513 * @buf: the data to write
1514 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001515 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001517static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001518 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001520 struct nand_chip *chip = mtd->priv;
1521 int ret;
1522
1523 /* Do not allow reads past end of device */
1524 if ((to + len) > mtd->size)
1525 return -EINVAL;
1526 if (!len)
1527 return 0;
1528
1529 nand_get_device(chip, mtd, FL_READING);
1530
1531 chip->ops.len = len;
1532 chip->ops.datbuf = (uint8_t *)buf;
1533 chip->ops.oobbuf = NULL;
1534
1535 ret = nand_do_write_ops(mtd, to, &chip->ops);
1536
1537 nand_release_device(mtd);
1538
1539 *retlen = chip->ops.retlen;
1540 return ret;
1541}
1542
1543/**
1544 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1545 * @mtd: MTD device structure
1546 * @to: offset to write to
1547 * @ops: oob operation description structure
1548 *
1549 * NAND write out-of-band
1550 */
1551static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1552 struct mtd_oob_ops *ops)
1553{
1554 int chipnr, page, status;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001555 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001557 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001558 (unsigned int)to, (int)ops->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 /* Do not allow write past end of page */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001561 if ((ops->ooboffs + ops->len) > mtd->oobsize) {
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001562 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1563 "Attempt to write past end of page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return -EINVAL;
1565 }
1566
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001567 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001568 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001570 /* Shift to get page */
1571 page = (int)(to >> chip->page_shift);
1572
1573 /*
1574 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1575 * of my DiskOnChip 2000 test units) will clear the whole data page too
1576 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1577 * it in the doc2000 driver in August 1999. dwmw2.
1578 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001579 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* Check, if it is write protected */
1582 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001583 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001586 if (page == chip->pagebuf)
1587 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001589 if (ops->mode == MTD_OOB_AUTO || NAND_MUST_PAD(chip)) {
1590 chip->oob_poi = chip->buffers.oobwbuf;
1591 memset(chip->oob_poi, 0xff, mtd->oobsize);
1592 nand_fill_oob(chip, ops->oobbuf, ops);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001593 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize,
1594 page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001595 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1596 memset(chip->oob_poi, 0xff, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 } else {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001598 chip->cmdfunc(mtd, NAND_CMD_SEQIN,
1599 mtd->writesize + ops->ooboffs,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001600 page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001601 chip->write_buf(mtd, ops->oobbuf, ops->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /* Send command to program the OOB data */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001605 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001607 status = chip->waitfunc(mtd, chip, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 /* See if device thinks it succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00001610 if (status & NAND_STATUS_FAIL) {
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001611 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1612 "Failed write, page 0x%08x\n", page);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001613 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001615 ops->retlen = ops->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001618 if (ops->mode != MTD_OOB_AUTO) {
1619 /* Send command to read back the data */
1620 chip->cmdfunc(mtd, NAND_CMD_READOOB, ops->ooboffs,
1621 page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001623 if (chip->verify_buf(mtd, ops->oobbuf, ops->len)) {
1624 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1625 "Failed write verify, page 0x%08x\n", page);
1626 return -EIO;
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629#endif
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001630 return 0;
1631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001633/**
1634 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1635 * @mtd: MTD device structure
1636 * @from: offset to read from
1637 * @ops: oob operation description structure
1638 */
1639static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1640 struct mtd_oob_ops *ops)
1641{
1642 void (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
1643 const uint8_t *buf) = NULL;
1644 struct nand_chip *chip = mtd->priv;
1645 int ret = -ENOTSUPP;
1646
1647 ops->retlen = 0;
1648
1649 /* Do not allow writes past end of device */
1650 if ((to + ops->len) > mtd->size) {
1651 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1652 "Attempt read beyond end of device\n");
1653 return -EINVAL;
1654 }
1655
1656 nand_get_device(chip, mtd, FL_READING);
1657
1658 switch(ops->mode) {
1659 case MTD_OOB_PLACE:
1660 case MTD_OOB_AUTO:
1661 break;
1662
1663 case MTD_OOB_RAW:
1664 /* Replace the write_page algorithm temporary */
1665 write_page = chip->ecc.write_page;
1666 chip->ecc.write_page = nand_write_page_raw;
1667 break;
1668
1669 default:
1670 goto out;
1671 }
1672
1673 if (!ops->datbuf)
1674 ret = nand_do_write_oob(mtd, to, ops);
1675 else
1676 ret = nand_do_write_ops(mtd, to, ops);
1677
1678 if (unlikely(ops->mode == MTD_OOB_RAW))
1679 chip->ecc.write_page = write_page;
1680 out:
1681 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return ret;
1683}
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1687 * @mtd: MTD device structure
1688 * @page: the page address of the block which will be erased
1689 *
1690 * Standard erase command for NAND chips
1691 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001692static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001694 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001696 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1697 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
1700/**
1701 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1702 * @mtd: MTD device structure
1703 * @page: the page address of the block which will be erased
1704 *
1705 * AND multi block erase command function
1706 * Erase 4 consecutive blocks
1707 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001708static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001710 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001712 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1713 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1714 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1715 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1716 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719/**
1720 * nand_erase - [MTD Interface] erase block(s)
1721 * @mtd: MTD device structure
1722 * @instr: erase instruction
1723 *
1724 * Erase one ore more blocks
1725 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001726static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
David Woodhousee0c7d762006-05-13 18:07:53 +01001728 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001730
David A. Marlin30f464b2005-01-17 18:35:25 +00001731#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001733 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * @mtd: MTD device structure
1735 * @instr: erase instruction
1736 * @allowbbt: allow erasing the bbt area
1737 *
1738 * Erase one ore more blocks
1739 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001740int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
1741 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
1743 int page, len, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001744 struct nand_chip *chip = mtd->priv;
1745 int rewrite_bbt[NAND_MAX_CHIPS]={0};
1746 unsigned int bbt_masked_page = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001748 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
1749 (unsigned int)instr->addr, (unsigned int)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001752 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001753 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return -EINVAL;
1755 }
1756
1757 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001758 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
1759 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1760 "Length not block aligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return -EINVAL;
1762 }
1763
1764 /* Do not allow erase past end of device */
1765 if ((instr->len + instr->addr) > mtd->size) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001766 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1767 "Erase past end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return -EINVAL;
1769 }
1770
1771 instr->fail_addr = 0xffffffff;
1772
1773 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001774 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001777 page = (int)(instr->addr >> chip->page_shift);
1778 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001781 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001784 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /* Check, if it is write protected */
1787 if (nand_check_wp(mtd)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001788 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1789 "Device is write protected!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 instr->state = MTD_ERASE_FAILED;
1791 goto erase_exit;
1792 }
1793
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001794 /*
1795 * If BBT requires refresh, set the BBT page mask to see if the BBT
1796 * should be rewritten. Otherwise the mask is set to 0xffffffff which
1797 * can not be matched. This is also done when the bbt is actually
1798 * erased to avoid recusrsive updates
1799 */
1800 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
1801 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /* Loop through the pages */
1804 len = instr->len;
1805
1806 instr->state = MTD_ERASING;
1807
1808 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001809 /*
1810 * heck if we have a bad block, we do not erase bad blocks !
1811 */
1812 if (nand_block_checkbad(mtd, ((loff_t) page) <<
1813 chip->page_shift, 0, allowbbt)) {
1814 printk(KERN_WARNING "nand_erase: attempt to erase a "
1815 "bad block at page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 instr->state = MTD_ERASE_FAILED;
1817 goto erase_exit;
1818 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001819
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001820 /*
1821 * Invalidate the page cache, if we erase the block which
1822 * contains the current cached page
1823 */
1824 if (page <= chip->pagebuf && chip->pagebuf <
1825 (page + pages_per_block))
1826 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001828 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001829
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001830 status = chip->waitfunc(mtd, chip, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001832 /*
1833 * See if operation failed and additional status checks are
1834 * available
1835 */
1836 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1837 status = chip->errstat(mtd, chip, FL_ERASING,
1838 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00001841 if (status & NAND_STATUS_FAIL) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001842 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1843 "Failed erase, page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 instr->state = MTD_ERASE_FAILED;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001845 instr->fail_addr = (page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto erase_exit;
1847 }
David A. Marlin30f464b2005-01-17 18:35:25 +00001848
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001849 /*
1850 * If BBT requires refresh, set the BBT rewrite flag to the
1851 * page being erased
1852 */
1853 if (bbt_masked_page != 0xffffffff &&
1854 (page & BBT_PAGE_MASK) == bbt_masked_page)
1855 rewrite_bbt[chipnr] = (page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001858 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 page += pages_per_block;
1860
1861 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001862 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001864 chip->select_chip(mtd, -1);
1865 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00001866
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001867 /*
1868 * If BBT requires refresh and BBT-PERCHIP, set the BBT
1869 * page mask to see if this BBT should be rewritten
1870 */
1871 if (bbt_masked_page != 0xffffffff &&
1872 (chip->bbt_td->options & NAND_BBT_PERCHIP))
1873 bbt_masked_page = chip->bbt_td->pages[chipnr] &
1874 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
1876 }
1877 instr->state = MTD_ERASE_DONE;
1878
David Woodhousee0c7d762006-05-13 18:07:53 +01001879 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1882 /* Do call back function */
1883 if (!ret)
1884 mtd_erase_callback(instr);
1885
1886 /* Deselect and wake up anyone waiting on the device */
1887 nand_release_device(mtd);
1888
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001889 /*
1890 * If BBT requires refresh and erase was successful, rewrite any
1891 * selected bad block tables
1892 */
1893 if (bbt_masked_page == 0xffffffff || ret)
1894 return ret;
1895
1896 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
1897 if (!rewrite_bbt[chipnr])
1898 continue;
1899 /* update the BBT for chip */
1900 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
1901 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
1902 chip->bbt_td->pages[chipnr]);
1903 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00001904 }
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* Return more or less happy */
1907 return ret;
1908}
1909
1910/**
1911 * nand_sync - [MTD Interface] sync
1912 * @mtd: MTD device structure
1913 *
1914 * Sync is actually a wait for chip ready function
1915 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001916static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001918 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
David Woodhousee0c7d762006-05-13 18:07:53 +01001920 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001923 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01001925 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001929 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 * @mtd: MTD device structure
1931 * @ofs: offset relative to mtd start
1932 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001933static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001936 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001938
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001939 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
1942/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001943 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 * @mtd: MTD device structure
1945 * @ofs: offset relative to mtd start
1946 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001947static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001949 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 int ret;
1951
David Woodhousee0c7d762006-05-13 18:07:53 +01001952 if ((ret = nand_block_isbad(mtd, ofs))) {
1953 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (ret > 0)
1955 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01001956 return ret;
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001959 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962/**
Vitaly Wool962034f2005-09-15 14:58:53 +01001963 * nand_suspend - [MTD Interface] Suspend the NAND flash
1964 * @mtd: MTD device structure
1965 */
1966static int nand_suspend(struct mtd_info *mtd)
1967{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001968 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01001969
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001970 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01001971}
1972
1973/**
1974 * nand_resume - [MTD Interface] Resume the NAND flash
1975 * @mtd: MTD device structure
1976 */
1977static void nand_resume(struct mtd_info *mtd)
1978{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001979 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01001980
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001981 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01001982 nand_release_device(mtd);
1983 else
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001984 printk(KERN_ERR "nand_resume() called for a chip which is not "
1985 "in suspended state\n");
Vitaly Wool962034f2005-09-15 14:58:53 +01001986}
1987
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001988/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02001989 * Set default functions
1990 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001991static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02001992{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001994 if (!chip->chip_delay)
1995 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001998 if (chip->cmdfunc == NULL)
1999 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002002 if (chip->waitfunc == NULL)
2003 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002005 if (!chip->select_chip)
2006 chip->select_chip = nand_select_chip;
2007 if (!chip->read_byte)
2008 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2009 if (!chip->read_word)
2010 chip->read_word = nand_read_word;
2011 if (!chip->block_bad)
2012 chip->block_bad = nand_block_bad;
2013 if (!chip->block_markbad)
2014 chip->block_markbad = nand_default_block_markbad;
2015 if (!chip->write_buf)
2016 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2017 if (!chip->read_buf)
2018 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2019 if (!chip->verify_buf)
2020 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2021 if (!chip->scan_bbt)
2022 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002023
2024 if (!chip->controller) {
2025 chip->controller = &chip->hwcontrol;
2026 spin_lock_init(&chip->controller->lock);
2027 init_waitqueue_head(&chip->controller->wq);
2028 }
2029
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002030}
2031
2032/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002033 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002034 */
2035static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002036 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002037 int busw, int *maf_id)
2038{
2039 struct nand_flash_dev *type = NULL;
2040 int i, dev_id, maf_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002043 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002046 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002049 *maf_id = chip->read_byte(mtd);
2050 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002052 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002054 if (dev_id == nand_flash_ids[i].id) {
2055 type = &nand_flash_ids[i];
2056 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002060 if (!type)
2061 return ERR_PTR(-ENODEV);
2062
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002063 if (!mtd->name)
2064 mtd->name = type->name;
2065
2066 chip->chipsize = type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002067
2068 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002069 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002070 int extid;
2071 /* The 3rd id byte contains non relevant data ATM */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002072 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002073 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002074 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002075 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002076 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002077 extid >>= 2;
2078 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002079 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002080 extid >>= 2;
2081 /* Calc blocksize. Blocksize is multiples of 64KiB */
2082 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2083 extid >>= 2;
2084 /* Get buswidth information */
2085 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2086
2087 } else {
2088 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002089 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002090 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002091 mtd->erasesize = type->erasesize;
2092 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002093 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002094 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002095 }
2096
2097 /* Try to identify manufacturer */
2098 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_id++) {
2099 if (nand_manuf_ids[maf_idx].id == *maf_id)
2100 break;
2101 }
2102
2103 /*
2104 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002105 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002106 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002107 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002108 printk(KERN_INFO "NAND device: Manufacturer ID:"
2109 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2110 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2111 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002112 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002113 busw ? 16 : 8);
2114 return ERR_PTR(-EINVAL);
2115 }
2116
2117 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002118 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002119 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002120 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002121
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002122 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002123 ffs(mtd->erasesize) - 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002124 chip->chip_shift = ffs(chip->chipsize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002125
2126 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002127 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002128 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2129
2130 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002131 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002132 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002133
2134 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002135 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002136 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002137 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002138
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002139 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002140 * options for chips which are not having an extended id.
2141 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002142 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002143 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002144
2145 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002146 if (chip->options & NAND_4PAGE_ARRAY)
2147 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002148 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002149 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002150
2151 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002152 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2153 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002154
2155 printk(KERN_INFO "NAND device: Manufacturer ID:"
2156 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2157 nand_manuf_ids[maf_idx].name, type->name);
2158
2159 return type;
2160}
2161
2162/* module_text_address() isn't exported, and it's mostly a pointless
2163 test if this is a module _anyway_ -- they'd have to try _really_ hard
2164 to call us from in-kernel code if the core NAND support is modular. */
2165#ifdef MODULE
2166#define caller_is_module() (1)
2167#else
2168#define caller_is_module() \
2169 module_text_address((unsigned long)__builtin_return_address(0))
2170#endif
2171
2172/**
2173 * nand_scan - [NAND Interface] Scan for the NAND device
2174 * @mtd: MTD device structure
2175 * @maxchips: Number of chips to scan for
2176 *
2177 * This fills out all the uninitialized function pointers
2178 * with the defaults.
2179 * The flash ID is read and the mtd/chip structures are
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002180 * filled with the appropriate values.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002181 * The mtd->owner field must be set to the module of the caller
2182 *
2183 */
2184int nand_scan(struct mtd_info *mtd, int maxchips)
2185{
2186 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002187 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002188 struct nand_flash_dev *type;
2189
2190 /* Many callers got this wrong, so check for it for a while... */
2191 if (!mtd->owner && caller_is_module()) {
2192 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2193 BUG();
2194 }
2195
2196 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002197 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002198 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002199 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002200
2201 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002202 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002203
2204 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002205 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002206 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002207 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002210 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002211 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002212 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002214 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002216 if (nand_maf_id != chip->read_byte(mtd) ||
2217 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 break;
2219 }
2220 if (i > 1)
2221 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002224 chip->numchips = i;
2225 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002227 /* Preset the internal oob write buffer */
2228 memset(chip->buffers.oobwbuf, 0xff, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002229
2230 /*
2231 * If no default placement scheme is given, select an appropriate one
2232 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002233 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002234 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002236 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 break;
2238 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002239 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 break;
2241 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002242 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 break;
2244 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002245 printk(KERN_WARNING "No oob scheme defined for "
2246 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 BUG();
2248 }
2249 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002250
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002251 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002252 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2253 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002254 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002255 switch (chip->ecc.mode) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002256 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002257 /* Use standard hwecc read page function ? */
2258 if (!chip->ecc.read_page)
2259 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002260 if (!chip->ecc.write_page)
2261 chip->ecc.write_page = nand_write_page_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002262
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002263 case NAND_ECC_HW_SYNDROME:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002264 if (!chip->ecc.calculate || !chip->ecc.correct ||
2265 !chip->ecc.hwctl) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002266 printk(KERN_WARNING "No ECC functions supplied, "
2267 "Hardware ECC not possible\n");
2268 BUG();
2269 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002270 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002271 if (!chip->ecc.read_page)
2272 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002273 if (!chip->ecc.write_page)
2274 chip->ecc.write_page = nand_write_page_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002275
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002276 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002277 break;
2278 printk(KERN_WARNING "%d byte HW ECC not possible on "
2279 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002280 chip->ecc.size, mtd->writesize);
2281 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002283 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002284 chip->ecc.calculate = nand_calculate_ecc;
2285 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002286 chip->ecc.read_page = nand_read_page_swecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002287 chip->ecc.write_page = nand_write_page_swecc;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002288 chip->ecc.size = 256;
2289 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002291
2292 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002293 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2294 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002295 chip->ecc.read_page = nand_read_page_raw;
2296 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002297 chip->ecc.size = mtd->writesize;
2298 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002301 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002302 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002303 BUG();
2304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002306 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002307 * The number of bytes available for a client to place data into
2308 * the out of band area
2309 */
2310 chip->ecc.layout->oobavail = 0;
2311 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2312 chip->ecc.layout->oobavail +=
2313 chip->ecc.layout->oobfree[i].length;
2314
2315 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002316 * Set the number of read / write steps for one page depending on ECC
2317 * mode
2318 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002319 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2320 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002321 printk(KERN_WARNING "Invalid ecc parameters\n");
2322 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002324 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002325
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002326 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002327 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002330 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002333 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 /* Fill in remaining MTD driver data */
2336 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002337 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 mtd->ecctype = MTD_ECC_SW;
2339 mtd->erase = nand_erase;
2340 mtd->point = NULL;
2341 mtd->unpoint = NULL;
2342 mtd->read = nand_read;
2343 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 mtd->read_oob = nand_read_oob;
2345 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 mtd->sync = nand_sync;
2347 mtd->lock = NULL;
2348 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002349 mtd->suspend = nand_suspend;
2350 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 mtd->block_isbad = nand_block_isbad;
2352 mtd->block_markbad = nand_block_markbad;
2353
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002354 /* propagate ecc.layout to mtd_info */
2355 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002357 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002358 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002362 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002366 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 * @mtd: MTD device structure
2368*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002369void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002371 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373#ifdef CONFIG_MTD_PARTITIONS
2374 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002375 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376#endif
2377 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002378 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Jesper Juhlfa671642005-11-07 01:01:27 -08002380 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002381 kfree(chip->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
David Woodhousee0c7d762006-05-13 18:07:53 +01002384EXPORT_SYMBOL_GPL(nand_scan);
2385EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002386
2387static int __init nand_base_init(void)
2388{
2389 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2390 return 0;
2391}
2392
2393static void __exit nand_base_exit(void)
2394{
2395 led_trigger_unregister_simple(nand_led_trigger);
2396}
2397
2398module_init(nand_base_init);
2399module_exit(nand_base_exit);
2400
David Woodhousee0c7d762006-05-13 18:07:53 +01002401MODULE_LICENSE("GPL");
2402MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2403MODULE_DESCRIPTION("Generic NAND flash driver code");