blob: c8cbc00243fece8bd9da98ef160e76d89e57b8ea [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;
365 chip->ops.len = 2;
366 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 */
418static void nand_wait_ready(struct mtd_info *mtd)
419{
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}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/**
434 * nand_command - [DEFAULT] Send command to NAND device
435 * @mtd: MTD device structure
436 * @command: the command to be sent
437 * @column: the column address for this command, -1 if none
438 * @page_addr: the page address for this command, -1 if none
439 *
440 * Send command to NAND device. This function is used for small page
441 * devices (256/512 Bytes per page)
442 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200443static void nand_command(struct mtd_info *mtd, unsigned int command,
444 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200446 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200447 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * Write out the command to the device.
451 */
452 if (command == NAND_CMD_SEQIN) {
453 int readcmd;
454
Joern Engel28318772006-05-22 23:18:05 +0200455 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200457 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 readcmd = NAND_CMD_READOOB;
459 } else if (column < 256) {
460 /* First 256 bytes --> READ0 */
461 readcmd = NAND_CMD_READ0;
462 } else {
463 column -= 256;
464 readcmd = NAND_CMD_READ1;
465 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200466 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200467 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200469 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200471 /*
472 * Address cycle, when necessary
473 */
474 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
475 /* Serially input address */
476 if (column != -1) {
477 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200478 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200479 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200480 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200481 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200483 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200484 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200485 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200486 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200487 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200488 if (chip->chipsize > (32 << 20))
489 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200490 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200491 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000492
493 /*
494 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case NAND_CMD_PAGEPROG:
500 case NAND_CMD_ERASE1:
501 case NAND_CMD_ERASE2:
502 case NAND_CMD_SEQIN:
503 case NAND_CMD_STATUS:
504 return;
505
506 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200507 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200509 udelay(chip->chip_delay);
510 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200511 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200512 chip->cmd_ctrl(mtd,
513 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200514 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
516
David Woodhousee0c7d762006-05-13 18:07:53 +0100517 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000519 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * If we don't have access to the busy pin, we apply the given
521 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100522 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200523 if (!chip->dev_ready) {
524 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* Apply this short delay always to ensure that we do wait tWB in
529 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100530 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000531
532 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/**
536 * nand_command_lp - [DEFAULT] Send command to NAND large page device
537 * @mtd: MTD device structure
538 * @command: the command to be sent
539 * @column: the column address for this command, -1 if none
540 * @page_addr: the page address for this command, -1 if none
541 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200542 * Send command to NAND device. This is the version for the new large page
543 * devices We dont have the separate regions as we have in the small page
544 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200546static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
547 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200549 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* Emulate NAND_CMD_READOOB */
552 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200553 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 command = NAND_CMD_READ0;
555 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000556
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200557 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200558 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200559 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200562 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Serially input address */
565 if (column != -1) {
566 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200567 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200569 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200570 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200571 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200574 chip->cmd_ctrl(mtd, page_addr, ctrl);
575 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200576 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200578 if (chip->chipsize > (128 << 20))
579 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200580 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200583 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584
585 /*
586 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000587 * status, sequential in, and deplete1 need no delay
588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 case NAND_CMD_CACHEDPROG:
592 case NAND_CMD_PAGEPROG:
593 case NAND_CMD_ERASE1:
594 case NAND_CMD_ERASE2:
595 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200596 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000598 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return;
600
David Woodhousee0c7d762006-05-13 18:07:53 +0100601 /*
602 * read error status commands require only a short delay
603 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000604 case NAND_CMD_STATUS_ERROR:
605 case NAND_CMD_STATUS_ERROR0:
606 case NAND_CMD_STATUS_ERROR1:
607 case NAND_CMD_STATUS_ERROR2:
608 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200613 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200615 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200616 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
617 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
618 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
619 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200620 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return;
622
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200623 case NAND_CMD_RNDOUT:
624 /* No ready / busy check necessary */
625 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
626 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
627 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
628 NAND_NCE | NAND_CTRL_CHANGE);
629 return;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200632 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
633 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
634 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
635 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000636
David Woodhousee0c7d762006-05-13 18:07:53 +0100637 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000639 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * If we don't have access to the busy pin, we apply the given
641 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100642 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200643 if (!chip->dev_ready) {
644 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Apply this short delay always to ensure that we do wait tWB in
650 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100651 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000652
653 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/**
657 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700658 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000660 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 *
662 * Get the device and lock it for exclusive access
663 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200664static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200665nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200667 spinlock_t *lock = &chip->controller->lock;
668 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100669 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100670 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100671 spin_lock(lock);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* Hardware controller shared among independend devices */
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200674 /* Hardware controller shared among independend devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200675 if (!chip->controller->active)
676 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200677
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200678 if (chip->controller->active == chip && chip->state == FL_READY) {
679 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100680 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100681 return 0;
682 }
683 if (new_state == FL_PM_SUSPENDED) {
684 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200685 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100686 }
687 set_current_state(TASK_UNINTERRUPTIBLE);
688 add_wait_queue(wq, &wait);
689 spin_unlock(lock);
690 schedule();
691 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 goto retry;
693}
694
695/**
696 * nand_wait - [DEFAULT] wait until the command is done
697 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700698 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *
700 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000701 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700703 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200704static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200708 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100711 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100713 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Richard Purdie8fe833c2006-03-31 02:31:14 -0800715 led_trigger_event(nand_led_trigger, LED_FULL);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Apply this short delay always to ensure that we do wait tWB in
718 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100719 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200721 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
722 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000723 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200724 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000726 while (time_before(jiffies, timeo)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 if (chip->dev_ready) {
728 if (chip->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000734 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800736 led_trigger_event(nand_led_trigger, LED_OFF);
737
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200738 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return status;
740}
741
742/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200743 * nand_read_page_raw - [Intern] read raw page data without ecc
744 * @mtd: mtd info structure
745 * @chip: nand chip info structure
746 * @buf: buffer to store read data
747 */
748static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
749 uint8_t *buf)
750{
751 chip->read_buf(mtd, buf, mtd->writesize);
752 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
753 return 0;
754}
755
756/**
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200757 * nand_read_page_swecc - {REPLACABLE] software ecc based page read function
758 * @mtd: mtd info structure
759 * @chip: nand chip info structure
760 * @buf: buffer to store read data
David A. Marlin068e3c02005-01-24 03:07:46 +0000761 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200762static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
763 uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200765 int i, eccsize = chip->ecc.size;
766 int eccbytes = chip->ecc.bytes;
767 int eccsteps = chip->ecc.steps;
768 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200769 uint8_t *ecc_calc = chip->buffers.ecccalc;
770 uint8_t *ecc_code = chip->buffers.ecccode;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200771 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200772
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200773 nand_read_page_raw(mtd, chip, buf);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200774
775 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
776 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
777
778 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200779 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200780
781 eccsteps = chip->ecc.steps;
782 p = buf;
783
784 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
785 int stat;
786
787 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
788 if (stat == -1)
789 mtd->ecc_stats.failed++;
790 else
791 mtd->ecc_stats.corrected += stat;
792 }
793 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/**
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200797 * nand_read_page_hwecc - {REPLACABLE] hardware ecc based page read function
798 * @mtd: mtd info structure
799 * @chip: nand chip info structure
800 * @buf: buffer to store read data
801 *
802 * Not for syndrome calculating ecc controllers which need a special oob layout
803 */
804static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
805 uint8_t *buf)
806{
807 int i, eccsize = chip->ecc.size;
808 int eccbytes = chip->ecc.bytes;
809 int eccsteps = chip->ecc.steps;
810 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200811 uint8_t *ecc_calc = chip->buffers.ecccalc;
812 uint8_t *ecc_code = chip->buffers.ecccode;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200813 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200814
815 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
816 chip->ecc.hwctl(mtd, NAND_ECC_READ);
817 chip->read_buf(mtd, p, eccsize);
818 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
819 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200820 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200821
822 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200823 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200824
825 eccsteps = chip->ecc.steps;
826 p = buf;
827
828 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
829 int stat;
830
831 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
832 if (stat == -1)
833 mtd->ecc_stats.failed++;
834 else
835 mtd->ecc_stats.corrected += stat;
836 }
837 return 0;
838}
839
840/**
841 * nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
842 * @mtd: mtd info structure
843 * @chip: nand chip info structure
844 * @buf: buffer to store read data
845 *
846 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200847 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200848 */
849static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
850 uint8_t *buf)
851{
852 int i, eccsize = chip->ecc.size;
853 int eccbytes = chip->ecc.bytes;
854 int eccsteps = chip->ecc.steps;
855 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200856 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200857
858 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
859 int stat;
860
861 chip->ecc.hwctl(mtd, NAND_ECC_READ);
862 chip->read_buf(mtd, p, eccsize);
863
864 if (chip->ecc.prepad) {
865 chip->read_buf(mtd, oob, chip->ecc.prepad);
866 oob += chip->ecc.prepad;
867 }
868
869 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
870 chip->read_buf(mtd, oob, eccbytes);
871 stat = chip->ecc.correct(mtd, p, oob, NULL);
872
873 if (stat == -1)
874 mtd->ecc_stats.failed++;
875 else
876 mtd->ecc_stats.corrected += stat;
877
878 oob += eccbytes;
879
880 if (chip->ecc.postpad) {
881 chip->read_buf(mtd, oob, chip->ecc.postpad);
882 oob += chip->ecc.postpad;
883 }
884 }
885
886 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +0400887 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200888 if (i)
889 chip->read_buf(mtd, oob, i);
890
891 return 0;
892}
893
894/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200895 * nand_transfer_oob - [Internal] Transfer oob to client buffer
896 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700897 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200898 * @ops: oob ops structure
899 */
900static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
901 struct mtd_oob_ops *ops)
902{
903 size_t len = ops->ooblen;
904
905 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;
962 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200964 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200966 chipnr = (int)(from >> chip->chip_shift);
967 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200969 realpage = (int)(from >> chip->page_shift);
970 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200972 col = (int)(from & (mtd->writesize - 1));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200973 chip->oob_poi = chip->buffers.oobrbuf;
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) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200984 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 */
992 ret = chip->ecc.read_page(mtd, chip, bufpoi);
993 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +0100994 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200995
996 /* Transfer not aligned data */
997 if (!aligned) {
998 chip->pagebuf = realpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200999 memcpy(buf, chip->buffers.databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001001
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001002 buf += bytes;
1003
1004 if (unlikely(oob)) {
1005 /* Raw mode does data:oob:data:oob */
1006 if (ops->mode != MTD_OOB_RAW)
1007 oob = nand_transfer_oob(chip, oob, ops);
1008 else
1009 buf = nand_transfer_oob(chip, buf, ops);
1010 }
1011
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001012 if (!(chip->options & NAND_NO_READRDY)) {
1013 /*
1014 * Apply delay or wait for ready/busy pin. Do
1015 * this before the AUTOINCR check, so no
1016 * problems arise if a chip which does auto
1017 * increment is marked as NOAUTOINCR by the
1018 * board driver.
1019 */
1020 if (!chip->dev_ready)
1021 udelay(chip->chip_delay);
1022 else
1023 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001025 } else {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001026 memcpy(buf, chip->buffers.databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001027 buf += bytes;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001030 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001031
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001032 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* For subsequent reads align to page boundary. */
1036 col = 0;
1037 /* Increment page address */
1038 realpage++;
1039
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001040 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* Check, if we cross a chip boundary */
1042 if (!page) {
1043 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001044 chip->select_chip(mtd, -1);
1045 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001047
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001048 /* Check, if the chip supports auto page increment
1049 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001050 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001051 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001052 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001055 ops->retlen = ops->len - (size_t) readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001057 if (ret)
1058 return ret;
1059
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001060 if (mtd->ecc_stats.failed - stats.failed)
1061 return -EBADMSG;
1062
1063 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001064}
1065
1066/**
1067 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1068 * @mtd: MTD device structure
1069 * @from: offset to read from
1070 * @len: number of bytes to read
1071 * @retlen: pointer to variable to store the number of read bytes
1072 * @buf: the databuffer to put data
1073 *
1074 * Get hold of the chip and call nand_do_read
1075 */
1076static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1077 size_t *retlen, uint8_t *buf)
1078{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001079 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001080 int ret;
1081
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001082 /* Do not allow reads past end of device */
1083 if ((from + len) > mtd->size)
1084 return -EINVAL;
1085 if (!len)
1086 return 0;
1087
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001088 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001089
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001090 chip->ops.len = len;
1091 chip->ops.datbuf = buf;
1092 chip->ops.oobbuf = NULL;
1093
1094 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001095
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001096 *retlen = chip->ops.retlen;
1097
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001098 nand_release_device(mtd);
1099
1100 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001104 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1105 * @mtd: mtd info structure
1106 * @chip: nand chip info structure
1107 * @page: page number to read
1108 * @sndcmd: flag whether to issue read command or not
1109 */
1110static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1111 int page, int sndcmd)
1112{
1113 if (sndcmd) {
1114 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1115 sndcmd = 0;
1116 }
1117 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1118 return sndcmd;
1119}
1120
1121/**
1122 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1123 * with syndromes
1124 * @mtd: mtd info structure
1125 * @chip: nand chip info structure
1126 * @page: page number to read
1127 * @sndcmd: flag whether to issue read command or not
1128 */
1129static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1130 int page, int sndcmd)
1131{
1132 uint8_t *buf = chip->oob_poi;
1133 int length = mtd->oobsize;
1134 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1135 int eccsize = chip->ecc.size;
1136 uint8_t *bufpoi = buf;
1137 int i, toread, sndrnd = 0, pos;
1138
1139 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1140 for (i = 0; i < chip->ecc.steps; i++) {
1141 if (sndrnd) {
1142 pos = eccsize + i * (eccsize + chunk);
1143 if (mtd->writesize > 512)
1144 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1145 else
1146 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1147 } else
1148 sndrnd = 1;
1149 toread = min_t(int, length, chunk);
1150 chip->read_buf(mtd, bufpoi, toread);
1151 bufpoi += toread;
1152 length -= toread;
1153 }
1154 if (length > 0)
1155 chip->read_buf(mtd, bufpoi, length);
1156
1157 return 1;
1158}
1159
1160/**
1161 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1162 * @mtd: mtd info structure
1163 * @chip: nand chip info structure
1164 * @page: page number to write
1165 */
1166static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1167 int page)
1168{
1169 int status = 0;
1170 const uint8_t *buf = chip->oob_poi;
1171 int length = mtd->oobsize;
1172
1173 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1174 chip->write_buf(mtd, buf, length);
1175 /* Send command to program the OOB data */
1176 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1177
1178 status = chip->waitfunc(mtd, chip);
1179
Savin Zlobec0d420f92006-06-21 11:51:20 +02001180 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001181}
1182
1183/**
1184 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1185 * with syndrome - only for large page flash !
1186 * @mtd: mtd info structure
1187 * @chip: nand chip info structure
1188 * @page: page number to write
1189 */
1190static int nand_write_oob_syndrome(struct mtd_info *mtd,
1191 struct nand_chip *chip, int page)
1192{
1193 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1194 int eccsize = chip->ecc.size, length = mtd->oobsize;
1195 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1196 const uint8_t *bufpoi = chip->oob_poi;
1197
1198 /*
1199 * data-ecc-data-ecc ... ecc-oob
1200 * or
1201 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1202 */
1203 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1204 pos = steps * (eccsize + chunk);
1205 steps = 0;
1206 } else
1207 pos = eccsize + chunk;
1208
1209 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1210 for (i = 0; i < steps; i++) {
1211 if (sndcmd) {
1212 if (mtd->writesize <= 512) {
1213 uint32_t fill = 0xFFFFFFFF;
1214
1215 len = eccsize;
1216 while (len > 0) {
1217 int num = min_t(int, len, 4);
1218 chip->write_buf(mtd, (uint8_t *)&fill,
1219 num);
1220 len -= num;
1221 }
1222 } else {
1223 pos = eccsize + i * (eccsize + chunk);
1224 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1225 }
1226 } else
1227 sndcmd = 1;
1228 len = min_t(int, length, chunk);
1229 chip->write_buf(mtd, bufpoi, len);
1230 bufpoi += len;
1231 length -= len;
1232 }
1233 if (length > 0)
1234 chip->write_buf(mtd, bufpoi, length);
1235
1236 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1237 status = chip->waitfunc(mtd, chip);
1238
1239 return status & NAND_STATUS_FAIL ? -EIO : 0;
1240}
1241
1242/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001243 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * @mtd: MTD device structure
1245 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001246 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 *
1248 * NAND read out-of-band data from the spare area
1249 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001250static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1251 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001253 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001254 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001255 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001256 int readlen = ops->len;
1257 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Andrew Morton7e9a0bb2006-05-30 09:06:41 +01001259 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1260 (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001262 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001263 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001265 /* Shift to get page */
1266 realpage = (int)(from >> chip->page_shift);
1267 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001269 chip->oob_poi = chip->buffers.oobrbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001271 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001272 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1273 buf = nand_transfer_oob(chip, buf, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001274
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001275 if (!(chip->options & NAND_NO_READRDY)) {
1276 /*
1277 * Apply delay or wait for ready/busy pin. Do this
1278 * before the AUTOINCR check, so no problems arise if a
1279 * chip which does auto increment is marked as
1280 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001281 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001282 if (!chip->dev_ready)
1283 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001284 else
1285 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001287
Savin Zlobec0d420f92006-06-21 11:51:20 +02001288 readlen -= ops->ooblen;
1289 if (!readlen)
1290 break;
1291
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001292 /* Increment page address */
1293 realpage++;
1294
1295 page = realpage & chip->pagemask;
1296 /* Check, if we cross a chip boundary */
1297 if (!page) {
1298 chipnr++;
1299 chip->select_chip(mtd, -1);
1300 chip->select_chip(mtd, chipnr);
1301 }
1302
1303 /* Check, if the chip supports auto page increment
1304 * or if we have hit a block boundary.
1305 */
1306 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1307 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001310 ops->retlen = ops->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 0;
1312}
1313
1314/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001315 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001318 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001320 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001322static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1323 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001325 int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
1326 uint8_t *buf) = NULL;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001327 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001328 int ret = -ENOTSUPP;
1329
1330 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /* Do not allow reads past end of device */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001333 if ((from + ops->len) > mtd->size) {
1334 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001335 "Attempt read beyond end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -EINVAL;
1337 }
1338
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001339 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001341 switch(ops->mode) {
1342 case MTD_OOB_PLACE:
1343 case MTD_OOB_AUTO:
1344 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001345
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001346 case MTD_OOB_RAW:
1347 /* Replace the read_page algorithm temporary */
1348 read_page = chip->ecc.read_page;
1349 chip->ecc.read_page = nand_read_page_raw;
1350 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001351
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001352 default:
1353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001356 if (!ops->datbuf)
1357 ret = nand_do_read_oob(mtd, from, ops);
1358 else
1359 ret = nand_do_read_ops(mtd, from, ops);
1360
1361 if (unlikely(ops->mode == MTD_OOB_RAW))
1362 chip->ecc.read_page = read_page;
1363 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001365 return ret;
1366}
1367
1368
1369/**
1370 * nand_write_page_raw - [Intern] raw page write function
1371 * @mtd: mtd info structure
1372 * @chip: nand chip info structure
1373 * @buf: data buffer
1374 */
1375static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1376 const uint8_t *buf)
1377{
1378 chip->write_buf(mtd, buf, mtd->writesize);
1379 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001382/**
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001383 * nand_write_page_swecc - {REPLACABLE] software ecc based page write function
1384 * @mtd: mtd info structure
1385 * @chip: nand chip info structure
1386 * @buf: data buffer
1387 */
1388static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1389 const uint8_t *buf)
1390{
1391 int i, eccsize = chip->ecc.size;
1392 int eccbytes = chip->ecc.bytes;
1393 int eccsteps = chip->ecc.steps;
1394 uint8_t *ecc_calc = chip->buffers.ecccalc;
1395 const uint8_t *p = buf;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001396 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001397
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001398 /* Software ecc calculation */
1399 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1400 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001401
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001402 for (i = 0; i < chip->ecc.total; i++)
1403 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001404
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001405 nand_write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001406}
1407
1408/**
1409 * nand_write_page_hwecc - {REPLACABLE] hardware ecc based page write function
1410 * @mtd: mtd info structure
1411 * @chip: nand chip info structure
1412 * @buf: data buffer
1413 */
1414static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1415 const uint8_t *buf)
1416{
1417 int i, eccsize = chip->ecc.size;
1418 int eccbytes = chip->ecc.bytes;
1419 int eccsteps = chip->ecc.steps;
1420 uint8_t *ecc_calc = chip->buffers.ecccalc;
1421 const uint8_t *p = buf;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001422 int *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001423
1424 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1425 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001426 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001427 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1428 }
1429
1430 for (i = 0; i < chip->ecc.total; i++)
1431 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1432
1433 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1434}
1435
1436/**
1437 * nand_write_page_syndrome - {REPLACABLE] hardware ecc syndrom based page write
1438 * @mtd: mtd info structure
1439 * @chip: nand chip info structure
1440 * @buf: data buffer
1441 *
1442 * The hw generator calculates the error syndrome automatically. Therefor
1443 * we need a special oob layout and handling.
1444 */
1445static void nand_write_page_syndrome(struct mtd_info *mtd,
1446 struct nand_chip *chip, const uint8_t *buf)
1447{
1448 int i, eccsize = chip->ecc.size;
1449 int eccbytes = chip->ecc.bytes;
1450 int eccsteps = chip->ecc.steps;
1451 const uint8_t *p = buf;
1452 uint8_t *oob = chip->oob_poi;
1453
1454 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1455
1456 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1457 chip->write_buf(mtd, p, eccsize);
1458
1459 if (chip->ecc.prepad) {
1460 chip->write_buf(mtd, oob, chip->ecc.prepad);
1461 oob += chip->ecc.prepad;
1462 }
1463
1464 chip->ecc.calculate(mtd, p, oob);
1465 chip->write_buf(mtd, oob, eccbytes);
1466 oob += eccbytes;
1467
1468 if (chip->ecc.postpad) {
1469 chip->write_buf(mtd, oob, chip->ecc.postpad);
1470 oob += chip->ecc.postpad;
1471 }
1472 }
1473
1474 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001475 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001476 if (i)
1477 chip->write_buf(mtd, oob, i);
1478}
1479
1480/**
1481 * nand_write_page - [INTERNAL] write one page
1482 * @mtd: MTD device structure
1483 * @chip: NAND chip descriptor
1484 * @buf: the data to write
1485 * @page: page number to write
1486 * @cached: cached programming
1487 */
1488static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1489 const uint8_t *buf, int page, int cached)
1490{
1491 int status;
1492
1493 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1494
1495 chip->ecc.write_page(mtd, chip, buf);
1496
1497 /*
1498 * Cached progamming disabled for now, Not sure if its worth the
1499 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1500 */
1501 cached = 0;
1502
1503 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1504
1505 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001506 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001507 /*
1508 * See if operation failed and additional status checks are
1509 * available
1510 */
1511 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1512 status = chip->errstat(mtd, chip, FL_WRITING, status,
1513 page);
1514
1515 if (status & NAND_STATUS_FAIL)
1516 return -EIO;
1517 } else {
1518 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001519 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001520 }
1521
1522#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1523 /* Send command to read back the data */
1524 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1525
1526 if (chip->verify_buf(mtd, buf, mtd->writesize))
1527 return -EIO;
1528#endif
1529 return 0;
1530}
1531
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001532/**
1533 * nand_fill_oob - [Internal] Transfer client buffer to oob
1534 * @chip: nand chip structure
1535 * @oob: oob data buffer
1536 * @ops: oob ops structure
1537 */
1538static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1539 struct mtd_oob_ops *ops)
1540{
1541 size_t len = ops->ooblen;
1542
1543 switch(ops->mode) {
1544
1545 case MTD_OOB_PLACE:
1546 case MTD_OOB_RAW:
1547 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1548 return oob + len;
1549
1550 case MTD_OOB_AUTO: {
1551 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001552 uint32_t boffs = 0, woffs = ops->ooboffs;
1553 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001554
1555 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001556 /* Write request not from offset 0 ? */
1557 if (unlikely(woffs)) {
1558 if (woffs >= free->length) {
1559 woffs -= free->length;
1560 continue;
1561 }
1562 boffs = free->offset + woffs;
1563 bytes = min_t(size_t, len,
1564 (free->length - woffs));
1565 woffs = 0;
1566 } else {
1567 bytes = min_t(size_t, len, free->length);
1568 boffs = free->offset;
1569 }
1570 memcpy(chip->oob_poi + woffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001571 oob += bytes;
1572 }
1573 return oob;
1574 }
1575 default:
1576 BUG();
1577 }
1578 return NULL;
1579}
1580
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001581#define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0
1582
1583/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001584 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001585 * @mtd: MTD device structure
1586 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001587 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001588 *
1589 * NAND write with ECC
1590 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001591static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1592 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001593{
1594 int chipnr, realpage, page, blockmask;
1595 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001596 uint32_t writelen = ops->len;
1597 uint8_t *oob = ops->oobbuf;
1598 uint8_t *buf = ops->datbuf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001599 int bytes = mtd->writesize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001600 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001601
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001602 ops->retlen = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001603
1604 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001605 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001606 printk(KERN_NOTICE "nand_write: "
1607 "Attempt to write not page aligned data\n");
1608 return -EINVAL;
1609 }
1610
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001611 if (!writelen)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001612 return 0;
1613
Thomas Gleixner6a930962006-06-28 00:11:45 +02001614 chipnr = (int)(to >> chip->chip_shift);
1615 chip->select_chip(mtd, chipnr);
1616
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001617 /* Check, if it is write protected */
1618 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001619 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001620
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001621 realpage = (int)(to >> chip->page_shift);
1622 page = realpage & chip->pagemask;
1623 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1624
1625 /* Invalidate the page cache, when we write to the cached page */
1626 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001627 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001628 chip->pagebuf = -1;
1629
1630 chip->oob_poi = chip->buffers.oobwbuf;
1631
1632 while(1) {
1633 int cached = writelen > bytes && page != blockmask;
1634
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001635 if (unlikely(oob))
1636 oob = nand_fill_oob(chip, oob, ops);
1637
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001638 ret = nand_write_page(mtd, chip, buf, page, cached);
1639 if (ret)
1640 break;
1641
1642 writelen -= bytes;
1643 if (!writelen)
1644 break;
1645
1646 buf += bytes;
1647 realpage++;
1648
1649 page = realpage & chip->pagemask;
1650 /* Check, if we cross a chip boundary */
1651 if (!page) {
1652 chipnr++;
1653 chip->select_chip(mtd, -1);
1654 chip->select_chip(mtd, chipnr);
1655 }
1656 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001657
1658 if (unlikely(oob))
1659 memset(chip->oob_poi, 0xff, mtd->oobsize);
1660
1661 ops->retlen = ops->len - writelen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001662 return ret;
1663}
1664
1665/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001666 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 * @mtd: MTD device structure
1668 * @to: offset to write to
1669 * @len: number of bytes to write
1670 * @retlen: pointer to variable to store the number of written bytes
1671 * @buf: the data to write
1672 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001673 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001675static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001676 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001678 struct nand_chip *chip = mtd->priv;
1679 int ret;
1680
1681 /* Do not allow reads past end of device */
1682 if ((to + len) > mtd->size)
1683 return -EINVAL;
1684 if (!len)
1685 return 0;
1686
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001687 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001688
1689 chip->ops.len = len;
1690 chip->ops.datbuf = (uint8_t *)buf;
1691 chip->ops.oobbuf = NULL;
1692
1693 ret = nand_do_write_ops(mtd, to, &chip->ops);
1694
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001695 *retlen = chip->ops.retlen;
1696
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001697 nand_release_device(mtd);
1698
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001699 return ret;
1700}
1701
1702/**
1703 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1704 * @mtd: MTD device structure
1705 * @to: offset to write to
1706 * @ops: oob operation description structure
1707 *
1708 * NAND write out-of-band
1709 */
1710static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1711 struct mtd_oob_ops *ops)
1712{
1713 int chipnr, page, status;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001714 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001716 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001717 (unsigned int)to, (int)ops->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 /* Do not allow write past end of page */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001720 if ((ops->ooboffs + ops->len) > mtd->oobsize) {
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001721 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1722 "Attempt to write past end of page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return -EINVAL;
1724 }
1725
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001726 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001727 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001729 /* Shift to get page */
1730 page = (int)(to >> chip->page_shift);
1731
1732 /*
1733 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1734 * of my DiskOnChip 2000 test units) will clear the whole data page too
1735 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1736 * it in the doc2000 driver in August 1999. dwmw2.
1737 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001738 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 /* Check, if it is write protected */
1741 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001742 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001745 if (page == chip->pagebuf)
1746 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001748 chip->oob_poi = chip->buffers.oobwbuf;
1749 memset(chip->oob_poi, 0xff, mtd->oobsize);
1750 nand_fill_oob(chip, ops->oobbuf, ops);
1751 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1752 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001753
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001754 if (status)
1755 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001757 ops->retlen = ops->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001759 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001760}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001762/**
1763 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1764 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001765 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001766 * @ops: oob operation description structure
1767 */
1768static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1769 struct mtd_oob_ops *ops)
1770{
1771 void (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
1772 const uint8_t *buf) = NULL;
1773 struct nand_chip *chip = mtd->priv;
1774 int ret = -ENOTSUPP;
1775
1776 ops->retlen = 0;
1777
1778 /* Do not allow writes past end of device */
1779 if ((to + ops->len) > mtd->size) {
1780 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1781 "Attempt read beyond end of device\n");
1782 return -EINVAL;
1783 }
1784
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001785 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001786
1787 switch(ops->mode) {
1788 case MTD_OOB_PLACE:
1789 case MTD_OOB_AUTO:
1790 break;
1791
1792 case MTD_OOB_RAW:
1793 /* Replace the write_page algorithm temporary */
1794 write_page = chip->ecc.write_page;
1795 chip->ecc.write_page = nand_write_page_raw;
1796 break;
1797
1798 default:
1799 goto out;
1800 }
1801
1802 if (!ops->datbuf)
1803 ret = nand_do_write_oob(mtd, to, ops);
1804 else
1805 ret = nand_do_write_ops(mtd, to, ops);
1806
1807 if (unlikely(ops->mode == MTD_OOB_RAW))
1808 chip->ecc.write_page = write_page;
1809 out:
1810 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return ret;
1812}
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1816 * @mtd: MTD device structure
1817 * @page: the page address of the block which will be erased
1818 *
1819 * Standard erase command for NAND chips
1820 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001821static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001823 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001825 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1826 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
1829/**
1830 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1831 * @mtd: MTD device structure
1832 * @page: the page address of the block which will be erased
1833 *
1834 * AND multi block erase command function
1835 * Erase 4 consecutive blocks
1836 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001837static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001839 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001841 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1842 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1843 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1844 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1845 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848/**
1849 * nand_erase - [MTD Interface] erase block(s)
1850 * @mtd: MTD device structure
1851 * @instr: erase instruction
1852 *
1853 * Erase one ore more blocks
1854 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001855static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
David Woodhousee0c7d762006-05-13 18:07:53 +01001857 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001859
David A. Marlin30f464b2005-01-17 18:35:25 +00001860#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001862 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 * @mtd: MTD device structure
1864 * @instr: erase instruction
1865 * @allowbbt: allow erasing the bbt area
1866 *
1867 * Erase one ore more blocks
1868 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001869int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
1870 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 int page, len, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001873 struct nand_chip *chip = mtd->priv;
1874 int rewrite_bbt[NAND_MAX_CHIPS]={0};
1875 unsigned int bbt_masked_page = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001877 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
1878 (unsigned int)instr->addr, (unsigned int)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001881 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001882 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return -EINVAL;
1884 }
1885
1886 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001887 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
1888 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1889 "Length not block aligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return -EINVAL;
1891 }
1892
1893 /* Do not allow erase past end of device */
1894 if ((instr->len + instr->addr) > mtd->size) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001895 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1896 "Erase past end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return -EINVAL;
1898 }
1899
1900 instr->fail_addr = 0xffffffff;
1901
1902 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001903 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001906 page = (int)(instr->addr >> chip->page_shift);
1907 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001910 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001913 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /* Check, if it is write protected */
1916 if (nand_check_wp(mtd)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001917 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1918 "Device is write protected!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 instr->state = MTD_ERASE_FAILED;
1920 goto erase_exit;
1921 }
1922
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001923 /*
1924 * If BBT requires refresh, set the BBT page mask to see if the BBT
1925 * should be rewritten. Otherwise the mask is set to 0xffffffff which
1926 * can not be matched. This is also done when the bbt is actually
1927 * erased to avoid recusrsive updates
1928 */
1929 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
1930 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /* Loop through the pages */
1933 len = instr->len;
1934
1935 instr->state = MTD_ERASING;
1936
1937 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001938 /*
1939 * heck if we have a bad block, we do not erase bad blocks !
1940 */
1941 if (nand_block_checkbad(mtd, ((loff_t) page) <<
1942 chip->page_shift, 0, allowbbt)) {
1943 printk(KERN_WARNING "nand_erase: attempt to erase a "
1944 "bad block at page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 instr->state = MTD_ERASE_FAILED;
1946 goto erase_exit;
1947 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001948
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001949 /*
1950 * Invalidate the page cache, if we erase the block which
1951 * contains the current cached page
1952 */
1953 if (page <= chip->pagebuf && chip->pagebuf <
1954 (page + pages_per_block))
1955 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001957 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001958
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001959 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001961 /*
1962 * See if operation failed and additional status checks are
1963 * available
1964 */
1965 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1966 status = chip->errstat(mtd, chip, FL_ERASING,
1967 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00001970 if (status & NAND_STATUS_FAIL) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001971 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1972 "Failed erase, page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 instr->state = MTD_ERASE_FAILED;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001974 instr->fail_addr = (page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 goto erase_exit;
1976 }
David A. Marlin30f464b2005-01-17 18:35:25 +00001977
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001978 /*
1979 * If BBT requires refresh, set the BBT rewrite flag to the
1980 * page being erased
1981 */
1982 if (bbt_masked_page != 0xffffffff &&
1983 (page & BBT_PAGE_MASK) == bbt_masked_page)
1984 rewrite_bbt[chipnr] = (page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001987 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 page += pages_per_block;
1989
1990 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001991 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001993 chip->select_chip(mtd, -1);
1994 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00001995
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001996 /*
1997 * If BBT requires refresh and BBT-PERCHIP, set the BBT
1998 * page mask to see if this BBT should be rewritten
1999 */
2000 if (bbt_masked_page != 0xffffffff &&
2001 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2002 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2003 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005 }
2006 instr->state = MTD_ERASE_DONE;
2007
David Woodhousee0c7d762006-05-13 18:07:53 +01002008 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2011 /* Do call back function */
2012 if (!ret)
2013 mtd_erase_callback(instr);
2014
2015 /* Deselect and wake up anyone waiting on the device */
2016 nand_release_device(mtd);
2017
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002018 /*
2019 * If BBT requires refresh and erase was successful, rewrite any
2020 * selected bad block tables
2021 */
2022 if (bbt_masked_page == 0xffffffff || ret)
2023 return ret;
2024
2025 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2026 if (!rewrite_bbt[chipnr])
2027 continue;
2028 /* update the BBT for chip */
2029 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2030 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2031 chip->bbt_td->pages[chipnr]);
2032 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002033 }
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /* Return more or less happy */
2036 return ret;
2037}
2038
2039/**
2040 * nand_sync - [MTD Interface] sync
2041 * @mtd: MTD device structure
2042 *
2043 * Sync is actually a wait for chip ready function
2044 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002045static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002047 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
David Woodhousee0c7d762006-05-13 18:07:53 +01002049 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002052 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002054 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002058 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002060 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002062static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002065 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002067
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002068 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
2071/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002072 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 * @mtd: MTD device structure
2074 * @ofs: offset relative to mtd start
2075 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002076static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002078 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 int ret;
2080
David Woodhousee0c7d762006-05-13 18:07:53 +01002081 if ((ret = nand_block_isbad(mtd, ofs))) {
2082 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (ret > 0)
2084 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002085 return ret;
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002088 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002092 * nand_suspend - [MTD Interface] Suspend the NAND flash
2093 * @mtd: MTD device structure
2094 */
2095static int nand_suspend(struct mtd_info *mtd)
2096{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002097 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002098
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002099 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002100}
2101
2102/**
2103 * nand_resume - [MTD Interface] Resume the NAND flash
2104 * @mtd: MTD device structure
2105 */
2106static void nand_resume(struct mtd_info *mtd)
2107{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002108 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002109
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002110 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002111 nand_release_device(mtd);
2112 else
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02002113 printk(KERN_ERR "nand_resume() called for a chip which is not "
2114 "in suspended state\n");
Vitaly Wool962034f2005-09-15 14:58:53 +01002115}
2116
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002117/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002118 * Set default functions
2119 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002120static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002121{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002123 if (!chip->chip_delay)
2124 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002127 if (chip->cmdfunc == NULL)
2128 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002131 if (chip->waitfunc == NULL)
2132 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002134 if (!chip->select_chip)
2135 chip->select_chip = nand_select_chip;
2136 if (!chip->read_byte)
2137 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2138 if (!chip->read_word)
2139 chip->read_word = nand_read_word;
2140 if (!chip->block_bad)
2141 chip->block_bad = nand_block_bad;
2142 if (!chip->block_markbad)
2143 chip->block_markbad = nand_default_block_markbad;
2144 if (!chip->write_buf)
2145 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2146 if (!chip->read_buf)
2147 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2148 if (!chip->verify_buf)
2149 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2150 if (!chip->scan_bbt)
2151 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002152
2153 if (!chip->controller) {
2154 chip->controller = &chip->hwcontrol;
2155 spin_lock_init(&chip->controller->lock);
2156 init_waitqueue_head(&chip->controller->wq);
2157 }
2158
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002159}
2160
2161/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002162 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002163 */
2164static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002165 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002166 int busw, int *maf_id)
2167{
2168 struct nand_flash_dev *type = NULL;
2169 int i, dev_id, maf_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002172 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002175 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002178 *maf_id = chip->read_byte(mtd);
2179 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002181 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002183 if (dev_id == nand_flash_ids[i].id) {
2184 type = &nand_flash_ids[i];
2185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002189 if (!type)
2190 return ERR_PTR(-ENODEV);
2191
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002192 if (!mtd->name)
2193 mtd->name = type->name;
2194
2195 chip->chipsize = type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002196
2197 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002198 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002199 int extid;
2200 /* The 3rd id byte contains non relevant data ATM */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002201 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002202 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002203 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002204 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002205 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002206 extid >>= 2;
2207 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002208 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002209 extid >>= 2;
2210 /* Calc blocksize. Blocksize is multiples of 64KiB */
2211 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2212 extid >>= 2;
2213 /* Get buswidth information */
2214 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2215
2216 } else {
2217 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002218 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002219 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002220 mtd->erasesize = type->erasesize;
2221 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002222 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002223 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002224 }
2225
2226 /* Try to identify manufacturer */
2227 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_id++) {
2228 if (nand_manuf_ids[maf_idx].id == *maf_id)
2229 break;
2230 }
2231
2232 /*
2233 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002234 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002235 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002236 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002237 printk(KERN_INFO "NAND device: Manufacturer ID:"
2238 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2239 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2240 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002241 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002242 busw ? 16 : 8);
2243 return ERR_PTR(-EINVAL);
2244 }
2245
2246 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002247 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002248 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002249 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002250
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002251 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002252 ffs(mtd->erasesize) - 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002253 chip->chip_shift = ffs(chip->chipsize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002254
2255 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002256 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002257 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2258
2259 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002260 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002261 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002262
2263 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002264 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002265 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002266 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002267
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002268 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002269 * options for chips which are not having an extended id.
2270 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002271 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002272 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002273
2274 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002275 if (chip->options & NAND_4PAGE_ARRAY)
2276 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002277 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002278 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002279
2280 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002281 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2282 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002283
2284 printk(KERN_INFO "NAND device: Manufacturer ID:"
2285 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2286 nand_manuf_ids[maf_idx].name, type->name);
2287
2288 return type;
2289}
2290
2291/* module_text_address() isn't exported, and it's mostly a pointless
2292 test if this is a module _anyway_ -- they'd have to try _really_ hard
2293 to call us from in-kernel code if the core NAND support is modular. */
2294#ifdef MODULE
2295#define caller_is_module() (1)
2296#else
2297#define caller_is_module() \
2298 module_text_address((unsigned long)__builtin_return_address(0))
2299#endif
2300
2301/**
2302 * nand_scan - [NAND Interface] Scan for the NAND device
2303 * @mtd: MTD device structure
2304 * @maxchips: Number of chips to scan for
2305 *
2306 * This fills out all the uninitialized function pointers
2307 * with the defaults.
2308 * The flash ID is read and the mtd/chip structures are
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002309 * filled with the appropriate values.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002310 * The mtd->owner field must be set to the module of the caller
2311 *
2312 */
2313int nand_scan(struct mtd_info *mtd, int maxchips)
2314{
2315 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002316 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002317 struct nand_flash_dev *type;
2318
2319 /* Many callers got this wrong, so check for it for a while... */
2320 if (!mtd->owner && caller_is_module()) {
2321 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2322 BUG();
2323 }
2324
2325 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002326 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002327 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002328 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002329
2330 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002331 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002332
2333 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002334 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002335 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002336 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002339 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002340 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002341 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002343 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002345 if (nand_maf_id != chip->read_byte(mtd) ||
2346 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 break;
2348 }
2349 if (i > 1)
2350 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002353 chip->numchips = i;
2354 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002356 /* Preset the internal oob write buffer */
2357 memset(chip->buffers.oobwbuf, 0xff, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002358
2359 /*
2360 * If no default placement scheme is given, select an appropriate one
2361 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002362 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002363 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002365 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 break;
2367 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002368 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 break;
2370 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002371 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 break;
2373 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002374 printk(KERN_WARNING "No oob scheme defined for "
2375 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 BUG();
2377 }
2378 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002379
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002380 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002381 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2382 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002383 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002384 switch (chip->ecc.mode) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002385 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002386 /* Use standard hwecc read page function ? */
2387 if (!chip->ecc.read_page)
2388 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002389 if (!chip->ecc.write_page)
2390 chip->ecc.write_page = nand_write_page_hwecc;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002391 if (!chip->ecc.read_oob)
2392 chip->ecc.read_oob = nand_read_oob_std;
2393 if (!chip->ecc.write_oob)
2394 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002395
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002396 case NAND_ECC_HW_SYNDROME:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002397 if (!chip->ecc.calculate || !chip->ecc.correct ||
2398 !chip->ecc.hwctl) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002399 printk(KERN_WARNING "No ECC functions supplied, "
2400 "Hardware ECC not possible\n");
2401 BUG();
2402 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002403 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002404 if (!chip->ecc.read_page)
2405 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002406 if (!chip->ecc.write_page)
2407 chip->ecc.write_page = nand_write_page_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002408 if (!chip->ecc.read_oob)
2409 chip->ecc.read_oob = nand_read_oob_syndrome;
2410 if (!chip->ecc.write_oob)
2411 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002412
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002413 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002414 break;
2415 printk(KERN_WARNING "%d byte HW ECC not possible on "
2416 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002417 chip->ecc.size, mtd->writesize);
2418 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002420 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002421 chip->ecc.calculate = nand_calculate_ecc;
2422 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002423 chip->ecc.read_page = nand_read_page_swecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002424 chip->ecc.write_page = nand_write_page_swecc;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002425 chip->ecc.read_oob = nand_read_oob_std;
2426 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002427 chip->ecc.size = 256;
2428 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002430
2431 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002432 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2433 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002434 chip->ecc.read_page = nand_read_page_raw;
2435 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002436 chip->ecc.read_oob = nand_read_oob_std;
2437 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002438 chip->ecc.size = mtd->writesize;
2439 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002442 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002443 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002444 BUG();
2445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002447 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002448 * The number of bytes available for a client to place data into
2449 * the out of band area
2450 */
2451 chip->ecc.layout->oobavail = 0;
2452 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2453 chip->ecc.layout->oobavail +=
2454 chip->ecc.layout->oobfree[i].length;
2455
2456 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002457 * Set the number of read / write steps for one page depending on ECC
2458 * mode
2459 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002460 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2461 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002462 printk(KERN_WARNING "Invalid ecc parameters\n");
2463 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002465 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002466
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002467 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002468 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002471 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002474 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 /* Fill in remaining MTD driver data */
2477 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002478 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 mtd->ecctype = MTD_ECC_SW;
2480 mtd->erase = nand_erase;
2481 mtd->point = NULL;
2482 mtd->unpoint = NULL;
2483 mtd->read = nand_read;
2484 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 mtd->read_oob = nand_read_oob;
2486 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 mtd->sync = nand_sync;
2488 mtd->lock = NULL;
2489 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002490 mtd->suspend = nand_suspend;
2491 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 mtd->block_isbad = nand_block_isbad;
2493 mtd->block_markbad = nand_block_markbad;
2494
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002495 /* propagate ecc.layout to mtd_info */
2496 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002498 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002499 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002503 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002507 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 * @mtd: MTD device structure
2509*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002510void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002512 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514#ifdef CONFIG_MTD_PARTITIONS
2515 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002516 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517#endif
2518 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002519 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Jesper Juhlfa671642005-11-07 01:01:27 -08002521 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002522 kfree(chip->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
David Woodhousee0c7d762006-05-13 18:07:53 +01002525EXPORT_SYMBOL_GPL(nand_scan);
2526EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002527
2528static int __init nand_base_init(void)
2529{
2530 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2531 return 0;
2532}
2533
2534static void __exit nand_base_exit(void)
2535{
2536 led_trigger_unregister_simple(nand_led_trigger);
2537}
2538
2539module_init(nand_base_init);
2540module_exit(nand_base_exit);
2541
David Woodhousee0c7d762006-05-13 18:07:53 +01002542MODULE_LICENSE("GPL");
2543MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2544MODULE_DESCRIPTION("Generic NAND flash driver code");