blob: 8f2958fe2148a1d14b24ef33fc1b509bff809a7a [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
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200431/**
432 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
433 * @mtd: MTD device structure
434 * @timeo: Timeout
435 *
436 * Helper function for nand_wait_ready used when needing to wait in interrupt
437 * context.
438 */
439static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
440{
441 struct nand_chip *chip = mtd->priv;
442 int i;
443
444 /* Wait for the device to get ready */
445 for (i = 0; i < timeo; i++) {
446 if (chip->dev_ready(mtd))
447 break;
448 touch_softlockup_watchdog();
449 mdelay(1);
450 }
451}
452
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000453/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000454 * Wait for the ready pin, after a command
455 * The timeout is catched later.
456 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100457void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000458{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200459 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100460 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000461
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200462 /* 400ms timeout */
463 if (in_interrupt() || oops_in_progress)
464 return panic_nand_wait_ready(mtd, 400);
465
Richard Purdie8fe833c2006-03-31 02:31:14 -0800466 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000467 /* wait until command is processed or timeout occures */
468 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200469 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800470 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700471 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000472 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800473 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000474}
David Woodhouse4b648b02006-09-25 17:05:24 +0100475EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/**
478 * nand_command - [DEFAULT] Send command to NAND device
479 * @mtd: MTD device structure
480 * @command: the command to be sent
481 * @column: the column address for this command, -1 if none
482 * @page_addr: the page address for this command, -1 if none
483 *
484 * Send command to NAND device. This function is used for small page
485 * devices (256/512 Bytes per page)
486 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200487static void nand_command(struct mtd_info *mtd, unsigned int command,
488 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200490 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200491 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * Write out the command to the device.
495 */
496 if (command == NAND_CMD_SEQIN) {
497 int readcmd;
498
Joern Engel28318772006-05-22 23:18:05 +0200499 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200501 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 readcmd = NAND_CMD_READOOB;
503 } else if (column < 256) {
504 /* First 256 bytes --> READ0 */
505 readcmd = NAND_CMD_READ0;
506 } else {
507 column -= 256;
508 readcmd = NAND_CMD_READ1;
509 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200510 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200511 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200513 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200515 /*
516 * Address cycle, when necessary
517 */
518 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
519 /* Serially input address */
520 if (column != -1) {
521 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200522 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200523 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200524 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200525 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200527 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200528 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200529 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200530 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200531 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200532 if (chip->chipsize > (32 << 20))
533 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200534 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200535 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000536
537 /*
538 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 case NAND_CMD_PAGEPROG:
544 case NAND_CMD_ERASE1:
545 case NAND_CMD_ERASE2:
546 case NAND_CMD_SEQIN:
547 case NAND_CMD_STATUS:
548 return;
549
550 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200551 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200553 udelay(chip->chip_delay);
554 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200555 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200556 chip->cmd_ctrl(mtd,
557 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200558 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return;
560
David Woodhousee0c7d762006-05-13 18:07:53 +0100561 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000563 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * If we don't have access to the busy pin, we apply the given
565 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100566 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200567 if (!chip->dev_ready) {
568 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Apply this short delay always to ensure that we do wait tWB in
573 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100574 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000575
576 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/**
580 * nand_command_lp - [DEFAULT] Send command to NAND large page device
581 * @mtd: MTD device structure
582 * @command: the command to be sent
583 * @column: the column address for this command, -1 if none
584 * @page_addr: the page address for this command, -1 if none
585 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200586 * Send command to NAND device. This is the version for the new large page
587 * devices We dont have the separate regions as we have in the small page
588 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200590static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
591 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200593 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* Emulate NAND_CMD_READOOB */
596 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200597 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 command = NAND_CMD_READ0;
599 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000600
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200601 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200602 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200603 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200606 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* Serially input address */
609 if (column != -1) {
610 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200611 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200613 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200614 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200615 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200618 chip->cmd_ctrl(mtd, page_addr, ctrl);
619 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200620 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200622 if (chip->chipsize > (128 << 20))
623 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200624 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200627 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000628
629 /*
630 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000631 * status, sequential in, and deplete1 need no delay
632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 case NAND_CMD_CACHEDPROG:
636 case NAND_CMD_PAGEPROG:
637 case NAND_CMD_ERASE1:
638 case NAND_CMD_ERASE2:
639 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200640 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000642 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return;
644
David Woodhousee0c7d762006-05-13 18:07:53 +0100645 /*
646 * read error status commands require only a short delay
647 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000648 case NAND_CMD_STATUS_ERROR:
649 case NAND_CMD_STATUS_ERROR0:
650 case NAND_CMD_STATUS_ERROR1:
651 case NAND_CMD_STATUS_ERROR2:
652 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200653 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000654 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200657 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200659 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200660 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
661 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
662 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
663 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200664 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return;
666
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200667 case NAND_CMD_RNDOUT:
668 /* No ready / busy check necessary */
669 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
670 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
671 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
672 NAND_NCE | NAND_CTRL_CHANGE);
673 return;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200676 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
677 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
678 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
679 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000680
David Woodhousee0c7d762006-05-13 18:07:53 +0100681 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000683 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * If we don't have access to the busy pin, we apply the given
685 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100686 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200687 if (!chip->dev_ready) {
688 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* Apply this short delay always to ensure that we do wait tWB in
694 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100695 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000696
697 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200701 * panic_nand_get_device - [GENERIC] Get chip for selected access
702 * @chip: the nand chip descriptor
703 * @mtd: MTD device structure
704 * @new_state: the state which is requested
705 *
706 * Used when in panic, no locks are taken.
707 */
708static void panic_nand_get_device(struct nand_chip *chip,
709 struct mtd_info *mtd, int new_state)
710{
711 /* Hardware controller shared among independend devices */
712 chip->controller->active = chip;
713 chip->state = new_state;
714}
715
716/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700718 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000720 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 *
722 * Get the device and lock it for exclusive access
723 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200724static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200725nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 spinlock_t *lock = &chip->controller->lock;
728 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100729 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100730 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100731 spin_lock(lock);
732
vimal singhb8b3ee92009-07-09 20:41:22 +0530733 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200734 if (!chip->controller->active)
735 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200736
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200737 if (chip->controller->active == chip && chip->state == FL_READY) {
738 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100739 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100740 return 0;
741 }
742 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800743 if (chip->controller->active->state == FL_PM_SUSPENDED) {
744 chip->state = FL_PM_SUSPENDED;
745 spin_unlock(lock);
746 return 0;
747 } else {
748 spin_unlock(lock);
749 return -EAGAIN;
750 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100751 }
752 set_current_state(TASK_UNINTERRUPTIBLE);
753 add_wait_queue(wq, &wait);
754 spin_unlock(lock);
755 schedule();
756 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto retry;
758}
759
760/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200761 * panic_nand_wait - [GENERIC] wait until the command is done
762 * @mtd: MTD device structure
763 * @chip: NAND chip structure
764 * @timeo: Timeout
765 *
766 * Wait for command done. This is a helper function for nand_wait used when
767 * we are in interrupt context. May happen when in panic and trying to write
768 * an oops trough mtdoops.
769 */
770static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
771 unsigned long timeo)
772{
773 int i;
774 for (i = 0; i < timeo; i++) {
775 if (chip->dev_ready) {
776 if (chip->dev_ready(mtd))
777 break;
778 } else {
779 if (chip->read_byte(mtd) & NAND_STATUS_READY)
780 break;
781 }
782 mdelay(1);
783 }
784}
785
786/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * nand_wait - [DEFAULT] wait until the command is done
788 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700789 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 *
791 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000792 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700794 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200795static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797
David Woodhousee0c7d762006-05-13 18:07:53 +0100798 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200799 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100802 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100804 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Richard Purdie8fe833c2006-03-31 02:31:14 -0800806 led_trigger_event(nand_led_trigger, LED_FULL);
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* Apply this short delay always to ensure that we do wait tWB in
809 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100810 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200812 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
813 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000814 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200815 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200817 if (in_interrupt() || oops_in_progress)
818 panic_nand_wait(mtd, chip, timeo);
819 else {
820 while (time_before(jiffies, timeo)) {
821 if (chip->dev_ready) {
822 if (chip->dev_ready(mtd))
823 break;
824 } else {
825 if (chip->read_byte(mtd) & NAND_STATUS_READY)
826 break;
827 }
828 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800831 led_trigger_event(nand_led_trigger, LED_OFF);
832
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200833 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return status;
835}
836
837/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200838 * nand_read_page_raw - [Intern] read raw page data without ecc
839 * @mtd: mtd info structure
840 * @chip: nand chip info structure
841 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100842 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -0800843 *
844 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200845 */
846static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700847 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200848{
849 chip->read_buf(mtd, buf, mtd->writesize);
850 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
851 return 0;
852}
853
854/**
David Brownell52ff49d2009-03-04 12:01:36 -0800855 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
856 * @mtd: mtd info structure
857 * @chip: nand chip info structure
858 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100859 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -0800860 *
861 * We need a special oob layout and handling even when OOB isn't used.
862 */
863static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700864 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -0800865{
866 int eccsize = chip->ecc.size;
867 int eccbytes = chip->ecc.bytes;
868 uint8_t *oob = chip->oob_poi;
869 int steps, size;
870
871 for (steps = chip->ecc.steps; steps > 0; steps--) {
872 chip->read_buf(mtd, buf, eccsize);
873 buf += eccsize;
874
875 if (chip->ecc.prepad) {
876 chip->read_buf(mtd, oob, chip->ecc.prepad);
877 oob += chip->ecc.prepad;
878 }
879
880 chip->read_buf(mtd, oob, eccbytes);
881 oob += eccbytes;
882
883 if (chip->ecc.postpad) {
884 chip->read_buf(mtd, oob, chip->ecc.postpad);
885 oob += chip->ecc.postpad;
886 }
887 }
888
889 size = mtd->oobsize - (oob - chip->oob_poi);
890 if (size)
891 chip->read_buf(mtd, oob, size);
892
893 return 0;
894}
895
896/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300897 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200898 * @mtd: mtd info structure
899 * @chip: nand chip info structure
900 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100901 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +0000902 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200903static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700904 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200906 int i, eccsize = chip->ecc.size;
907 int eccbytes = chip->ecc.bytes;
908 int eccsteps = chip->ecc.steps;
909 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100910 uint8_t *ecc_calc = chip->buffers->ecccalc;
911 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100912 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200913
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700914 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200915
916 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
917 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
918
919 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200920 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200921
922 eccsteps = chip->ecc.steps;
923 p = buf;
924
925 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
926 int stat;
927
928 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700929 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200930 mtd->ecc_stats.failed++;
931 else
932 mtd->ecc_stats.corrected += stat;
933 }
934 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100935}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/**
Alexey Korolev3d459552008-05-15 17:23:18 +0100938 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
939 * @mtd: mtd info structure
940 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +0100941 * @data_offs: offset of requested data within the page
942 * @readlen: data length
943 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +0100944 */
945static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
946{
947 int start_step, end_step, num_steps;
948 uint32_t *eccpos = chip->ecc.layout->eccpos;
949 uint8_t *p;
950 int data_col_addr, i, gaps = 0;
951 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
952 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
953
954 /* Column address wihin the page aligned to ECC size (256bytes). */
955 start_step = data_offs / chip->ecc.size;
956 end_step = (data_offs + readlen - 1) / chip->ecc.size;
957 num_steps = end_step - start_step + 1;
958
959 /* Data size aligned to ECC ecc.size*/
960 datafrag_len = num_steps * chip->ecc.size;
961 eccfrag_len = num_steps * chip->ecc.bytes;
962
963 data_col_addr = start_step * chip->ecc.size;
964 /* If we read not a page aligned data */
965 if (data_col_addr != 0)
966 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
967
968 p = bufpoi + data_col_addr;
969 chip->read_buf(mtd, p, datafrag_len);
970
971 /* Calculate ECC */
972 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
973 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
974
975 /* The performance is faster if to position offsets
976 according to ecc.pos. Let make sure here that
977 there are no gaps in ecc positions */
978 for (i = 0; i < eccfrag_len - 1; i++) {
979 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
980 eccpos[i + start_step * chip->ecc.bytes + 1]) {
981 gaps = 1;
982 break;
983 }
984 }
985 if (gaps) {
986 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
987 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
988 } else {
989 /* send the command to read the particular ecc bytes */
990 /* take care about buswidth alignment in read_buf */
991 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
992 aligned_len = eccfrag_len;
993 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
994 aligned_len++;
995 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
996 aligned_len++;
997
998 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
999 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1000 }
1001
1002 for (i = 0; i < eccfrag_len; i++)
1003 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1004
1005 p = bufpoi + data_col_addr;
1006 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1007 int stat;
1008
1009 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1010 if (stat == -1)
1011 mtd->ecc_stats.failed++;
1012 else
1013 mtd->ecc_stats.corrected += stat;
1014 }
1015 return 0;
1016}
1017
1018/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001019 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001020 * @mtd: mtd info structure
1021 * @chip: nand chip info structure
1022 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001023 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001024 *
1025 * Not for syndrome calculating ecc controllers which need a special oob layout
1026 */
1027static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001028 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001029{
1030 int i, eccsize = chip->ecc.size;
1031 int eccbytes = chip->ecc.bytes;
1032 int eccsteps = chip->ecc.steps;
1033 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001034 uint8_t *ecc_calc = chip->buffers->ecccalc;
1035 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001036 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001037
1038 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1039 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1040 chip->read_buf(mtd, p, eccsize);
1041 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1042 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001043 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001044
1045 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001046 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001047
1048 eccsteps = chip->ecc.steps;
1049 p = buf;
1050
1051 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1052 int stat;
1053
1054 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001055 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001056 mtd->ecc_stats.failed++;
1057 else
1058 mtd->ecc_stats.corrected += stat;
1059 }
1060 return 0;
1061}
1062
1063/**
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001064 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1065 * @mtd: mtd info structure
1066 * @chip: nand chip info structure
1067 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001068 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001069 *
1070 * Hardware ECC for large page chips, require OOB to be read first.
1071 * For this ECC mode, the write_page method is re-used from ECC_HW.
1072 * These methods read/write ECC from the OOB area, unlike the
1073 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1074 * "infix ECC" scheme and reads/writes ECC from the data area, by
1075 * overwriting the NAND manufacturer bad block markings.
1076 */
1077static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1078 struct nand_chip *chip, uint8_t *buf, int page)
1079{
1080 int i, eccsize = chip->ecc.size;
1081 int eccbytes = chip->ecc.bytes;
1082 int eccsteps = chip->ecc.steps;
1083 uint8_t *p = buf;
1084 uint8_t *ecc_code = chip->buffers->ecccode;
1085 uint32_t *eccpos = chip->ecc.layout->eccpos;
1086 uint8_t *ecc_calc = chip->buffers->ecccalc;
1087
1088 /* Read the OOB area first */
1089 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1090 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1091 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1092
1093 for (i = 0; i < chip->ecc.total; i++)
1094 ecc_code[i] = chip->oob_poi[eccpos[i]];
1095
1096 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1097 int stat;
1098
1099 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1100 chip->read_buf(mtd, p, eccsize);
1101 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1102
1103 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1104 if (stat < 0)
1105 mtd->ecc_stats.failed++;
1106 else
1107 mtd->ecc_stats.corrected += stat;
1108 }
1109 return 0;
1110}
1111
1112/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001113 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001114 * @mtd: mtd info structure
1115 * @chip: nand chip info structure
1116 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001117 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001118 *
1119 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001120 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001121 */
1122static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001123 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001124{
1125 int i, eccsize = chip->ecc.size;
1126 int eccbytes = chip->ecc.bytes;
1127 int eccsteps = chip->ecc.steps;
1128 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001129 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001130
1131 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1132 int stat;
1133
1134 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1135 chip->read_buf(mtd, p, eccsize);
1136
1137 if (chip->ecc.prepad) {
1138 chip->read_buf(mtd, oob, chip->ecc.prepad);
1139 oob += chip->ecc.prepad;
1140 }
1141
1142 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1143 chip->read_buf(mtd, oob, eccbytes);
1144 stat = chip->ecc.correct(mtd, p, oob, NULL);
1145
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001146 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001147 mtd->ecc_stats.failed++;
1148 else
1149 mtd->ecc_stats.corrected += stat;
1150
1151 oob += eccbytes;
1152
1153 if (chip->ecc.postpad) {
1154 chip->read_buf(mtd, oob, chip->ecc.postpad);
1155 oob += chip->ecc.postpad;
1156 }
1157 }
1158
1159 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001160 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001161 if (i)
1162 chip->read_buf(mtd, oob, i);
1163
1164 return 0;
1165}
1166
1167/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001168 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1169 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001170 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001171 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001172 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001173 */
1174static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001175 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001176{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001177 switch(ops->mode) {
1178
1179 case MTD_OOB_PLACE:
1180 case MTD_OOB_RAW:
1181 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1182 return oob + len;
1183
1184 case MTD_OOB_AUTO: {
1185 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001186 uint32_t boffs = 0, roffs = ops->ooboffs;
1187 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001188
1189 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001190 /* Read request not from offset 0 ? */
1191 if (unlikely(roffs)) {
1192 if (roffs >= free->length) {
1193 roffs -= free->length;
1194 continue;
1195 }
1196 boffs = free->offset + roffs;
1197 bytes = min_t(size_t, len,
1198 (free->length - roffs));
1199 roffs = 0;
1200 } else {
1201 bytes = min_t(size_t, len, free->length);
1202 boffs = free->offset;
1203 }
1204 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001205 oob += bytes;
1206 }
1207 return oob;
1208 }
1209 default:
1210 BUG();
1211 }
1212 return NULL;
1213}
1214
1215/**
1216 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001217 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001218 * @mtd: MTD device structure
1219 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001220 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001221 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001222 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001223 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001224static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1225 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001226{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001227 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001228 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001229 struct mtd_ecc_stats stats;
1230 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1231 int sndcmd = 1;
1232 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001233 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001234 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001235 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001237 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001239 chipnr = (int)(from >> chip->chip_shift);
1240 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001242 realpage = (int)(from >> chip->page_shift);
1243 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001245 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001247 buf = ops->datbuf;
1248 oob = ops->oobbuf;
1249
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001250 while(1) {
1251 bytes = min(mtd->writesize - col, readlen);
1252 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001253
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001254 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001255 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001256 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001258 if (likely(sndcmd)) {
1259 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1260 sndcmd = 0;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001263 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001264 if (unlikely(ops->mode == MTD_OOB_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001265 ret = chip->ecc.read_page_raw(mtd, chip,
1266 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001267 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1268 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001269 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001270 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1271 page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001272 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001273 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001274
1275 /* Transfer not aligned data */
1276 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001277 if (!NAND_SUBPAGE_READ(chip) && !oob)
1278 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001279 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001281
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001282 buf += bytes;
1283
1284 if (unlikely(oob)) {
1285 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001286 if (ops->mode != MTD_OOB_RAW) {
1287 int toread = min(oobreadlen,
1288 chip->ecc.layout->oobavail);
1289 if (toread) {
1290 oob = nand_transfer_oob(chip,
1291 oob, ops, toread);
1292 oobreadlen -= toread;
1293 }
1294 } else
1295 buf = nand_transfer_oob(chip,
1296 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001297 }
1298
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001299 if (!(chip->options & NAND_NO_READRDY)) {
1300 /*
1301 * Apply delay or wait for ready/busy pin. Do
1302 * this before the AUTOINCR check, so no
1303 * problems arise if a chip which does auto
1304 * increment is marked as NOAUTOINCR by the
1305 * board driver.
1306 */
1307 if (!chip->dev_ready)
1308 udelay(chip->chip_delay);
1309 else
1310 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001312 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001313 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001314 buf += bytes;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001317 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001318
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001319 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /* For subsequent reads align to page boundary. */
1323 col = 0;
1324 /* Increment page address */
1325 realpage++;
1326
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001327 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* Check, if we cross a chip boundary */
1329 if (!page) {
1330 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001331 chip->select_chip(mtd, -1);
1332 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001334
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001335 /* Check, if the chip supports auto page increment
1336 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001337 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001338 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001339 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001342 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001343 if (oob)
1344 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001346 if (ret)
1347 return ret;
1348
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001349 if (mtd->ecc_stats.failed - stats.failed)
1350 return -EBADMSG;
1351
1352 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001353}
1354
1355/**
1356 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1357 * @mtd: MTD device structure
1358 * @from: offset to read from
1359 * @len: number of bytes to read
1360 * @retlen: pointer to variable to store the number of read bytes
1361 * @buf: the databuffer to put data
1362 *
1363 * Get hold of the chip and call nand_do_read
1364 */
1365static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1366 size_t *retlen, uint8_t *buf)
1367{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001368 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001369 int ret;
1370
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001371 /* Do not allow reads past end of device */
1372 if ((from + len) > mtd->size)
1373 return -EINVAL;
1374 if (!len)
1375 return 0;
1376
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001377 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001378
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001379 chip->ops.len = len;
1380 chip->ops.datbuf = buf;
1381 chip->ops.oobbuf = NULL;
1382
1383 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001384
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001385 *retlen = chip->ops.retlen;
1386
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001387 nand_release_device(mtd);
1388
1389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001393 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1394 * @mtd: mtd info structure
1395 * @chip: nand chip info structure
1396 * @page: page number to read
1397 * @sndcmd: flag whether to issue read command or not
1398 */
1399static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1400 int page, int sndcmd)
1401{
1402 if (sndcmd) {
1403 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1404 sndcmd = 0;
1405 }
1406 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1407 return sndcmd;
1408}
1409
1410/**
1411 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1412 * with syndromes
1413 * @mtd: mtd info structure
1414 * @chip: nand chip info structure
1415 * @page: page number to read
1416 * @sndcmd: flag whether to issue read command or not
1417 */
1418static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1419 int page, int sndcmd)
1420{
1421 uint8_t *buf = chip->oob_poi;
1422 int length = mtd->oobsize;
1423 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1424 int eccsize = chip->ecc.size;
1425 uint8_t *bufpoi = buf;
1426 int i, toread, sndrnd = 0, pos;
1427
1428 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1429 for (i = 0; i < chip->ecc.steps; i++) {
1430 if (sndrnd) {
1431 pos = eccsize + i * (eccsize + chunk);
1432 if (mtd->writesize > 512)
1433 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1434 else
1435 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1436 } else
1437 sndrnd = 1;
1438 toread = min_t(int, length, chunk);
1439 chip->read_buf(mtd, bufpoi, toread);
1440 bufpoi += toread;
1441 length -= toread;
1442 }
1443 if (length > 0)
1444 chip->read_buf(mtd, bufpoi, length);
1445
1446 return 1;
1447}
1448
1449/**
1450 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1451 * @mtd: mtd info structure
1452 * @chip: nand chip info structure
1453 * @page: page number to write
1454 */
1455static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1456 int page)
1457{
1458 int status = 0;
1459 const uint8_t *buf = chip->oob_poi;
1460 int length = mtd->oobsize;
1461
1462 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1463 chip->write_buf(mtd, buf, length);
1464 /* Send command to program the OOB data */
1465 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1466
1467 status = chip->waitfunc(mtd, chip);
1468
Savin Zlobec0d420f92006-06-21 11:51:20 +02001469 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001470}
1471
1472/**
1473 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1474 * with syndrome - only for large page flash !
1475 * @mtd: mtd info structure
1476 * @chip: nand chip info structure
1477 * @page: page number to write
1478 */
1479static int nand_write_oob_syndrome(struct mtd_info *mtd,
1480 struct nand_chip *chip, int page)
1481{
1482 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1483 int eccsize = chip->ecc.size, length = mtd->oobsize;
1484 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1485 const uint8_t *bufpoi = chip->oob_poi;
1486
1487 /*
1488 * data-ecc-data-ecc ... ecc-oob
1489 * or
1490 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1491 */
1492 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1493 pos = steps * (eccsize + chunk);
1494 steps = 0;
1495 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001496 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001497
1498 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1499 for (i = 0; i < steps; i++) {
1500 if (sndcmd) {
1501 if (mtd->writesize <= 512) {
1502 uint32_t fill = 0xFFFFFFFF;
1503
1504 len = eccsize;
1505 while (len > 0) {
1506 int num = min_t(int, len, 4);
1507 chip->write_buf(mtd, (uint8_t *)&fill,
1508 num);
1509 len -= num;
1510 }
1511 } else {
1512 pos = eccsize + i * (eccsize + chunk);
1513 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1514 }
1515 } else
1516 sndcmd = 1;
1517 len = min_t(int, length, chunk);
1518 chip->write_buf(mtd, bufpoi, len);
1519 bufpoi += len;
1520 length -= len;
1521 }
1522 if (length > 0)
1523 chip->write_buf(mtd, bufpoi, length);
1524
1525 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1526 status = chip->waitfunc(mtd, chip);
1527
1528 return status & NAND_STATUS_FAIL ? -EIO : 0;
1529}
1530
1531/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001532 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 * @mtd: MTD device structure
1534 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001535 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 *
1537 * NAND read out-of-band data from the spare area
1538 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001539static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1540 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001542 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001543 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001544 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001545 int readlen = ops->ooblen;
1546 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001547 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
vimal singh20d8e242009-07-07 15:49:49 +05301549 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1550 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Adrian Hunter03736152007-01-31 17:58:29 +02001552 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001553 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001554 else
1555 len = mtd->oobsize;
1556
1557 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301558 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1559 "outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001560 return -EINVAL;
1561 }
1562
1563 /* Do not allow reads past end of device */
1564 if (unlikely(from >= mtd->size ||
1565 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1566 (from >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301567 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1568 "of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001569 return -EINVAL;
1570 }
Vitaly Wool70145682006-11-03 18:20:38 +03001571
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001572 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001573 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001575 /* Shift to get page */
1576 realpage = (int)(from >> chip->page_shift);
1577 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001579 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001580 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001581
1582 len = min(len, readlen);
1583 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001584
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001585 if (!(chip->options & NAND_NO_READRDY)) {
1586 /*
1587 * Apply delay or wait for ready/busy pin. Do this
1588 * before the AUTOINCR check, so no problems arise if a
1589 * chip which does auto increment is marked as
1590 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001591 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001592 if (!chip->dev_ready)
1593 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001594 else
1595 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001597
Vitaly Wool70145682006-11-03 18:20:38 +03001598 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001599 if (!readlen)
1600 break;
1601
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001602 /* Increment page address */
1603 realpage++;
1604
1605 page = realpage & chip->pagemask;
1606 /* Check, if we cross a chip boundary */
1607 if (!page) {
1608 chipnr++;
1609 chip->select_chip(mtd, -1);
1610 chip->select_chip(mtd, chipnr);
1611 }
1612
1613 /* Check, if the chip supports auto page increment
1614 * or if we have hit a block boundary.
1615 */
1616 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1617 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619
Vitaly Wool70145682006-11-03 18:20:38 +03001620 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return 0;
1622}
1623
1624/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001625 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001628 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001630 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001632static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1633 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001635 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001636 int ret = -ENOTSUPP;
1637
1638 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001641 if (ops->datbuf && (from + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05301642 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1643 "beyond end of device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return -EINVAL;
1645 }
1646
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001647 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001649 switch(ops->mode) {
1650 case MTD_OOB_PLACE:
1651 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001652 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001653 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001654
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001655 default:
1656 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001659 if (!ops->datbuf)
1660 ret = nand_do_read_oob(mtd, from, ops);
1661 else
1662 ret = nand_do_read_ops(mtd, from, ops);
1663
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001664 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001666 return ret;
1667}
1668
1669
1670/**
1671 * nand_write_page_raw - [Intern] raw page write function
1672 * @mtd: mtd info structure
1673 * @chip: nand chip info structure
1674 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001675 *
1676 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001677 */
1678static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1679 const uint8_t *buf)
1680{
1681 chip->write_buf(mtd, buf, mtd->writesize);
1682 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001685/**
David Brownell52ff49d2009-03-04 12:01:36 -08001686 * nand_write_page_raw_syndrome - [Intern] raw page write function
1687 * @mtd: mtd info structure
1688 * @chip: nand chip info structure
1689 * @buf: data buffer
1690 *
1691 * We need a special oob layout and handling even when ECC isn't checked.
1692 */
1693static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1694 const uint8_t *buf)
1695{
1696 int eccsize = chip->ecc.size;
1697 int eccbytes = chip->ecc.bytes;
1698 uint8_t *oob = chip->oob_poi;
1699 int steps, size;
1700
1701 for (steps = chip->ecc.steps; steps > 0; steps--) {
1702 chip->write_buf(mtd, buf, eccsize);
1703 buf += eccsize;
1704
1705 if (chip->ecc.prepad) {
1706 chip->write_buf(mtd, oob, chip->ecc.prepad);
1707 oob += chip->ecc.prepad;
1708 }
1709
1710 chip->read_buf(mtd, oob, eccbytes);
1711 oob += eccbytes;
1712
1713 if (chip->ecc.postpad) {
1714 chip->write_buf(mtd, oob, chip->ecc.postpad);
1715 oob += chip->ecc.postpad;
1716 }
1717 }
1718
1719 size = mtd->oobsize - (oob - chip->oob_poi);
1720 if (size)
1721 chip->write_buf(mtd, oob, size);
1722}
1723/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001724 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001725 * @mtd: mtd info structure
1726 * @chip: nand chip info structure
1727 * @buf: data buffer
1728 */
1729static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1730 const uint8_t *buf)
1731{
1732 int i, eccsize = chip->ecc.size;
1733 int eccbytes = chip->ecc.bytes;
1734 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001735 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001736 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001737 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001738
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001739 /* Software ecc calculation */
1740 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1741 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001742
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001743 for (i = 0; i < chip->ecc.total; i++)
1744 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001745
Thomas Gleixner90424de2007-04-05 11:44:05 +02001746 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001747}
1748
1749/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001750 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001751 * @mtd: mtd info structure
1752 * @chip: nand chip info structure
1753 * @buf: data buffer
1754 */
1755static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1756 const uint8_t *buf)
1757{
1758 int i, eccsize = chip->ecc.size;
1759 int eccbytes = chip->ecc.bytes;
1760 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001761 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001762 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001763 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001764
1765 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1766 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001767 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001768 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1769 }
1770
1771 for (i = 0; i < chip->ecc.total; i++)
1772 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1773
1774 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1775}
1776
1777/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001778 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001779 * @mtd: mtd info structure
1780 * @chip: nand chip info structure
1781 * @buf: data buffer
1782 *
1783 * The hw generator calculates the error syndrome automatically. Therefor
1784 * we need a special oob layout and handling.
1785 */
1786static void nand_write_page_syndrome(struct mtd_info *mtd,
1787 struct nand_chip *chip, const uint8_t *buf)
1788{
1789 int i, eccsize = chip->ecc.size;
1790 int eccbytes = chip->ecc.bytes;
1791 int eccsteps = chip->ecc.steps;
1792 const uint8_t *p = buf;
1793 uint8_t *oob = chip->oob_poi;
1794
1795 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1796
1797 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1798 chip->write_buf(mtd, p, eccsize);
1799
1800 if (chip->ecc.prepad) {
1801 chip->write_buf(mtd, oob, chip->ecc.prepad);
1802 oob += chip->ecc.prepad;
1803 }
1804
1805 chip->ecc.calculate(mtd, p, oob);
1806 chip->write_buf(mtd, oob, eccbytes);
1807 oob += eccbytes;
1808
1809 if (chip->ecc.postpad) {
1810 chip->write_buf(mtd, oob, chip->ecc.postpad);
1811 oob += chip->ecc.postpad;
1812 }
1813 }
1814
1815 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001816 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001817 if (i)
1818 chip->write_buf(mtd, oob, i);
1819}
1820
1821/**
David Woodhouse956e9442006-09-25 17:12:39 +01001822 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001823 * @mtd: MTD device structure
1824 * @chip: NAND chip descriptor
1825 * @buf: the data to write
1826 * @page: page number to write
1827 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02001828 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001829 */
1830static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001831 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001832{
1833 int status;
1834
1835 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1836
David Woodhouse956e9442006-09-25 17:12:39 +01001837 if (unlikely(raw))
1838 chip->ecc.write_page_raw(mtd, chip, buf);
1839 else
1840 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001841
1842 /*
1843 * Cached progamming disabled for now, Not sure if its worth the
1844 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1845 */
1846 cached = 0;
1847
1848 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1849
1850 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001851 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001852 /*
1853 * See if operation failed and additional status checks are
1854 * available
1855 */
1856 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1857 status = chip->errstat(mtd, chip, FL_WRITING, status,
1858 page);
1859
1860 if (status & NAND_STATUS_FAIL)
1861 return -EIO;
1862 } else {
1863 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001864 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001865 }
1866
1867#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1868 /* Send command to read back the data */
1869 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1870
1871 if (chip->verify_buf(mtd, buf, mtd->writesize))
1872 return -EIO;
1873#endif
1874 return 0;
1875}
1876
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001877/**
1878 * nand_fill_oob - [Internal] Transfer client buffer to oob
1879 * @chip: nand chip structure
1880 * @oob: oob data buffer
1881 * @ops: oob ops structure
1882 */
1883static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1884 struct mtd_oob_ops *ops)
1885{
1886 size_t len = ops->ooblen;
1887
1888 switch(ops->mode) {
1889
1890 case MTD_OOB_PLACE:
1891 case MTD_OOB_RAW:
1892 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1893 return oob + len;
1894
1895 case MTD_OOB_AUTO: {
1896 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001897 uint32_t boffs = 0, woffs = ops->ooboffs;
1898 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001899
1900 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001901 /* Write request not from offset 0 ? */
1902 if (unlikely(woffs)) {
1903 if (woffs >= free->length) {
1904 woffs -= free->length;
1905 continue;
1906 }
1907 boffs = free->offset + woffs;
1908 bytes = min_t(size_t, len,
1909 (free->length - woffs));
1910 woffs = 0;
1911 } else {
1912 bytes = min_t(size_t, len, free->length);
1913 boffs = free->offset;
1914 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001915 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001916 oob += bytes;
1917 }
1918 return oob;
1919 }
1920 default:
1921 BUG();
1922 }
1923 return NULL;
1924}
1925
Thomas Gleixner29072b92006-09-28 15:38:36 +02001926#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001927
1928/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001929 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001930 * @mtd: MTD device structure
1931 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001932 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001933 *
1934 * NAND write with ECC
1935 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001936static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1937 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001938{
Thomas Gleixner29072b92006-09-28 15:38:36 +02001939 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001940 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001941 uint32_t writelen = ops->len;
1942 uint8_t *oob = ops->oobbuf;
1943 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001944 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001945
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001946 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001947 if (!writelen)
1948 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001949
1950 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001951 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301952 printk(KERN_NOTICE "%s: Attempt to write not "
1953 "page aligned data\n", __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001954 return -EINVAL;
1955 }
1956
Thomas Gleixner29072b92006-09-28 15:38:36 +02001957 column = to & (mtd->writesize - 1);
1958 subpage = column || (writelen & (mtd->writesize - 1));
1959
1960 if (subpage && oob)
1961 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001962
Thomas Gleixner6a930962006-06-28 00:11:45 +02001963 chipnr = (int)(to >> chip->chip_shift);
1964 chip->select_chip(mtd, chipnr);
1965
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001966 /* Check, if it is write protected */
1967 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001968 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001969
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001970 realpage = (int)(to >> chip->page_shift);
1971 page = realpage & chip->pagemask;
1972 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1973
1974 /* Invalidate the page cache, when we write to the cached page */
1975 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001976 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001977 chip->pagebuf = -1;
1978
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001979 /* If we're not given explicit OOB data, let it be 0xFF */
1980 if (likely(!oob))
1981 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001982
1983 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02001984 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001985 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001986 uint8_t *wbuf = buf;
1987
1988 /* Partial page write ? */
1989 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1990 cached = 0;
1991 bytes = min_t(int, bytes - column, (int) writelen);
1992 chip->pagebuf = -1;
1993 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1994 memcpy(&chip->buffers->databuf[column], buf, bytes);
1995 wbuf = chip->buffers->databuf;
1996 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001997
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001998 if (unlikely(oob))
1999 oob = nand_fill_oob(chip, oob, ops);
2000
Thomas Gleixner29072b92006-09-28 15:38:36 +02002001 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01002002 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002003 if (ret)
2004 break;
2005
2006 writelen -= bytes;
2007 if (!writelen)
2008 break;
2009
Thomas Gleixner29072b92006-09-28 15:38:36 +02002010 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002011 buf += bytes;
2012 realpage++;
2013
2014 page = realpage & chip->pagemask;
2015 /* Check, if we cross a chip boundary */
2016 if (!page) {
2017 chipnr++;
2018 chip->select_chip(mtd, -1);
2019 chip->select_chip(mtd, chipnr);
2020 }
2021 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002022
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002023 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002024 if (unlikely(oob))
2025 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002026 return ret;
2027}
2028
2029/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002030 * panic_nand_write - [MTD Interface] NAND write with ECC
2031 * @mtd: MTD device structure
2032 * @to: offset to write to
2033 * @len: number of bytes to write
2034 * @retlen: pointer to variable to store the number of written bytes
2035 * @buf: the data to write
2036 *
2037 * NAND write with ECC. Used when performing writes in interrupt context, this
2038 * may for example be called by mtdoops when writing an oops while in panic.
2039 */
2040static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2041 size_t *retlen, const uint8_t *buf)
2042{
2043 struct nand_chip *chip = mtd->priv;
2044 int ret;
2045
2046 /* Do not allow reads past end of device */
2047 if ((to + len) > mtd->size)
2048 return -EINVAL;
2049 if (!len)
2050 return 0;
2051
2052 /* Wait for the device to get ready. */
2053 panic_nand_wait(mtd, chip, 400);
2054
2055 /* Grab the device. */
2056 panic_nand_get_device(chip, mtd, FL_WRITING);
2057
2058 chip->ops.len = len;
2059 chip->ops.datbuf = (uint8_t *)buf;
2060 chip->ops.oobbuf = NULL;
2061
2062 ret = nand_do_write_ops(mtd, to, &chip->ops);
2063
2064 *retlen = chip->ops.retlen;
2065 return ret;
2066}
2067
2068/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002069 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 * @mtd: MTD device structure
2071 * @to: offset to write to
2072 * @len: number of bytes to write
2073 * @retlen: pointer to variable to store the number of written bytes
2074 * @buf: the data to write
2075 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002076 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002078static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002079 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002081 struct nand_chip *chip = mtd->priv;
2082 int ret;
2083
2084 /* Do not allow reads past end of device */
2085 if ((to + len) > mtd->size)
2086 return -EINVAL;
2087 if (!len)
2088 return 0;
2089
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002090 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002091
2092 chip->ops.len = len;
2093 chip->ops.datbuf = (uint8_t *)buf;
2094 chip->ops.oobbuf = NULL;
2095
2096 ret = nand_do_write_ops(mtd, to, &chip->ops);
2097
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002098 *retlen = chip->ops.retlen;
2099
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002100 nand_release_device(mtd);
2101
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002102 return ret;
2103}
2104
2105/**
2106 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2107 * @mtd: MTD device structure
2108 * @to: offset to write to
2109 * @ops: oob operation description structure
2110 *
2111 * NAND write out-of-band
2112 */
2113static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2114 struct mtd_oob_ops *ops)
2115{
Adrian Hunter03736152007-01-31 17:58:29 +02002116 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002117 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
vimal singh20d8e242009-07-07 15:49:49 +05302119 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2120 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Adrian Hunter03736152007-01-31 17:58:29 +02002122 if (ops->mode == MTD_OOB_AUTO)
2123 len = chip->ecc.layout->oobavail;
2124 else
2125 len = mtd->oobsize;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002128 if ((ops->ooboffs + ops->ooblen) > len) {
vimal singh20d8e242009-07-07 15:49:49 +05302129 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2130 "past end of page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return -EINVAL;
2132 }
2133
Adrian Hunter03736152007-01-31 17:58:29 +02002134 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302135 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2136 "write outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002137 return -EINVAL;
2138 }
2139
2140 /* Do not allow reads past end of device */
2141 if (unlikely(to >= mtd->size ||
2142 ops->ooboffs + ops->ooblen >
2143 ((mtd->size >> chip->page_shift) -
2144 (to >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302145 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2146 "end of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002147 return -EINVAL;
2148 }
2149
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002150 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002151 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002153 /* Shift to get page */
2154 page = (int)(to >> chip->page_shift);
2155
2156 /*
2157 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2158 * of my DiskOnChip 2000 test units) will clear the whole data page too
2159 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2160 * it in the doc2000 driver in August 1999. dwmw2.
2161 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002162 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 /* Check, if it is write protected */
2165 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002166 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002169 if (page == chip->pagebuf)
2170 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002172 memset(chip->oob_poi, 0xff, mtd->oobsize);
2173 nand_fill_oob(chip, ops->oobbuf, ops);
2174 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2175 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002176
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002177 if (status)
2178 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Vitaly Wool70145682006-11-03 18:20:38 +03002180 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002182 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002185/**
2186 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2187 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002188 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002189 * @ops: oob operation description structure
2190 */
2191static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2192 struct mtd_oob_ops *ops)
2193{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002194 struct nand_chip *chip = mtd->priv;
2195 int ret = -ENOTSUPP;
2196
2197 ops->retlen = 0;
2198
2199 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002200 if (ops->datbuf && (to + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302201 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2202 "end of device\n", __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002203 return -EINVAL;
2204 }
2205
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002206 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002207
2208 switch(ops->mode) {
2209 case MTD_OOB_PLACE:
2210 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002211 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002212 break;
2213
2214 default:
2215 goto out;
2216 }
2217
2218 if (!ops->datbuf)
2219 ret = nand_do_write_oob(mtd, to, ops);
2220 else
2221 ret = nand_do_write_ops(mtd, to, ops);
2222
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002223 out:
2224 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return ret;
2226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2230 * @mtd: MTD device structure
2231 * @page: the page address of the block which will be erased
2232 *
2233 * Standard erase command for NAND chips
2234 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002235static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002237 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002239 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2240 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
2243/**
2244 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2245 * @mtd: MTD device structure
2246 * @page: the page address of the block which will be erased
2247 *
2248 * AND multi block erase command function
2249 * Erase 4 consecutive blocks
2250 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002251static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002253 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002255 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2256 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2257 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2258 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2259 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
2262/**
2263 * nand_erase - [MTD Interface] erase block(s)
2264 * @mtd: MTD device structure
2265 * @instr: erase instruction
2266 *
2267 * Erase one ore more blocks
2268 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002269static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
David Woodhousee0c7d762006-05-13 18:07:53 +01002271 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002273
David A. Marlin30f464b2005-01-17 18:35:25 +00002274#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002276 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * @mtd: MTD device structure
2278 * @instr: erase instruction
2279 * @allowbbt: allow erasing the bbt area
2280 *
2281 * Erase one ore more blocks
2282 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002283int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2284 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Adrian Hunter69423d92008-12-10 13:37:21 +00002286 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002287 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002288 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002289 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002290 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
vimal singh20d8e242009-07-07 15:49:49 +05302292 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2293 __func__, (unsigned long long)instr->addr,
2294 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002297 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
vimal singh20d8e242009-07-07 15:49:49 +05302298 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return -EINVAL;
2300 }
2301
2302 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002303 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
vimal singh20d8e242009-07-07 15:49:49 +05302304 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
2305 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return -EINVAL;
2307 }
2308
2309 /* Do not allow erase past end of device */
2310 if ((instr->len + instr->addr) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302311 DEBUG(MTD_DEBUG_LEVEL0, "%s: Erase past end of device\n",
2312 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return -EINVAL;
2314 }
2315
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002316 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002319 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002322 page = (int)(instr->addr >> chip->page_shift);
2323 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002326 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002329 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 /* Check, if it is write protected */
2332 if (nand_check_wp(mtd)) {
vimal singh20d8e242009-07-07 15:49:49 +05302333 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2334 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 instr->state = MTD_ERASE_FAILED;
2336 goto erase_exit;
2337 }
2338
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002339 /*
2340 * If BBT requires refresh, set the BBT page mask to see if the BBT
2341 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2342 * can not be matched. This is also done when the bbt is actually
2343 * erased to avoid recusrsive updates
2344 */
2345 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2346 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /* Loop through the pages */
2349 len = instr->len;
2350
2351 instr->state = MTD_ERASING;
2352
2353 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002354 /*
2355 * heck if we have a bad block, we do not erase bad blocks !
2356 */
2357 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2358 chip->page_shift, 0, allowbbt)) {
vimal singh20d8e242009-07-07 15:49:49 +05302359 printk(KERN_WARNING "%s: attempt to erase a bad block "
2360 "at page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 instr->state = MTD_ERASE_FAILED;
2362 goto erase_exit;
2363 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002364
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002365 /*
2366 * Invalidate the page cache, if we erase the block which
2367 * contains the current cached page
2368 */
2369 if (page <= chip->pagebuf && chip->pagebuf <
2370 (page + pages_per_block))
2371 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002373 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002374
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002375 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002377 /*
2378 * See if operation failed and additional status checks are
2379 * available
2380 */
2381 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2382 status = chip->errstat(mtd, chip, FL_ERASING,
2383 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002386 if (status & NAND_STATUS_FAIL) {
vimal singh20d8e242009-07-07 15:49:49 +05302387 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2388 "page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002390 instr->fail_addr =
2391 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 goto erase_exit;
2393 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002394
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002395 /*
2396 * If BBT requires refresh, set the BBT rewrite flag to the
2397 * page being erased
2398 */
2399 if (bbt_masked_page != 0xffffffff &&
2400 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002401 rewrite_bbt[chipnr] =
2402 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002405 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 page += pages_per_block;
2407
2408 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002409 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002411 chip->select_chip(mtd, -1);
2412 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002413
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002414 /*
2415 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2416 * page mask to see if this BBT should be rewritten
2417 */
2418 if (bbt_masked_page != 0xffffffff &&
2419 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2420 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2421 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423 }
2424 instr->state = MTD_ERASE_DONE;
2425
David Woodhousee0c7d762006-05-13 18:07:53 +01002426 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 /* Deselect and wake up anyone waiting on the device */
2431 nand_release_device(mtd);
2432
David Woodhouse49defc02007-10-06 15:01:59 -04002433 /* Do call back function */
2434 if (!ret)
2435 mtd_erase_callback(instr);
2436
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002437 /*
2438 * If BBT requires refresh and erase was successful, rewrite any
2439 * selected bad block tables
2440 */
2441 if (bbt_masked_page == 0xffffffff || ret)
2442 return ret;
2443
2444 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2445 if (!rewrite_bbt[chipnr])
2446 continue;
2447 /* update the BBT for chip */
vimal singh20d8e242009-07-07 15:49:49 +05302448 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2449 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2450 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002451 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002452 }
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 /* Return more or less happy */
2455 return ret;
2456}
2457
2458/**
2459 * nand_sync - [MTD Interface] sync
2460 * @mtd: MTD device structure
2461 *
2462 * Sync is actually a wait for chip ready function
2463 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002464static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002466 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
vimal singh20d8e242009-07-07 15:49:49 +05302468 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002471 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002473 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002477 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002479 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002481static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002484 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002486
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002487 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489
2490/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002491 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 * @mtd: MTD device structure
2493 * @ofs: offset relative to mtd start
2494 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002495static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002497 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 int ret;
2499
David Woodhousee0c7d762006-05-13 18:07:53 +01002500 if ((ret = nand_block_isbad(mtd, ofs))) {
2501 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (ret > 0)
2503 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002504 return ret;
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002507 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002511 * nand_suspend - [MTD Interface] Suspend the NAND flash
2512 * @mtd: MTD device structure
2513 */
2514static int nand_suspend(struct mtd_info *mtd)
2515{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002516 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002517
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002518 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002519}
2520
2521/**
2522 * nand_resume - [MTD Interface] Resume the NAND flash
2523 * @mtd: MTD device structure
2524 */
2525static void nand_resume(struct mtd_info *mtd)
2526{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002527 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002528
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002529 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002530 nand_release_device(mtd);
2531 else
vimal singh20d8e242009-07-07 15:49:49 +05302532 printk(KERN_ERR "%s called for a chip which is not "
2533 "in suspended state\n", __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002534}
2535
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002536/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002537 * Set default functions
2538 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002539static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002540{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002542 if (!chip->chip_delay)
2543 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
2545 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002546 if (chip->cmdfunc == NULL)
2547 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002550 if (chip->waitfunc == NULL)
2551 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002553 if (!chip->select_chip)
2554 chip->select_chip = nand_select_chip;
2555 if (!chip->read_byte)
2556 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2557 if (!chip->read_word)
2558 chip->read_word = nand_read_word;
2559 if (!chip->block_bad)
2560 chip->block_bad = nand_block_bad;
2561 if (!chip->block_markbad)
2562 chip->block_markbad = nand_default_block_markbad;
2563 if (!chip->write_buf)
2564 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2565 if (!chip->read_buf)
2566 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2567 if (!chip->verify_buf)
2568 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2569 if (!chip->scan_bbt)
2570 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002571
2572 if (!chip->controller) {
2573 chip->controller = &chip->hwcontrol;
2574 spin_lock_init(&chip->controller->lock);
2575 init_waitqueue_head(&chip->controller->wq);
2576 }
2577
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002578}
2579
2580/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002581 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002582 */
2583static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002584 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002585 int busw, int *maf_id)
2586{
2587 struct nand_flash_dev *type = NULL;
2588 int i, dev_id, maf_idx;
Ben Dooksed8165c2008-04-14 14:58:58 +01002589 int tmp_id, tmp_manf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002592 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Karl Beldanef89a882008-09-15 14:37:29 +02002594 /*
2595 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2596 * after power-up
2597 */
2598 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002601 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002604 *maf_id = chip->read_byte(mtd);
2605 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Ben Dooksed8165c2008-04-14 14:58:58 +01002607 /* Try again to make sure, as some systems the bus-hold or other
2608 * interface concerns can cause random data which looks like a
2609 * possibly credible NAND flash to appear. If the two results do
2610 * not match, ignore the device completely.
2611 */
2612
2613 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2614
2615 /* Read manufacturer and device IDs */
2616
2617 tmp_manf = chip->read_byte(mtd);
2618 tmp_id = chip->read_byte(mtd);
2619
2620 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2621 printk(KERN_INFO "%s: second ID read did not match "
2622 "%02x,%02x against %02x,%02x\n", __func__,
2623 *maf_id, dev_id, tmp_manf, tmp_id);
2624 return ERR_PTR(-ENODEV);
2625 }
2626
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002627 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002629 if (dev_id == nand_flash_ids[i].id) {
2630 type = &nand_flash_ids[i];
2631 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
2634
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002635 if (!type)
2636 return ERR_PTR(-ENODEV);
2637
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002638 if (!mtd->name)
2639 mtd->name = type->name;
2640
Adrian Hunter69423d92008-12-10 13:37:21 +00002641 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002642
2643 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002644 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002645 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002646 /* The 3rd id byte holds MLC / multichip data */
2647 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002648 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002649 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002650 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002651 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002652 extid >>= 2;
2653 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002654 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002655 extid >>= 2;
2656 /* Calc blocksize. Blocksize is multiples of 64KiB */
2657 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2658 extid >>= 2;
2659 /* Get buswidth information */
2660 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2661
2662 } else {
2663 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002664 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002665 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002666 mtd->erasesize = type->erasesize;
2667 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002668 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002669 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002670 }
2671
2672 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002673 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002674 if (nand_manuf_ids[maf_idx].id == *maf_id)
2675 break;
2676 }
2677
2678 /*
2679 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002680 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002681 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002682 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002683 printk(KERN_INFO "NAND device: Manufacturer ID:"
2684 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2685 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2686 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002687 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002688 busw ? 16 : 8);
2689 return ERR_PTR(-EINVAL);
2690 }
2691
2692 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002693 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002694 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002695 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002696
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002697 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002698 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002699 if (chip->chipsize & 0xffffffff)
2700 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2701 else
2702 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002703
2704 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002705 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002706 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2707
2708 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002709 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002710 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002711
2712 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002713 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002714 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002715 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002716
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002717 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002718 * options for chips which are not having an extended id.
2719 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002720 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002721 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002722
2723 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002724 if (chip->options & NAND_4PAGE_ARRAY)
2725 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002726 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002727 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002728
2729 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002730 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2731 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002732
2733 printk(KERN_INFO "NAND device: Manufacturer ID:"
2734 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2735 nand_manuf_ids[maf_idx].name, type->name);
2736
2737 return type;
2738}
2739
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002740/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002741 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2742 * @mtd: MTD device structure
2743 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002744 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002745 * This is the first phase of the normal nand_scan() function. It
2746 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002747 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002748 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002749 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002750int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002751{
2752 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002753 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002754 struct nand_flash_dev *type;
2755
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002756 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002757 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002758 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002759 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002760
2761 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002762 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002763
2764 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00002765 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
2766 printk(KERN_WARNING "No NAND device found.\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002767 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002768 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
2770
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002771 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002772 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002773 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02002774 /* See comment in nand_get_flash_type for reset */
2775 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002777 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002779 if (nand_maf_id != chip->read_byte(mtd) ||
2780 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 break;
2782 }
2783 if (i > 1)
2784 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002787 chip->numchips = i;
2788 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
David Woodhouse3b85c322006-09-25 17:06:53 +01002790 return 0;
2791}
2792
2793
2794/**
2795 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2796 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01002797 *
2798 * This is the second phase of the normal nand_scan() function. It
2799 * fills out all the uninitialized function pointers with the defaults
2800 * and scans for a bad block table if appropriate.
2801 */
2802int nand_scan_tail(struct mtd_info *mtd)
2803{
2804 int i;
2805 struct nand_chip *chip = mtd->priv;
2806
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002807 if (!(chip->options & NAND_OWN_BUFFERS))
2808 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2809 if (!chip->buffers)
2810 return -ENOMEM;
2811
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002812 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002813 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002814
2815 /*
2816 * If no default placement scheme is given, select an appropriate one
2817 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002818 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002819 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002821 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 break;
2823 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002824 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 break;
2826 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002827 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002829 case 128:
2830 chip->ecc.layout = &nand_oob_128;
2831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002833 printk(KERN_WARNING "No oob scheme defined for "
2834 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 BUG();
2836 }
2837 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002838
David Woodhouse956e9442006-09-25 17:12:39 +01002839 if (!chip->write_page)
2840 chip->write_page = nand_write_page;
2841
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002842 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002843 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2844 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002845 */
David Woodhouse956e9442006-09-25 17:12:39 +01002846
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002847 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002848 case NAND_ECC_HW_OOB_FIRST:
2849 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2850 if (!chip->ecc.calculate || !chip->ecc.correct ||
2851 !chip->ecc.hwctl) {
2852 printk(KERN_WARNING "No ECC functions supplied; "
2853 "Hardware ECC not possible\n");
2854 BUG();
2855 }
2856 if (!chip->ecc.read_page)
2857 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2858
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002859 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002860 /* Use standard hwecc read page function ? */
2861 if (!chip->ecc.read_page)
2862 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002863 if (!chip->ecc.write_page)
2864 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002865 if (!chip->ecc.read_page_raw)
2866 chip->ecc.read_page_raw = nand_read_page_raw;
2867 if (!chip->ecc.write_page_raw)
2868 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002869 if (!chip->ecc.read_oob)
2870 chip->ecc.read_oob = nand_read_oob_std;
2871 if (!chip->ecc.write_oob)
2872 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002873
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002874 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06002875 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2876 !chip->ecc.hwctl) &&
2877 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002878 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06002879 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002880 chip->ecc.write_page == nand_write_page_hwecc)) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002881 printk(KERN_WARNING "No ECC functions supplied; "
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002882 "Hardware ECC not possible\n");
2883 BUG();
2884 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002885 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002886 if (!chip->ecc.read_page)
2887 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002888 if (!chip->ecc.write_page)
2889 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08002890 if (!chip->ecc.read_page_raw)
2891 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2892 if (!chip->ecc.write_page_raw)
2893 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002894 if (!chip->ecc.read_oob)
2895 chip->ecc.read_oob = nand_read_oob_syndrome;
2896 if (!chip->ecc.write_oob)
2897 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002898
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002899 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002900 break;
2901 printk(KERN_WARNING "%d byte HW ECC not possible on "
2902 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002903 chip->ecc.size, mtd->writesize);
2904 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002906 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002907 chip->ecc.calculate = nand_calculate_ecc;
2908 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002909 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01002910 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002911 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002912 chip->ecc.read_page_raw = nand_read_page_raw;
2913 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002914 chip->ecc.read_oob = nand_read_oob_std;
2915 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00002916 if (!chip->ecc.size)
2917 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002918 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002920
2921 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002922 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2923 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002924 chip->ecc.read_page = nand_read_page_raw;
2925 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002926 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08002927 chip->ecc.read_page_raw = nand_read_page_raw;
2928 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002929 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002930 chip->ecc.size = mtd->writesize;
2931 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002935 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002936 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002937 BUG();
2938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002940 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002941 * The number of bytes available for a client to place data into
2942 * the out of band area
2943 */
2944 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07002945 for (i = 0; chip->ecc.layout->oobfree[i].length
2946 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002947 chip->ecc.layout->oobavail +=
2948 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002949 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002950
2951 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002952 * Set the number of read / write steps for one page depending on ECC
2953 * mode
2954 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002955 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2956 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002957 printk(KERN_WARNING "Invalid ecc parameters\n");
2958 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002960 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002961
Thomas Gleixner29072b92006-09-28 15:38:36 +02002962 /*
2963 * Allow subpage writes up to ecc.steps. Not possible for MLC
2964 * FLASH.
2965 */
2966 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2967 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2968 switch(chip->ecc.steps) {
2969 case 2:
2970 mtd->subpage_sft = 1;
2971 break;
2972 case 4:
2973 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002974 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02002975 mtd->subpage_sft = 2;
2976 break;
2977 }
2978 }
2979 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2980
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002981 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002982 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002985 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002988 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990 /* Fill in remaining MTD driver data */
2991 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002992 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 mtd->erase = nand_erase;
2994 mtd->point = NULL;
2995 mtd->unpoint = NULL;
2996 mtd->read = nand_read;
2997 mtd->write = nand_write;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002998 mtd->panic_write = panic_nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 mtd->read_oob = nand_read_oob;
3000 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 mtd->sync = nand_sync;
3002 mtd->lock = NULL;
3003 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01003004 mtd->suspend = nand_suspend;
3005 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 mtd->block_isbad = nand_block_isbad;
3007 mtd->block_markbad = nand_block_markbad;
3008
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003009 /* propagate ecc.layout to mtd_info */
3010 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003012 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003013 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003017 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
Rusty Russella6e6abd2009-03-31 13:05:31 -06003020/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01003021 test if this is a module _anyway_ -- they'd have to try _really_ hard
3022 to call us from in-kernel code if the core NAND support is modular. */
3023#ifdef MODULE
3024#define caller_is_module() (1)
3025#else
3026#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003027 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003028#endif
3029
3030/**
3031 * nand_scan - [NAND Interface] Scan for the NAND device
3032 * @mtd: MTD device structure
3033 * @maxchips: Number of chips to scan for
3034 *
3035 * This fills out all the uninitialized function pointers
3036 * with the defaults.
3037 * The flash ID is read and the mtd/chip structures are
3038 * filled with the appropriate values.
3039 * The mtd->owner field must be set to the module of the caller
3040 *
3041 */
3042int nand_scan(struct mtd_info *mtd, int maxchips)
3043{
3044 int ret;
3045
3046 /* Many callers got this wrong, so check for it for a while... */
3047 if (!mtd->owner && caller_is_module()) {
vimal singh20d8e242009-07-07 15:49:49 +05303048 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
3049 __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003050 BUG();
3051 }
3052
3053 ret = nand_scan_ident(mtd, maxchips);
3054 if (!ret)
3055 ret = nand_scan_tail(mtd);
3056 return ret;
3057}
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003060 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 * @mtd: MTD device structure
3062*/
David Woodhousee0c7d762006-05-13 18:07:53 +01003063void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003065 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067#ifdef CONFIG_MTD_PARTITIONS
3068 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01003069 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070#endif
3071 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01003072 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Jesper Juhlfa671642005-11-07 01:01:27 -08003074 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003075 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003076 if (!(chip->options & NAND_OWN_BUFFERS))
3077 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
David Woodhousee0c7d762006-05-13 18:07:53 +01003080EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003081EXPORT_SYMBOL_GPL(nand_scan_ident);
3082EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01003083EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003084
3085static int __init nand_base_init(void)
3086{
3087 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3088 return 0;
3089}
3090
3091static void __exit nand_base_exit(void)
3092{
3093 led_trigger_unregister_simple(nand_led_trigger);
3094}
3095
3096module_init(nand_base_init);
3097module_exit(nand_base_exit);
3098
David Woodhousee0c7d762006-05-13 18:07:53 +01003099MODULE_LICENSE("GPL");
3100MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3101MODULE_DESCRIPTION("Generic NAND flash driver code");