blob: 724cb2c9ad3f08cbd021bd4df7320000d7aa248b [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) {
743 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200744 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100745 }
746 set_current_state(TASK_UNINTERRUPTIBLE);
747 add_wait_queue(wq, &wait);
748 spin_unlock(lock);
749 schedule();
750 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto retry;
752}
753
754/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200755 * panic_nand_wait - [GENERIC] wait until the command is done
756 * @mtd: MTD device structure
757 * @chip: NAND chip structure
758 * @timeo: Timeout
759 *
760 * Wait for command done. This is a helper function for nand_wait used when
761 * we are in interrupt context. May happen when in panic and trying to write
762 * an oops trough mtdoops.
763 */
764static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
765 unsigned long timeo)
766{
767 int i;
768 for (i = 0; i < timeo; i++) {
769 if (chip->dev_ready) {
770 if (chip->dev_ready(mtd))
771 break;
772 } else {
773 if (chip->read_byte(mtd) & NAND_STATUS_READY)
774 break;
775 }
776 mdelay(1);
777 }
778}
779
780/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * nand_wait - [DEFAULT] wait until the command is done
782 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700783 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 *
785 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000786 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700788 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200789static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791
David Woodhousee0c7d762006-05-13 18:07:53 +0100792 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200793 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100796 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100798 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Richard Purdie8fe833c2006-03-31 02:31:14 -0800800 led_trigger_event(nand_led_trigger, LED_FULL);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* Apply this short delay always to ensure that we do wait tWB in
803 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100804 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200806 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
807 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000808 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200809 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200811 if (in_interrupt() || oops_in_progress)
812 panic_nand_wait(mtd, chip, timeo);
813 else {
814 while (time_before(jiffies, timeo)) {
815 if (chip->dev_ready) {
816 if (chip->dev_ready(mtd))
817 break;
818 } else {
819 if (chip->read_byte(mtd) & NAND_STATUS_READY)
820 break;
821 }
822 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800825 led_trigger_event(nand_led_trigger, LED_OFF);
826
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200827 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return status;
829}
830
831/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200832 * nand_read_page_raw - [Intern] read raw page data without ecc
833 * @mtd: mtd info structure
834 * @chip: nand chip info structure
835 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100836 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -0800837 *
838 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200839 */
840static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700841 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200842{
843 chip->read_buf(mtd, buf, mtd->writesize);
844 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
845 return 0;
846}
847
848/**
David Brownell52ff49d2009-03-04 12:01:36 -0800849 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
850 * @mtd: mtd info structure
851 * @chip: nand chip info structure
852 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100853 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -0800854 *
855 * We need a special oob layout and handling even when OOB isn't used.
856 */
857static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700858 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -0800859{
860 int eccsize = chip->ecc.size;
861 int eccbytes = chip->ecc.bytes;
862 uint8_t *oob = chip->oob_poi;
863 int steps, size;
864
865 for (steps = chip->ecc.steps; steps > 0; steps--) {
866 chip->read_buf(mtd, buf, eccsize);
867 buf += eccsize;
868
869 if (chip->ecc.prepad) {
870 chip->read_buf(mtd, oob, chip->ecc.prepad);
871 oob += chip->ecc.prepad;
872 }
873
874 chip->read_buf(mtd, oob, eccbytes);
875 oob += eccbytes;
876
877 if (chip->ecc.postpad) {
878 chip->read_buf(mtd, oob, chip->ecc.postpad);
879 oob += chip->ecc.postpad;
880 }
881 }
882
883 size = mtd->oobsize - (oob - chip->oob_poi);
884 if (size)
885 chip->read_buf(mtd, oob, size);
886
887 return 0;
888}
889
890/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300891 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200892 * @mtd: mtd info structure
893 * @chip: nand chip info structure
894 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100895 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +0000896 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200897static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700898 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200900 int i, eccsize = chip->ecc.size;
901 int eccbytes = chip->ecc.bytes;
902 int eccsteps = chip->ecc.steps;
903 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100904 uint8_t *ecc_calc = chip->buffers->ecccalc;
905 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100906 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200907
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700908 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200909
910 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
911 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
912
913 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200914 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200915
916 eccsteps = chip->ecc.steps;
917 p = buf;
918
919 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
920 int stat;
921
922 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700923 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200924 mtd->ecc_stats.failed++;
925 else
926 mtd->ecc_stats.corrected += stat;
927 }
928 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100929}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/**
Alexey Korolev3d459552008-05-15 17:23:18 +0100932 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
933 * @mtd: mtd info structure
934 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +0100935 * @data_offs: offset of requested data within the page
936 * @readlen: data length
937 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +0100938 */
939static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
940{
941 int start_step, end_step, num_steps;
942 uint32_t *eccpos = chip->ecc.layout->eccpos;
943 uint8_t *p;
944 int data_col_addr, i, gaps = 0;
945 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
946 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
947
948 /* Column address wihin the page aligned to ECC size (256bytes). */
949 start_step = data_offs / chip->ecc.size;
950 end_step = (data_offs + readlen - 1) / chip->ecc.size;
951 num_steps = end_step - start_step + 1;
952
953 /* Data size aligned to ECC ecc.size*/
954 datafrag_len = num_steps * chip->ecc.size;
955 eccfrag_len = num_steps * chip->ecc.bytes;
956
957 data_col_addr = start_step * chip->ecc.size;
958 /* If we read not a page aligned data */
959 if (data_col_addr != 0)
960 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
961
962 p = bufpoi + data_col_addr;
963 chip->read_buf(mtd, p, datafrag_len);
964
965 /* Calculate ECC */
966 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
967 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
968
969 /* The performance is faster if to position offsets
970 according to ecc.pos. Let make sure here that
971 there are no gaps in ecc positions */
972 for (i = 0; i < eccfrag_len - 1; i++) {
973 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
974 eccpos[i + start_step * chip->ecc.bytes + 1]) {
975 gaps = 1;
976 break;
977 }
978 }
979 if (gaps) {
980 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
981 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
982 } else {
983 /* send the command to read the particular ecc bytes */
984 /* take care about buswidth alignment in read_buf */
985 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
986 aligned_len = eccfrag_len;
987 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
988 aligned_len++;
989 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
990 aligned_len++;
991
992 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
993 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
994 }
995
996 for (i = 0; i < eccfrag_len; i++)
997 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
998
999 p = bufpoi + data_col_addr;
1000 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1001 int stat;
1002
1003 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1004 if (stat == -1)
1005 mtd->ecc_stats.failed++;
1006 else
1007 mtd->ecc_stats.corrected += stat;
1008 }
1009 return 0;
1010}
1011
1012/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001013 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001014 * @mtd: mtd info structure
1015 * @chip: nand chip info structure
1016 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001017 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001018 *
1019 * Not for syndrome calculating ecc controllers which need a special oob layout
1020 */
1021static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001022 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001023{
1024 int i, eccsize = chip->ecc.size;
1025 int eccbytes = chip->ecc.bytes;
1026 int eccsteps = chip->ecc.steps;
1027 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001028 uint8_t *ecc_calc = chip->buffers->ecccalc;
1029 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001030 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001031
1032 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1033 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1034 chip->read_buf(mtd, p, eccsize);
1035 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1036 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001037 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001038
1039 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001040 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001041
1042 eccsteps = chip->ecc.steps;
1043 p = buf;
1044
1045 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1046 int stat;
1047
1048 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001049 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001050 mtd->ecc_stats.failed++;
1051 else
1052 mtd->ecc_stats.corrected += stat;
1053 }
1054 return 0;
1055}
1056
1057/**
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001058 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1059 * @mtd: mtd info structure
1060 * @chip: nand chip info structure
1061 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001062 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001063 *
1064 * Hardware ECC for large page chips, require OOB to be read first.
1065 * For this ECC mode, the write_page method is re-used from ECC_HW.
1066 * These methods read/write ECC from the OOB area, unlike the
1067 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1068 * "infix ECC" scheme and reads/writes ECC from the data area, by
1069 * overwriting the NAND manufacturer bad block markings.
1070 */
1071static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1072 struct nand_chip *chip, uint8_t *buf, int page)
1073{
1074 int i, eccsize = chip->ecc.size;
1075 int eccbytes = chip->ecc.bytes;
1076 int eccsteps = chip->ecc.steps;
1077 uint8_t *p = buf;
1078 uint8_t *ecc_code = chip->buffers->ecccode;
1079 uint32_t *eccpos = chip->ecc.layout->eccpos;
1080 uint8_t *ecc_calc = chip->buffers->ecccalc;
1081
1082 /* Read the OOB area first */
1083 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1084 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1085 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1086
1087 for (i = 0; i < chip->ecc.total; i++)
1088 ecc_code[i] = chip->oob_poi[eccpos[i]];
1089
1090 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1091 int stat;
1092
1093 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1094 chip->read_buf(mtd, p, eccsize);
1095 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1096
1097 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1098 if (stat < 0)
1099 mtd->ecc_stats.failed++;
1100 else
1101 mtd->ecc_stats.corrected += stat;
1102 }
1103 return 0;
1104}
1105
1106/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001107 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001108 * @mtd: mtd info structure
1109 * @chip: nand chip info structure
1110 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001111 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001112 *
1113 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001114 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001115 */
1116static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001117 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001118{
1119 int i, eccsize = chip->ecc.size;
1120 int eccbytes = chip->ecc.bytes;
1121 int eccsteps = chip->ecc.steps;
1122 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001123 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001124
1125 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1126 int stat;
1127
1128 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1129 chip->read_buf(mtd, p, eccsize);
1130
1131 if (chip->ecc.prepad) {
1132 chip->read_buf(mtd, oob, chip->ecc.prepad);
1133 oob += chip->ecc.prepad;
1134 }
1135
1136 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1137 chip->read_buf(mtd, oob, eccbytes);
1138 stat = chip->ecc.correct(mtd, p, oob, NULL);
1139
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001140 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001141 mtd->ecc_stats.failed++;
1142 else
1143 mtd->ecc_stats.corrected += stat;
1144
1145 oob += eccbytes;
1146
1147 if (chip->ecc.postpad) {
1148 chip->read_buf(mtd, oob, chip->ecc.postpad);
1149 oob += chip->ecc.postpad;
1150 }
1151 }
1152
1153 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001154 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001155 if (i)
1156 chip->read_buf(mtd, oob, i);
1157
1158 return 0;
1159}
1160
1161/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001162 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1163 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001164 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001165 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001166 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001167 */
1168static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001169 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001170{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001171 switch(ops->mode) {
1172
1173 case MTD_OOB_PLACE:
1174 case MTD_OOB_RAW:
1175 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1176 return oob + len;
1177
1178 case MTD_OOB_AUTO: {
1179 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001180 uint32_t boffs = 0, roffs = ops->ooboffs;
1181 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001182
1183 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001184 /* Read request not from offset 0 ? */
1185 if (unlikely(roffs)) {
1186 if (roffs >= free->length) {
1187 roffs -= free->length;
1188 continue;
1189 }
1190 boffs = free->offset + roffs;
1191 bytes = min_t(size_t, len,
1192 (free->length - roffs));
1193 roffs = 0;
1194 } else {
1195 bytes = min_t(size_t, len, free->length);
1196 boffs = free->offset;
1197 }
1198 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001199 oob += bytes;
1200 }
1201 return oob;
1202 }
1203 default:
1204 BUG();
1205 }
1206 return NULL;
1207}
1208
1209/**
1210 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001211 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001212 * @mtd: MTD device structure
1213 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001214 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001215 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001216 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001217 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001218static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1219 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001220{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001221 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001222 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001223 struct mtd_ecc_stats stats;
1224 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1225 int sndcmd = 1;
1226 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001227 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001228 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001229 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001231 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001233 chipnr = (int)(from >> chip->chip_shift);
1234 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001236 realpage = (int)(from >> chip->page_shift);
1237 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001239 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001241 buf = ops->datbuf;
1242 oob = ops->oobbuf;
1243
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001244 while(1) {
1245 bytes = min(mtd->writesize - col, readlen);
1246 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001247
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001248 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001249 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001250 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001252 if (likely(sndcmd)) {
1253 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1254 sndcmd = 0;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001257 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001258 if (unlikely(ops->mode == MTD_OOB_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001259 ret = chip->ecc.read_page_raw(mtd, chip,
1260 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001261 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1262 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001263 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001264 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1265 page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001266 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001267 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001268
1269 /* Transfer not aligned data */
1270 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001271 if (!NAND_SUBPAGE_READ(chip) && !oob)
1272 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001273 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001275
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001276 buf += bytes;
1277
1278 if (unlikely(oob)) {
1279 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001280 if (ops->mode != MTD_OOB_RAW) {
1281 int toread = min(oobreadlen,
1282 chip->ecc.layout->oobavail);
1283 if (toread) {
1284 oob = nand_transfer_oob(chip,
1285 oob, ops, toread);
1286 oobreadlen -= toread;
1287 }
1288 } else
1289 buf = nand_transfer_oob(chip,
1290 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001291 }
1292
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001293 if (!(chip->options & NAND_NO_READRDY)) {
1294 /*
1295 * Apply delay or wait for ready/busy pin. Do
1296 * this before the AUTOINCR check, so no
1297 * problems arise if a chip which does auto
1298 * increment is marked as NOAUTOINCR by the
1299 * board driver.
1300 */
1301 if (!chip->dev_ready)
1302 udelay(chip->chip_delay);
1303 else
1304 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001306 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001307 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001308 buf += bytes;
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001311 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001312
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001313 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /* For subsequent reads align to page boundary. */
1317 col = 0;
1318 /* Increment page address */
1319 realpage++;
1320
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001321 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /* Check, if we cross a chip boundary */
1323 if (!page) {
1324 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001325 chip->select_chip(mtd, -1);
1326 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001328
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001329 /* Check, if the chip supports auto page increment
1330 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001331 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001332 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001333 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001336 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001337 if (oob)
1338 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001340 if (ret)
1341 return ret;
1342
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001343 if (mtd->ecc_stats.failed - stats.failed)
1344 return -EBADMSG;
1345
1346 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001347}
1348
1349/**
1350 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1351 * @mtd: MTD device structure
1352 * @from: offset to read from
1353 * @len: number of bytes to read
1354 * @retlen: pointer to variable to store the number of read bytes
1355 * @buf: the databuffer to put data
1356 *
1357 * Get hold of the chip and call nand_do_read
1358 */
1359static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1360 size_t *retlen, uint8_t *buf)
1361{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001362 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001363 int ret;
1364
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001365 /* Do not allow reads past end of device */
1366 if ((from + len) > mtd->size)
1367 return -EINVAL;
1368 if (!len)
1369 return 0;
1370
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001371 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001372
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001373 chip->ops.len = len;
1374 chip->ops.datbuf = buf;
1375 chip->ops.oobbuf = NULL;
1376
1377 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001378
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001379 *retlen = chip->ops.retlen;
1380
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001381 nand_release_device(mtd);
1382
1383 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001387 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1388 * @mtd: mtd info structure
1389 * @chip: nand chip info structure
1390 * @page: page number to read
1391 * @sndcmd: flag whether to issue read command or not
1392 */
1393static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1394 int page, int sndcmd)
1395{
1396 if (sndcmd) {
1397 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1398 sndcmd = 0;
1399 }
1400 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1401 return sndcmd;
1402}
1403
1404/**
1405 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1406 * with syndromes
1407 * @mtd: mtd info structure
1408 * @chip: nand chip info structure
1409 * @page: page number to read
1410 * @sndcmd: flag whether to issue read command or not
1411 */
1412static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1413 int page, int sndcmd)
1414{
1415 uint8_t *buf = chip->oob_poi;
1416 int length = mtd->oobsize;
1417 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1418 int eccsize = chip->ecc.size;
1419 uint8_t *bufpoi = buf;
1420 int i, toread, sndrnd = 0, pos;
1421
1422 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1423 for (i = 0; i < chip->ecc.steps; i++) {
1424 if (sndrnd) {
1425 pos = eccsize + i * (eccsize + chunk);
1426 if (mtd->writesize > 512)
1427 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1428 else
1429 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1430 } else
1431 sndrnd = 1;
1432 toread = min_t(int, length, chunk);
1433 chip->read_buf(mtd, bufpoi, toread);
1434 bufpoi += toread;
1435 length -= toread;
1436 }
1437 if (length > 0)
1438 chip->read_buf(mtd, bufpoi, length);
1439
1440 return 1;
1441}
1442
1443/**
1444 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1445 * @mtd: mtd info structure
1446 * @chip: nand chip info structure
1447 * @page: page number to write
1448 */
1449static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1450 int page)
1451{
1452 int status = 0;
1453 const uint8_t *buf = chip->oob_poi;
1454 int length = mtd->oobsize;
1455
1456 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1457 chip->write_buf(mtd, buf, length);
1458 /* Send command to program the OOB data */
1459 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1460
1461 status = chip->waitfunc(mtd, chip);
1462
Savin Zlobec0d420f92006-06-21 11:51:20 +02001463 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001464}
1465
1466/**
1467 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1468 * with syndrome - only for large page flash !
1469 * @mtd: mtd info structure
1470 * @chip: nand chip info structure
1471 * @page: page number to write
1472 */
1473static int nand_write_oob_syndrome(struct mtd_info *mtd,
1474 struct nand_chip *chip, int page)
1475{
1476 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1477 int eccsize = chip->ecc.size, length = mtd->oobsize;
1478 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1479 const uint8_t *bufpoi = chip->oob_poi;
1480
1481 /*
1482 * data-ecc-data-ecc ... ecc-oob
1483 * or
1484 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1485 */
1486 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1487 pos = steps * (eccsize + chunk);
1488 steps = 0;
1489 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001490 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001491
1492 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1493 for (i = 0; i < steps; i++) {
1494 if (sndcmd) {
1495 if (mtd->writesize <= 512) {
1496 uint32_t fill = 0xFFFFFFFF;
1497
1498 len = eccsize;
1499 while (len > 0) {
1500 int num = min_t(int, len, 4);
1501 chip->write_buf(mtd, (uint8_t *)&fill,
1502 num);
1503 len -= num;
1504 }
1505 } else {
1506 pos = eccsize + i * (eccsize + chunk);
1507 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1508 }
1509 } else
1510 sndcmd = 1;
1511 len = min_t(int, length, chunk);
1512 chip->write_buf(mtd, bufpoi, len);
1513 bufpoi += len;
1514 length -= len;
1515 }
1516 if (length > 0)
1517 chip->write_buf(mtd, bufpoi, length);
1518
1519 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1520 status = chip->waitfunc(mtd, chip);
1521
1522 return status & NAND_STATUS_FAIL ? -EIO : 0;
1523}
1524
1525/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001526 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 * @mtd: MTD device structure
1528 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001529 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 *
1531 * NAND read out-of-band data from the spare area
1532 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001533static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1534 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001536 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001537 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001538 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001539 int readlen = ops->ooblen;
1540 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001541 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
vimal singh20d8e242009-07-07 15:49:49 +05301543 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1544 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Adrian Hunter03736152007-01-31 17:58:29 +02001546 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001547 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001548 else
1549 len = mtd->oobsize;
1550
1551 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301552 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1553 "outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001554 return -EINVAL;
1555 }
1556
1557 /* Do not allow reads past end of device */
1558 if (unlikely(from >= mtd->size ||
1559 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1560 (from >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301561 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1562 "of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001563 return -EINVAL;
1564 }
Vitaly Wool70145682006-11-03 18:20:38 +03001565
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001566 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001567 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001569 /* Shift to get page */
1570 realpage = (int)(from >> chip->page_shift);
1571 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001573 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001574 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001575
1576 len = min(len, readlen);
1577 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001578
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001579 if (!(chip->options & NAND_NO_READRDY)) {
1580 /*
1581 * Apply delay or wait for ready/busy pin. Do this
1582 * before the AUTOINCR check, so no problems arise if a
1583 * chip which does auto increment is marked as
1584 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001585 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001586 if (!chip->dev_ready)
1587 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001588 else
1589 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001591
Vitaly Wool70145682006-11-03 18:20:38 +03001592 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001593 if (!readlen)
1594 break;
1595
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001596 /* Increment page address */
1597 realpage++;
1598
1599 page = realpage & chip->pagemask;
1600 /* Check, if we cross a chip boundary */
1601 if (!page) {
1602 chipnr++;
1603 chip->select_chip(mtd, -1);
1604 chip->select_chip(mtd, chipnr);
1605 }
1606
1607 /* Check, if the chip supports auto page increment
1608 * or if we have hit a block boundary.
1609 */
1610 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1611 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613
Vitaly Wool70145682006-11-03 18:20:38 +03001614 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 0;
1616}
1617
1618/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001619 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001622 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001624 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001626static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1627 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001629 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001630 int ret = -ENOTSUPP;
1631
1632 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001635 if (ops->datbuf && (from + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05301636 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1637 "beyond end of device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return -EINVAL;
1639 }
1640
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001641 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001643 switch(ops->mode) {
1644 case MTD_OOB_PLACE:
1645 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001646 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001647 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001648
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001649 default:
1650 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001653 if (!ops->datbuf)
1654 ret = nand_do_read_oob(mtd, from, ops);
1655 else
1656 ret = nand_do_read_ops(mtd, from, ops);
1657
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001658 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001660 return ret;
1661}
1662
1663
1664/**
1665 * nand_write_page_raw - [Intern] raw page write function
1666 * @mtd: mtd info structure
1667 * @chip: nand chip info structure
1668 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001669 *
1670 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001671 */
1672static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1673 const uint8_t *buf)
1674{
1675 chip->write_buf(mtd, buf, mtd->writesize);
1676 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001679/**
David Brownell52ff49d2009-03-04 12:01:36 -08001680 * nand_write_page_raw_syndrome - [Intern] raw page write function
1681 * @mtd: mtd info structure
1682 * @chip: nand chip info structure
1683 * @buf: data buffer
1684 *
1685 * We need a special oob layout and handling even when ECC isn't checked.
1686 */
1687static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1688 const uint8_t *buf)
1689{
1690 int eccsize = chip->ecc.size;
1691 int eccbytes = chip->ecc.bytes;
1692 uint8_t *oob = chip->oob_poi;
1693 int steps, size;
1694
1695 for (steps = chip->ecc.steps; steps > 0; steps--) {
1696 chip->write_buf(mtd, buf, eccsize);
1697 buf += eccsize;
1698
1699 if (chip->ecc.prepad) {
1700 chip->write_buf(mtd, oob, chip->ecc.prepad);
1701 oob += chip->ecc.prepad;
1702 }
1703
1704 chip->read_buf(mtd, oob, eccbytes);
1705 oob += eccbytes;
1706
1707 if (chip->ecc.postpad) {
1708 chip->write_buf(mtd, oob, chip->ecc.postpad);
1709 oob += chip->ecc.postpad;
1710 }
1711 }
1712
1713 size = mtd->oobsize - (oob - chip->oob_poi);
1714 if (size)
1715 chip->write_buf(mtd, oob, size);
1716}
1717/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001718 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001719 * @mtd: mtd info structure
1720 * @chip: nand chip info structure
1721 * @buf: data buffer
1722 */
1723static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1724 const uint8_t *buf)
1725{
1726 int i, eccsize = chip->ecc.size;
1727 int eccbytes = chip->ecc.bytes;
1728 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001729 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001730 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001731 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001732
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001733 /* Software ecc calculation */
1734 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1735 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001736
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001737 for (i = 0; i < chip->ecc.total; i++)
1738 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001739
Thomas Gleixner90424de2007-04-05 11:44:05 +02001740 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001741}
1742
1743/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001744 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001745 * @mtd: mtd info structure
1746 * @chip: nand chip info structure
1747 * @buf: data buffer
1748 */
1749static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1750 const uint8_t *buf)
1751{
1752 int i, eccsize = chip->ecc.size;
1753 int eccbytes = chip->ecc.bytes;
1754 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001755 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001756 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001757 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001758
1759 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1760 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001761 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001762 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1763 }
1764
1765 for (i = 0; i < chip->ecc.total; i++)
1766 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1767
1768 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1769}
1770
1771/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001772 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001773 * @mtd: mtd info structure
1774 * @chip: nand chip info structure
1775 * @buf: data buffer
1776 *
1777 * The hw generator calculates the error syndrome automatically. Therefor
1778 * we need a special oob layout and handling.
1779 */
1780static void nand_write_page_syndrome(struct mtd_info *mtd,
1781 struct nand_chip *chip, const uint8_t *buf)
1782{
1783 int i, eccsize = chip->ecc.size;
1784 int eccbytes = chip->ecc.bytes;
1785 int eccsteps = chip->ecc.steps;
1786 const uint8_t *p = buf;
1787 uint8_t *oob = chip->oob_poi;
1788
1789 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1790
1791 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1792 chip->write_buf(mtd, p, eccsize);
1793
1794 if (chip->ecc.prepad) {
1795 chip->write_buf(mtd, oob, chip->ecc.prepad);
1796 oob += chip->ecc.prepad;
1797 }
1798
1799 chip->ecc.calculate(mtd, p, oob);
1800 chip->write_buf(mtd, oob, eccbytes);
1801 oob += eccbytes;
1802
1803 if (chip->ecc.postpad) {
1804 chip->write_buf(mtd, oob, chip->ecc.postpad);
1805 oob += chip->ecc.postpad;
1806 }
1807 }
1808
1809 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001810 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001811 if (i)
1812 chip->write_buf(mtd, oob, i);
1813}
1814
1815/**
David Woodhouse956e9442006-09-25 17:12:39 +01001816 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001817 * @mtd: MTD device structure
1818 * @chip: NAND chip descriptor
1819 * @buf: the data to write
1820 * @page: page number to write
1821 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02001822 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001823 */
1824static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001825 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001826{
1827 int status;
1828
1829 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1830
David Woodhouse956e9442006-09-25 17:12:39 +01001831 if (unlikely(raw))
1832 chip->ecc.write_page_raw(mtd, chip, buf);
1833 else
1834 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001835
1836 /*
1837 * Cached progamming disabled for now, Not sure if its worth the
1838 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1839 */
1840 cached = 0;
1841
1842 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1843
1844 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001845 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001846 /*
1847 * See if operation failed and additional status checks are
1848 * available
1849 */
1850 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1851 status = chip->errstat(mtd, chip, FL_WRITING, status,
1852 page);
1853
1854 if (status & NAND_STATUS_FAIL)
1855 return -EIO;
1856 } else {
1857 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001858 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001859 }
1860
1861#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1862 /* Send command to read back the data */
1863 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1864
1865 if (chip->verify_buf(mtd, buf, mtd->writesize))
1866 return -EIO;
1867#endif
1868 return 0;
1869}
1870
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001871/**
1872 * nand_fill_oob - [Internal] Transfer client buffer to oob
1873 * @chip: nand chip structure
1874 * @oob: oob data buffer
1875 * @ops: oob ops structure
1876 */
1877static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1878 struct mtd_oob_ops *ops)
1879{
1880 size_t len = ops->ooblen;
1881
1882 switch(ops->mode) {
1883
1884 case MTD_OOB_PLACE:
1885 case MTD_OOB_RAW:
1886 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1887 return oob + len;
1888
1889 case MTD_OOB_AUTO: {
1890 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001891 uint32_t boffs = 0, woffs = ops->ooboffs;
1892 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001893
1894 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001895 /* Write request not from offset 0 ? */
1896 if (unlikely(woffs)) {
1897 if (woffs >= free->length) {
1898 woffs -= free->length;
1899 continue;
1900 }
1901 boffs = free->offset + woffs;
1902 bytes = min_t(size_t, len,
1903 (free->length - woffs));
1904 woffs = 0;
1905 } else {
1906 bytes = min_t(size_t, len, free->length);
1907 boffs = free->offset;
1908 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001909 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001910 oob += bytes;
1911 }
1912 return oob;
1913 }
1914 default:
1915 BUG();
1916 }
1917 return NULL;
1918}
1919
Thomas Gleixner29072b92006-09-28 15:38:36 +02001920#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001921
1922/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001923 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001924 * @mtd: MTD device structure
1925 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001926 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001927 *
1928 * NAND write with ECC
1929 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001930static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1931 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001932{
Thomas Gleixner29072b92006-09-28 15:38:36 +02001933 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001934 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001935 uint32_t writelen = ops->len;
1936 uint8_t *oob = ops->oobbuf;
1937 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001938 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001939
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001940 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001941 if (!writelen)
1942 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001943
1944 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001945 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301946 printk(KERN_NOTICE "%s: Attempt to write not "
1947 "page aligned data\n", __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001948 return -EINVAL;
1949 }
1950
Thomas Gleixner29072b92006-09-28 15:38:36 +02001951 column = to & (mtd->writesize - 1);
1952 subpage = column || (writelen & (mtd->writesize - 1));
1953
1954 if (subpage && oob)
1955 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001956
Thomas Gleixner6a930962006-06-28 00:11:45 +02001957 chipnr = (int)(to >> chip->chip_shift);
1958 chip->select_chip(mtd, chipnr);
1959
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001960 /* Check, if it is write protected */
1961 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001962 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001963
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001964 realpage = (int)(to >> chip->page_shift);
1965 page = realpage & chip->pagemask;
1966 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1967
1968 /* Invalidate the page cache, when we write to the cached page */
1969 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001970 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001971 chip->pagebuf = -1;
1972
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001973 /* If we're not given explicit OOB data, let it be 0xFF */
1974 if (likely(!oob))
1975 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001976
1977 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02001978 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001979 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001980 uint8_t *wbuf = buf;
1981
1982 /* Partial page write ? */
1983 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1984 cached = 0;
1985 bytes = min_t(int, bytes - column, (int) writelen);
1986 chip->pagebuf = -1;
1987 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1988 memcpy(&chip->buffers->databuf[column], buf, bytes);
1989 wbuf = chip->buffers->databuf;
1990 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001991
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001992 if (unlikely(oob))
1993 oob = nand_fill_oob(chip, oob, ops);
1994
Thomas Gleixner29072b92006-09-28 15:38:36 +02001995 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01001996 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001997 if (ret)
1998 break;
1999
2000 writelen -= bytes;
2001 if (!writelen)
2002 break;
2003
Thomas Gleixner29072b92006-09-28 15:38:36 +02002004 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002005 buf += bytes;
2006 realpage++;
2007
2008 page = realpage & chip->pagemask;
2009 /* Check, if we cross a chip boundary */
2010 if (!page) {
2011 chipnr++;
2012 chip->select_chip(mtd, -1);
2013 chip->select_chip(mtd, chipnr);
2014 }
2015 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002016
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002017 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002018 if (unlikely(oob))
2019 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002020 return ret;
2021}
2022
2023/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002024 * panic_nand_write - [MTD Interface] NAND write with ECC
2025 * @mtd: MTD device structure
2026 * @to: offset to write to
2027 * @len: number of bytes to write
2028 * @retlen: pointer to variable to store the number of written bytes
2029 * @buf: the data to write
2030 *
2031 * NAND write with ECC. Used when performing writes in interrupt context, this
2032 * may for example be called by mtdoops when writing an oops while in panic.
2033 */
2034static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2035 size_t *retlen, const uint8_t *buf)
2036{
2037 struct nand_chip *chip = mtd->priv;
2038 int ret;
2039
2040 /* Do not allow reads past end of device */
2041 if ((to + len) > mtd->size)
2042 return -EINVAL;
2043 if (!len)
2044 return 0;
2045
2046 /* Wait for the device to get ready. */
2047 panic_nand_wait(mtd, chip, 400);
2048
2049 /* Grab the device. */
2050 panic_nand_get_device(chip, mtd, FL_WRITING);
2051
2052 chip->ops.len = len;
2053 chip->ops.datbuf = (uint8_t *)buf;
2054 chip->ops.oobbuf = NULL;
2055
2056 ret = nand_do_write_ops(mtd, to, &chip->ops);
2057
2058 *retlen = chip->ops.retlen;
2059 return ret;
2060}
2061
2062/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002063 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 * @mtd: MTD device structure
2065 * @to: offset to write to
2066 * @len: number of bytes to write
2067 * @retlen: pointer to variable to store the number of written bytes
2068 * @buf: the data to write
2069 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002070 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002072static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002073 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002075 struct nand_chip *chip = mtd->priv;
2076 int ret;
2077
2078 /* Do not allow reads past end of device */
2079 if ((to + len) > mtd->size)
2080 return -EINVAL;
2081 if (!len)
2082 return 0;
2083
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002084 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002085
2086 chip->ops.len = len;
2087 chip->ops.datbuf = (uint8_t *)buf;
2088 chip->ops.oobbuf = NULL;
2089
2090 ret = nand_do_write_ops(mtd, to, &chip->ops);
2091
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002092 *retlen = chip->ops.retlen;
2093
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002094 nand_release_device(mtd);
2095
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002096 return ret;
2097}
2098
2099/**
2100 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2101 * @mtd: MTD device structure
2102 * @to: offset to write to
2103 * @ops: oob operation description structure
2104 *
2105 * NAND write out-of-band
2106 */
2107static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2108 struct mtd_oob_ops *ops)
2109{
Adrian Hunter03736152007-01-31 17:58:29 +02002110 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002111 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
vimal singh20d8e242009-07-07 15:49:49 +05302113 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2114 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Adrian Hunter03736152007-01-31 17:58:29 +02002116 if (ops->mode == MTD_OOB_AUTO)
2117 len = chip->ecc.layout->oobavail;
2118 else
2119 len = mtd->oobsize;
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002122 if ((ops->ooboffs + ops->ooblen) > len) {
vimal singh20d8e242009-07-07 15:49:49 +05302123 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2124 "past end of page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return -EINVAL;
2126 }
2127
Adrian Hunter03736152007-01-31 17:58:29 +02002128 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302129 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2130 "write outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002131 return -EINVAL;
2132 }
2133
2134 /* Do not allow reads past end of device */
2135 if (unlikely(to >= mtd->size ||
2136 ops->ooboffs + ops->ooblen >
2137 ((mtd->size >> chip->page_shift) -
2138 (to >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302139 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2140 "end of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002141 return -EINVAL;
2142 }
2143
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002144 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002145 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002147 /* Shift to get page */
2148 page = (int)(to >> chip->page_shift);
2149
2150 /*
2151 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2152 * of my DiskOnChip 2000 test units) will clear the whole data page too
2153 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2154 * it in the doc2000 driver in August 1999. dwmw2.
2155 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002156 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 /* Check, if it is write protected */
2159 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002160 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002163 if (page == chip->pagebuf)
2164 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002166 memset(chip->oob_poi, 0xff, mtd->oobsize);
2167 nand_fill_oob(chip, ops->oobbuf, ops);
2168 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2169 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002170
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002171 if (status)
2172 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Vitaly Wool70145682006-11-03 18:20:38 +03002174 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002176 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002179/**
2180 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2181 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002182 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002183 * @ops: oob operation description structure
2184 */
2185static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2186 struct mtd_oob_ops *ops)
2187{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002188 struct nand_chip *chip = mtd->priv;
2189 int ret = -ENOTSUPP;
2190
2191 ops->retlen = 0;
2192
2193 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002194 if (ops->datbuf && (to + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302195 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2196 "end of device\n", __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002197 return -EINVAL;
2198 }
2199
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002200 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002201
2202 switch(ops->mode) {
2203 case MTD_OOB_PLACE:
2204 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002205 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002206 break;
2207
2208 default:
2209 goto out;
2210 }
2211
2212 if (!ops->datbuf)
2213 ret = nand_do_write_oob(mtd, to, ops);
2214 else
2215 ret = nand_do_write_ops(mtd, to, ops);
2216
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002217 out:
2218 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return ret;
2220}
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2224 * @mtd: MTD device structure
2225 * @page: the page address of the block which will be erased
2226 *
2227 * Standard erase command for NAND chips
2228 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002229static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002231 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002233 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2234 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
2237/**
2238 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2239 * @mtd: MTD device structure
2240 * @page: the page address of the block which will be erased
2241 *
2242 * AND multi block erase command function
2243 * Erase 4 consecutive blocks
2244 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002245static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002247 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002249 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2250 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2251 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2252 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2253 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
2256/**
2257 * nand_erase - [MTD Interface] erase block(s)
2258 * @mtd: MTD device structure
2259 * @instr: erase instruction
2260 *
2261 * Erase one ore more blocks
2262 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002263static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
David Woodhousee0c7d762006-05-13 18:07:53 +01002265 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002267
David A. Marlin30f464b2005-01-17 18:35:25 +00002268#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002270 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 * @mtd: MTD device structure
2272 * @instr: erase instruction
2273 * @allowbbt: allow erasing the bbt area
2274 *
2275 * Erase one ore more blocks
2276 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002277int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2278 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Adrian Hunter69423d92008-12-10 13:37:21 +00002280 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002281 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002282 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002283 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002284 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
vimal singh20d8e242009-07-07 15:49:49 +05302286 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2287 __func__, (unsigned long long)instr->addr,
2288 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002291 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
vimal singh20d8e242009-07-07 15:49:49 +05302292 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return -EINVAL;
2294 }
2295
2296 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002297 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
vimal singh20d8e242009-07-07 15:49:49 +05302298 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
2299 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return -EINVAL;
2301 }
2302
2303 /* Do not allow erase past end of device */
2304 if ((instr->len + instr->addr) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302305 DEBUG(MTD_DEBUG_LEVEL0, "%s: Erase past end of device\n",
2306 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return -EINVAL;
2308 }
2309
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002310 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002313 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002316 page = (int)(instr->addr >> chip->page_shift);
2317 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002320 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002323 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 /* Check, if it is write protected */
2326 if (nand_check_wp(mtd)) {
vimal singh20d8e242009-07-07 15:49:49 +05302327 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2328 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 instr->state = MTD_ERASE_FAILED;
2330 goto erase_exit;
2331 }
2332
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002333 /*
2334 * If BBT requires refresh, set the BBT page mask to see if the BBT
2335 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2336 * can not be matched. This is also done when the bbt is actually
2337 * erased to avoid recusrsive updates
2338 */
2339 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2340 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* Loop through the pages */
2343 len = instr->len;
2344
2345 instr->state = MTD_ERASING;
2346
2347 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002348 /*
2349 * heck if we have a bad block, we do not erase bad blocks !
2350 */
2351 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2352 chip->page_shift, 0, allowbbt)) {
vimal singh20d8e242009-07-07 15:49:49 +05302353 printk(KERN_WARNING "%s: attempt to erase a bad block "
2354 "at page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 instr->state = MTD_ERASE_FAILED;
2356 goto erase_exit;
2357 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002358
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002359 /*
2360 * Invalidate the page cache, if we erase the block which
2361 * contains the current cached page
2362 */
2363 if (page <= chip->pagebuf && chip->pagebuf <
2364 (page + pages_per_block))
2365 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002367 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002368
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002369 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002371 /*
2372 * See if operation failed and additional status checks are
2373 * available
2374 */
2375 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2376 status = chip->errstat(mtd, chip, FL_ERASING,
2377 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002380 if (status & NAND_STATUS_FAIL) {
vimal singh20d8e242009-07-07 15:49:49 +05302381 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2382 "page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002384 instr->fail_addr =
2385 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 goto erase_exit;
2387 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002388
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002389 /*
2390 * If BBT requires refresh, set the BBT rewrite flag to the
2391 * page being erased
2392 */
2393 if (bbt_masked_page != 0xffffffff &&
2394 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002395 rewrite_bbt[chipnr] =
2396 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002399 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 page += pages_per_block;
2401
2402 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002403 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002405 chip->select_chip(mtd, -1);
2406 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002407
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002408 /*
2409 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2410 * page mask to see if this BBT should be rewritten
2411 */
2412 if (bbt_masked_page != 0xffffffff &&
2413 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2414 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2415 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417 }
2418 instr->state = MTD_ERASE_DONE;
2419
David Woodhousee0c7d762006-05-13 18:07:53 +01002420 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 /* Deselect and wake up anyone waiting on the device */
2425 nand_release_device(mtd);
2426
David Woodhouse49defc02007-10-06 15:01:59 -04002427 /* Do call back function */
2428 if (!ret)
2429 mtd_erase_callback(instr);
2430
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002431 /*
2432 * If BBT requires refresh and erase was successful, rewrite any
2433 * selected bad block tables
2434 */
2435 if (bbt_masked_page == 0xffffffff || ret)
2436 return ret;
2437
2438 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2439 if (!rewrite_bbt[chipnr])
2440 continue;
2441 /* update the BBT for chip */
vimal singh20d8e242009-07-07 15:49:49 +05302442 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2443 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2444 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002445 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002446 }
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* Return more or less happy */
2449 return ret;
2450}
2451
2452/**
2453 * nand_sync - [MTD Interface] sync
2454 * @mtd: MTD device structure
2455 *
2456 * Sync is actually a wait for chip ready function
2457 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002458static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002460 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
vimal singh20d8e242009-07-07 15:49:49 +05302462 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002465 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002467 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002471 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002473 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002475static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002478 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002480
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002481 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002485 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * @mtd: MTD device structure
2487 * @ofs: offset relative to mtd start
2488 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002489static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002491 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 int ret;
2493
David Woodhousee0c7d762006-05-13 18:07:53 +01002494 if ((ret = nand_block_isbad(mtd, ofs))) {
2495 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (ret > 0)
2497 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002498 return ret;
2499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002501 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002505 * nand_suspend - [MTD Interface] Suspend the NAND flash
2506 * @mtd: MTD device structure
2507 */
2508static int nand_suspend(struct mtd_info *mtd)
2509{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002510 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002511
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002512 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002513}
2514
2515/**
2516 * nand_resume - [MTD Interface] Resume the NAND flash
2517 * @mtd: MTD device structure
2518 */
2519static void nand_resume(struct mtd_info *mtd)
2520{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002521 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002522
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002523 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002524 nand_release_device(mtd);
2525 else
vimal singh20d8e242009-07-07 15:49:49 +05302526 printk(KERN_ERR "%s called for a chip which is not "
2527 "in suspended state\n", __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002528}
2529
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002530/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002531 * Set default functions
2532 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002533static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002534{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002536 if (!chip->chip_delay)
2537 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002540 if (chip->cmdfunc == NULL)
2541 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002544 if (chip->waitfunc == NULL)
2545 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002547 if (!chip->select_chip)
2548 chip->select_chip = nand_select_chip;
2549 if (!chip->read_byte)
2550 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2551 if (!chip->read_word)
2552 chip->read_word = nand_read_word;
2553 if (!chip->block_bad)
2554 chip->block_bad = nand_block_bad;
2555 if (!chip->block_markbad)
2556 chip->block_markbad = nand_default_block_markbad;
2557 if (!chip->write_buf)
2558 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2559 if (!chip->read_buf)
2560 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2561 if (!chip->verify_buf)
2562 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2563 if (!chip->scan_bbt)
2564 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002565
2566 if (!chip->controller) {
2567 chip->controller = &chip->hwcontrol;
2568 spin_lock_init(&chip->controller->lock);
2569 init_waitqueue_head(&chip->controller->wq);
2570 }
2571
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002572}
2573
2574/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002575 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002576 */
2577static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002578 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002579 int busw, int *maf_id)
2580{
2581 struct nand_flash_dev *type = NULL;
2582 int i, dev_id, maf_idx;
Ben Dooksed8165c2008-04-14 14:58:58 +01002583 int tmp_id, tmp_manf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002586 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Karl Beldanef89a882008-09-15 14:37:29 +02002588 /*
2589 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2590 * after power-up
2591 */
2592 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002595 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002598 *maf_id = chip->read_byte(mtd);
2599 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Ben Dooksed8165c2008-04-14 14:58:58 +01002601 /* Try again to make sure, as some systems the bus-hold or other
2602 * interface concerns can cause random data which looks like a
2603 * possibly credible NAND flash to appear. If the two results do
2604 * not match, ignore the device completely.
2605 */
2606
2607 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2608
2609 /* Read manufacturer and device IDs */
2610
2611 tmp_manf = chip->read_byte(mtd);
2612 tmp_id = chip->read_byte(mtd);
2613
2614 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2615 printk(KERN_INFO "%s: second ID read did not match "
2616 "%02x,%02x against %02x,%02x\n", __func__,
2617 *maf_id, dev_id, tmp_manf, tmp_id);
2618 return ERR_PTR(-ENODEV);
2619 }
2620
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002621 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002623 if (dev_id == nand_flash_ids[i].id) {
2624 type = &nand_flash_ids[i];
2625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
2628
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002629 if (!type)
2630 return ERR_PTR(-ENODEV);
2631
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002632 if (!mtd->name)
2633 mtd->name = type->name;
2634
Adrian Hunter69423d92008-12-10 13:37:21 +00002635 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002636
2637 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002638 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002639 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002640 /* The 3rd id byte holds MLC / multichip data */
2641 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002642 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002643 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002644 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002645 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002646 extid >>= 2;
2647 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002648 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002649 extid >>= 2;
2650 /* Calc blocksize. Blocksize is multiples of 64KiB */
2651 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2652 extid >>= 2;
2653 /* Get buswidth information */
2654 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2655
2656 } else {
2657 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002658 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002659 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002660 mtd->erasesize = type->erasesize;
2661 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002662 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002663 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002664 }
2665
2666 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002667 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002668 if (nand_manuf_ids[maf_idx].id == *maf_id)
2669 break;
2670 }
2671
2672 /*
2673 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002674 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002675 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002676 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002677 printk(KERN_INFO "NAND device: Manufacturer ID:"
2678 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2679 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2680 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002681 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002682 busw ? 16 : 8);
2683 return ERR_PTR(-EINVAL);
2684 }
2685
2686 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002687 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002688 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002689 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002690
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002691 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002692 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002693 if (chip->chipsize & 0xffffffff)
2694 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2695 else
2696 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002697
2698 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002699 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002700 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2701
2702 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002703 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002704 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002705
2706 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002707 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002708 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002709 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002710
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002711 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002712 * options for chips which are not having an extended id.
2713 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002714 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002715 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002716
2717 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002718 if (chip->options & NAND_4PAGE_ARRAY)
2719 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002720 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002721 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002722
2723 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002724 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2725 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002726
2727 printk(KERN_INFO "NAND device: Manufacturer ID:"
2728 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2729 nand_manuf_ids[maf_idx].name, type->name);
2730
2731 return type;
2732}
2733
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002734/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002735 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2736 * @mtd: MTD device structure
2737 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002738 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002739 * This is the first phase of the normal nand_scan() function. It
2740 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002741 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002742 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002743 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002744int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002745{
2746 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002747 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002748 struct nand_flash_dev *type;
2749
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002750 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002751 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002752 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002753 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002754
2755 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002756 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002757
2758 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00002759 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
2760 printk(KERN_WARNING "No NAND device found.\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002761 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002762 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002765 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002766 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002767 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02002768 /* See comment in nand_get_flash_type for reset */
2769 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002771 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002773 if (nand_maf_id != chip->read_byte(mtd) ||
2774 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 break;
2776 }
2777 if (i > 1)
2778 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002781 chip->numchips = i;
2782 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
David Woodhouse3b85c322006-09-25 17:06:53 +01002784 return 0;
2785}
2786
2787
2788/**
2789 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2790 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01002791 *
2792 * This is the second phase of the normal nand_scan() function. It
2793 * fills out all the uninitialized function pointers with the defaults
2794 * and scans for a bad block table if appropriate.
2795 */
2796int nand_scan_tail(struct mtd_info *mtd)
2797{
2798 int i;
2799 struct nand_chip *chip = mtd->priv;
2800
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002801 if (!(chip->options & NAND_OWN_BUFFERS))
2802 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2803 if (!chip->buffers)
2804 return -ENOMEM;
2805
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002806 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002807 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002808
2809 /*
2810 * If no default placement scheme is given, select an appropriate one
2811 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002812 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002813 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002815 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 break;
2817 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002818 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 break;
2820 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002821 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002823 case 128:
2824 chip->ecc.layout = &nand_oob_128;
2825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002827 printk(KERN_WARNING "No oob scheme defined for "
2828 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 BUG();
2830 }
2831 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002832
David Woodhouse956e9442006-09-25 17:12:39 +01002833 if (!chip->write_page)
2834 chip->write_page = nand_write_page;
2835
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002836 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002837 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2838 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002839 */
David Woodhouse956e9442006-09-25 17:12:39 +01002840
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002841 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002842 case NAND_ECC_HW_OOB_FIRST:
2843 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2844 if (!chip->ecc.calculate || !chip->ecc.correct ||
2845 !chip->ecc.hwctl) {
2846 printk(KERN_WARNING "No ECC functions supplied; "
2847 "Hardware ECC not possible\n");
2848 BUG();
2849 }
2850 if (!chip->ecc.read_page)
2851 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2852
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002853 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002854 /* Use standard hwecc read page function ? */
2855 if (!chip->ecc.read_page)
2856 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002857 if (!chip->ecc.write_page)
2858 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002859 if (!chip->ecc.read_page_raw)
2860 chip->ecc.read_page_raw = nand_read_page_raw;
2861 if (!chip->ecc.write_page_raw)
2862 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002863 if (!chip->ecc.read_oob)
2864 chip->ecc.read_oob = nand_read_oob_std;
2865 if (!chip->ecc.write_oob)
2866 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002867
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002868 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06002869 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2870 !chip->ecc.hwctl) &&
2871 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002872 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06002873 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002874 chip->ecc.write_page == nand_write_page_hwecc)) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002875 printk(KERN_WARNING "No ECC functions supplied; "
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002876 "Hardware ECC not possible\n");
2877 BUG();
2878 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002879 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002880 if (!chip->ecc.read_page)
2881 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002882 if (!chip->ecc.write_page)
2883 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08002884 if (!chip->ecc.read_page_raw)
2885 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2886 if (!chip->ecc.write_page_raw)
2887 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002888 if (!chip->ecc.read_oob)
2889 chip->ecc.read_oob = nand_read_oob_syndrome;
2890 if (!chip->ecc.write_oob)
2891 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002892
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002893 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002894 break;
2895 printk(KERN_WARNING "%d byte HW ECC not possible on "
2896 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002897 chip->ecc.size, mtd->writesize);
2898 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002900 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002901 chip->ecc.calculate = nand_calculate_ecc;
2902 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002903 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01002904 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002905 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002906 chip->ecc.read_page_raw = nand_read_page_raw;
2907 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002908 chip->ecc.read_oob = nand_read_oob_std;
2909 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00002910 if (!chip->ecc.size)
2911 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002912 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002914
2915 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002916 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2917 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002918 chip->ecc.read_page = nand_read_page_raw;
2919 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002920 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08002921 chip->ecc.read_page_raw = nand_read_page_raw;
2922 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002923 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002924 chip->ecc.size = mtd->writesize;
2925 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002929 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002930 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002931 BUG();
2932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002934 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002935 * The number of bytes available for a client to place data into
2936 * the out of band area
2937 */
2938 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07002939 for (i = 0; chip->ecc.layout->oobfree[i].length
2940 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002941 chip->ecc.layout->oobavail +=
2942 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002943 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002944
2945 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002946 * Set the number of read / write steps for one page depending on ECC
2947 * mode
2948 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002949 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2950 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002951 printk(KERN_WARNING "Invalid ecc parameters\n");
2952 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002954 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002955
Thomas Gleixner29072b92006-09-28 15:38:36 +02002956 /*
2957 * Allow subpage writes up to ecc.steps. Not possible for MLC
2958 * FLASH.
2959 */
2960 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2961 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2962 switch(chip->ecc.steps) {
2963 case 2:
2964 mtd->subpage_sft = 1;
2965 break;
2966 case 4:
2967 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002968 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02002969 mtd->subpage_sft = 2;
2970 break;
2971 }
2972 }
2973 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2974
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002975 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002976 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002979 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002982 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 /* Fill in remaining MTD driver data */
2985 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002986 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 mtd->erase = nand_erase;
2988 mtd->point = NULL;
2989 mtd->unpoint = NULL;
2990 mtd->read = nand_read;
2991 mtd->write = nand_write;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002992 mtd->panic_write = panic_nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 mtd->read_oob = nand_read_oob;
2994 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 mtd->sync = nand_sync;
2996 mtd->lock = NULL;
2997 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002998 mtd->suspend = nand_suspend;
2999 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 mtd->block_isbad = nand_block_isbad;
3001 mtd->block_markbad = nand_block_markbad;
3002
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003003 /* propagate ecc.layout to mtd_info */
3004 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003006 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003007 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003011 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
Rusty Russella6e6abd2009-03-31 13:05:31 -06003014/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01003015 test if this is a module _anyway_ -- they'd have to try _really_ hard
3016 to call us from in-kernel code if the core NAND support is modular. */
3017#ifdef MODULE
3018#define caller_is_module() (1)
3019#else
3020#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003021 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003022#endif
3023
3024/**
3025 * nand_scan - [NAND Interface] Scan for the NAND device
3026 * @mtd: MTD device structure
3027 * @maxchips: Number of chips to scan for
3028 *
3029 * This fills out all the uninitialized function pointers
3030 * with the defaults.
3031 * The flash ID is read and the mtd/chip structures are
3032 * filled with the appropriate values.
3033 * The mtd->owner field must be set to the module of the caller
3034 *
3035 */
3036int nand_scan(struct mtd_info *mtd, int maxchips)
3037{
3038 int ret;
3039
3040 /* Many callers got this wrong, so check for it for a while... */
3041 if (!mtd->owner && caller_is_module()) {
vimal singh20d8e242009-07-07 15:49:49 +05303042 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
3043 __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003044 BUG();
3045 }
3046
3047 ret = nand_scan_ident(mtd, maxchips);
3048 if (!ret)
3049 ret = nand_scan_tail(mtd);
3050 return ret;
3051}
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003054 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 * @mtd: MTD device structure
3056*/
David Woodhousee0c7d762006-05-13 18:07:53 +01003057void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003059 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061#ifdef CONFIG_MTD_PARTITIONS
3062 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01003063 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064#endif
3065 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01003066 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Jesper Juhlfa671642005-11-07 01:01:27 -08003068 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003069 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003070 if (!(chip->options & NAND_OWN_BUFFERS))
3071 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
David Woodhousee0c7d762006-05-13 18:07:53 +01003074EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003075EXPORT_SYMBOL_GPL(nand_scan_ident);
3076EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01003077EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003078
3079static int __init nand_base_init(void)
3080{
3081 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3082 return 0;
3083}
3084
3085static void __exit nand_base_exit(void)
3086{
3087 led_trigger_unregister_simple(nand_led_trigger);
3088}
3089
3090module_init(nand_base_init);
3091module_exit(nand_base_exit);
3092
David Woodhousee0c7d762006-05-13 18:07:53 +01003093MODULE_LICENSE("GPL");
3094MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3095MODULE_DESCRIPTION("Generic NAND flash driver code");