blob: 8c21b89d2d0c2a7ea79eccf0d9c840771c68a58a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +020010 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020015 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000016 * David Woodhouse for adding multichip support
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020021 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030027 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
32 *
33 */
34
David Woodhouse552d9202006-05-14 01:20:46 +010035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020038#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
45#include <linux/mtd/compatmac.h>
46#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50
51#ifdef CONFIG_MTD_PARTITIONS
52#include <linux/mtd/partitions.h>
53#endif
54
55/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020056static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .eccbytes = 3,
58 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020059 .oobfree = {
60 {.offset = 3,
61 .length = 2},
62 {.offset = 6,
63 .length = 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020066static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .eccbytes = 6,
68 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020069 .oobfree = {
70 {.offset = 8,
71 . length = 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020074static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .eccbytes = 24,
76 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010077 40, 41, 42, 43, 44, 45, 46, 47,
78 48, 49, 50, 51, 52, 53, 54, 55,
79 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020080 .oobfree = {
81 {.offset = 2,
82 .length = 38}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Thomas Gleixner81ec5362007-12-12 17:27:03 +010085static struct nand_ecclayout nand_oob_128 = {
86 .eccbytes = 48,
87 .eccpos = {
88 80, 81, 82, 83, 84, 85, 86, 87,
89 88, 89, 90, 91, 92, 93, 94, 95,
90 96, 97, 98, 99, 100, 101, 102, 103,
91 104, 105, 106, 107, 108, 109, 110, 111,
92 112, 113, 114, 115, 116, 117, 118, 119,
93 120, 121, 122, 123, 124, 125, 126, 127},
94 .oobfree = {
95 {.offset = 2,
96 .length = 78}}
97};
98
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020099static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200100 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200102static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
103 struct mtd_oob_ops *ops);
104
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200105/*
Joe Perches8e87d782008-02-03 17:22:34 +0200106 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200107 * compiled away when LED support is disabled.
108 */
109DEFINE_LED_TRIGGER(nand_led_trigger);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/**
112 * nand_release_device - [GENERIC] release chip
113 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000114 *
115 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100117static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200119 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200122 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100123
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200124 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200125 spin_lock(&chip->controller->lock);
126 chip->controller->active = NULL;
127 chip->state = FL_READY;
128 wake_up(&chip->controller->wq);
129 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/**
133 * nand_read_byte - [DEFAULT] read one byte from the chip
134 * @mtd: MTD device structure
135 *
136 * Default read function for 8bit buswith
137 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200138static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200140 struct nand_chip *chip = mtd->priv;
141 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
146 * @mtd: MTD device structure
147 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000148 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * endianess conversion
150 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200151static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200153 struct nand_chip *chip = mtd->priv;
154 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * nand_read_word - [DEFAULT] read one word from the chip
159 * @mtd: MTD device structure
160 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000161 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * endianess conversion
163 */
164static u16 nand_read_word(struct mtd_info *mtd)
165{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200166 struct nand_chip *chip = mtd->priv;
167 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * nand_select_chip - [DEFAULT] control CE line
172 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700173 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Default select function for 1 chip devices.
176 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200177static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200179 struct nand_chip *chip = mtd->priv;
180
181 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200183 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187
188 default:
189 BUG();
190 }
191}
192
193/**
194 * nand_write_buf - [DEFAULT] write buffer to chip
195 * @mtd: MTD device structure
196 * @buf: data buffer
197 * @len: number of bytes to write
198 *
199 * Default write function for 8bit buswith
200 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200201static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200204 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
David Woodhousee0c7d762006-05-13 18:07:53 +0100206 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200207 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000211 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * @mtd: MTD device structure
213 * @buf: buffer to store date
214 * @len: number of bytes to read
215 *
216 * Default read function for 8bit buswith
217 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200218static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200221 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David Woodhousee0c7d762006-05-13 18:07:53 +0100223 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200224 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000228 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * @mtd: MTD device structure
230 * @buf: buffer containing the data to compare
231 * @len: number of bytes to compare
232 *
233 * Default verify function for 8bit buswith
234 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200235static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200238 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
David Woodhousee0c7d762006-05-13 18:07:53 +0100240 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200241 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
246/**
247 * nand_write_buf16 - [DEFAULT] write buffer to chip
248 * @mtd: MTD device structure
249 * @buf: data buffer
250 * @len: number of bytes to write
251 *
252 * Default write function for 16bit buswith
253 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200254static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200257 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 u16 *p = (u16 *) buf;
259 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000260
David Woodhousee0c7d762006-05-13 18:07:53 +0100261 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000267 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * @mtd: MTD device structure
269 * @buf: buffer to store date
270 * @len: number of bytes to read
271 *
272 * Default read function for 16bit buswith
273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 u16 *p = (u16 *) buf;
279 len >>= 1;
280
David Woodhousee0c7d762006-05-13 18:07:53 +0100281 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200282 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000286 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * @mtd: MTD device structure
288 * @buf: buffer containing the data to compare
289 * @len: number of bytes to compare
290 *
291 * Default verify function for 16bit buswith
292 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200293static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200296 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 u16 *p = (u16 *) buf;
298 len >>= 1;
299
David Woodhousee0c7d762006-05-13 18:07:53 +0100300 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200301 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -EFAULT;
303
304 return 0;
305}
306
307/**
308 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
309 * @mtd: MTD device structure
310 * @ofs: offset from device start
311 * @getchip: 0, if the chip is already selected
312 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000313 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
316{
317 int page, chipnr, res = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200318 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 u16 bad;
320
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100321 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200324 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200326 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200329 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200332 if (chip->options & NAND_BUSWIDTH_16) {
333 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100334 page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200335 bad = cpu_to_le16(chip->read_word(mtd));
336 if (chip->badblockpos & 0x1)
Vitaly Wool49196f32005-11-02 16:54:46 +0000337 bad >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if ((bad & 0xFF) != 0xff)
339 res = 1;
340 } else {
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100341 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200342 if (chip->read_byte(mtd) != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 res = 1;
344 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000345
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200346 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return res;
350}
351
352/**
353 * nand_default_block_markbad - [DEFAULT] mark a block bad
354 * @mtd: MTD device structure
355 * @ofs: offset from device start
356 *
357 * This is the default implementation, which can be overridden by
358 * a hardware specific driver.
359*/
360static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
361{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200362 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200363 uint8_t buf[2] = { 0, 0 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200364 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400367 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200368 if (chip->bbt)
369 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200372 if (chip->options & NAND_USE_FLASH_BBT)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200373 ret = nand_update_bbt(mtd, ofs);
374 else {
375 /* We write two bytes, so we dont have to mess with 16 bit
376 * access
377 */
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300378 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200379 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200380 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200381 chip->ops.datbuf = NULL;
382 chip->ops.oobbuf = buf;
383 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000384
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200385 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300386 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200387 }
388 if (!ret)
389 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300390
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000394/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * nand_check_wp - [GENERIC] check if the chip is write protected
396 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000397 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000399 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100401static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200403 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200405 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
406 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409/**
410 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
411 * @mtd: MTD device structure
412 * @ofs: offset from device start
413 * @getchip: 0, if the chip is already selected
414 * @allowbbt: 1, if its allowed to access the bbt area
415 *
416 * Check, if the block is bad. Either by reading the bad block table or
417 * calling of the scan function.
418 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200419static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
420 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200422 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000423
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200424 if (!chip->bbt)
425 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100428 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000431/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000432 * Wait for the ready pin, after a command
433 * The timeout is catched later.
434 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100435void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000436{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200437 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100438 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000439
Richard Purdie8fe833c2006-03-31 02:31:14 -0800440 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000441 /* wait until command is processed or timeout occures */
442 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200443 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800444 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700445 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000446 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800447 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000448}
David Woodhouse4b648b02006-09-25 17:05:24 +0100449EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
452 * nand_command - [DEFAULT] Send command to NAND device
453 * @mtd: MTD device structure
454 * @command: the command to be sent
455 * @column: the column address for this command, -1 if none
456 * @page_addr: the page address for this command, -1 if none
457 *
458 * Send command to NAND device. This function is used for small page
459 * devices (256/512 Bytes per page)
460 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200461static void nand_command(struct mtd_info *mtd, unsigned int command,
462 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200464 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200465 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
468 * Write out the command to the device.
469 */
470 if (command == NAND_CMD_SEQIN) {
471 int readcmd;
472
Joern Engel28318772006-05-22 23:18:05 +0200473 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200475 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 readcmd = NAND_CMD_READOOB;
477 } else if (column < 256) {
478 /* First 256 bytes --> READ0 */
479 readcmd = NAND_CMD_READ0;
480 } else {
481 column -= 256;
482 readcmd = NAND_CMD_READ1;
483 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200484 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200485 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200487 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200489 /*
490 * Address cycle, when necessary
491 */
492 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
493 /* Serially input address */
494 if (column != -1) {
495 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200496 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200497 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200498 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200499 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200501 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200502 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200503 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200504 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200505 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200506 if (chip->chipsize > (32 << 20))
507 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200508 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200509 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000510
511 /*
512 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 case NAND_CMD_PAGEPROG:
518 case NAND_CMD_ERASE1:
519 case NAND_CMD_ERASE2:
520 case NAND_CMD_SEQIN:
521 case NAND_CMD_STATUS:
522 return;
523
524 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200525 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200527 udelay(chip->chip_delay);
528 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200529 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200530 chip->cmd_ctrl(mtd,
531 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200532 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return;
534
David Woodhousee0c7d762006-05-13 18:07:53 +0100535 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000537 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * If we don't have access to the busy pin, we apply the given
539 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100540 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200541 if (!chip->dev_ready) {
542 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Apply this short delay always to ensure that we do wait tWB in
547 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100548 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000549
550 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/**
554 * nand_command_lp - [DEFAULT] Send command to NAND large page device
555 * @mtd: MTD device structure
556 * @command: the command to be sent
557 * @column: the column address for this command, -1 if none
558 * @page_addr: the page address for this command, -1 if none
559 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200560 * Send command to NAND device. This is the version for the new large page
561 * devices We dont have the separate regions as we have in the small page
562 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200564static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
565 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200567 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Emulate NAND_CMD_READOOB */
570 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200571 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 command = NAND_CMD_READ0;
573 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000574
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200575 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200576 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200577 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200580 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* Serially input address */
583 if (column != -1) {
584 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200585 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200587 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200588 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200589 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200592 chip->cmd_ctrl(mtd, page_addr, ctrl);
593 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200594 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200596 if (chip->chipsize > (128 << 20))
597 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200598 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200601 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000602
603 /*
604 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000605 * status, sequential in, and deplete1 need no delay
606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 case NAND_CMD_CACHEDPROG:
610 case NAND_CMD_PAGEPROG:
611 case NAND_CMD_ERASE1:
612 case NAND_CMD_ERASE2:
613 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200614 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000616 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return;
618
David Woodhousee0c7d762006-05-13 18:07:53 +0100619 /*
620 * read error status commands require only a short delay
621 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000622 case NAND_CMD_STATUS_ERROR:
623 case NAND_CMD_STATUS_ERROR0:
624 case NAND_CMD_STATUS_ERROR1:
625 case NAND_CMD_STATUS_ERROR2:
626 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200627 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000628 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200631 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200633 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200634 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
635 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
636 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
637 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200638 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return;
640
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200641 case NAND_CMD_RNDOUT:
642 /* No ready / busy check necessary */
643 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
644 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
645 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
646 NAND_NCE | NAND_CTRL_CHANGE);
647 return;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200650 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
651 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
652 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
653 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000654
David Woodhousee0c7d762006-05-13 18:07:53 +0100655 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000657 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * If we don't have access to the busy pin, we apply the given
659 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100660 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200661 if (!chip->dev_ready) {
662 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Apply this short delay always to ensure that we do wait tWB in
668 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100669 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000670
671 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/**
675 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700676 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000678 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *
680 * Get the device and lock it for exclusive access
681 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200682static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200685 spinlock_t *lock = &chip->controller->lock;
686 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100687 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100688 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100689 spin_lock(lock);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* Hardware controller shared among independend devices */
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200692 /* Hardware controller shared among independend devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200693 if (!chip->controller->active)
694 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200695
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200696 if (chip->controller->active == chip && chip->state == FL_READY) {
697 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100698 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100699 return 0;
700 }
701 if (new_state == FL_PM_SUSPENDED) {
702 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200703 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100704 }
705 set_current_state(TASK_UNINTERRUPTIBLE);
706 add_wait_queue(wq, &wait);
707 spin_unlock(lock);
708 schedule();
709 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto retry;
711}
712
713/**
714 * nand_wait - [DEFAULT] wait until the command is done
715 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700716 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 *
718 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000719 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700721 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200722static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724
David Woodhousee0c7d762006-05-13 18:07:53 +0100725 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200726 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100729 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100731 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Richard Purdie8fe833c2006-03-31 02:31:14 -0800733 led_trigger_event(nand_led_trigger, LED_FULL);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* Apply this short delay always to ensure that we do wait tWB in
736 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100737 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200739 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
740 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000741 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200742 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000744 while (time_before(jiffies, timeo)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200745 if (chip->dev_ready) {
746 if (chip->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200749 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000752 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800754 led_trigger_event(nand_led_trigger, LED_OFF);
755
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200756 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return status;
758}
759
760/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200761 * nand_read_page_raw - [Intern] read raw page data without ecc
762 * @mtd: mtd info structure
763 * @chip: nand chip info structure
764 * @buf: buffer to store read data
David Brownell52ff49d2009-03-04 12:01:36 -0800765 *
766 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200767 */
768static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
769 uint8_t *buf)
770{
771 chip->read_buf(mtd, buf, mtd->writesize);
772 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
773 return 0;
774}
775
776/**
David Brownell52ff49d2009-03-04 12:01:36 -0800777 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
778 * @mtd: mtd info structure
779 * @chip: nand chip info structure
780 * @buf: buffer to store read data
781 *
782 * We need a special oob layout and handling even when OOB isn't used.
783 */
784static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
785 uint8_t *buf)
786{
787 int eccsize = chip->ecc.size;
788 int eccbytes = chip->ecc.bytes;
789 uint8_t *oob = chip->oob_poi;
790 int steps, size;
791
792 for (steps = chip->ecc.steps; steps > 0; steps--) {
793 chip->read_buf(mtd, buf, eccsize);
794 buf += eccsize;
795
796 if (chip->ecc.prepad) {
797 chip->read_buf(mtd, oob, chip->ecc.prepad);
798 oob += chip->ecc.prepad;
799 }
800
801 chip->read_buf(mtd, oob, eccbytes);
802 oob += eccbytes;
803
804 if (chip->ecc.postpad) {
805 chip->read_buf(mtd, oob, chip->ecc.postpad);
806 oob += chip->ecc.postpad;
807 }
808 }
809
810 size = mtd->oobsize - (oob - chip->oob_poi);
811 if (size)
812 chip->read_buf(mtd, oob, size);
813
814 return 0;
815}
816
817/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300818 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200819 * @mtd: mtd info structure
820 * @chip: nand chip info structure
821 * @buf: buffer to store read data
David A. Marlin068e3c02005-01-24 03:07:46 +0000822 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200823static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
824 uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200826 int i, eccsize = chip->ecc.size;
827 int eccbytes = chip->ecc.bytes;
828 int eccsteps = chip->ecc.steps;
829 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100830 uint8_t *ecc_calc = chip->buffers->ecccalc;
831 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100832 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200833
Thomas Gleixner90424de2007-04-05 11:44:05 +0200834 chip->ecc.read_page_raw(mtd, chip, buf);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200835
836 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
837 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
838
839 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200840 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200841
842 eccsteps = chip->ecc.steps;
843 p = buf;
844
845 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
846 int stat;
847
848 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700849 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200850 mtd->ecc_stats.failed++;
851 else
852 mtd->ecc_stats.corrected += stat;
853 }
854 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/**
Alexey Korolev3d459552008-05-15 17:23:18 +0100858 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
859 * @mtd: mtd info structure
860 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +0100861 * @data_offs: offset of requested data within the page
862 * @readlen: data length
863 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +0100864 */
865static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
866{
867 int start_step, end_step, num_steps;
868 uint32_t *eccpos = chip->ecc.layout->eccpos;
869 uint8_t *p;
870 int data_col_addr, i, gaps = 0;
871 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
872 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
873
874 /* Column address wihin the page aligned to ECC size (256bytes). */
875 start_step = data_offs / chip->ecc.size;
876 end_step = (data_offs + readlen - 1) / chip->ecc.size;
877 num_steps = end_step - start_step + 1;
878
879 /* Data size aligned to ECC ecc.size*/
880 datafrag_len = num_steps * chip->ecc.size;
881 eccfrag_len = num_steps * chip->ecc.bytes;
882
883 data_col_addr = start_step * chip->ecc.size;
884 /* If we read not a page aligned data */
885 if (data_col_addr != 0)
886 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
887
888 p = bufpoi + data_col_addr;
889 chip->read_buf(mtd, p, datafrag_len);
890
891 /* Calculate ECC */
892 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
893 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
894
895 /* The performance is faster if to position offsets
896 according to ecc.pos. Let make sure here that
897 there are no gaps in ecc positions */
898 for (i = 0; i < eccfrag_len - 1; i++) {
899 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
900 eccpos[i + start_step * chip->ecc.bytes + 1]) {
901 gaps = 1;
902 break;
903 }
904 }
905 if (gaps) {
906 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
907 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
908 } else {
909 /* send the command to read the particular ecc bytes */
910 /* take care about buswidth alignment in read_buf */
911 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
912 aligned_len = eccfrag_len;
913 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
914 aligned_len++;
915 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
916 aligned_len++;
917
918 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
919 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
920 }
921
922 for (i = 0; i < eccfrag_len; i++)
923 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
924
925 p = bufpoi + data_col_addr;
926 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
927 int stat;
928
929 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
930 if (stat == -1)
931 mtd->ecc_stats.failed++;
932 else
933 mtd->ecc_stats.corrected += stat;
934 }
935 return 0;
936}
937
938/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300939 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200940 * @mtd: mtd info structure
941 * @chip: nand chip info structure
942 * @buf: buffer to store read data
943 *
944 * Not for syndrome calculating ecc controllers which need a special oob layout
945 */
946static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
947 uint8_t *buf)
948{
949 int i, eccsize = chip->ecc.size;
950 int eccbytes = chip->ecc.bytes;
951 int eccsteps = chip->ecc.steps;
952 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100953 uint8_t *ecc_calc = chip->buffers->ecccalc;
954 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100955 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200956
957 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
958 chip->ecc.hwctl(mtd, NAND_ECC_READ);
959 chip->read_buf(mtd, p, eccsize);
960 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
961 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200962 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200963
964 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200965 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200966
967 eccsteps = chip->ecc.steps;
968 p = buf;
969
970 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
971 int stat;
972
973 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700974 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200975 mtd->ecc_stats.failed++;
976 else
977 mtd->ecc_stats.corrected += stat;
978 }
979 return 0;
980}
981
982/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300983 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200984 * @mtd: mtd info structure
985 * @chip: nand chip info structure
986 * @buf: buffer to store read data
987 *
988 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200989 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200990 */
991static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
992 uint8_t *buf)
993{
994 int i, eccsize = chip->ecc.size;
995 int eccbytes = chip->ecc.bytes;
996 int eccsteps = chip->ecc.steps;
997 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200998 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200999
1000 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1001 int stat;
1002
1003 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1004 chip->read_buf(mtd, p, eccsize);
1005
1006 if (chip->ecc.prepad) {
1007 chip->read_buf(mtd, oob, chip->ecc.prepad);
1008 oob += chip->ecc.prepad;
1009 }
1010
1011 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1012 chip->read_buf(mtd, oob, eccbytes);
1013 stat = chip->ecc.correct(mtd, p, oob, NULL);
1014
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001015 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001016 mtd->ecc_stats.failed++;
1017 else
1018 mtd->ecc_stats.corrected += stat;
1019
1020 oob += eccbytes;
1021
1022 if (chip->ecc.postpad) {
1023 chip->read_buf(mtd, oob, chip->ecc.postpad);
1024 oob += chip->ecc.postpad;
1025 }
1026 }
1027
1028 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001029 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001030 if (i)
1031 chip->read_buf(mtd, oob, i);
1032
1033 return 0;
1034}
1035
1036/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001037 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1038 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001039 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001040 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001041 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001042 */
1043static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001044 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001045{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001046 switch(ops->mode) {
1047
1048 case MTD_OOB_PLACE:
1049 case MTD_OOB_RAW:
1050 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1051 return oob + len;
1052
1053 case MTD_OOB_AUTO: {
1054 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001055 uint32_t boffs = 0, roffs = ops->ooboffs;
1056 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001057
1058 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001059 /* Read request not from offset 0 ? */
1060 if (unlikely(roffs)) {
1061 if (roffs >= free->length) {
1062 roffs -= free->length;
1063 continue;
1064 }
1065 boffs = free->offset + roffs;
1066 bytes = min_t(size_t, len,
1067 (free->length - roffs));
1068 roffs = 0;
1069 } else {
1070 bytes = min_t(size_t, len, free->length);
1071 boffs = free->offset;
1072 }
1073 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001074 oob += bytes;
1075 }
1076 return oob;
1077 }
1078 default:
1079 BUG();
1080 }
1081 return NULL;
1082}
1083
1084/**
1085 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001086 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001087 * @mtd: MTD device structure
1088 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001089 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001090 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001091 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001092 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001093static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1094 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001095{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001096 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001097 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001098 struct mtd_ecc_stats stats;
1099 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1100 int sndcmd = 1;
1101 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001102 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001103 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001104 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001106 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001108 chipnr = (int)(from >> chip->chip_shift);
1109 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001111 realpage = (int)(from >> chip->page_shift);
1112 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001114 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001116 buf = ops->datbuf;
1117 oob = ops->oobbuf;
1118
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001119 while(1) {
1120 bytes = min(mtd->writesize - col, readlen);
1121 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001122
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001123 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001124 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001125 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001127 if (likely(sndcmd)) {
1128 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1129 sndcmd = 0;
1130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001132 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001133 if (unlikely(ops->mode == MTD_OOB_RAW))
1134 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
Alexey Korolev3d459552008-05-15 17:23:18 +01001135 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1136 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001137 else
1138 ret = chip->ecc.read_page(mtd, chip, bufpoi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001139 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001140 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001141
1142 /* Transfer not aligned data */
1143 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001144 if (!NAND_SUBPAGE_READ(chip) && !oob)
1145 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001146 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001148
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001149 buf += bytes;
1150
1151 if (unlikely(oob)) {
1152 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001153 if (ops->mode != MTD_OOB_RAW) {
1154 int toread = min(oobreadlen,
1155 chip->ecc.layout->oobavail);
1156 if (toread) {
1157 oob = nand_transfer_oob(chip,
1158 oob, ops, toread);
1159 oobreadlen -= toread;
1160 }
1161 } else
1162 buf = nand_transfer_oob(chip,
1163 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001164 }
1165
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001166 if (!(chip->options & NAND_NO_READRDY)) {
1167 /*
1168 * Apply delay or wait for ready/busy pin. Do
1169 * this before the AUTOINCR check, so no
1170 * problems arise if a chip which does auto
1171 * increment is marked as NOAUTOINCR by the
1172 * board driver.
1173 */
1174 if (!chip->dev_ready)
1175 udelay(chip->chip_delay);
1176 else
1177 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001179 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001180 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001181 buf += bytes;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001184 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001185
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001186 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /* For subsequent reads align to page boundary. */
1190 col = 0;
1191 /* Increment page address */
1192 realpage++;
1193
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001194 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* Check, if we cross a chip boundary */
1196 if (!page) {
1197 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001198 chip->select_chip(mtd, -1);
1199 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001201
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001202 /* Check, if the chip supports auto page increment
1203 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001204 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001205 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001206 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001209 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001210 if (oob)
1211 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001213 if (ret)
1214 return ret;
1215
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001216 if (mtd->ecc_stats.failed - stats.failed)
1217 return -EBADMSG;
1218
1219 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001220}
1221
1222/**
1223 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1224 * @mtd: MTD device structure
1225 * @from: offset to read from
1226 * @len: number of bytes to read
1227 * @retlen: pointer to variable to store the number of read bytes
1228 * @buf: the databuffer to put data
1229 *
1230 * Get hold of the chip and call nand_do_read
1231 */
1232static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1233 size_t *retlen, uint8_t *buf)
1234{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001235 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001236 int ret;
1237
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001238 /* Do not allow reads past end of device */
1239 if ((from + len) > mtd->size)
1240 return -EINVAL;
1241 if (!len)
1242 return 0;
1243
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001244 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001245
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001246 chip->ops.len = len;
1247 chip->ops.datbuf = buf;
1248 chip->ops.oobbuf = NULL;
1249
1250 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001251
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001252 *retlen = chip->ops.retlen;
1253
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001254 nand_release_device(mtd);
1255
1256 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001260 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1261 * @mtd: mtd info structure
1262 * @chip: nand chip info structure
1263 * @page: page number to read
1264 * @sndcmd: flag whether to issue read command or not
1265 */
1266static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1267 int page, int sndcmd)
1268{
1269 if (sndcmd) {
1270 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1271 sndcmd = 0;
1272 }
1273 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1274 return sndcmd;
1275}
1276
1277/**
1278 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1279 * with syndromes
1280 * @mtd: mtd info structure
1281 * @chip: nand chip info structure
1282 * @page: page number to read
1283 * @sndcmd: flag whether to issue read command or not
1284 */
1285static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1286 int page, int sndcmd)
1287{
1288 uint8_t *buf = chip->oob_poi;
1289 int length = mtd->oobsize;
1290 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1291 int eccsize = chip->ecc.size;
1292 uint8_t *bufpoi = buf;
1293 int i, toread, sndrnd = 0, pos;
1294
1295 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1296 for (i = 0; i < chip->ecc.steps; i++) {
1297 if (sndrnd) {
1298 pos = eccsize + i * (eccsize + chunk);
1299 if (mtd->writesize > 512)
1300 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1301 else
1302 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1303 } else
1304 sndrnd = 1;
1305 toread = min_t(int, length, chunk);
1306 chip->read_buf(mtd, bufpoi, toread);
1307 bufpoi += toread;
1308 length -= toread;
1309 }
1310 if (length > 0)
1311 chip->read_buf(mtd, bufpoi, length);
1312
1313 return 1;
1314}
1315
1316/**
1317 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1318 * @mtd: mtd info structure
1319 * @chip: nand chip info structure
1320 * @page: page number to write
1321 */
1322static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1323 int page)
1324{
1325 int status = 0;
1326 const uint8_t *buf = chip->oob_poi;
1327 int length = mtd->oobsize;
1328
1329 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1330 chip->write_buf(mtd, buf, length);
1331 /* Send command to program the OOB data */
1332 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1333
1334 status = chip->waitfunc(mtd, chip);
1335
Savin Zlobec0d420f92006-06-21 11:51:20 +02001336 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001337}
1338
1339/**
1340 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1341 * with syndrome - only for large page flash !
1342 * @mtd: mtd info structure
1343 * @chip: nand chip info structure
1344 * @page: page number to write
1345 */
1346static int nand_write_oob_syndrome(struct mtd_info *mtd,
1347 struct nand_chip *chip, int page)
1348{
1349 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1350 int eccsize = chip->ecc.size, length = mtd->oobsize;
1351 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1352 const uint8_t *bufpoi = chip->oob_poi;
1353
1354 /*
1355 * data-ecc-data-ecc ... ecc-oob
1356 * or
1357 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1358 */
1359 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1360 pos = steps * (eccsize + chunk);
1361 steps = 0;
1362 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001363 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001364
1365 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1366 for (i = 0; i < steps; i++) {
1367 if (sndcmd) {
1368 if (mtd->writesize <= 512) {
1369 uint32_t fill = 0xFFFFFFFF;
1370
1371 len = eccsize;
1372 while (len > 0) {
1373 int num = min_t(int, len, 4);
1374 chip->write_buf(mtd, (uint8_t *)&fill,
1375 num);
1376 len -= num;
1377 }
1378 } else {
1379 pos = eccsize + i * (eccsize + chunk);
1380 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1381 }
1382 } else
1383 sndcmd = 1;
1384 len = min_t(int, length, chunk);
1385 chip->write_buf(mtd, bufpoi, len);
1386 bufpoi += len;
1387 length -= len;
1388 }
1389 if (length > 0)
1390 chip->write_buf(mtd, bufpoi, length);
1391
1392 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1393 status = chip->waitfunc(mtd, chip);
1394
1395 return status & NAND_STATUS_FAIL ? -EIO : 0;
1396}
1397
1398/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001399 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * @mtd: MTD device structure
1401 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001402 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
1404 * NAND read out-of-band data from the spare area
1405 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001406static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1407 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001409 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001410 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001411 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001412 int readlen = ops->ooblen;
1413 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001414 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Andrew Morton7e9a0bb2006-05-30 09:06:41 +01001416 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1417 (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Adrian Hunter03736152007-01-31 17:58:29 +02001419 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001420 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001421 else
1422 len = mtd->oobsize;
1423
1424 if (unlikely(ops->ooboffs >= len)) {
1425 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1426 "Attempt to start read outside oob\n");
1427 return -EINVAL;
1428 }
1429
1430 /* Do not allow reads past end of device */
1431 if (unlikely(from >= mtd->size ||
1432 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1433 (from >> chip->page_shift)) * len)) {
1434 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1435 "Attempt read beyond end of device\n");
1436 return -EINVAL;
1437 }
Vitaly Wool70145682006-11-03 18:20:38 +03001438
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001439 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001440 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001442 /* Shift to get page */
1443 realpage = (int)(from >> chip->page_shift);
1444 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001446 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001447 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001448
1449 len = min(len, readlen);
1450 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001451
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001452 if (!(chip->options & NAND_NO_READRDY)) {
1453 /*
1454 * Apply delay or wait for ready/busy pin. Do this
1455 * before the AUTOINCR check, so no problems arise if a
1456 * chip which does auto increment is marked as
1457 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001458 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001459 if (!chip->dev_ready)
1460 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001461 else
1462 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001464
Vitaly Wool70145682006-11-03 18:20:38 +03001465 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001466 if (!readlen)
1467 break;
1468
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001469 /* Increment page address */
1470 realpage++;
1471
1472 page = realpage & chip->pagemask;
1473 /* Check, if we cross a chip boundary */
1474 if (!page) {
1475 chipnr++;
1476 chip->select_chip(mtd, -1);
1477 chip->select_chip(mtd, chipnr);
1478 }
1479
1480 /* Check, if the chip supports auto page increment
1481 * or if we have hit a block boundary.
1482 */
1483 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1484 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Vitaly Wool70145682006-11-03 18:20:38 +03001487 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489}
1490
1491/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001492 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001495 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001497 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001499static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1500 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001502 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001503 int ret = -ENOTSUPP;
1504
1505 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001508 if (ops->datbuf && (from + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001509 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001510 "Attempt read beyond end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EINVAL;
1512 }
1513
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001514 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001516 switch(ops->mode) {
1517 case MTD_OOB_PLACE:
1518 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001519 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001520 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001521
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001522 default:
1523 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001526 if (!ops->datbuf)
1527 ret = nand_do_read_oob(mtd, from, ops);
1528 else
1529 ret = nand_do_read_ops(mtd, from, ops);
1530
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001531 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001533 return ret;
1534}
1535
1536
1537/**
1538 * nand_write_page_raw - [Intern] raw page write function
1539 * @mtd: mtd info structure
1540 * @chip: nand chip info structure
1541 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001542 *
1543 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001544 */
1545static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1546 const uint8_t *buf)
1547{
1548 chip->write_buf(mtd, buf, mtd->writesize);
1549 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001552/**
David Brownell52ff49d2009-03-04 12:01:36 -08001553 * nand_write_page_raw_syndrome - [Intern] raw page write function
1554 * @mtd: mtd info structure
1555 * @chip: nand chip info structure
1556 * @buf: data buffer
1557 *
1558 * We need a special oob layout and handling even when ECC isn't checked.
1559 */
1560static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1561 const uint8_t *buf)
1562{
1563 int eccsize = chip->ecc.size;
1564 int eccbytes = chip->ecc.bytes;
1565 uint8_t *oob = chip->oob_poi;
1566 int steps, size;
1567
1568 for (steps = chip->ecc.steps; steps > 0; steps--) {
1569 chip->write_buf(mtd, buf, eccsize);
1570 buf += eccsize;
1571
1572 if (chip->ecc.prepad) {
1573 chip->write_buf(mtd, oob, chip->ecc.prepad);
1574 oob += chip->ecc.prepad;
1575 }
1576
1577 chip->read_buf(mtd, oob, eccbytes);
1578 oob += eccbytes;
1579
1580 if (chip->ecc.postpad) {
1581 chip->write_buf(mtd, oob, chip->ecc.postpad);
1582 oob += chip->ecc.postpad;
1583 }
1584 }
1585
1586 size = mtd->oobsize - (oob - chip->oob_poi);
1587 if (size)
1588 chip->write_buf(mtd, oob, size);
1589}
1590/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001591 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001592 * @mtd: mtd info structure
1593 * @chip: nand chip info structure
1594 * @buf: data buffer
1595 */
1596static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1597 const uint8_t *buf)
1598{
1599 int i, eccsize = chip->ecc.size;
1600 int eccbytes = chip->ecc.bytes;
1601 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001602 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001603 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001604 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001605
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001606 /* Software ecc calculation */
1607 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1608 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001609
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001610 for (i = 0; i < chip->ecc.total; i++)
1611 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001612
Thomas Gleixner90424de2007-04-05 11:44:05 +02001613 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001614}
1615
1616/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001617 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001618 * @mtd: mtd info structure
1619 * @chip: nand chip info structure
1620 * @buf: data buffer
1621 */
1622static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1623 const uint8_t *buf)
1624{
1625 int i, eccsize = chip->ecc.size;
1626 int eccbytes = chip->ecc.bytes;
1627 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001628 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001629 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001630 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001631
1632 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1633 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001634 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001635 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1636 }
1637
1638 for (i = 0; i < chip->ecc.total; i++)
1639 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1640
1641 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1642}
1643
1644/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001645 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001646 * @mtd: mtd info structure
1647 * @chip: nand chip info structure
1648 * @buf: data buffer
1649 *
1650 * The hw generator calculates the error syndrome automatically. Therefor
1651 * we need a special oob layout and handling.
1652 */
1653static void nand_write_page_syndrome(struct mtd_info *mtd,
1654 struct nand_chip *chip, const uint8_t *buf)
1655{
1656 int i, eccsize = chip->ecc.size;
1657 int eccbytes = chip->ecc.bytes;
1658 int eccsteps = chip->ecc.steps;
1659 const uint8_t *p = buf;
1660 uint8_t *oob = chip->oob_poi;
1661
1662 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1663
1664 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1665 chip->write_buf(mtd, p, eccsize);
1666
1667 if (chip->ecc.prepad) {
1668 chip->write_buf(mtd, oob, chip->ecc.prepad);
1669 oob += chip->ecc.prepad;
1670 }
1671
1672 chip->ecc.calculate(mtd, p, oob);
1673 chip->write_buf(mtd, oob, eccbytes);
1674 oob += eccbytes;
1675
1676 if (chip->ecc.postpad) {
1677 chip->write_buf(mtd, oob, chip->ecc.postpad);
1678 oob += chip->ecc.postpad;
1679 }
1680 }
1681
1682 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001683 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001684 if (i)
1685 chip->write_buf(mtd, oob, i);
1686}
1687
1688/**
David Woodhouse956e9442006-09-25 17:12:39 +01001689 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001690 * @mtd: MTD device structure
1691 * @chip: NAND chip descriptor
1692 * @buf: the data to write
1693 * @page: page number to write
1694 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02001695 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001696 */
1697static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001698 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001699{
1700 int status;
1701
1702 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1703
David Woodhouse956e9442006-09-25 17:12:39 +01001704 if (unlikely(raw))
1705 chip->ecc.write_page_raw(mtd, chip, buf);
1706 else
1707 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001708
1709 /*
1710 * Cached progamming disabled for now, Not sure if its worth the
1711 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1712 */
1713 cached = 0;
1714
1715 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1716
1717 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001718 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001719 /*
1720 * See if operation failed and additional status checks are
1721 * available
1722 */
1723 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1724 status = chip->errstat(mtd, chip, FL_WRITING, status,
1725 page);
1726
1727 if (status & NAND_STATUS_FAIL)
1728 return -EIO;
1729 } else {
1730 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001731 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001732 }
1733
1734#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1735 /* Send command to read back the data */
1736 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1737
1738 if (chip->verify_buf(mtd, buf, mtd->writesize))
1739 return -EIO;
1740#endif
1741 return 0;
1742}
1743
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001744/**
1745 * nand_fill_oob - [Internal] Transfer client buffer to oob
1746 * @chip: nand chip structure
1747 * @oob: oob data buffer
1748 * @ops: oob ops structure
1749 */
1750static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1751 struct mtd_oob_ops *ops)
1752{
1753 size_t len = ops->ooblen;
1754
1755 switch(ops->mode) {
1756
1757 case MTD_OOB_PLACE:
1758 case MTD_OOB_RAW:
1759 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1760 return oob + len;
1761
1762 case MTD_OOB_AUTO: {
1763 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001764 uint32_t boffs = 0, woffs = ops->ooboffs;
1765 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001766
1767 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001768 /* Write request not from offset 0 ? */
1769 if (unlikely(woffs)) {
1770 if (woffs >= free->length) {
1771 woffs -= free->length;
1772 continue;
1773 }
1774 boffs = free->offset + woffs;
1775 bytes = min_t(size_t, len,
1776 (free->length - woffs));
1777 woffs = 0;
1778 } else {
1779 bytes = min_t(size_t, len, free->length);
1780 boffs = free->offset;
1781 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001782 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001783 oob += bytes;
1784 }
1785 return oob;
1786 }
1787 default:
1788 BUG();
1789 }
1790 return NULL;
1791}
1792
Thomas Gleixner29072b92006-09-28 15:38:36 +02001793#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001794
1795/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001796 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001797 * @mtd: MTD device structure
1798 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001799 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001800 *
1801 * NAND write with ECC
1802 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001803static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1804 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001805{
Thomas Gleixner29072b92006-09-28 15:38:36 +02001806 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001807 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001808 uint32_t writelen = ops->len;
1809 uint8_t *oob = ops->oobbuf;
1810 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001811 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001812
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001813 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001814 if (!writelen)
1815 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001816
1817 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001818 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001819 printk(KERN_NOTICE "nand_write: "
1820 "Attempt to write not page aligned data\n");
1821 return -EINVAL;
1822 }
1823
Thomas Gleixner29072b92006-09-28 15:38:36 +02001824 column = to & (mtd->writesize - 1);
1825 subpage = column || (writelen & (mtd->writesize - 1));
1826
1827 if (subpage && oob)
1828 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001829
Thomas Gleixner6a930962006-06-28 00:11:45 +02001830 chipnr = (int)(to >> chip->chip_shift);
1831 chip->select_chip(mtd, chipnr);
1832
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001833 /* Check, if it is write protected */
1834 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001835 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001836
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001837 realpage = (int)(to >> chip->page_shift);
1838 page = realpage & chip->pagemask;
1839 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1840
1841 /* Invalidate the page cache, when we write to the cached page */
1842 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001843 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001844 chip->pagebuf = -1;
1845
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001846 /* If we're not given explicit OOB data, let it be 0xFF */
1847 if (likely(!oob))
1848 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001849
1850 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02001851 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001852 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001853 uint8_t *wbuf = buf;
1854
1855 /* Partial page write ? */
1856 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1857 cached = 0;
1858 bytes = min_t(int, bytes - column, (int) writelen);
1859 chip->pagebuf = -1;
1860 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1861 memcpy(&chip->buffers->databuf[column], buf, bytes);
1862 wbuf = chip->buffers->databuf;
1863 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001864
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001865 if (unlikely(oob))
1866 oob = nand_fill_oob(chip, oob, ops);
1867
Thomas Gleixner29072b92006-09-28 15:38:36 +02001868 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01001869 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001870 if (ret)
1871 break;
1872
1873 writelen -= bytes;
1874 if (!writelen)
1875 break;
1876
Thomas Gleixner29072b92006-09-28 15:38:36 +02001877 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001878 buf += bytes;
1879 realpage++;
1880
1881 page = realpage & chip->pagemask;
1882 /* Check, if we cross a chip boundary */
1883 if (!page) {
1884 chipnr++;
1885 chip->select_chip(mtd, -1);
1886 chip->select_chip(mtd, chipnr);
1887 }
1888 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001889
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001890 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03001891 if (unlikely(oob))
1892 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001893 return ret;
1894}
1895
1896/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001897 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 * @mtd: MTD device structure
1899 * @to: offset to write to
1900 * @len: number of bytes to write
1901 * @retlen: pointer to variable to store the number of written bytes
1902 * @buf: the data to write
1903 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001904 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001906static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001907 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001909 struct nand_chip *chip = mtd->priv;
1910 int ret;
1911
1912 /* Do not allow reads past end of device */
1913 if ((to + len) > mtd->size)
1914 return -EINVAL;
1915 if (!len)
1916 return 0;
1917
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001918 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001919
1920 chip->ops.len = len;
1921 chip->ops.datbuf = (uint8_t *)buf;
1922 chip->ops.oobbuf = NULL;
1923
1924 ret = nand_do_write_ops(mtd, to, &chip->ops);
1925
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001926 *retlen = chip->ops.retlen;
1927
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001928 nand_release_device(mtd);
1929
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001930 return ret;
1931}
1932
1933/**
1934 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1935 * @mtd: MTD device structure
1936 * @to: offset to write to
1937 * @ops: oob operation description structure
1938 *
1939 * NAND write out-of-band
1940 */
1941static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1942 struct mtd_oob_ops *ops)
1943{
Adrian Hunter03736152007-01-31 17:58:29 +02001944 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001945 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001947 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
Vitaly Wool70145682006-11-03 18:20:38 +03001948 (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Adrian Hunter03736152007-01-31 17:58:29 +02001950 if (ops->mode == MTD_OOB_AUTO)
1951 len = chip->ecc.layout->oobavail;
1952 else
1953 len = mtd->oobsize;
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02001956 if ((ops->ooboffs + ops->ooblen) > len) {
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001957 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1958 "Attempt to write past end of page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return -EINVAL;
1960 }
1961
Adrian Hunter03736152007-01-31 17:58:29 +02001962 if (unlikely(ops->ooboffs >= len)) {
David Brownell374555a2009-03-04 12:01:38 -08001963 DEBUG(MTD_DEBUG_LEVEL0, "nand_do_write_oob: "
Adrian Hunter03736152007-01-31 17:58:29 +02001964 "Attempt to start write outside oob\n");
1965 return -EINVAL;
1966 }
1967
1968 /* Do not allow reads past end of device */
1969 if (unlikely(to >= mtd->size ||
1970 ops->ooboffs + ops->ooblen >
1971 ((mtd->size >> chip->page_shift) -
1972 (to >> chip->page_shift)) * len)) {
David Brownell374555a2009-03-04 12:01:38 -08001973 DEBUG(MTD_DEBUG_LEVEL0, "nand_do_write_oob: "
Adrian Hunter03736152007-01-31 17:58:29 +02001974 "Attempt write beyond end of device\n");
1975 return -EINVAL;
1976 }
1977
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001978 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001979 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001981 /* Shift to get page */
1982 page = (int)(to >> chip->page_shift);
1983
1984 /*
1985 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1986 * of my DiskOnChip 2000 test units) will clear the whole data page too
1987 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1988 * it in the doc2000 driver in August 1999. dwmw2.
1989 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001990 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 /* Check, if it is write protected */
1993 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001994 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001997 if (page == chip->pagebuf)
1998 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002000 memset(chip->oob_poi, 0xff, mtd->oobsize);
2001 nand_fill_oob(chip, ops->oobbuf, ops);
2002 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2003 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002004
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002005 if (status)
2006 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Vitaly Wool70145682006-11-03 18:20:38 +03002008 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002010 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002013/**
2014 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2015 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002016 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002017 * @ops: oob operation description structure
2018 */
2019static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2020 struct mtd_oob_ops *ops)
2021{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002022 struct nand_chip *chip = mtd->priv;
2023 int ret = -ENOTSUPP;
2024
2025 ops->retlen = 0;
2026
2027 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002028 if (ops->datbuf && (to + ops->len) > mtd->size) {
David Brownell374555a2009-03-04 12:01:38 -08002029 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
2030 "Attempt write beyond end of device\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002031 return -EINVAL;
2032 }
2033
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002034 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002035
2036 switch(ops->mode) {
2037 case MTD_OOB_PLACE:
2038 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002039 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002040 break;
2041
2042 default:
2043 goto out;
2044 }
2045
2046 if (!ops->datbuf)
2047 ret = nand_do_write_oob(mtd, to, ops);
2048 else
2049 ret = nand_do_write_ops(mtd, to, ops);
2050
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002051 out:
2052 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return ret;
2054}
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2058 * @mtd: MTD device structure
2059 * @page: the page address of the block which will be erased
2060 *
2061 * Standard erase command for NAND chips
2062 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002063static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002065 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002067 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2068 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
2071/**
2072 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2073 * @mtd: MTD device structure
2074 * @page: the page address of the block which will be erased
2075 *
2076 * AND multi block erase command function
2077 * Erase 4 consecutive blocks
2078 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002079static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002081 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002083 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2084 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2085 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2086 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2087 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
2090/**
2091 * nand_erase - [MTD Interface] erase block(s)
2092 * @mtd: MTD device structure
2093 * @instr: erase instruction
2094 *
2095 * Erase one ore more blocks
2096 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002097static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
David Woodhousee0c7d762006-05-13 18:07:53 +01002099 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002101
David A. Marlin30f464b2005-01-17 18:35:25 +00002102#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002104 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 * @mtd: MTD device structure
2106 * @instr: erase instruction
2107 * @allowbbt: allow erasing the bbt area
2108 *
2109 * Erase one ore more blocks
2110 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002111int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2112 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Adrian Hunter69423d92008-12-10 13:37:21 +00002114 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002115 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002116 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002117 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002118 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Adrian Hunter69423d92008-12-10 13:37:21 +00002120 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%012llx, len = %llu\n",
2121 (unsigned long long)instr->addr, (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002124 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002125 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return -EINVAL;
2127 }
2128
2129 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002130 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
2131 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2132 "Length not block aligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return -EINVAL;
2134 }
2135
2136 /* Do not allow erase past end of device */
2137 if ((instr->len + instr->addr) > mtd->size) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002138 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2139 "Erase past end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return -EINVAL;
2141 }
2142
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002143 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002146 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002149 page = (int)(instr->addr >> chip->page_shift);
2150 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002153 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002156 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /* Check, if it is write protected */
2159 if (nand_check_wp(mtd)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002160 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2161 "Device is write protected!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 instr->state = MTD_ERASE_FAILED;
2163 goto erase_exit;
2164 }
2165
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002166 /*
2167 * If BBT requires refresh, set the BBT page mask to see if the BBT
2168 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2169 * can not be matched. This is also done when the bbt is actually
2170 * erased to avoid recusrsive updates
2171 */
2172 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2173 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /* Loop through the pages */
2176 len = instr->len;
2177
2178 instr->state = MTD_ERASING;
2179
2180 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002181 /*
2182 * heck if we have a bad block, we do not erase bad blocks !
2183 */
2184 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2185 chip->page_shift, 0, allowbbt)) {
2186 printk(KERN_WARNING "nand_erase: attempt to erase a "
2187 "bad block at page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 instr->state = MTD_ERASE_FAILED;
2189 goto erase_exit;
2190 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002191
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002192 /*
2193 * Invalidate the page cache, if we erase the block which
2194 * contains the current cached page
2195 */
2196 if (page <= chip->pagebuf && chip->pagebuf <
2197 (page + pages_per_block))
2198 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002200 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002201
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002202 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002204 /*
2205 * See if operation failed and additional status checks are
2206 * available
2207 */
2208 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2209 status = chip->errstat(mtd, chip, FL_ERASING,
2210 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002213 if (status & NAND_STATUS_FAIL) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002214 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2215 "Failed erase, page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002217 instr->fail_addr =
2218 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto erase_exit;
2220 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002221
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002222 /*
2223 * If BBT requires refresh, set the BBT rewrite flag to the
2224 * page being erased
2225 */
2226 if (bbt_masked_page != 0xffffffff &&
2227 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002228 rewrite_bbt[chipnr] =
2229 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002232 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 page += pages_per_block;
2234
2235 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002236 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002238 chip->select_chip(mtd, -1);
2239 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002240
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002241 /*
2242 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2243 * page mask to see if this BBT should be rewritten
2244 */
2245 if (bbt_masked_page != 0xffffffff &&
2246 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2247 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2248 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 }
2251 instr->state = MTD_ERASE_DONE;
2252
David Woodhousee0c7d762006-05-13 18:07:53 +01002253 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 /* Deselect and wake up anyone waiting on the device */
2258 nand_release_device(mtd);
2259
David Woodhouse49defc02007-10-06 15:01:59 -04002260 /* Do call back function */
2261 if (!ret)
2262 mtd_erase_callback(instr);
2263
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002264 /*
2265 * If BBT requires refresh and erase was successful, rewrite any
2266 * selected bad block tables
2267 */
2268 if (bbt_masked_page == 0xffffffff || ret)
2269 return ret;
2270
2271 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2272 if (!rewrite_bbt[chipnr])
2273 continue;
2274 /* update the BBT for chip */
2275 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
Adrian Hunter69423d92008-12-10 13:37:21 +00002276 "(%d:0x%0llx 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002277 chip->bbt_td->pages[chipnr]);
2278 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002279 }
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /* Return more or less happy */
2282 return ret;
2283}
2284
2285/**
2286 * nand_sync - [MTD Interface] sync
2287 * @mtd: MTD device structure
2288 *
2289 * Sync is actually a wait for chip ready function
2290 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002291static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002293 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
David Woodhousee0c7d762006-05-13 18:07:53 +01002295 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002298 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002300 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002304 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002306 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002308static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002311 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002313
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002314 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
2317/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002318 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 * @mtd: MTD device structure
2320 * @ofs: offset relative to mtd start
2321 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002322static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002324 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 int ret;
2326
David Woodhousee0c7d762006-05-13 18:07:53 +01002327 if ((ret = nand_block_isbad(mtd, ofs))) {
2328 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (ret > 0)
2330 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002331 return ret;
2332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002334 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
2337/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002338 * nand_suspend - [MTD Interface] Suspend the NAND flash
2339 * @mtd: MTD device structure
2340 */
2341static int nand_suspend(struct mtd_info *mtd)
2342{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002343 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002344
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002345 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002346}
2347
2348/**
2349 * nand_resume - [MTD Interface] Resume the NAND flash
2350 * @mtd: MTD device structure
2351 */
2352static void nand_resume(struct mtd_info *mtd)
2353{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002354 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002355
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002356 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002357 nand_release_device(mtd);
2358 else
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02002359 printk(KERN_ERR "nand_resume() called for a chip which is not "
2360 "in suspended state\n");
Vitaly Wool962034f2005-09-15 14:58:53 +01002361}
2362
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002363/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002364 * Set default functions
2365 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002366static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002369 if (!chip->chip_delay)
2370 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002373 if (chip->cmdfunc == NULL)
2374 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002377 if (chip->waitfunc == NULL)
2378 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002380 if (!chip->select_chip)
2381 chip->select_chip = nand_select_chip;
2382 if (!chip->read_byte)
2383 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2384 if (!chip->read_word)
2385 chip->read_word = nand_read_word;
2386 if (!chip->block_bad)
2387 chip->block_bad = nand_block_bad;
2388 if (!chip->block_markbad)
2389 chip->block_markbad = nand_default_block_markbad;
2390 if (!chip->write_buf)
2391 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2392 if (!chip->read_buf)
2393 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2394 if (!chip->verify_buf)
2395 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2396 if (!chip->scan_bbt)
2397 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002398
2399 if (!chip->controller) {
2400 chip->controller = &chip->hwcontrol;
2401 spin_lock_init(&chip->controller->lock);
2402 init_waitqueue_head(&chip->controller->wq);
2403 }
2404
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002405}
2406
2407/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002408 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002409 */
2410static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002411 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002412 int busw, int *maf_id)
2413{
2414 struct nand_flash_dev *type = NULL;
2415 int i, dev_id, maf_idx;
Ben Dooksed8165c2008-04-14 14:58:58 +01002416 int tmp_id, tmp_manf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002419 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Karl Beldanef89a882008-09-15 14:37:29 +02002421 /*
2422 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2423 * after power-up
2424 */
2425 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002428 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002431 *maf_id = chip->read_byte(mtd);
2432 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Ben Dooksed8165c2008-04-14 14:58:58 +01002434 /* Try again to make sure, as some systems the bus-hold or other
2435 * interface concerns can cause random data which looks like a
2436 * possibly credible NAND flash to appear. If the two results do
2437 * not match, ignore the device completely.
2438 */
2439
2440 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2441
2442 /* Read manufacturer and device IDs */
2443
2444 tmp_manf = chip->read_byte(mtd);
2445 tmp_id = chip->read_byte(mtd);
2446
2447 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2448 printk(KERN_INFO "%s: second ID read did not match "
2449 "%02x,%02x against %02x,%02x\n", __func__,
2450 *maf_id, dev_id, tmp_manf, tmp_id);
2451 return ERR_PTR(-ENODEV);
2452 }
2453
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002454 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002456 if (dev_id == nand_flash_ids[i].id) {
2457 type = &nand_flash_ids[i];
2458 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
2461
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002462 if (!type)
2463 return ERR_PTR(-ENODEV);
2464
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002465 if (!mtd->name)
2466 mtd->name = type->name;
2467
Adrian Hunter69423d92008-12-10 13:37:21 +00002468 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002469
2470 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002471 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002472 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002473 /* The 3rd id byte holds MLC / multichip data */
2474 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002475 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002476 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002477 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002478 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002479 extid >>= 2;
2480 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002481 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002482 extid >>= 2;
2483 /* Calc blocksize. Blocksize is multiples of 64KiB */
2484 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2485 extid >>= 2;
2486 /* Get buswidth information */
2487 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2488
2489 } else {
2490 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002491 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002492 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002493 mtd->erasesize = type->erasesize;
2494 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002495 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002496 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002497 }
2498
2499 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002500 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002501 if (nand_manuf_ids[maf_idx].id == *maf_id)
2502 break;
2503 }
2504
2505 /*
2506 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002507 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002508 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002509 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002510 printk(KERN_INFO "NAND device: Manufacturer ID:"
2511 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2512 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2513 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002514 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002515 busw ? 16 : 8);
2516 return ERR_PTR(-EINVAL);
2517 }
2518
2519 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002520 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002521 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002522 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002523
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002524 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002525 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002526 if (chip->chipsize & 0xffffffff)
2527 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2528 else
2529 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002530
2531 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002532 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002533 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2534
2535 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002536 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002537 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002538
2539 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002540 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002541 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002542 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002543
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002544 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002545 * options for chips which are not having an extended id.
2546 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002547 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002548 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002549
2550 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002551 if (chip->options & NAND_4PAGE_ARRAY)
2552 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002553 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002554 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002555
2556 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002557 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2558 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002559
2560 printk(KERN_INFO "NAND device: Manufacturer ID:"
2561 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2562 nand_manuf_ids[maf_idx].name, type->name);
2563
2564 return type;
2565}
2566
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002567/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002568 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2569 * @mtd: MTD device structure
2570 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002571 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002572 * This is the first phase of the normal nand_scan() function. It
2573 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002574 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002575 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002576 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002577int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002578{
2579 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002580 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002581 struct nand_flash_dev *type;
2582
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002583 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002584 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002585 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002586 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002587
2588 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002589 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002590
2591 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002592 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002593 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002594 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002597 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002598 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002599 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02002600 /* See comment in nand_get_flash_type for reset */
2601 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002603 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002605 if (nand_maf_id != chip->read_byte(mtd) ||
2606 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 break;
2608 }
2609 if (i > 1)
2610 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002613 chip->numchips = i;
2614 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
David Woodhouse3b85c322006-09-25 17:06:53 +01002616 return 0;
2617}
2618
2619
2620/**
2621 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2622 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01002623 *
2624 * This is the second phase of the normal nand_scan() function. It
2625 * fills out all the uninitialized function pointers with the defaults
2626 * and scans for a bad block table if appropriate.
2627 */
2628int nand_scan_tail(struct mtd_info *mtd)
2629{
2630 int i;
2631 struct nand_chip *chip = mtd->priv;
2632
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002633 if (!(chip->options & NAND_OWN_BUFFERS))
2634 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2635 if (!chip->buffers)
2636 return -ENOMEM;
2637
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002638 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002639 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002640
2641 /*
2642 * If no default placement scheme is given, select an appropriate one
2643 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002644 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002645 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002647 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 break;
2649 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002650 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 break;
2652 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002653 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002655 case 128:
2656 chip->ecc.layout = &nand_oob_128;
2657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002659 printk(KERN_WARNING "No oob scheme defined for "
2660 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 BUG();
2662 }
2663 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002664
David Woodhouse956e9442006-09-25 17:12:39 +01002665 if (!chip->write_page)
2666 chip->write_page = nand_write_page;
2667
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002668 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002669 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2670 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002671 */
David Woodhouse956e9442006-09-25 17:12:39 +01002672
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002673 switch (chip->ecc.mode) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002674 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002675 /* Use standard hwecc read page function ? */
2676 if (!chip->ecc.read_page)
2677 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002678 if (!chip->ecc.write_page)
2679 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002680 if (!chip->ecc.read_page_raw)
2681 chip->ecc.read_page_raw = nand_read_page_raw;
2682 if (!chip->ecc.write_page_raw)
2683 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002684 if (!chip->ecc.read_oob)
2685 chip->ecc.read_oob = nand_read_oob_std;
2686 if (!chip->ecc.write_oob)
2687 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002688
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002689 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06002690 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2691 !chip->ecc.hwctl) &&
2692 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002693 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06002694 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002695 chip->ecc.write_page == nand_write_page_hwecc)) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002696 printk(KERN_WARNING "No ECC functions supplied, "
2697 "Hardware ECC not possible\n");
2698 BUG();
2699 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002700 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002701 if (!chip->ecc.read_page)
2702 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002703 if (!chip->ecc.write_page)
2704 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08002705 if (!chip->ecc.read_page_raw)
2706 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2707 if (!chip->ecc.write_page_raw)
2708 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002709 if (!chip->ecc.read_oob)
2710 chip->ecc.read_oob = nand_read_oob_syndrome;
2711 if (!chip->ecc.write_oob)
2712 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002713
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002714 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002715 break;
2716 printk(KERN_WARNING "%d byte HW ECC not possible on "
2717 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002718 chip->ecc.size, mtd->writesize);
2719 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002721 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002722 chip->ecc.calculate = nand_calculate_ecc;
2723 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002724 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01002725 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002726 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002727 chip->ecc.read_page_raw = nand_read_page_raw;
2728 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002729 chip->ecc.read_oob = nand_read_oob_std;
2730 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002731 chip->ecc.size = 256;
2732 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002734
2735 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002736 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2737 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002738 chip->ecc.read_page = nand_read_page_raw;
2739 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002740 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08002741 chip->ecc.read_page_raw = nand_read_page_raw;
2742 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002743 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002744 chip->ecc.size = mtd->writesize;
2745 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002749 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002750 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002751 BUG();
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002754 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002755 * The number of bytes available for a client to place data into
2756 * the out of band area
2757 */
2758 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07002759 for (i = 0; chip->ecc.layout->oobfree[i].length
2760 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002761 chip->ecc.layout->oobavail +=
2762 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002763 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002764
2765 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002766 * Set the number of read / write steps for one page depending on ECC
2767 * mode
2768 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002769 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2770 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002771 printk(KERN_WARNING "Invalid ecc parameters\n");
2772 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002774 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002775
Thomas Gleixner29072b92006-09-28 15:38:36 +02002776 /*
2777 * Allow subpage writes up to ecc.steps. Not possible for MLC
2778 * FLASH.
2779 */
2780 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2781 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2782 switch(chip->ecc.steps) {
2783 case 2:
2784 mtd->subpage_sft = 1;
2785 break;
2786 case 4:
2787 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002788 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02002789 mtd->subpage_sft = 2;
2790 break;
2791 }
2792 }
2793 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2794
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002795 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002796 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002799 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002802 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 /* Fill in remaining MTD driver data */
2805 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002806 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 mtd->erase = nand_erase;
2808 mtd->point = NULL;
2809 mtd->unpoint = NULL;
2810 mtd->read = nand_read;
2811 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 mtd->read_oob = nand_read_oob;
2813 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 mtd->sync = nand_sync;
2815 mtd->lock = NULL;
2816 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002817 mtd->suspend = nand_suspend;
2818 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 mtd->block_isbad = nand_block_isbad;
2820 mtd->block_markbad = nand_block_markbad;
2821
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002822 /* propagate ecc.layout to mtd_info */
2823 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002825 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002826 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002830 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
Rusty Russella6e6abd2009-03-31 13:05:31 -06002833/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01002834 test if this is a module _anyway_ -- they'd have to try _really_ hard
2835 to call us from in-kernel code if the core NAND support is modular. */
2836#ifdef MODULE
2837#define caller_is_module() (1)
2838#else
2839#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06002840 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01002841#endif
2842
2843/**
2844 * nand_scan - [NAND Interface] Scan for the NAND device
2845 * @mtd: MTD device structure
2846 * @maxchips: Number of chips to scan for
2847 *
2848 * This fills out all the uninitialized function pointers
2849 * with the defaults.
2850 * The flash ID is read and the mtd/chip structures are
2851 * filled with the appropriate values.
2852 * The mtd->owner field must be set to the module of the caller
2853 *
2854 */
2855int nand_scan(struct mtd_info *mtd, int maxchips)
2856{
2857 int ret;
2858
2859 /* Many callers got this wrong, so check for it for a while... */
2860 if (!mtd->owner && caller_is_module()) {
2861 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2862 BUG();
2863 }
2864
2865 ret = nand_scan_ident(mtd, maxchips);
2866 if (!ret)
2867 ret = nand_scan_tail(mtd);
2868 return ret;
2869}
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002872 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 * @mtd: MTD device structure
2874*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002875void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002877 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879#ifdef CONFIG_MTD_PARTITIONS
2880 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002881 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882#endif
2883 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002884 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Jesper Juhlfa671642005-11-07 01:01:27 -08002886 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002887 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002888 if (!(chip->options & NAND_OWN_BUFFERS))
2889 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
David Woodhousee0c7d762006-05-13 18:07:53 +01002892EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01002893EXPORT_SYMBOL_GPL(nand_scan_ident);
2894EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01002895EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002896
2897static int __init nand_base_init(void)
2898{
2899 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2900 return 0;
2901}
2902
2903static void __exit nand_base_exit(void)
2904{
2905 led_trigger_unregister_simple(nand_led_trigger);
2906}
2907
2908module_init(nand_base_init);
2909module_exit(nand_base_exit);
2910
David Woodhousee0c7d762006-05-13 18:07:53 +01002911MODULE_LICENSE("GPL");
2912MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2913MODULE_DESCRIPTION("Generic NAND flash driver code");