blob: 5dcb2e066ce71f3a5b8bb2f2ad37f9e7422f2620 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020015 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000016 * David Woodhouse for adding multichip support
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020021 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
27 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License version 2 as
30 * published by the Free Software Foundation.
31 *
32 */
33
David Woodhouse552d9202006-05-14 01:20:46 +010034#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/delay.h>
36#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020037#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sched.h>
39#include <linux/slab.h>
40#include <linux/types.h>
41#include <linux/mtd/mtd.h>
42#include <linux/mtd/nand.h>
43#include <linux/mtd/nand_ecc.h>
44#include <linux/mtd/compatmac.h>
45#include <linux/interrupt.h>
46#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080047#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/io.h>
49
50#ifdef CONFIG_MTD_PARTITIONS
51#include <linux/mtd/partitions.h>
52#endif
53
54/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020055static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .eccbytes = 3,
57 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020058 .oobfree = {
59 {.offset = 3,
60 .length = 2},
61 {.offset = 6,
62 .length = 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020065static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .eccbytes = 6,
67 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020068 .oobfree = {
69 {.offset = 8,
70 . length = 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020073static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .eccbytes = 24,
75 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010076 40, 41, 42, 43, 44, 45, 46, 47,
77 48, 49, 50, 51, 52, 53, 54, 55,
78 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020079 .oobfree = {
80 {.offset = 2,
81 .length = 38}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020084static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +020085 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020087static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
88 struct mtd_oob_ops *ops);
89
Thomas Gleixnerd470a972006-05-23 23:48:57 +020090/*
91 * For devices which display every fart in the system on a seperate LED. Is
92 * compiled away when LED support is disabled.
93 */
94DEFINE_LED_TRIGGER(nand_led_trigger);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/**
97 * nand_release_device - [GENERIC] release chip
98 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000099 *
100 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100102static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200104 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200107 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100108
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200109 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200110 spin_lock(&chip->controller->lock);
111 chip->controller->active = NULL;
112 chip->state = FL_READY;
113 wake_up(&chip->controller->wq);
114 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/**
118 * nand_read_byte - [DEFAULT] read one byte from the chip
119 * @mtd: MTD device structure
120 *
121 * Default read function for 8bit buswith
122 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200123static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200125 struct nand_chip *chip = mtd->priv;
126 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
131 * @mtd: MTD device structure
132 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000133 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * endianess conversion
135 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200136static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200138 struct nand_chip *chip = mtd->priv;
139 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * nand_read_word - [DEFAULT] read one word from the chip
144 * @mtd: MTD device structure
145 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000146 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * endianess conversion
148 */
149static u16 nand_read_word(struct mtd_info *mtd)
150{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200151 struct nand_chip *chip = mtd->priv;
152 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * nand_select_chip - [DEFAULT] control CE line
157 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700158 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * Default select function for 1 chip devices.
161 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200162static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200164 struct nand_chip *chip = mtd->priv;
165
166 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200168 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
172
173 default:
174 BUG();
175 }
176}
177
178/**
179 * nand_write_buf - [DEFAULT] write buffer to chip
180 * @mtd: MTD device structure
181 * @buf: data buffer
182 * @len: number of bytes to write
183 *
184 * Default write function for 8bit buswith
185 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200186static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200189 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David Woodhousee0c7d762006-05-13 18:07:53 +0100191 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200192 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000196 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * @mtd: MTD device structure
198 * @buf: buffer to store date
199 * @len: number of bytes to read
200 *
201 * Default read function for 8bit buswith
202 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200203static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200206 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
David Woodhousee0c7d762006-05-13 18:07:53 +0100208 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200209 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000213 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * @mtd: MTD device structure
215 * @buf: buffer containing the data to compare
216 * @len: number of bytes to compare
217 *
218 * Default verify function for 8bit buswith
219 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200220static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200223 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
David Woodhousee0c7d762006-05-13 18:07:53 +0100225 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200226 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
229}
230
231/**
232 * nand_write_buf16 - [DEFAULT] write buffer to chip
233 * @mtd: MTD device structure
234 * @buf: data buffer
235 * @len: number of bytes to write
236 *
237 * Default write function for 16bit buswith
238 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200239static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200242 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 u16 *p = (u16 *) buf;
244 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000245
David Woodhousee0c7d762006-05-13 18:07:53 +0100246 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200247 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000252 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * @mtd: MTD device structure
254 * @buf: buffer to store date
255 * @len: number of bytes to read
256 *
257 * Default read function for 16bit buswith
258 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200259static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 u16 *p = (u16 *) buf;
264 len >>= 1;
265
David Woodhousee0c7d762006-05-13 18:07:53 +0100266 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200267 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000271 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * @mtd: MTD device structure
273 * @buf: buffer containing the data to compare
274 * @len: number of bytes to compare
275 *
276 * Default verify function for 16bit buswith
277 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200278static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200281 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 u16 *p = (u16 *) buf;
283 len >>= 1;
284
David Woodhousee0c7d762006-05-13 18:07:53 +0100285 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200286 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EFAULT;
288
289 return 0;
290}
291
292/**
293 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
294 * @mtd: MTD device structure
295 * @ofs: offset from device start
296 * @getchip: 0, if the chip is already selected
297 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000298 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
300static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
301{
302 int page, chipnr, res = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200303 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 u16 bad;
305
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 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200350 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* Get block number */
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)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200359 ret = nand_update_bbt(mtd, ofs);
360 else {
361 /* We write two bytes, so we dont have to mess with 16 bit
362 * access
363 */
364 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200365 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200366 chip->ops.datbuf = NULL;
367 chip->ops.oobbuf = buf;
368 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000369
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200370 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
371 }
372 if (!ret)
373 mtd->ecc_stats.badblocks++;
374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000377/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * nand_check_wp - [GENERIC] check if the chip is write protected
379 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000380 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000382 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100384static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200386 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200388 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
389 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392/**
393 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
394 * @mtd: MTD device structure
395 * @ofs: offset from device start
396 * @getchip: 0, if the chip is already selected
397 * @allowbbt: 1, if its allowed to access the bbt area
398 *
399 * Check, if the block is bad. Either by reading the bad block table or
400 * calling of the scan function.
401 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200402static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
403 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200405 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000406
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200407 if (!chip->bbt)
408 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100411 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000414/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000415 * Wait for the ready pin, after a command
416 * The timeout is catched later.
417 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100418void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000419{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200420 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100421 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000422
Richard Purdie8fe833c2006-03-31 02:31:14 -0800423 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000424 /* wait until command is processed or timeout occures */
425 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200426 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800427 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700428 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000429 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800430 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000431}
David Woodhouse4b648b02006-09-25 17:05:24 +0100432EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/**
435 * nand_command - [DEFAULT] Send command to NAND device
436 * @mtd: MTD device structure
437 * @command: the command to be sent
438 * @column: the column address for this command, -1 if none
439 * @page_addr: the page address for this command, -1 if none
440 *
441 * Send command to NAND device. This function is used for small page
442 * devices (256/512 Bytes per page)
443 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200444static void nand_command(struct mtd_info *mtd, unsigned int command,
445 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200447 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200448 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /*
451 * Write out the command to the device.
452 */
453 if (command == NAND_CMD_SEQIN) {
454 int readcmd;
455
Joern Engel28318772006-05-22 23:18:05 +0200456 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200458 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 readcmd = NAND_CMD_READOOB;
460 } else if (column < 256) {
461 /* First 256 bytes --> READ0 */
462 readcmd = NAND_CMD_READ0;
463 } else {
464 column -= 256;
465 readcmd = NAND_CMD_READ1;
466 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200467 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200468 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200470 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200472 /*
473 * Address cycle, when necessary
474 */
475 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
476 /* Serially input address */
477 if (column != -1) {
478 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200479 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200480 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200481 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200482 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200484 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200485 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200486 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200487 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200488 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200489 if (chip->chipsize > (32 << 20))
490 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200491 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200492 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000493
494 /*
495 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 case NAND_CMD_PAGEPROG:
501 case NAND_CMD_ERASE1:
502 case NAND_CMD_ERASE2:
503 case NAND_CMD_SEQIN:
504 case NAND_CMD_STATUS:
505 return;
506
507 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200508 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200510 udelay(chip->chip_delay);
511 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200512 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200513 chip->cmd_ctrl(mtd,
514 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200515 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return;
517
David Woodhousee0c7d762006-05-13 18:07:53 +0100518 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000520 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * If we don't have access to the busy pin, we apply the given
522 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100523 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200524 if (!chip->dev_ready) {
525 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Apply this short delay always to ensure that we do wait tWB in
530 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100531 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000532
533 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/**
537 * nand_command_lp - [DEFAULT] Send command to NAND large page device
538 * @mtd: MTD device structure
539 * @command: the command to be sent
540 * @column: the column address for this command, -1 if none
541 * @page_addr: the page address for this command, -1 if none
542 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200543 * Send command to NAND device. This is the version for the new large page
544 * devices We dont have the separate regions as we have in the small page
545 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200547static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
548 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200550 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* Emulate NAND_CMD_READOOB */
553 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200554 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 command = NAND_CMD_READ0;
556 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000557
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200558 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200559 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200560 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200563 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* Serially input address */
566 if (column != -1) {
567 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200568 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200570 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200571 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200572 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200575 chip->cmd_ctrl(mtd, page_addr, ctrl);
576 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200577 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200579 if (chip->chipsize > (128 << 20))
580 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200581 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200584 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000585
586 /*
587 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000588 * status, sequential in, and deplete1 need no delay
589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 case NAND_CMD_CACHEDPROG:
593 case NAND_CMD_PAGEPROG:
594 case NAND_CMD_ERASE1:
595 case NAND_CMD_ERASE2:
596 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200597 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000599 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return;
601
David Woodhousee0c7d762006-05-13 18:07:53 +0100602 /*
603 * read error status commands require only a short delay
604 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000605 case NAND_CMD_STATUS_ERROR:
606 case NAND_CMD_STATUS_ERROR0:
607 case NAND_CMD_STATUS_ERROR1:
608 case NAND_CMD_STATUS_ERROR2:
609 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200610 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200614 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200616 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200617 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
618 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
619 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
620 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200621 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return;
623
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200624 case NAND_CMD_RNDOUT:
625 /* No ready / busy check necessary */
626 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
627 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
628 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
629 NAND_NCE | NAND_CTRL_CHANGE);
630 return;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200633 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
634 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
635 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
636 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000637
David Woodhousee0c7d762006-05-13 18:07:53 +0100638 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000640 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * If we don't have access to the busy pin, we apply the given
642 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100643 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 if (!chip->dev_ready) {
645 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* Apply this short delay always to ensure that we do wait tWB in
651 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100652 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000653
654 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657/**
658 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700659 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000661 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *
663 * Get the device and lock it for exclusive access
664 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200665static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200666nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200668 spinlock_t *lock = &chip->controller->lock;
669 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100670 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100671 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100672 spin_lock(lock);
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* Hardware controller shared among independend devices */
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200675 /* Hardware controller shared among independend devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200676 if (!chip->controller->active)
677 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200678
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200679 if (chip->controller->active == chip && chip->state == FL_READY) {
680 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100681 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100682 return 0;
683 }
684 if (new_state == FL_PM_SUSPENDED) {
685 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200686 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100687 }
688 set_current_state(TASK_UNINTERRUPTIBLE);
689 add_wait_queue(wq, &wait);
690 spin_unlock(lock);
691 schedule();
692 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 goto retry;
694}
695
696/**
697 * nand_wait - [DEFAULT] wait until the command is done
698 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700699 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
701 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000702 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700704 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200705static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707
David Woodhousee0c7d762006-05-13 18:07:53 +0100708 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200709 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100712 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100714 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Richard Purdie8fe833c2006-03-31 02:31:14 -0800716 led_trigger_event(nand_led_trigger, LED_FULL);
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* Apply this short delay always to ensure that we do wait tWB in
719 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100720 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200722 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
723 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000724 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200725 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000727 while (time_before(jiffies, timeo)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200728 if (chip->dev_ready) {
729 if (chip->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000730 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } else {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200732 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
734 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000735 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800737 led_trigger_event(nand_led_trigger, LED_OFF);
738
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200739 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return status;
741}
742
743/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200744 * nand_read_page_raw - [Intern] read raw page data without ecc
745 * @mtd: mtd info structure
746 * @chip: nand chip info structure
747 * @buf: buffer to store read data
748 */
749static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
750 uint8_t *buf)
751{
752 chip->read_buf(mtd, buf, mtd->writesize);
753 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
754 return 0;
755}
756
757/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300758 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200759 * @mtd: mtd info structure
760 * @chip: nand chip info structure
761 * @buf: buffer to store read data
David A. Marlin068e3c02005-01-24 03:07:46 +0000762 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200763static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
764 uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200766 int i, eccsize = chip->ecc.size;
767 int eccbytes = chip->ecc.bytes;
768 int eccsteps = chip->ecc.steps;
769 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100770 uint8_t *ecc_calc = chip->buffers->ecccalc;
771 uint8_t *ecc_code = chip->buffers->ecccode;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200772 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200773
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200774 nand_read_page_raw(mtd, chip, buf);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200775
776 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
777 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
778
779 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200780 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200781
782 eccsteps = chip->ecc.steps;
783 p = buf;
784
785 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
786 int stat;
787
788 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
789 if (stat == -1)
790 mtd->ecc_stats.failed++;
791 else
792 mtd->ecc_stats.corrected += stat;
793 }
794 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100795}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300798 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200799 * @mtd: mtd info structure
800 * @chip: nand chip info structure
801 * @buf: buffer to store read data
802 *
803 * Not for syndrome calculating ecc controllers which need a special oob layout
804 */
805static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
806 uint8_t *buf)
807{
808 int i, eccsize = chip->ecc.size;
809 int eccbytes = chip->ecc.bytes;
810 int eccsteps = chip->ecc.steps;
811 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100812 uint8_t *ecc_calc = chip->buffers->ecccalc;
813 uint8_t *ecc_code = chip->buffers->ecccode;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200814 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200815
816 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
817 chip->ecc.hwctl(mtd, NAND_ECC_READ);
818 chip->read_buf(mtd, p, eccsize);
819 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
820 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200821 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200822
823 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200824 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200825
826 eccsteps = chip->ecc.steps;
827 p = buf;
828
829 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
830 int stat;
831
832 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
833 if (stat == -1)
834 mtd->ecc_stats.failed++;
835 else
836 mtd->ecc_stats.corrected += stat;
837 }
838 return 0;
839}
840
841/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300842 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200843 * @mtd: mtd info structure
844 * @chip: nand chip info structure
845 * @buf: buffer to store read data
846 *
847 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200848 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200849 */
850static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
851 uint8_t *buf)
852{
853 int i, eccsize = chip->ecc.size;
854 int eccbytes = chip->ecc.bytes;
855 int eccsteps = chip->ecc.steps;
856 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200857 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200858
859 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
860 int stat;
861
862 chip->ecc.hwctl(mtd, NAND_ECC_READ);
863 chip->read_buf(mtd, p, eccsize);
864
865 if (chip->ecc.prepad) {
866 chip->read_buf(mtd, oob, chip->ecc.prepad);
867 oob += chip->ecc.prepad;
868 }
869
870 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
871 chip->read_buf(mtd, oob, eccbytes);
872 stat = chip->ecc.correct(mtd, p, oob, NULL);
873
874 if (stat == -1)
875 mtd->ecc_stats.failed++;
876 else
877 mtd->ecc_stats.corrected += stat;
878
879 oob += eccbytes;
880
881 if (chip->ecc.postpad) {
882 chip->read_buf(mtd, oob, chip->ecc.postpad);
883 oob += chip->ecc.postpad;
884 }
885 }
886
887 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +0400888 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200889 if (i)
890 chip->read_buf(mtd, oob, i);
891
892 return 0;
893}
894
895/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200896 * nand_transfer_oob - [Internal] Transfer oob to client buffer
897 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700898 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200899 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +0300900 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200901 */
902static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +0300903 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200904{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200905 switch(ops->mode) {
906
907 case MTD_OOB_PLACE:
908 case MTD_OOB_RAW:
909 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
910 return oob + len;
911
912 case MTD_OOB_AUTO: {
913 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200914 uint32_t boffs = 0, roffs = ops->ooboffs;
915 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200916
917 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200918 /* Read request not from offset 0 ? */
919 if (unlikely(roffs)) {
920 if (roffs >= free->length) {
921 roffs -= free->length;
922 continue;
923 }
924 boffs = free->offset + roffs;
925 bytes = min_t(size_t, len,
926 (free->length - roffs));
927 roffs = 0;
928 } else {
929 bytes = min_t(size_t, len, free->length);
930 boffs = free->offset;
931 }
932 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200933 oob += bytes;
934 }
935 return oob;
936 }
937 default:
938 BUG();
939 }
940 return NULL;
941}
942
943/**
944 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200945 *
David A. Marlin068e3c02005-01-24 03:07:46 +0000946 * @mtd: MTD device structure
947 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -0700948 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +0000949 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200950 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +0000951 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200952static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
953 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +0000954{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200955 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200956 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200957 struct mtd_ecc_stats stats;
958 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
959 int sndcmd = 1;
960 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200961 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +0300962 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200963 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200965 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200967 chipnr = (int)(from >> chip->chip_shift);
968 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200970 realpage = (int)(from >> chip->page_shift);
971 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200973 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200975 buf = ops->datbuf;
976 oob = ops->oobbuf;
977
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200978 while(1) {
979 bytes = min(mtd->writesize - col, readlen);
980 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000981
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200982 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200983 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100984 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200986 if (likely(sndcmd)) {
987 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
988 sndcmd = 0;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200991 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +0100992 if (unlikely(ops->mode == MTD_OOB_RAW))
993 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
994 else
995 ret = chip->ecc.read_page(mtd, chip, bufpoi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200996 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +0100997 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200998
999 /* Transfer not aligned data */
1000 if (!aligned) {
1001 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001002 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001004
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001005 buf += bytes;
1006
1007 if (unlikely(oob)) {
1008 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001009 if (ops->mode != MTD_OOB_RAW) {
1010 int toread = min(oobreadlen,
1011 chip->ecc.layout->oobavail);
1012 if (toread) {
1013 oob = nand_transfer_oob(chip,
1014 oob, ops, toread);
1015 oobreadlen -= toread;
1016 }
1017 } else
1018 buf = nand_transfer_oob(chip,
1019 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001020 }
1021
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001022 if (!(chip->options & NAND_NO_READRDY)) {
1023 /*
1024 * Apply delay or wait for ready/busy pin. Do
1025 * this before the AUTOINCR check, so no
1026 * problems arise if a chip which does auto
1027 * increment is marked as NOAUTOINCR by the
1028 * board driver.
1029 */
1030 if (!chip->dev_ready)
1031 udelay(chip->chip_delay);
1032 else
1033 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001035 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001036 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001037 buf += bytes;
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001040 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001041
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001042 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /* For subsequent reads align to page boundary. */
1046 col = 0;
1047 /* Increment page address */
1048 realpage++;
1049
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001050 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* Check, if we cross a chip boundary */
1052 if (!page) {
1053 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001054 chip->select_chip(mtd, -1);
1055 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001057
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001058 /* Check, if the chip supports auto page increment
1059 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001060 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001061 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001062 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001065 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001066 if (oob)
1067 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001069 if (ret)
1070 return ret;
1071
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001072 if (mtd->ecc_stats.failed - stats.failed)
1073 return -EBADMSG;
1074
1075 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001076}
1077
1078/**
1079 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1080 * @mtd: MTD device structure
1081 * @from: offset to read from
1082 * @len: number of bytes to read
1083 * @retlen: pointer to variable to store the number of read bytes
1084 * @buf: the databuffer to put data
1085 *
1086 * Get hold of the chip and call nand_do_read
1087 */
1088static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1089 size_t *retlen, uint8_t *buf)
1090{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001091 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001092 int ret;
1093
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001094 /* Do not allow reads past end of device */
1095 if ((from + len) > mtd->size)
1096 return -EINVAL;
1097 if (!len)
1098 return 0;
1099
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001100 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001101
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001102 chip->ops.len = len;
1103 chip->ops.datbuf = buf;
1104 chip->ops.oobbuf = NULL;
1105
1106 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001107
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001108 *retlen = chip->ops.retlen;
1109
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001110 nand_release_device(mtd);
1111
1112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001116 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1117 * @mtd: mtd info structure
1118 * @chip: nand chip info structure
1119 * @page: page number to read
1120 * @sndcmd: flag whether to issue read command or not
1121 */
1122static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1123 int page, int sndcmd)
1124{
1125 if (sndcmd) {
1126 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1127 sndcmd = 0;
1128 }
1129 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1130 return sndcmd;
1131}
1132
1133/**
1134 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1135 * with syndromes
1136 * @mtd: mtd info structure
1137 * @chip: nand chip info structure
1138 * @page: page number to read
1139 * @sndcmd: flag whether to issue read command or not
1140 */
1141static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1142 int page, int sndcmd)
1143{
1144 uint8_t *buf = chip->oob_poi;
1145 int length = mtd->oobsize;
1146 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1147 int eccsize = chip->ecc.size;
1148 uint8_t *bufpoi = buf;
1149 int i, toread, sndrnd = 0, pos;
1150
1151 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1152 for (i = 0; i < chip->ecc.steps; i++) {
1153 if (sndrnd) {
1154 pos = eccsize + i * (eccsize + chunk);
1155 if (mtd->writesize > 512)
1156 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1157 else
1158 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1159 } else
1160 sndrnd = 1;
1161 toread = min_t(int, length, chunk);
1162 chip->read_buf(mtd, bufpoi, toread);
1163 bufpoi += toread;
1164 length -= toread;
1165 }
1166 if (length > 0)
1167 chip->read_buf(mtd, bufpoi, length);
1168
1169 return 1;
1170}
1171
1172/**
1173 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1174 * @mtd: mtd info structure
1175 * @chip: nand chip info structure
1176 * @page: page number to write
1177 */
1178static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1179 int page)
1180{
1181 int status = 0;
1182 const uint8_t *buf = chip->oob_poi;
1183 int length = mtd->oobsize;
1184
1185 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1186 chip->write_buf(mtd, buf, length);
1187 /* Send command to program the OOB data */
1188 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1189
1190 status = chip->waitfunc(mtd, chip);
1191
Savin Zlobec0d420f92006-06-21 11:51:20 +02001192 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001193}
1194
1195/**
1196 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1197 * with syndrome - only for large page flash !
1198 * @mtd: mtd info structure
1199 * @chip: nand chip info structure
1200 * @page: page number to write
1201 */
1202static int nand_write_oob_syndrome(struct mtd_info *mtd,
1203 struct nand_chip *chip, int page)
1204{
1205 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1206 int eccsize = chip->ecc.size, length = mtd->oobsize;
1207 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1208 const uint8_t *bufpoi = chip->oob_poi;
1209
1210 /*
1211 * data-ecc-data-ecc ... ecc-oob
1212 * or
1213 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1214 */
1215 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1216 pos = steps * (eccsize + chunk);
1217 steps = 0;
1218 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001219 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001220
1221 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1222 for (i = 0; i < steps; i++) {
1223 if (sndcmd) {
1224 if (mtd->writesize <= 512) {
1225 uint32_t fill = 0xFFFFFFFF;
1226
1227 len = eccsize;
1228 while (len > 0) {
1229 int num = min_t(int, len, 4);
1230 chip->write_buf(mtd, (uint8_t *)&fill,
1231 num);
1232 len -= num;
1233 }
1234 } else {
1235 pos = eccsize + i * (eccsize + chunk);
1236 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1237 }
1238 } else
1239 sndcmd = 1;
1240 len = min_t(int, length, chunk);
1241 chip->write_buf(mtd, bufpoi, len);
1242 bufpoi += len;
1243 length -= len;
1244 }
1245 if (length > 0)
1246 chip->write_buf(mtd, bufpoi, length);
1247
1248 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1249 status = chip->waitfunc(mtd, chip);
1250
1251 return status & NAND_STATUS_FAIL ? -EIO : 0;
1252}
1253
1254/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001255 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 * @mtd: MTD device structure
1257 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001258 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 *
1260 * NAND read out-of-band data from the spare area
1261 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001262static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1263 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001265 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001266 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001267 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001268 int readlen = ops->ooblen;
1269 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001270 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Andrew Morton7e9a0bb2006-05-30 09:06:41 +01001272 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1273 (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Vitaly Wool70145682006-11-03 18:20:38 +03001275 if (ops->mode == MTD_OOB_RAW)
1276 len = mtd->oobsize;
1277 else
1278 len = chip->ecc.layout->oobavail;
1279
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001280 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001281 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001283 /* Shift to get page */
1284 realpage = (int)(from >> chip->page_shift);
1285 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001287 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001288 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001289
1290 len = min(len, readlen);
1291 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001292
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001293 if (!(chip->options & NAND_NO_READRDY)) {
1294 /*
1295 * Apply delay or wait for ready/busy pin. Do this
1296 * before the AUTOINCR check, so no problems arise if a
1297 * chip which does auto increment is marked as
1298 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001299 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001300 if (!chip->dev_ready)
1301 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001302 else
1303 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001305
Vitaly Wool70145682006-11-03 18:20:38 +03001306 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001307 if (!readlen)
1308 break;
1309
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001310 /* Increment page address */
1311 realpage++;
1312
1313 page = realpage & chip->pagemask;
1314 /* Check, if we cross a chip boundary */
1315 if (!page) {
1316 chipnr++;
1317 chip->select_chip(mtd, -1);
1318 chip->select_chip(mtd, chipnr);
1319 }
1320
1321 /* Check, if the chip supports auto page increment
1322 * or if we have hit a block boundary.
1323 */
1324 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1325 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
Vitaly Wool70145682006-11-03 18:20:38 +03001328 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
1330}
1331
1332/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001333 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001336 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001338 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001340static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1341 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001343 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001344 int ret = -ENOTSUPP;
1345
1346 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001349 if (ops->datbuf && (from + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001350 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001351 "Attempt read beyond end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -EINVAL;
1353 }
1354
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001355 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001357 switch(ops->mode) {
1358 case MTD_OOB_PLACE:
1359 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001360 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001361 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001362
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001363 default:
1364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001367 if (!ops->datbuf)
1368 ret = nand_do_read_oob(mtd, from, ops);
1369 else
1370 ret = nand_do_read_ops(mtd, from, ops);
1371
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001372 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001374 return ret;
1375}
1376
1377
1378/**
1379 * nand_write_page_raw - [Intern] raw page write function
1380 * @mtd: mtd info structure
1381 * @chip: nand chip info structure
1382 * @buf: data buffer
1383 */
1384static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1385 const uint8_t *buf)
1386{
1387 chip->write_buf(mtd, buf, mtd->writesize);
1388 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001391/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001392 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001393 * @mtd: mtd info structure
1394 * @chip: nand chip info structure
1395 * @buf: data buffer
1396 */
1397static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1398 const uint8_t *buf)
1399{
1400 int i, eccsize = chip->ecc.size;
1401 int eccbytes = chip->ecc.bytes;
1402 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001403 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001404 const uint8_t *p = buf;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001405 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001406
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001407 /* Software ecc calculation */
1408 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1409 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001410
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001411 for (i = 0; i < chip->ecc.total; i++)
1412 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001413
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001414 nand_write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001415}
1416
1417/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001418 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001419 * @mtd: mtd info structure
1420 * @chip: nand chip info structure
1421 * @buf: data buffer
1422 */
1423static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1424 const uint8_t *buf)
1425{
1426 int i, eccsize = chip->ecc.size;
1427 int eccbytes = chip->ecc.bytes;
1428 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001429 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001430 const uint8_t *p = buf;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001431 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001432
1433 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1434 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001435 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001436 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1437 }
1438
1439 for (i = 0; i < chip->ecc.total; i++)
1440 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1441
1442 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1443}
1444
1445/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001446 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001447 * @mtd: mtd info structure
1448 * @chip: nand chip info structure
1449 * @buf: data buffer
1450 *
1451 * The hw generator calculates the error syndrome automatically. Therefor
1452 * we need a special oob layout and handling.
1453 */
1454static void nand_write_page_syndrome(struct mtd_info *mtd,
1455 struct nand_chip *chip, const uint8_t *buf)
1456{
1457 int i, eccsize = chip->ecc.size;
1458 int eccbytes = chip->ecc.bytes;
1459 int eccsteps = chip->ecc.steps;
1460 const uint8_t *p = buf;
1461 uint8_t *oob = chip->oob_poi;
1462
1463 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1464
1465 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1466 chip->write_buf(mtd, p, eccsize);
1467
1468 if (chip->ecc.prepad) {
1469 chip->write_buf(mtd, oob, chip->ecc.prepad);
1470 oob += chip->ecc.prepad;
1471 }
1472
1473 chip->ecc.calculate(mtd, p, oob);
1474 chip->write_buf(mtd, oob, eccbytes);
1475 oob += eccbytes;
1476
1477 if (chip->ecc.postpad) {
1478 chip->write_buf(mtd, oob, chip->ecc.postpad);
1479 oob += chip->ecc.postpad;
1480 }
1481 }
1482
1483 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001484 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001485 if (i)
1486 chip->write_buf(mtd, oob, i);
1487}
1488
1489/**
David Woodhouse956e9442006-09-25 17:12:39 +01001490 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001491 * @mtd: MTD device structure
1492 * @chip: NAND chip descriptor
1493 * @buf: the data to write
1494 * @page: page number to write
1495 * @cached: cached programming
1496 */
1497static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001498 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001499{
1500 int status;
1501
1502 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1503
David Woodhouse956e9442006-09-25 17:12:39 +01001504 if (unlikely(raw))
1505 chip->ecc.write_page_raw(mtd, chip, buf);
1506 else
1507 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001508
1509 /*
1510 * Cached progamming disabled for now, Not sure if its worth the
1511 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1512 */
1513 cached = 0;
1514
1515 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1516
1517 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001518 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001519 /*
1520 * See if operation failed and additional status checks are
1521 * available
1522 */
1523 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1524 status = chip->errstat(mtd, chip, FL_WRITING, status,
1525 page);
1526
1527 if (status & NAND_STATUS_FAIL)
1528 return -EIO;
1529 } else {
1530 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001531 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001532 }
1533
1534#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1535 /* Send command to read back the data */
1536 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1537
1538 if (chip->verify_buf(mtd, buf, mtd->writesize))
1539 return -EIO;
1540#endif
1541 return 0;
1542}
1543
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001544/**
1545 * nand_fill_oob - [Internal] Transfer client buffer to oob
1546 * @chip: nand chip structure
1547 * @oob: oob data buffer
1548 * @ops: oob ops structure
1549 */
1550static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1551 struct mtd_oob_ops *ops)
1552{
1553 size_t len = ops->ooblen;
1554
1555 switch(ops->mode) {
1556
1557 case MTD_OOB_PLACE:
1558 case MTD_OOB_RAW:
1559 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1560 return oob + len;
1561
1562 case MTD_OOB_AUTO: {
1563 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001564 uint32_t boffs = 0, woffs = ops->ooboffs;
1565 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001566
1567 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001568 /* Write request not from offset 0 ? */
1569 if (unlikely(woffs)) {
1570 if (woffs >= free->length) {
1571 woffs -= free->length;
1572 continue;
1573 }
1574 boffs = free->offset + woffs;
1575 bytes = min_t(size_t, len,
1576 (free->length - woffs));
1577 woffs = 0;
1578 } else {
1579 bytes = min_t(size_t, len, free->length);
1580 boffs = free->offset;
1581 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001582 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001583 oob += bytes;
1584 }
1585 return oob;
1586 }
1587 default:
1588 BUG();
1589 }
1590 return NULL;
1591}
1592
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001593#define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0
1594
1595/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001596 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001597 * @mtd: MTD device structure
1598 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001599 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001600 *
1601 * NAND write with ECC
1602 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001603static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1604 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001605{
1606 int chipnr, realpage, page, blockmask;
1607 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001608 uint32_t writelen = ops->len;
1609 uint8_t *oob = ops->oobbuf;
1610 uint8_t *buf = ops->datbuf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001611 int bytes = mtd->writesize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001612 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001613
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001614 ops->retlen = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001615
1616 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001617 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001618 printk(KERN_NOTICE "nand_write: "
1619 "Attempt to write not page aligned data\n");
1620 return -EINVAL;
1621 }
1622
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001623 if (!writelen)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001624 return 0;
1625
Thomas Gleixner6a930962006-06-28 00:11:45 +02001626 chipnr = (int)(to >> chip->chip_shift);
1627 chip->select_chip(mtd, chipnr);
1628
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001629 /* Check, if it is write protected */
1630 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001631 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001632
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001633 realpage = (int)(to >> chip->page_shift);
1634 page = realpage & chip->pagemask;
1635 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1636
1637 /* Invalidate the page cache, when we write to the cached page */
1638 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001639 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001640 chip->pagebuf = -1;
1641
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001642 /* If we're not given explicit OOB data, let it be 0xFF */
1643 if (likely(!oob))
1644 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001645
1646 while(1) {
1647 int cached = writelen > bytes && page != blockmask;
1648
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001649 if (unlikely(oob))
1650 oob = nand_fill_oob(chip, oob, ops);
1651
David Woodhouse956e9442006-09-25 17:12:39 +01001652 ret = chip->write_page(mtd, chip, buf, page, cached,
1653 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001654 if (ret)
1655 break;
1656
1657 writelen -= bytes;
1658 if (!writelen)
1659 break;
1660
1661 buf += bytes;
1662 realpage++;
1663
1664 page = realpage & chip->pagemask;
1665 /* Check, if we cross a chip boundary */
1666 if (!page) {
1667 chipnr++;
1668 chip->select_chip(mtd, -1);
1669 chip->select_chip(mtd, chipnr);
1670 }
1671 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001672
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001673 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03001674 if (unlikely(oob))
1675 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001676 return ret;
1677}
1678
1679/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001680 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * @mtd: MTD device structure
1682 * @to: offset to write to
1683 * @len: number of bytes to write
1684 * @retlen: pointer to variable to store the number of written bytes
1685 * @buf: the data to write
1686 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001687 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001689static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001690 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001692 struct nand_chip *chip = mtd->priv;
1693 int ret;
1694
1695 /* Do not allow reads past end of device */
1696 if ((to + len) > mtd->size)
1697 return -EINVAL;
1698 if (!len)
1699 return 0;
1700
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001701 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001702
1703 chip->ops.len = len;
1704 chip->ops.datbuf = (uint8_t *)buf;
1705 chip->ops.oobbuf = NULL;
1706
1707 ret = nand_do_write_ops(mtd, to, &chip->ops);
1708
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001709 *retlen = chip->ops.retlen;
1710
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001711 nand_release_device(mtd);
1712
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001713 return ret;
1714}
1715
1716/**
1717 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1718 * @mtd: MTD device structure
1719 * @to: offset to write to
1720 * @ops: oob operation description structure
1721 *
1722 * NAND write out-of-band
1723 */
1724static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1725 struct mtd_oob_ops *ops)
1726{
1727 int chipnr, page, status;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001728 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001730 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
Vitaly Wool70145682006-11-03 18:20:38 +03001731 (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 /* Do not allow write past end of page */
Vitaly Wool70145682006-11-03 18:20:38 +03001734 if ((ops->ooboffs + ops->ooblen) > mtd->oobsize) {
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001735 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1736 "Attempt to write past end of page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return -EINVAL;
1738 }
1739
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001740 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001741 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001743 /* Shift to get page */
1744 page = (int)(to >> chip->page_shift);
1745
1746 /*
1747 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1748 * of my DiskOnChip 2000 test units) will clear the whole data page too
1749 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1750 * it in the doc2000 driver in August 1999. dwmw2.
1751 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001752 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /* Check, if it is write protected */
1755 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001756 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001759 if (page == chip->pagebuf)
1760 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001762 memset(chip->oob_poi, 0xff, mtd->oobsize);
1763 nand_fill_oob(chip, ops->oobbuf, ops);
1764 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1765 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001766
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001767 if (status)
1768 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Vitaly Wool70145682006-11-03 18:20:38 +03001770 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001772 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001773}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001775/**
1776 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1777 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001778 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001779 * @ops: oob operation description structure
1780 */
1781static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1782 struct mtd_oob_ops *ops)
1783{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001784 struct nand_chip *chip = mtd->priv;
1785 int ret = -ENOTSUPP;
1786
1787 ops->retlen = 0;
1788
1789 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001790 if (ops->datbuf && (to + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001791 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1792 "Attempt read beyond end of device\n");
1793 return -EINVAL;
1794 }
1795
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001796 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001797
1798 switch(ops->mode) {
1799 case MTD_OOB_PLACE:
1800 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001801 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001802 break;
1803
1804 default:
1805 goto out;
1806 }
1807
1808 if (!ops->datbuf)
1809 ret = nand_do_write_oob(mtd, to, ops);
1810 else
1811 ret = nand_do_write_ops(mtd, to, ops);
1812
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001813 out:
1814 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return ret;
1816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1820 * @mtd: MTD device structure
1821 * @page: the page address of the block which will be erased
1822 *
1823 * Standard erase command for NAND chips
1824 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001825static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001827 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001829 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1830 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
1833/**
1834 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1835 * @mtd: MTD device structure
1836 * @page: the page address of the block which will be erased
1837 *
1838 * AND multi block erase command function
1839 * Erase 4 consecutive blocks
1840 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001841static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001843 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001845 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1846 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1847 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1848 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1849 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
1852/**
1853 * nand_erase - [MTD Interface] erase block(s)
1854 * @mtd: MTD device structure
1855 * @instr: erase instruction
1856 *
1857 * Erase one ore more blocks
1858 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001859static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
David Woodhousee0c7d762006-05-13 18:07:53 +01001861 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001863
David A. Marlin30f464b2005-01-17 18:35:25 +00001864#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001866 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * @mtd: MTD device structure
1868 * @instr: erase instruction
1869 * @allowbbt: allow erasing the bbt area
1870 *
1871 * Erase one ore more blocks
1872 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001873int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
1874 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 int page, len, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001877 struct nand_chip *chip = mtd->priv;
1878 int rewrite_bbt[NAND_MAX_CHIPS]={0};
1879 unsigned int bbt_masked_page = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001881 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
1882 (unsigned int)instr->addr, (unsigned int)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001885 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001886 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return -EINVAL;
1888 }
1889
1890 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001891 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
1892 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1893 "Length not block aligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return -EINVAL;
1895 }
1896
1897 /* Do not allow erase past end of device */
1898 if ((instr->len + instr->addr) > mtd->size) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001899 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1900 "Erase past end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return -EINVAL;
1902 }
1903
1904 instr->fail_addr = 0xffffffff;
1905
1906 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001907 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001910 page = (int)(instr->addr >> chip->page_shift);
1911 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001914 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001917 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /* Check, if it is write protected */
1920 if (nand_check_wp(mtd)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001921 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1922 "Device is write protected!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 instr->state = MTD_ERASE_FAILED;
1924 goto erase_exit;
1925 }
1926
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001927 /*
1928 * If BBT requires refresh, set the BBT page mask to see if the BBT
1929 * should be rewritten. Otherwise the mask is set to 0xffffffff which
1930 * can not be matched. This is also done when the bbt is actually
1931 * erased to avoid recusrsive updates
1932 */
1933 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
1934 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /* Loop through the pages */
1937 len = instr->len;
1938
1939 instr->state = MTD_ERASING;
1940
1941 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001942 /*
1943 * heck if we have a bad block, we do not erase bad blocks !
1944 */
1945 if (nand_block_checkbad(mtd, ((loff_t) page) <<
1946 chip->page_shift, 0, allowbbt)) {
1947 printk(KERN_WARNING "nand_erase: attempt to erase a "
1948 "bad block at page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 instr->state = MTD_ERASE_FAILED;
1950 goto erase_exit;
1951 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001952
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001953 /*
1954 * Invalidate the page cache, if we erase the block which
1955 * contains the current cached page
1956 */
1957 if (page <= chip->pagebuf && chip->pagebuf <
1958 (page + pages_per_block))
1959 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001961 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001962
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001963 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001965 /*
1966 * See if operation failed and additional status checks are
1967 * available
1968 */
1969 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1970 status = chip->errstat(mtd, chip, FL_ERASING,
1971 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00001974 if (status & NAND_STATUS_FAIL) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001975 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1976 "Failed erase, page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 instr->state = MTD_ERASE_FAILED;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001978 instr->fail_addr = (page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto erase_exit;
1980 }
David A. Marlin30f464b2005-01-17 18:35:25 +00001981
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001982 /*
1983 * If BBT requires refresh, set the BBT rewrite flag to the
1984 * page being erased
1985 */
1986 if (bbt_masked_page != 0xffffffff &&
1987 (page & BBT_PAGE_MASK) == bbt_masked_page)
1988 rewrite_bbt[chipnr] = (page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001991 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 page += pages_per_block;
1993
1994 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001995 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001997 chip->select_chip(mtd, -1);
1998 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00001999
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002000 /*
2001 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2002 * page mask to see if this BBT should be rewritten
2003 */
2004 if (bbt_masked_page != 0xffffffff &&
2005 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2006 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2007 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 }
2010 instr->state = MTD_ERASE_DONE;
2011
David Woodhousee0c7d762006-05-13 18:07:53 +01002012 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2015 /* Do call back function */
2016 if (!ret)
2017 mtd_erase_callback(instr);
2018
2019 /* Deselect and wake up anyone waiting on the device */
2020 nand_release_device(mtd);
2021
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002022 /*
2023 * If BBT requires refresh and erase was successful, rewrite any
2024 * selected bad block tables
2025 */
2026 if (bbt_masked_page == 0xffffffff || ret)
2027 return ret;
2028
2029 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2030 if (!rewrite_bbt[chipnr])
2031 continue;
2032 /* update the BBT for chip */
2033 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2034 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2035 chip->bbt_td->pages[chipnr]);
2036 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002037 }
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /* Return more or less happy */
2040 return ret;
2041}
2042
2043/**
2044 * nand_sync - [MTD Interface] sync
2045 * @mtd: MTD device structure
2046 *
2047 * Sync is actually a wait for chip ready function
2048 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002049static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002051 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
David Woodhousee0c7d762006-05-13 18:07:53 +01002053 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002056 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002058 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002062 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002064 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002066static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002069 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002071
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002072 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
2075/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002076 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 * @mtd: MTD device structure
2078 * @ofs: offset relative to mtd start
2079 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002080static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002082 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 int ret;
2084
David Woodhousee0c7d762006-05-13 18:07:53 +01002085 if ((ret = nand_block_isbad(mtd, ofs))) {
2086 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (ret > 0)
2088 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002089 return ret;
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002092 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
2095/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002096 * nand_suspend - [MTD Interface] Suspend the NAND flash
2097 * @mtd: MTD device structure
2098 */
2099static int nand_suspend(struct mtd_info *mtd)
2100{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002101 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002102
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002103 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002104}
2105
2106/**
2107 * nand_resume - [MTD Interface] Resume the NAND flash
2108 * @mtd: MTD device structure
2109 */
2110static void nand_resume(struct mtd_info *mtd)
2111{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002112 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002113
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002114 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002115 nand_release_device(mtd);
2116 else
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02002117 printk(KERN_ERR "nand_resume() called for a chip which is not "
2118 "in suspended state\n");
Vitaly Wool962034f2005-09-15 14:58:53 +01002119}
2120
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002121/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002122 * Set default functions
2123 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002124static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002127 if (!chip->chip_delay)
2128 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002131 if (chip->cmdfunc == NULL)
2132 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002135 if (chip->waitfunc == NULL)
2136 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002138 if (!chip->select_chip)
2139 chip->select_chip = nand_select_chip;
2140 if (!chip->read_byte)
2141 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2142 if (!chip->read_word)
2143 chip->read_word = nand_read_word;
2144 if (!chip->block_bad)
2145 chip->block_bad = nand_block_bad;
2146 if (!chip->block_markbad)
2147 chip->block_markbad = nand_default_block_markbad;
2148 if (!chip->write_buf)
2149 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2150 if (!chip->read_buf)
2151 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2152 if (!chip->verify_buf)
2153 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2154 if (!chip->scan_bbt)
2155 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002156
2157 if (!chip->controller) {
2158 chip->controller = &chip->hwcontrol;
2159 spin_lock_init(&chip->controller->lock);
2160 init_waitqueue_head(&chip->controller->wq);
2161 }
2162
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002163}
2164
2165/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002166 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002167 */
2168static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002169 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002170 int busw, int *maf_id)
2171{
2172 struct nand_flash_dev *type = NULL;
2173 int i, dev_id, maf_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002176 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002179 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002182 *maf_id = chip->read_byte(mtd);
2183 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002185 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002187 if (dev_id == nand_flash_ids[i].id) {
2188 type = &nand_flash_ids[i];
2189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002193 if (!type)
2194 return ERR_PTR(-ENODEV);
2195
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002196 if (!mtd->name)
2197 mtd->name = type->name;
2198
2199 chip->chipsize = type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002200
2201 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002202 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002203 int extid;
2204 /* The 3rd id byte contains non relevant data ATM */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002205 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002206 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002207 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002208 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002209 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002210 extid >>= 2;
2211 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002212 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002213 extid >>= 2;
2214 /* Calc blocksize. Blocksize is multiples of 64KiB */
2215 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2216 extid >>= 2;
2217 /* Get buswidth information */
2218 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2219
2220 } else {
2221 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002222 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002223 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002224 mtd->erasesize = type->erasesize;
2225 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002226 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002227 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002228 }
2229
2230 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002231 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002232 if (nand_manuf_ids[maf_idx].id == *maf_id)
2233 break;
2234 }
2235
2236 /*
2237 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002238 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002239 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002240 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002241 printk(KERN_INFO "NAND device: Manufacturer ID:"
2242 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2243 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2244 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002245 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002246 busw ? 16 : 8);
2247 return ERR_PTR(-EINVAL);
2248 }
2249
2250 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002251 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002252 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002253 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002254
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002255 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002256 ffs(mtd->erasesize) - 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002257 chip->chip_shift = ffs(chip->chipsize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002258
2259 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002260 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002261 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2262
2263 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002264 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002265 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002266
2267 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002268 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002269 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002270 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002271
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002272 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002273 * options for chips which are not having an extended id.
2274 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002275 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002276 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002277
2278 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002279 if (chip->options & NAND_4PAGE_ARRAY)
2280 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002281 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002282 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002283
2284 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002285 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2286 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002287
2288 printk(KERN_INFO "NAND device: Manufacturer ID:"
2289 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2290 nand_manuf_ids[maf_idx].name, type->name);
2291
2292 return type;
2293}
2294
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002295/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002296 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2297 * @mtd: MTD device structure
2298 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002299 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002300 * This is the first phase of the normal nand_scan() function. It
2301 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002302 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002303 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002304 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002305int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002306{
2307 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002308 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002309 struct nand_flash_dev *type;
2310
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002311 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002312 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002313 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002314 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002315
2316 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002317 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002318
2319 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002320 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002321 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002322 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002325 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002326 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002327 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002329 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002331 if (nand_maf_id != chip->read_byte(mtd) ||
2332 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 break;
2334 }
2335 if (i > 1)
2336 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002339 chip->numchips = i;
2340 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
David Woodhouse3b85c322006-09-25 17:06:53 +01002342 return 0;
2343}
2344
2345
2346/**
2347 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2348 * @mtd: MTD device structure
2349 * @maxchips: Number of chips to scan for
2350 *
2351 * This is the second phase of the normal nand_scan() function. It
2352 * fills out all the uninitialized function pointers with the defaults
2353 * and scans for a bad block table if appropriate.
2354 */
2355int nand_scan_tail(struct mtd_info *mtd)
2356{
2357 int i;
2358 struct nand_chip *chip = mtd->priv;
2359
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002360 if (!(chip->options & NAND_OWN_BUFFERS))
2361 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2362 if (!chip->buffers)
2363 return -ENOMEM;
2364
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002365 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002366 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002367
2368 /*
2369 * If no default placement scheme is given, select an appropriate one
2370 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002371 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002372 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002374 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 break;
2376 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002377 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 break;
2379 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002380 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 break;
2382 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002383 printk(KERN_WARNING "No oob scheme defined for "
2384 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 BUG();
2386 }
2387 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002388
David Woodhouse956e9442006-09-25 17:12:39 +01002389 if (!chip->write_page)
2390 chip->write_page = nand_write_page;
2391
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002392 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002393 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2394 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002395 */
David Woodhouse956e9442006-09-25 17:12:39 +01002396 if (!chip->ecc.read_page_raw)
2397 chip->ecc.read_page_raw = nand_read_page_raw;
2398 if (!chip->ecc.write_page_raw)
2399 chip->ecc.write_page_raw = nand_write_page_raw;
2400
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002401 switch (chip->ecc.mode) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002402 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002403 /* Use standard hwecc read page function ? */
2404 if (!chip->ecc.read_page)
2405 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002406 if (!chip->ecc.write_page)
2407 chip->ecc.write_page = nand_write_page_hwecc;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002408 if (!chip->ecc.read_oob)
2409 chip->ecc.read_oob = nand_read_oob_std;
2410 if (!chip->ecc.write_oob)
2411 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002412
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002413 case NAND_ECC_HW_SYNDROME:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002414 if (!chip->ecc.calculate || !chip->ecc.correct ||
2415 !chip->ecc.hwctl) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002416 printk(KERN_WARNING "No ECC functions supplied, "
2417 "Hardware ECC not possible\n");
2418 BUG();
2419 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002420 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002421 if (!chip->ecc.read_page)
2422 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002423 if (!chip->ecc.write_page)
2424 chip->ecc.write_page = nand_write_page_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002425 if (!chip->ecc.read_oob)
2426 chip->ecc.read_oob = nand_read_oob_syndrome;
2427 if (!chip->ecc.write_oob)
2428 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002429
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002430 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002431 break;
2432 printk(KERN_WARNING "%d byte HW ECC not possible on "
2433 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002434 chip->ecc.size, mtd->writesize);
2435 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002437 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002438 chip->ecc.calculate = nand_calculate_ecc;
2439 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002440 chip->ecc.read_page = nand_read_page_swecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002441 chip->ecc.write_page = nand_write_page_swecc;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002442 chip->ecc.read_oob = nand_read_oob_std;
2443 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002444 chip->ecc.size = 256;
2445 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002447
2448 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002449 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2450 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002451 chip->ecc.read_page = nand_read_page_raw;
2452 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002453 chip->ecc.read_oob = nand_read_oob_std;
2454 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002455 chip->ecc.size = mtd->writesize;
2456 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002460 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002461 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002462 BUG();
2463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002465 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002466 * The number of bytes available for a client to place data into
2467 * the out of band area
2468 */
2469 chip->ecc.layout->oobavail = 0;
2470 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2471 chip->ecc.layout->oobavail +=
2472 chip->ecc.layout->oobfree[i].length;
2473
2474 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002475 * Set the number of read / write steps for one page depending on ECC
2476 * mode
2477 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002478 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2479 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002480 printk(KERN_WARNING "Invalid ecc parameters\n");
2481 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002483 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002484
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002485 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002486 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002489 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002492 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 /* Fill in remaining MTD driver data */
2495 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002496 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 mtd->ecctype = MTD_ECC_SW;
2498 mtd->erase = nand_erase;
2499 mtd->point = NULL;
2500 mtd->unpoint = NULL;
2501 mtd->read = nand_read;
2502 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 mtd->read_oob = nand_read_oob;
2504 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 mtd->sync = nand_sync;
2506 mtd->lock = NULL;
2507 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002508 mtd->suspend = nand_suspend;
2509 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 mtd->block_isbad = nand_block_isbad;
2511 mtd->block_markbad = nand_block_markbad;
2512
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002513 /* propagate ecc.layout to mtd_info */
2514 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002516 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002517 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002521 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
David Woodhouse3b85c322006-09-25 17:06:53 +01002524/* module_text_address() isn't exported, and it's mostly a pointless
2525 test if this is a module _anyway_ -- they'd have to try _really_ hard
2526 to call us from in-kernel code if the core NAND support is modular. */
2527#ifdef MODULE
2528#define caller_is_module() (1)
2529#else
2530#define caller_is_module() \
2531 module_text_address((unsigned long)__builtin_return_address(0))
2532#endif
2533
2534/**
2535 * nand_scan - [NAND Interface] Scan for the NAND device
2536 * @mtd: MTD device structure
2537 * @maxchips: Number of chips to scan for
2538 *
2539 * This fills out all the uninitialized function pointers
2540 * with the defaults.
2541 * The flash ID is read and the mtd/chip structures are
2542 * filled with the appropriate values.
2543 * The mtd->owner field must be set to the module of the caller
2544 *
2545 */
2546int nand_scan(struct mtd_info *mtd, int maxchips)
2547{
2548 int ret;
2549
2550 /* Many callers got this wrong, so check for it for a while... */
2551 if (!mtd->owner && caller_is_module()) {
2552 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2553 BUG();
2554 }
2555
2556 ret = nand_scan_ident(mtd, maxchips);
2557 if (!ret)
2558 ret = nand_scan_tail(mtd);
2559 return ret;
2560}
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002563 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 * @mtd: MTD device structure
2565*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002566void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002568 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570#ifdef CONFIG_MTD_PARTITIONS
2571 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002572 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573#endif
2574 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002575 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Jesper Juhlfa671642005-11-07 01:01:27 -08002577 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002578 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002579 if (!(chip->options & NAND_OWN_BUFFERS))
2580 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
David Woodhousee0c7d762006-05-13 18:07:53 +01002583EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01002584EXPORT_SYMBOL_GPL(nand_scan_ident);
2585EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01002586EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002587
2588static int __init nand_base_init(void)
2589{
2590 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2591 return 0;
2592}
2593
2594static void __exit nand_base_exit(void)
2595{
2596 led_trigger_unregister_simple(nand_led_trigger);
2597}
2598
2599module_init(nand_base_init);
2600module_exit(nand_base_exit);
2601
David Woodhousee0c7d762006-05-13 18:07:53 +01002602MODULE_LICENSE("GPL");
2603MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2604MODULE_DESCRIPTION("Generic NAND flash driver code");