blob: 2957cc70da3d16cb25477c54adcbbaee6d4fcc18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +020010 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020015 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000016 * David Woodhouse for adding multichip support
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020021 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030027 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
32 *
33 */
34
David Woodhouse552d9202006-05-14 01:20:46 +010035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020038#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
45#include <linux/mtd/compatmac.h>
46#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50
51#ifdef CONFIG_MTD_PARTITIONS
52#include <linux/mtd/partitions.h>
53#endif
54
55/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020056static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .eccbytes = 3,
58 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020059 .oobfree = {
60 {.offset = 3,
61 .length = 2},
62 {.offset = 6,
63 .length = 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020066static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .eccbytes = 6,
68 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020069 .oobfree = {
70 {.offset = 8,
71 . length = 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020074static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .eccbytes = 24,
76 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010077 40, 41, 42, 43, 44, 45, 46, 47,
78 48, 49, 50, 51, 52, 53, 54, 55,
79 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020080 .oobfree = {
81 {.offset = 2,
82 .length = 38}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Thomas Gleixner81ec5362007-12-12 17:27:03 +010085static struct nand_ecclayout nand_oob_128 = {
86 .eccbytes = 48,
87 .eccpos = {
88 80, 81, 82, 83, 84, 85, 86, 87,
89 88, 89, 90, 91, 92, 93, 94, 95,
90 96, 97, 98, 99, 100, 101, 102, 103,
91 104, 105, 106, 107, 108, 109, 110, 111,
92 112, 113, 114, 115, 116, 117, 118, 119,
93 120, 121, 122, 123, 124, 125, 126, 127},
94 .oobfree = {
95 {.offset = 2,
96 .length = 78}}
97};
98
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020099static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200100 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200102static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
103 struct mtd_oob_ops *ops);
104
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200105/*
Joe Perches8e87d782008-02-03 17:22:34 +0200106 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200107 * compiled away when LED support is disabled.
108 */
109DEFINE_LED_TRIGGER(nand_led_trigger);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/**
112 * nand_release_device - [GENERIC] release chip
113 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000114 *
115 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100117static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200119 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200122 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100123
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200124 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200125 spin_lock(&chip->controller->lock);
126 chip->controller->active = NULL;
127 chip->state = FL_READY;
128 wake_up(&chip->controller->wq);
129 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/**
133 * nand_read_byte - [DEFAULT] read one byte from the chip
134 * @mtd: MTD device structure
135 *
136 * Default read function for 8bit buswith
137 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200138static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200140 struct nand_chip *chip = mtd->priv;
141 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
146 * @mtd: MTD device structure
147 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000148 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * endianess conversion
150 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200151static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200153 struct nand_chip *chip = mtd->priv;
154 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * nand_read_word - [DEFAULT] read one word from the chip
159 * @mtd: MTD device structure
160 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000161 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * endianess conversion
163 */
164static u16 nand_read_word(struct mtd_info *mtd)
165{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200166 struct nand_chip *chip = mtd->priv;
167 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * nand_select_chip - [DEFAULT] control CE line
172 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700173 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Default select function for 1 chip devices.
176 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200177static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200179 struct nand_chip *chip = mtd->priv;
180
181 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200183 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187
188 default:
189 BUG();
190 }
191}
192
193/**
194 * nand_write_buf - [DEFAULT] write buffer to chip
195 * @mtd: MTD device structure
196 * @buf: data buffer
197 * @len: number of bytes to write
198 *
199 * Default write function for 8bit buswith
200 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200201static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200204 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
David Woodhousee0c7d762006-05-13 18:07:53 +0100206 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200207 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000211 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * @mtd: MTD device structure
213 * @buf: buffer to store date
214 * @len: number of bytes to read
215 *
216 * Default read function for 8bit buswith
217 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200218static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200221 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David Woodhousee0c7d762006-05-13 18:07:53 +0100223 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200224 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000228 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * @mtd: MTD device structure
230 * @buf: buffer containing the data to compare
231 * @len: number of bytes to compare
232 *
233 * Default verify function for 8bit buswith
234 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200235static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200238 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
David Woodhousee0c7d762006-05-13 18:07:53 +0100240 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200241 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
246/**
247 * nand_write_buf16 - [DEFAULT] write buffer to chip
248 * @mtd: MTD device structure
249 * @buf: data buffer
250 * @len: number of bytes to write
251 *
252 * Default write function for 16bit buswith
253 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200254static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200257 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 u16 *p = (u16 *) buf;
259 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000260
David Woodhousee0c7d762006-05-13 18:07:53 +0100261 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000267 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * @mtd: MTD device structure
269 * @buf: buffer to store date
270 * @len: number of bytes to read
271 *
272 * Default read function for 16bit buswith
273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 u16 *p = (u16 *) buf;
279 len >>= 1;
280
David Woodhousee0c7d762006-05-13 18:07:53 +0100281 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200282 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000286 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * @mtd: MTD device structure
288 * @buf: buffer containing the data to compare
289 * @len: number of bytes to compare
290 *
291 * Default verify function for 16bit buswith
292 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200293static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200296 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 u16 *p = (u16 *) buf;
298 len >>= 1;
299
David Woodhousee0c7d762006-05-13 18:07:53 +0100300 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200301 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -EFAULT;
303
304 return 0;
305}
306
307/**
308 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
309 * @mtd: MTD device structure
310 * @ofs: offset from device start
311 * @getchip: 0, if the chip is already selected
312 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000313 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
316{
317 int page, chipnr, res = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200318 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 u16 bad;
320
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100321 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200324 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200326 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200329 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200332 if (chip->options & NAND_BUSWIDTH_16) {
333 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100334 page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200335 bad = cpu_to_le16(chip->read_word(mtd));
336 if (chip->badblockpos & 0x1)
Vitaly Wool49196f32005-11-02 16:54:46 +0000337 bad >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if ((bad & 0xFF) != 0xff)
339 res = 1;
340 } else {
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100341 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200342 if (chip->read_byte(mtd) != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 res = 1;
344 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000345
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200346 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return res;
350}
351
352/**
353 * nand_default_block_markbad - [DEFAULT] mark a block bad
354 * @mtd: MTD device structure
355 * @ofs: offset from device start
356 *
357 * This is the default implementation, which can be overridden by
358 * a hardware specific driver.
359*/
360static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
361{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200362 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200363 uint8_t buf[2] = { 0, 0 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200364 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400367 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200368 if (chip->bbt)
369 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200372 if (chip->options & NAND_USE_FLASH_BBT)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200373 ret = nand_update_bbt(mtd, ofs);
374 else {
375 /* We write two bytes, so we dont have to mess with 16 bit
376 * access
377 */
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300378 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200379 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200380 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200381 chip->ops.datbuf = NULL;
382 chip->ops.oobbuf = buf;
383 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000384
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200385 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300386 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200387 }
388 if (!ret)
389 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300390
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000394/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * nand_check_wp - [GENERIC] check if the chip is write protected
396 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000397 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000399 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100401static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200403 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200405 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
406 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409/**
410 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
411 * @mtd: MTD device structure
412 * @ofs: offset from device start
413 * @getchip: 0, if the chip is already selected
414 * @allowbbt: 1, if its allowed to access the bbt area
415 *
416 * Check, if the block is bad. Either by reading the bad block table or
417 * calling of the scan function.
418 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200419static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
420 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200422 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000423
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200424 if (!chip->bbt)
425 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100428 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000431/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000432 * Wait for the ready pin, after a command
433 * The timeout is catched later.
434 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100435void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000436{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200437 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100438 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000439
Richard Purdie8fe833c2006-03-31 02:31:14 -0800440 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000441 /* wait until command is processed or timeout occures */
442 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200443 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800444 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700445 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000446 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800447 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000448}
David Woodhouse4b648b02006-09-25 17:05:24 +0100449EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
452 * nand_command - [DEFAULT] Send command to NAND device
453 * @mtd: MTD device structure
454 * @command: the command to be sent
455 * @column: the column address for this command, -1 if none
456 * @page_addr: the page address for this command, -1 if none
457 *
458 * Send command to NAND device. This function is used for small page
459 * devices (256/512 Bytes per page)
460 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200461static void nand_command(struct mtd_info *mtd, unsigned int command,
462 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200464 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200465 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
468 * Write out the command to the device.
469 */
470 if (command == NAND_CMD_SEQIN) {
471 int readcmd;
472
Joern Engel28318772006-05-22 23:18:05 +0200473 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200475 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 readcmd = NAND_CMD_READOOB;
477 } else if (column < 256) {
478 /* First 256 bytes --> READ0 */
479 readcmd = NAND_CMD_READ0;
480 } else {
481 column -= 256;
482 readcmd = NAND_CMD_READ1;
483 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200484 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200485 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200487 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200489 /*
490 * Address cycle, when necessary
491 */
492 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
493 /* Serially input address */
494 if (column != -1) {
495 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200496 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200497 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200498 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200499 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200501 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200502 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200503 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200504 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200505 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200506 if (chip->chipsize > (32 << 20))
507 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200508 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200509 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000510
511 /*
512 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 case NAND_CMD_PAGEPROG:
518 case NAND_CMD_ERASE1:
519 case NAND_CMD_ERASE2:
520 case NAND_CMD_SEQIN:
521 case NAND_CMD_STATUS:
522 return;
523
524 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200525 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200527 udelay(chip->chip_delay);
528 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200529 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200530 chip->cmd_ctrl(mtd,
531 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200532 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return;
534
David Woodhousee0c7d762006-05-13 18:07:53 +0100535 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000537 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * If we don't have access to the busy pin, we apply the given
539 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100540 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200541 if (!chip->dev_ready) {
542 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Apply this short delay always to ensure that we do wait tWB in
547 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100548 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000549
550 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/**
554 * nand_command_lp - [DEFAULT] Send command to NAND large page device
555 * @mtd: MTD device structure
556 * @command: the command to be sent
557 * @column: the column address for this command, -1 if none
558 * @page_addr: the page address for this command, -1 if none
559 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200560 * Send command to NAND device. This is the version for the new large page
561 * devices We dont have the separate regions as we have in the small page
562 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200564static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
565 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200567 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Emulate NAND_CMD_READOOB */
570 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200571 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 command = NAND_CMD_READ0;
573 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000574
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200575 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200576 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200577 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200580 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* Serially input address */
583 if (column != -1) {
584 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200585 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200587 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200588 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200589 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200592 chip->cmd_ctrl(mtd, page_addr, ctrl);
593 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200594 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200596 if (chip->chipsize > (128 << 20))
597 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200598 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200601 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000602
603 /*
604 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000605 * status, sequential in, and deplete1 need no delay
606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 case NAND_CMD_CACHEDPROG:
610 case NAND_CMD_PAGEPROG:
611 case NAND_CMD_ERASE1:
612 case NAND_CMD_ERASE2:
613 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200614 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000616 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return;
618
David Woodhousee0c7d762006-05-13 18:07:53 +0100619 /*
620 * read error status commands require only a short delay
621 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000622 case NAND_CMD_STATUS_ERROR:
623 case NAND_CMD_STATUS_ERROR0:
624 case NAND_CMD_STATUS_ERROR1:
625 case NAND_CMD_STATUS_ERROR2:
626 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200627 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000628 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200631 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200633 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200634 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
635 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
636 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
637 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200638 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return;
640
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200641 case NAND_CMD_RNDOUT:
642 /* No ready / busy check necessary */
643 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
644 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
645 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
646 NAND_NCE | NAND_CTRL_CHANGE);
647 return;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200650 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
651 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
652 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
653 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000654
David Woodhousee0c7d762006-05-13 18:07:53 +0100655 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000657 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * If we don't have access to the busy pin, we apply the given
659 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100660 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200661 if (!chip->dev_ready) {
662 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Apply this short delay always to ensure that we do wait tWB in
668 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100669 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000670
671 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/**
675 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700676 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000678 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *
680 * Get the device and lock it for exclusive access
681 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200682static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200685 spinlock_t *lock = &chip->controller->lock;
686 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100687 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100688 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100689 spin_lock(lock);
690
vimal singhb8b3ee92009-07-09 20:41:22 +0530691 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200692 if (!chip->controller->active)
693 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200694
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200695 if (chip->controller->active == chip && chip->state == FL_READY) {
696 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100697 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100698 return 0;
699 }
700 if (new_state == FL_PM_SUSPENDED) {
701 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200702 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100703 }
704 set_current_state(TASK_UNINTERRUPTIBLE);
705 add_wait_queue(wq, &wait);
706 spin_unlock(lock);
707 schedule();
708 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto retry;
710}
711
712/**
713 * nand_wait - [DEFAULT] wait until the command is done
714 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700715 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 *
717 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000718 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700720 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200721static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723
David Woodhousee0c7d762006-05-13 18:07:53 +0100724 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200725 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100728 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100730 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Richard Purdie8fe833c2006-03-31 02:31:14 -0800732 led_trigger_event(nand_led_trigger, LED_FULL);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* Apply this short delay always to ensure that we do wait tWB in
735 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100736 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200738 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
739 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000740 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200741 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000743 while (time_before(jiffies, timeo)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200744 if (chip->dev_ready) {
745 if (chip->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 } else {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200748 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000751 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800753 led_trigger_event(nand_led_trigger, LED_OFF);
754
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200755 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return status;
757}
758
759/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200760 * nand_read_page_raw - [Intern] read raw page data without ecc
761 * @mtd: mtd info structure
762 * @chip: nand chip info structure
763 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100764 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -0800765 *
766 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200767 */
768static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700769 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200770{
771 chip->read_buf(mtd, buf, mtd->writesize);
772 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
773 return 0;
774}
775
776/**
David Brownell52ff49d2009-03-04 12:01:36 -0800777 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
778 * @mtd: mtd info structure
779 * @chip: nand chip info structure
780 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100781 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -0800782 *
783 * We need a special oob layout and handling even when OOB isn't used.
784 */
785static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700786 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -0800787{
788 int eccsize = chip->ecc.size;
789 int eccbytes = chip->ecc.bytes;
790 uint8_t *oob = chip->oob_poi;
791 int steps, size;
792
793 for (steps = chip->ecc.steps; steps > 0; steps--) {
794 chip->read_buf(mtd, buf, eccsize);
795 buf += eccsize;
796
797 if (chip->ecc.prepad) {
798 chip->read_buf(mtd, oob, chip->ecc.prepad);
799 oob += chip->ecc.prepad;
800 }
801
802 chip->read_buf(mtd, oob, eccbytes);
803 oob += eccbytes;
804
805 if (chip->ecc.postpad) {
806 chip->read_buf(mtd, oob, chip->ecc.postpad);
807 oob += chip->ecc.postpad;
808 }
809 }
810
811 size = mtd->oobsize - (oob - chip->oob_poi);
812 if (size)
813 chip->read_buf(mtd, oob, size);
814
815 return 0;
816}
817
818/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300819 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200820 * @mtd: mtd info structure
821 * @chip: nand chip info structure
822 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100823 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +0000824 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200825static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700826 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200828 int i, eccsize = chip->ecc.size;
829 int eccbytes = chip->ecc.bytes;
830 int eccsteps = chip->ecc.steps;
831 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100832 uint8_t *ecc_calc = chip->buffers->ecccalc;
833 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100834 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200835
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700836 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200837
838 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
839 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
840
841 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200842 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200843
844 eccsteps = chip->ecc.steps;
845 p = buf;
846
847 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
848 int stat;
849
850 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700851 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200852 mtd->ecc_stats.failed++;
853 else
854 mtd->ecc_stats.corrected += stat;
855 }
856 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100857}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/**
Alexey Korolev3d459552008-05-15 17:23:18 +0100860 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
861 * @mtd: mtd info structure
862 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +0100863 * @data_offs: offset of requested data within the page
864 * @readlen: data length
865 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +0100866 */
867static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
868{
869 int start_step, end_step, num_steps;
870 uint32_t *eccpos = chip->ecc.layout->eccpos;
871 uint8_t *p;
872 int data_col_addr, i, gaps = 0;
873 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
874 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
875
876 /* Column address wihin the page aligned to ECC size (256bytes). */
877 start_step = data_offs / chip->ecc.size;
878 end_step = (data_offs + readlen - 1) / chip->ecc.size;
879 num_steps = end_step - start_step + 1;
880
881 /* Data size aligned to ECC ecc.size*/
882 datafrag_len = num_steps * chip->ecc.size;
883 eccfrag_len = num_steps * chip->ecc.bytes;
884
885 data_col_addr = start_step * chip->ecc.size;
886 /* If we read not a page aligned data */
887 if (data_col_addr != 0)
888 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
889
890 p = bufpoi + data_col_addr;
891 chip->read_buf(mtd, p, datafrag_len);
892
893 /* Calculate ECC */
894 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
895 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
896
897 /* The performance is faster if to position offsets
898 according to ecc.pos. Let make sure here that
899 there are no gaps in ecc positions */
900 for (i = 0; i < eccfrag_len - 1; i++) {
901 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
902 eccpos[i + start_step * chip->ecc.bytes + 1]) {
903 gaps = 1;
904 break;
905 }
906 }
907 if (gaps) {
908 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
909 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
910 } else {
911 /* send the command to read the particular ecc bytes */
912 /* take care about buswidth alignment in read_buf */
913 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
914 aligned_len = eccfrag_len;
915 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
916 aligned_len++;
917 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
918 aligned_len++;
919
920 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
921 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
922 }
923
924 for (i = 0; i < eccfrag_len; i++)
925 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
926
927 p = bufpoi + data_col_addr;
928 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
929 int stat;
930
931 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
932 if (stat == -1)
933 mtd->ecc_stats.failed++;
934 else
935 mtd->ecc_stats.corrected += stat;
936 }
937 return 0;
938}
939
940/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300941 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200942 * @mtd: mtd info structure
943 * @chip: nand chip info structure
944 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100945 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200946 *
947 * Not for syndrome calculating ecc controllers which need a special oob layout
948 */
949static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700950 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200951{
952 int i, eccsize = chip->ecc.size;
953 int eccbytes = chip->ecc.bytes;
954 int eccsteps = chip->ecc.steps;
955 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100956 uint8_t *ecc_calc = chip->buffers->ecccalc;
957 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100958 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200959
960 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
961 chip->ecc.hwctl(mtd, NAND_ECC_READ);
962 chip->read_buf(mtd, p, eccsize);
963 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
964 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200965 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200966
967 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200968 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200969
970 eccsteps = chip->ecc.steps;
971 p = buf;
972
973 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
974 int stat;
975
976 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700977 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200978 mtd->ecc_stats.failed++;
979 else
980 mtd->ecc_stats.corrected += stat;
981 }
982 return 0;
983}
984
985/**
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -0700986 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
987 * @mtd: mtd info structure
988 * @chip: nand chip info structure
989 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +0100990 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -0700991 *
992 * Hardware ECC for large page chips, require OOB to be read first.
993 * For this ECC mode, the write_page method is re-used from ECC_HW.
994 * These methods read/write ECC from the OOB area, unlike the
995 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
996 * "infix ECC" scheme and reads/writes ECC from the data area, by
997 * overwriting the NAND manufacturer bad block markings.
998 */
999static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1000 struct nand_chip *chip, uint8_t *buf, int page)
1001{
1002 int i, eccsize = chip->ecc.size;
1003 int eccbytes = chip->ecc.bytes;
1004 int eccsteps = chip->ecc.steps;
1005 uint8_t *p = buf;
1006 uint8_t *ecc_code = chip->buffers->ecccode;
1007 uint32_t *eccpos = chip->ecc.layout->eccpos;
1008 uint8_t *ecc_calc = chip->buffers->ecccalc;
1009
1010 /* Read the OOB area first */
1011 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1012 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1013 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1014
1015 for (i = 0; i < chip->ecc.total; i++)
1016 ecc_code[i] = chip->oob_poi[eccpos[i]];
1017
1018 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1019 int stat;
1020
1021 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1022 chip->read_buf(mtd, p, eccsize);
1023 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1024
1025 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1026 if (stat < 0)
1027 mtd->ecc_stats.failed++;
1028 else
1029 mtd->ecc_stats.corrected += stat;
1030 }
1031 return 0;
1032}
1033
1034/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001035 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001036 * @mtd: mtd info structure
1037 * @chip: nand chip info structure
1038 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001039 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001040 *
1041 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001042 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001043 */
1044static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001045 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001046{
1047 int i, eccsize = chip->ecc.size;
1048 int eccbytes = chip->ecc.bytes;
1049 int eccsteps = chip->ecc.steps;
1050 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001051 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001052
1053 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1054 int stat;
1055
1056 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1057 chip->read_buf(mtd, p, eccsize);
1058
1059 if (chip->ecc.prepad) {
1060 chip->read_buf(mtd, oob, chip->ecc.prepad);
1061 oob += chip->ecc.prepad;
1062 }
1063
1064 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1065 chip->read_buf(mtd, oob, eccbytes);
1066 stat = chip->ecc.correct(mtd, p, oob, NULL);
1067
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001068 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001069 mtd->ecc_stats.failed++;
1070 else
1071 mtd->ecc_stats.corrected += stat;
1072
1073 oob += eccbytes;
1074
1075 if (chip->ecc.postpad) {
1076 chip->read_buf(mtd, oob, chip->ecc.postpad);
1077 oob += chip->ecc.postpad;
1078 }
1079 }
1080
1081 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001082 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001083 if (i)
1084 chip->read_buf(mtd, oob, i);
1085
1086 return 0;
1087}
1088
1089/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001090 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1091 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001092 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001093 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001094 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001095 */
1096static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001097 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001098{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001099 switch(ops->mode) {
1100
1101 case MTD_OOB_PLACE:
1102 case MTD_OOB_RAW:
1103 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1104 return oob + len;
1105
1106 case MTD_OOB_AUTO: {
1107 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001108 uint32_t boffs = 0, roffs = ops->ooboffs;
1109 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001110
1111 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001112 /* Read request not from offset 0 ? */
1113 if (unlikely(roffs)) {
1114 if (roffs >= free->length) {
1115 roffs -= free->length;
1116 continue;
1117 }
1118 boffs = free->offset + roffs;
1119 bytes = min_t(size_t, len,
1120 (free->length - roffs));
1121 roffs = 0;
1122 } else {
1123 bytes = min_t(size_t, len, free->length);
1124 boffs = free->offset;
1125 }
1126 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001127 oob += bytes;
1128 }
1129 return oob;
1130 }
1131 default:
1132 BUG();
1133 }
1134 return NULL;
1135}
1136
1137/**
1138 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001139 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001140 * @mtd: MTD device structure
1141 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001142 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001143 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001144 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001145 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001146static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1147 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001148{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001149 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001150 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001151 struct mtd_ecc_stats stats;
1152 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1153 int sndcmd = 1;
1154 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001155 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001156 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001157 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001159 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001161 chipnr = (int)(from >> chip->chip_shift);
1162 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001164 realpage = (int)(from >> chip->page_shift);
1165 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001167 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001169 buf = ops->datbuf;
1170 oob = ops->oobbuf;
1171
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001172 while(1) {
1173 bytes = min(mtd->writesize - col, readlen);
1174 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001175
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001176 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001177 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001178 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001180 if (likely(sndcmd)) {
1181 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1182 sndcmd = 0;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001185 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001186 if (unlikely(ops->mode == MTD_OOB_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001187 ret = chip->ecc.read_page_raw(mtd, chip,
1188 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001189 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1190 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001191 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001192 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1193 page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001194 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001195 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001196
1197 /* Transfer not aligned data */
1198 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001199 if (!NAND_SUBPAGE_READ(chip) && !oob)
1200 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001201 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001203
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001204 buf += bytes;
1205
1206 if (unlikely(oob)) {
1207 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001208 if (ops->mode != MTD_OOB_RAW) {
1209 int toread = min(oobreadlen,
1210 chip->ecc.layout->oobavail);
1211 if (toread) {
1212 oob = nand_transfer_oob(chip,
1213 oob, ops, toread);
1214 oobreadlen -= toread;
1215 }
1216 } else
1217 buf = nand_transfer_oob(chip,
1218 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001219 }
1220
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001221 if (!(chip->options & NAND_NO_READRDY)) {
1222 /*
1223 * Apply delay or wait for ready/busy pin. Do
1224 * this before the AUTOINCR check, so no
1225 * problems arise if a chip which does auto
1226 * increment is marked as NOAUTOINCR by the
1227 * board driver.
1228 */
1229 if (!chip->dev_ready)
1230 udelay(chip->chip_delay);
1231 else
1232 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001234 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001235 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001236 buf += bytes;
1237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001239 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001240
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001241 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 /* For subsequent reads align to page boundary. */
1245 col = 0;
1246 /* Increment page address */
1247 realpage++;
1248
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001249 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* Check, if we cross a chip boundary */
1251 if (!page) {
1252 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001253 chip->select_chip(mtd, -1);
1254 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001256
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001257 /* Check, if the chip supports auto page increment
1258 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001259 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001260 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001261 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001264 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001265 if (oob)
1266 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001268 if (ret)
1269 return ret;
1270
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001271 if (mtd->ecc_stats.failed - stats.failed)
1272 return -EBADMSG;
1273
1274 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001275}
1276
1277/**
1278 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1279 * @mtd: MTD device structure
1280 * @from: offset to read from
1281 * @len: number of bytes to read
1282 * @retlen: pointer to variable to store the number of read bytes
1283 * @buf: the databuffer to put data
1284 *
1285 * Get hold of the chip and call nand_do_read
1286 */
1287static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1288 size_t *retlen, uint8_t *buf)
1289{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001290 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001291 int ret;
1292
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001293 /* Do not allow reads past end of device */
1294 if ((from + len) > mtd->size)
1295 return -EINVAL;
1296 if (!len)
1297 return 0;
1298
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001299 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001300
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001301 chip->ops.len = len;
1302 chip->ops.datbuf = buf;
1303 chip->ops.oobbuf = NULL;
1304
1305 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001306
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001307 *retlen = chip->ops.retlen;
1308
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001309 nand_release_device(mtd);
1310
1311 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001315 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1316 * @mtd: mtd info structure
1317 * @chip: nand chip info structure
1318 * @page: page number to read
1319 * @sndcmd: flag whether to issue read command or not
1320 */
1321static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1322 int page, int sndcmd)
1323{
1324 if (sndcmd) {
1325 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1326 sndcmd = 0;
1327 }
1328 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1329 return sndcmd;
1330}
1331
1332/**
1333 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1334 * with syndromes
1335 * @mtd: mtd info structure
1336 * @chip: nand chip info structure
1337 * @page: page number to read
1338 * @sndcmd: flag whether to issue read command or not
1339 */
1340static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1341 int page, int sndcmd)
1342{
1343 uint8_t *buf = chip->oob_poi;
1344 int length = mtd->oobsize;
1345 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1346 int eccsize = chip->ecc.size;
1347 uint8_t *bufpoi = buf;
1348 int i, toread, sndrnd = 0, pos;
1349
1350 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1351 for (i = 0; i < chip->ecc.steps; i++) {
1352 if (sndrnd) {
1353 pos = eccsize + i * (eccsize + chunk);
1354 if (mtd->writesize > 512)
1355 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1356 else
1357 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1358 } else
1359 sndrnd = 1;
1360 toread = min_t(int, length, chunk);
1361 chip->read_buf(mtd, bufpoi, toread);
1362 bufpoi += toread;
1363 length -= toread;
1364 }
1365 if (length > 0)
1366 chip->read_buf(mtd, bufpoi, length);
1367
1368 return 1;
1369}
1370
1371/**
1372 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1373 * @mtd: mtd info structure
1374 * @chip: nand chip info structure
1375 * @page: page number to write
1376 */
1377static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1378 int page)
1379{
1380 int status = 0;
1381 const uint8_t *buf = chip->oob_poi;
1382 int length = mtd->oobsize;
1383
1384 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1385 chip->write_buf(mtd, buf, length);
1386 /* Send command to program the OOB data */
1387 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1388
1389 status = chip->waitfunc(mtd, chip);
1390
Savin Zlobec0d420f92006-06-21 11:51:20 +02001391 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001392}
1393
1394/**
1395 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1396 * with syndrome - only for large page flash !
1397 * @mtd: mtd info structure
1398 * @chip: nand chip info structure
1399 * @page: page number to write
1400 */
1401static int nand_write_oob_syndrome(struct mtd_info *mtd,
1402 struct nand_chip *chip, int page)
1403{
1404 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1405 int eccsize = chip->ecc.size, length = mtd->oobsize;
1406 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1407 const uint8_t *bufpoi = chip->oob_poi;
1408
1409 /*
1410 * data-ecc-data-ecc ... ecc-oob
1411 * or
1412 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1413 */
1414 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1415 pos = steps * (eccsize + chunk);
1416 steps = 0;
1417 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001418 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001419
1420 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1421 for (i = 0; i < steps; i++) {
1422 if (sndcmd) {
1423 if (mtd->writesize <= 512) {
1424 uint32_t fill = 0xFFFFFFFF;
1425
1426 len = eccsize;
1427 while (len > 0) {
1428 int num = min_t(int, len, 4);
1429 chip->write_buf(mtd, (uint8_t *)&fill,
1430 num);
1431 len -= num;
1432 }
1433 } else {
1434 pos = eccsize + i * (eccsize + chunk);
1435 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1436 }
1437 } else
1438 sndcmd = 1;
1439 len = min_t(int, length, chunk);
1440 chip->write_buf(mtd, bufpoi, len);
1441 bufpoi += len;
1442 length -= len;
1443 }
1444 if (length > 0)
1445 chip->write_buf(mtd, bufpoi, length);
1446
1447 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1448 status = chip->waitfunc(mtd, chip);
1449
1450 return status & NAND_STATUS_FAIL ? -EIO : 0;
1451}
1452
1453/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001454 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * @mtd: MTD device structure
1456 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001457 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 *
1459 * NAND read out-of-band data from the spare area
1460 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001461static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1462 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001464 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001465 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001466 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001467 int readlen = ops->ooblen;
1468 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001469 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
vimal singh20d8e242009-07-07 15:49:49 +05301471 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1472 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Adrian Hunter03736152007-01-31 17:58:29 +02001474 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001475 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001476 else
1477 len = mtd->oobsize;
1478
1479 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301480 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1481 "outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001482 return -EINVAL;
1483 }
1484
1485 /* Do not allow reads past end of device */
1486 if (unlikely(from >= mtd->size ||
1487 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1488 (from >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301489 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1490 "of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001491 return -EINVAL;
1492 }
Vitaly Wool70145682006-11-03 18:20:38 +03001493
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001494 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001495 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001497 /* Shift to get page */
1498 realpage = (int)(from >> chip->page_shift);
1499 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001501 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001502 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001503
1504 len = min(len, readlen);
1505 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001506
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001507 if (!(chip->options & NAND_NO_READRDY)) {
1508 /*
1509 * Apply delay or wait for ready/busy pin. Do this
1510 * before the AUTOINCR check, so no problems arise if a
1511 * chip which does auto increment is marked as
1512 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001513 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001514 if (!chip->dev_ready)
1515 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001516 else
1517 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001519
Vitaly Wool70145682006-11-03 18:20:38 +03001520 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001521 if (!readlen)
1522 break;
1523
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001524 /* Increment page address */
1525 realpage++;
1526
1527 page = realpage & chip->pagemask;
1528 /* Check, if we cross a chip boundary */
1529 if (!page) {
1530 chipnr++;
1531 chip->select_chip(mtd, -1);
1532 chip->select_chip(mtd, chipnr);
1533 }
1534
1535 /* Check, if the chip supports auto page increment
1536 * or if we have hit a block boundary.
1537 */
1538 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1539 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541
Vitaly Wool70145682006-11-03 18:20:38 +03001542 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
1544}
1545
1546/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001547 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001550 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001552 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001554static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1555 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001557 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001558 int ret = -ENOTSUPP;
1559
1560 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001563 if (ops->datbuf && (from + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05301564 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1565 "beyond end of device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return -EINVAL;
1567 }
1568
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001569 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001571 switch(ops->mode) {
1572 case MTD_OOB_PLACE:
1573 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001574 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001575 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001576
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001577 default:
1578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001581 if (!ops->datbuf)
1582 ret = nand_do_read_oob(mtd, from, ops);
1583 else
1584 ret = nand_do_read_ops(mtd, from, ops);
1585
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001586 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001588 return ret;
1589}
1590
1591
1592/**
1593 * nand_write_page_raw - [Intern] raw page write function
1594 * @mtd: mtd info structure
1595 * @chip: nand chip info structure
1596 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001597 *
1598 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001599 */
1600static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1601 const uint8_t *buf)
1602{
1603 chip->write_buf(mtd, buf, mtd->writesize);
1604 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001607/**
David Brownell52ff49d2009-03-04 12:01:36 -08001608 * nand_write_page_raw_syndrome - [Intern] raw page write function
1609 * @mtd: mtd info structure
1610 * @chip: nand chip info structure
1611 * @buf: data buffer
1612 *
1613 * We need a special oob layout and handling even when ECC isn't checked.
1614 */
1615static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1616 const uint8_t *buf)
1617{
1618 int eccsize = chip->ecc.size;
1619 int eccbytes = chip->ecc.bytes;
1620 uint8_t *oob = chip->oob_poi;
1621 int steps, size;
1622
1623 for (steps = chip->ecc.steps; steps > 0; steps--) {
1624 chip->write_buf(mtd, buf, eccsize);
1625 buf += eccsize;
1626
1627 if (chip->ecc.prepad) {
1628 chip->write_buf(mtd, oob, chip->ecc.prepad);
1629 oob += chip->ecc.prepad;
1630 }
1631
1632 chip->read_buf(mtd, oob, eccbytes);
1633 oob += eccbytes;
1634
1635 if (chip->ecc.postpad) {
1636 chip->write_buf(mtd, oob, chip->ecc.postpad);
1637 oob += chip->ecc.postpad;
1638 }
1639 }
1640
1641 size = mtd->oobsize - (oob - chip->oob_poi);
1642 if (size)
1643 chip->write_buf(mtd, oob, size);
1644}
1645/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001646 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001647 * @mtd: mtd info structure
1648 * @chip: nand chip info structure
1649 * @buf: data buffer
1650 */
1651static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1652 const uint8_t *buf)
1653{
1654 int i, eccsize = chip->ecc.size;
1655 int eccbytes = chip->ecc.bytes;
1656 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001657 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001658 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001659 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001660
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001661 /* Software ecc calculation */
1662 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1663 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001664
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001665 for (i = 0; i < chip->ecc.total; i++)
1666 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001667
Thomas Gleixner90424de2007-04-05 11:44:05 +02001668 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001669}
1670
1671/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001672 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001673 * @mtd: mtd info structure
1674 * @chip: nand chip info structure
1675 * @buf: data buffer
1676 */
1677static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1678 const uint8_t *buf)
1679{
1680 int i, eccsize = chip->ecc.size;
1681 int eccbytes = chip->ecc.bytes;
1682 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001683 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001684 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001685 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001686
1687 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1688 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001689 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001690 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1691 }
1692
1693 for (i = 0; i < chip->ecc.total; i++)
1694 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1695
1696 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1697}
1698
1699/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001700 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001701 * @mtd: mtd info structure
1702 * @chip: nand chip info structure
1703 * @buf: data buffer
1704 *
1705 * The hw generator calculates the error syndrome automatically. Therefor
1706 * we need a special oob layout and handling.
1707 */
1708static void nand_write_page_syndrome(struct mtd_info *mtd,
1709 struct nand_chip *chip, const uint8_t *buf)
1710{
1711 int i, eccsize = chip->ecc.size;
1712 int eccbytes = chip->ecc.bytes;
1713 int eccsteps = chip->ecc.steps;
1714 const uint8_t *p = buf;
1715 uint8_t *oob = chip->oob_poi;
1716
1717 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1718
1719 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1720 chip->write_buf(mtd, p, eccsize);
1721
1722 if (chip->ecc.prepad) {
1723 chip->write_buf(mtd, oob, chip->ecc.prepad);
1724 oob += chip->ecc.prepad;
1725 }
1726
1727 chip->ecc.calculate(mtd, p, oob);
1728 chip->write_buf(mtd, oob, eccbytes);
1729 oob += eccbytes;
1730
1731 if (chip->ecc.postpad) {
1732 chip->write_buf(mtd, oob, chip->ecc.postpad);
1733 oob += chip->ecc.postpad;
1734 }
1735 }
1736
1737 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001738 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001739 if (i)
1740 chip->write_buf(mtd, oob, i);
1741}
1742
1743/**
David Woodhouse956e9442006-09-25 17:12:39 +01001744 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001745 * @mtd: MTD device structure
1746 * @chip: NAND chip descriptor
1747 * @buf: the data to write
1748 * @page: page number to write
1749 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02001750 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001751 */
1752static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001753 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001754{
1755 int status;
1756
1757 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1758
David Woodhouse956e9442006-09-25 17:12:39 +01001759 if (unlikely(raw))
1760 chip->ecc.write_page_raw(mtd, chip, buf);
1761 else
1762 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001763
1764 /*
1765 * Cached progamming disabled for now, Not sure if its worth the
1766 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1767 */
1768 cached = 0;
1769
1770 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1771
1772 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001773 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001774 /*
1775 * See if operation failed and additional status checks are
1776 * available
1777 */
1778 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1779 status = chip->errstat(mtd, chip, FL_WRITING, status,
1780 page);
1781
1782 if (status & NAND_STATUS_FAIL)
1783 return -EIO;
1784 } else {
1785 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001786 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001787 }
1788
1789#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1790 /* Send command to read back the data */
1791 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1792
1793 if (chip->verify_buf(mtd, buf, mtd->writesize))
1794 return -EIO;
1795#endif
1796 return 0;
1797}
1798
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001799/**
1800 * nand_fill_oob - [Internal] Transfer client buffer to oob
1801 * @chip: nand chip structure
1802 * @oob: oob data buffer
1803 * @ops: oob ops structure
1804 */
1805static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1806 struct mtd_oob_ops *ops)
1807{
1808 size_t len = ops->ooblen;
1809
1810 switch(ops->mode) {
1811
1812 case MTD_OOB_PLACE:
1813 case MTD_OOB_RAW:
1814 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1815 return oob + len;
1816
1817 case MTD_OOB_AUTO: {
1818 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001819 uint32_t boffs = 0, woffs = ops->ooboffs;
1820 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001821
1822 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001823 /* Write request not from offset 0 ? */
1824 if (unlikely(woffs)) {
1825 if (woffs >= free->length) {
1826 woffs -= free->length;
1827 continue;
1828 }
1829 boffs = free->offset + woffs;
1830 bytes = min_t(size_t, len,
1831 (free->length - woffs));
1832 woffs = 0;
1833 } else {
1834 bytes = min_t(size_t, len, free->length);
1835 boffs = free->offset;
1836 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001837 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001838 oob += bytes;
1839 }
1840 return oob;
1841 }
1842 default:
1843 BUG();
1844 }
1845 return NULL;
1846}
1847
Thomas Gleixner29072b92006-09-28 15:38:36 +02001848#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001849
1850/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001851 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001852 * @mtd: MTD device structure
1853 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001854 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001855 *
1856 * NAND write with ECC
1857 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001858static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1859 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001860{
Thomas Gleixner29072b92006-09-28 15:38:36 +02001861 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001862 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001863 uint32_t writelen = ops->len;
1864 uint8_t *oob = ops->oobbuf;
1865 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001866 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001867
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001868 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001869 if (!writelen)
1870 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001871
1872 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001873 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301874 printk(KERN_NOTICE "%s: Attempt to write not "
1875 "page aligned data\n", __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001876 return -EINVAL;
1877 }
1878
Thomas Gleixner29072b92006-09-28 15:38:36 +02001879 column = to & (mtd->writesize - 1);
1880 subpage = column || (writelen & (mtd->writesize - 1));
1881
1882 if (subpage && oob)
1883 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001884
Thomas Gleixner6a930962006-06-28 00:11:45 +02001885 chipnr = (int)(to >> chip->chip_shift);
1886 chip->select_chip(mtd, chipnr);
1887
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001888 /* Check, if it is write protected */
1889 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001890 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001891
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001892 realpage = (int)(to >> chip->page_shift);
1893 page = realpage & chip->pagemask;
1894 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1895
1896 /* Invalidate the page cache, when we write to the cached page */
1897 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001898 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001899 chip->pagebuf = -1;
1900
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001901 /* If we're not given explicit OOB data, let it be 0xFF */
1902 if (likely(!oob))
1903 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001904
1905 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02001906 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001907 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001908 uint8_t *wbuf = buf;
1909
1910 /* Partial page write ? */
1911 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1912 cached = 0;
1913 bytes = min_t(int, bytes - column, (int) writelen);
1914 chip->pagebuf = -1;
1915 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1916 memcpy(&chip->buffers->databuf[column], buf, bytes);
1917 wbuf = chip->buffers->databuf;
1918 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001919
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001920 if (unlikely(oob))
1921 oob = nand_fill_oob(chip, oob, ops);
1922
Thomas Gleixner29072b92006-09-28 15:38:36 +02001923 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01001924 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001925 if (ret)
1926 break;
1927
1928 writelen -= bytes;
1929 if (!writelen)
1930 break;
1931
Thomas Gleixner29072b92006-09-28 15:38:36 +02001932 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001933 buf += bytes;
1934 realpage++;
1935
1936 page = realpage & chip->pagemask;
1937 /* Check, if we cross a chip boundary */
1938 if (!page) {
1939 chipnr++;
1940 chip->select_chip(mtd, -1);
1941 chip->select_chip(mtd, chipnr);
1942 }
1943 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001944
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001945 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03001946 if (unlikely(oob))
1947 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001948 return ret;
1949}
1950
1951/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001952 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 * @mtd: MTD device structure
1954 * @to: offset to write to
1955 * @len: number of bytes to write
1956 * @retlen: pointer to variable to store the number of written bytes
1957 * @buf: the data to write
1958 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001959 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001961static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001962 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001964 struct nand_chip *chip = mtd->priv;
1965 int ret;
1966
1967 /* Do not allow reads past end of device */
1968 if ((to + len) > mtd->size)
1969 return -EINVAL;
1970 if (!len)
1971 return 0;
1972
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001973 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001974
1975 chip->ops.len = len;
1976 chip->ops.datbuf = (uint8_t *)buf;
1977 chip->ops.oobbuf = NULL;
1978
1979 ret = nand_do_write_ops(mtd, to, &chip->ops);
1980
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001981 *retlen = chip->ops.retlen;
1982
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001983 nand_release_device(mtd);
1984
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001985 return ret;
1986}
1987
1988/**
1989 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1990 * @mtd: MTD device structure
1991 * @to: offset to write to
1992 * @ops: oob operation description structure
1993 *
1994 * NAND write out-of-band
1995 */
1996static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1997 struct mtd_oob_ops *ops)
1998{
Adrian Hunter03736152007-01-31 17:58:29 +02001999 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002000 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
vimal singh20d8e242009-07-07 15:49:49 +05302002 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2003 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Adrian Hunter03736152007-01-31 17:58:29 +02002005 if (ops->mode == MTD_OOB_AUTO)
2006 len = chip->ecc.layout->oobavail;
2007 else
2008 len = mtd->oobsize;
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002011 if ((ops->ooboffs + ops->ooblen) > len) {
vimal singh20d8e242009-07-07 15:49:49 +05302012 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2013 "past end of page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return -EINVAL;
2015 }
2016
Adrian Hunter03736152007-01-31 17:58:29 +02002017 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302018 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2019 "write outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002020 return -EINVAL;
2021 }
2022
2023 /* Do not allow reads past end of device */
2024 if (unlikely(to >= mtd->size ||
2025 ops->ooboffs + ops->ooblen >
2026 ((mtd->size >> chip->page_shift) -
2027 (to >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302028 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2029 "end of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002030 return -EINVAL;
2031 }
2032
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002033 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002034 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002036 /* Shift to get page */
2037 page = (int)(to >> chip->page_shift);
2038
2039 /*
2040 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2041 * of my DiskOnChip 2000 test units) will clear the whole data page too
2042 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2043 * it in the doc2000 driver in August 1999. dwmw2.
2044 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002045 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 /* Check, if it is write protected */
2048 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002049 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002052 if (page == chip->pagebuf)
2053 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002055 memset(chip->oob_poi, 0xff, mtd->oobsize);
2056 nand_fill_oob(chip, ops->oobbuf, ops);
2057 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2058 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002059
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002060 if (status)
2061 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Vitaly Wool70145682006-11-03 18:20:38 +03002063 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002065 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002068/**
2069 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2070 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002071 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002072 * @ops: oob operation description structure
2073 */
2074static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2075 struct mtd_oob_ops *ops)
2076{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002077 struct nand_chip *chip = mtd->priv;
2078 int ret = -ENOTSUPP;
2079
2080 ops->retlen = 0;
2081
2082 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002083 if (ops->datbuf && (to + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302084 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2085 "end of device\n", __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002086 return -EINVAL;
2087 }
2088
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002089 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002090
2091 switch(ops->mode) {
2092 case MTD_OOB_PLACE:
2093 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002094 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002095 break;
2096
2097 default:
2098 goto out;
2099 }
2100
2101 if (!ops->datbuf)
2102 ret = nand_do_write_oob(mtd, to, ops);
2103 else
2104 ret = nand_do_write_ops(mtd, to, ops);
2105
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002106 out:
2107 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return ret;
2109}
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2113 * @mtd: MTD device structure
2114 * @page: the page address of the block which will be erased
2115 *
2116 * Standard erase command for NAND chips
2117 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002118static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002120 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002122 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2123 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126/**
2127 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2128 * @mtd: MTD device structure
2129 * @page: the page address of the block which will be erased
2130 *
2131 * AND multi block erase command function
2132 * Erase 4 consecutive blocks
2133 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002134static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002136 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002138 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2139 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2140 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2141 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2142 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145/**
2146 * nand_erase - [MTD Interface] erase block(s)
2147 * @mtd: MTD device structure
2148 * @instr: erase instruction
2149 *
2150 * Erase one ore more blocks
2151 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002152static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
David Woodhousee0c7d762006-05-13 18:07:53 +01002154 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002156
David A. Marlin30f464b2005-01-17 18:35:25 +00002157#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002159 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 * @mtd: MTD device structure
2161 * @instr: erase instruction
2162 * @allowbbt: allow erasing the bbt area
2163 *
2164 * Erase one ore more blocks
2165 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002166int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2167 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Adrian Hunter69423d92008-12-10 13:37:21 +00002169 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002170 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002171 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002172 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002173 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
vimal singh20d8e242009-07-07 15:49:49 +05302175 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2176 __func__, (unsigned long long)instr->addr,
2177 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002180 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
vimal singh20d8e242009-07-07 15:49:49 +05302181 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return -EINVAL;
2183 }
2184
2185 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002186 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
vimal singh20d8e242009-07-07 15:49:49 +05302187 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
2188 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return -EINVAL;
2190 }
2191
2192 /* Do not allow erase past end of device */
2193 if ((instr->len + instr->addr) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302194 DEBUG(MTD_DEBUG_LEVEL0, "%s: Erase past end of device\n",
2195 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return -EINVAL;
2197 }
2198
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002199 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002202 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002205 page = (int)(instr->addr >> chip->page_shift);
2206 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002209 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002212 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* Check, if it is write protected */
2215 if (nand_check_wp(mtd)) {
vimal singh20d8e242009-07-07 15:49:49 +05302216 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2217 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 instr->state = MTD_ERASE_FAILED;
2219 goto erase_exit;
2220 }
2221
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002222 /*
2223 * If BBT requires refresh, set the BBT page mask to see if the BBT
2224 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2225 * can not be matched. This is also done when the bbt is actually
2226 * erased to avoid recusrsive updates
2227 */
2228 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2229 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* Loop through the pages */
2232 len = instr->len;
2233
2234 instr->state = MTD_ERASING;
2235
2236 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002237 /*
2238 * heck if we have a bad block, we do not erase bad blocks !
2239 */
2240 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2241 chip->page_shift, 0, allowbbt)) {
vimal singh20d8e242009-07-07 15:49:49 +05302242 printk(KERN_WARNING "%s: attempt to erase a bad block "
2243 "at page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 instr->state = MTD_ERASE_FAILED;
2245 goto erase_exit;
2246 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002247
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002248 /*
2249 * Invalidate the page cache, if we erase the block which
2250 * contains the current cached page
2251 */
2252 if (page <= chip->pagebuf && chip->pagebuf <
2253 (page + pages_per_block))
2254 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002256 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002257
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002258 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002260 /*
2261 * See if operation failed and additional status checks are
2262 * available
2263 */
2264 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2265 status = chip->errstat(mtd, chip, FL_ERASING,
2266 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002269 if (status & NAND_STATUS_FAIL) {
vimal singh20d8e242009-07-07 15:49:49 +05302270 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2271 "page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002273 instr->fail_addr =
2274 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto erase_exit;
2276 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002277
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002278 /*
2279 * If BBT requires refresh, set the BBT rewrite flag to the
2280 * page being erased
2281 */
2282 if (bbt_masked_page != 0xffffffff &&
2283 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002284 rewrite_bbt[chipnr] =
2285 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002288 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 page += pages_per_block;
2290
2291 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002292 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002294 chip->select_chip(mtd, -1);
2295 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002296
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002297 /*
2298 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2299 * page mask to see if this BBT should be rewritten
2300 */
2301 if (bbt_masked_page != 0xffffffff &&
2302 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2303 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2304 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 }
2307 instr->state = MTD_ERASE_DONE;
2308
David Woodhousee0c7d762006-05-13 18:07:53 +01002309 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 /* Deselect and wake up anyone waiting on the device */
2314 nand_release_device(mtd);
2315
David Woodhouse49defc02007-10-06 15:01:59 -04002316 /* Do call back function */
2317 if (!ret)
2318 mtd_erase_callback(instr);
2319
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002320 /*
2321 * If BBT requires refresh and erase was successful, rewrite any
2322 * selected bad block tables
2323 */
2324 if (bbt_masked_page == 0xffffffff || ret)
2325 return ret;
2326
2327 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2328 if (!rewrite_bbt[chipnr])
2329 continue;
2330 /* update the BBT for chip */
vimal singh20d8e242009-07-07 15:49:49 +05302331 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2332 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2333 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002334 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002335 }
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /* Return more or less happy */
2338 return ret;
2339}
2340
2341/**
2342 * nand_sync - [MTD Interface] sync
2343 * @mtd: MTD device structure
2344 *
2345 * Sync is actually a wait for chip ready function
2346 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002347static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002349 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
vimal singh20d8e242009-07-07 15:49:49 +05302351 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002354 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002356 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002360 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002362 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002364static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002367 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002369
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002370 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
2373/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002374 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 * @mtd: MTD device structure
2376 * @ofs: offset relative to mtd start
2377 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002378static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002380 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 int ret;
2382
David Woodhousee0c7d762006-05-13 18:07:53 +01002383 if ((ret = nand_block_isbad(mtd, ofs))) {
2384 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (ret > 0)
2386 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002387 return ret;
2388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002390 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
2393/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002394 * nand_suspend - [MTD Interface] Suspend the NAND flash
2395 * @mtd: MTD device structure
2396 */
2397static int nand_suspend(struct mtd_info *mtd)
2398{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002399 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002400
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002401 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002402}
2403
2404/**
2405 * nand_resume - [MTD Interface] Resume the NAND flash
2406 * @mtd: MTD device structure
2407 */
2408static void nand_resume(struct mtd_info *mtd)
2409{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002410 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002411
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002412 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002413 nand_release_device(mtd);
2414 else
vimal singh20d8e242009-07-07 15:49:49 +05302415 printk(KERN_ERR "%s called for a chip which is not "
2416 "in suspended state\n", __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002417}
2418
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002419/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002420 * Set default functions
2421 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002422static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002423{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002425 if (!chip->chip_delay)
2426 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002429 if (chip->cmdfunc == NULL)
2430 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002433 if (chip->waitfunc == NULL)
2434 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002436 if (!chip->select_chip)
2437 chip->select_chip = nand_select_chip;
2438 if (!chip->read_byte)
2439 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2440 if (!chip->read_word)
2441 chip->read_word = nand_read_word;
2442 if (!chip->block_bad)
2443 chip->block_bad = nand_block_bad;
2444 if (!chip->block_markbad)
2445 chip->block_markbad = nand_default_block_markbad;
2446 if (!chip->write_buf)
2447 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2448 if (!chip->read_buf)
2449 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2450 if (!chip->verify_buf)
2451 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2452 if (!chip->scan_bbt)
2453 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002454
2455 if (!chip->controller) {
2456 chip->controller = &chip->hwcontrol;
2457 spin_lock_init(&chip->controller->lock);
2458 init_waitqueue_head(&chip->controller->wq);
2459 }
2460
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002461}
2462
2463/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002464 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002465 */
2466static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002467 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002468 int busw, int *maf_id)
2469{
2470 struct nand_flash_dev *type = NULL;
2471 int i, dev_id, maf_idx;
Ben Dooksed8165c2008-04-14 14:58:58 +01002472 int tmp_id, tmp_manf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002475 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Karl Beldanef89a882008-09-15 14:37:29 +02002477 /*
2478 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2479 * after power-up
2480 */
2481 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002484 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002487 *maf_id = chip->read_byte(mtd);
2488 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Ben Dooksed8165c2008-04-14 14:58:58 +01002490 /* Try again to make sure, as some systems the bus-hold or other
2491 * interface concerns can cause random data which looks like a
2492 * possibly credible NAND flash to appear. If the two results do
2493 * not match, ignore the device completely.
2494 */
2495
2496 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2497
2498 /* Read manufacturer and device IDs */
2499
2500 tmp_manf = chip->read_byte(mtd);
2501 tmp_id = chip->read_byte(mtd);
2502
2503 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2504 printk(KERN_INFO "%s: second ID read did not match "
2505 "%02x,%02x against %02x,%02x\n", __func__,
2506 *maf_id, dev_id, tmp_manf, tmp_id);
2507 return ERR_PTR(-ENODEV);
2508 }
2509
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002510 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002512 if (dev_id == nand_flash_ids[i].id) {
2513 type = &nand_flash_ids[i];
2514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002518 if (!type)
2519 return ERR_PTR(-ENODEV);
2520
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002521 if (!mtd->name)
2522 mtd->name = type->name;
2523
Adrian Hunter69423d92008-12-10 13:37:21 +00002524 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002525
2526 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002527 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002528 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002529 /* The 3rd id byte holds MLC / multichip data */
2530 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002531 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002532 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002533 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002534 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002535 extid >>= 2;
2536 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002537 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002538 extid >>= 2;
2539 /* Calc blocksize. Blocksize is multiples of 64KiB */
2540 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2541 extid >>= 2;
2542 /* Get buswidth information */
2543 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2544
2545 } else {
2546 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002547 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002548 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002549 mtd->erasesize = type->erasesize;
2550 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002551 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002552 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002553 }
2554
2555 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002556 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002557 if (nand_manuf_ids[maf_idx].id == *maf_id)
2558 break;
2559 }
2560
2561 /*
2562 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002563 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002564 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002565 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002566 printk(KERN_INFO "NAND device: Manufacturer ID:"
2567 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2568 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2569 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002570 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002571 busw ? 16 : 8);
2572 return ERR_PTR(-EINVAL);
2573 }
2574
2575 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002576 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002577 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002578 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002579
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002580 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002581 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002582 if (chip->chipsize & 0xffffffff)
2583 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2584 else
2585 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002586
2587 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002588 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002589 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2590
2591 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002592 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002593 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002594
2595 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002596 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002597 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002598 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002599
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002600 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002601 * options for chips which are not having an extended id.
2602 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002603 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002604 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002605
2606 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002607 if (chip->options & NAND_4PAGE_ARRAY)
2608 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002609 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002610 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002611
2612 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002613 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2614 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002615
2616 printk(KERN_INFO "NAND device: Manufacturer ID:"
2617 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2618 nand_manuf_ids[maf_idx].name, type->name);
2619
2620 return type;
2621}
2622
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002623/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002624 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2625 * @mtd: MTD device structure
2626 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002627 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002628 * This is the first phase of the normal nand_scan() function. It
2629 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002630 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002631 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002632 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002633int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002634{
2635 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002636 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002637 struct nand_flash_dev *type;
2638
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002639 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002640 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002641 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002642 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002643
2644 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002645 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002646
2647 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002648 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002649 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002650 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
2652
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002653 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002654 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002655 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02002656 /* See comment in nand_get_flash_type for reset */
2657 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002659 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002661 if (nand_maf_id != chip->read_byte(mtd) ||
2662 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 break;
2664 }
2665 if (i > 1)
2666 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002669 chip->numchips = i;
2670 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
David Woodhouse3b85c322006-09-25 17:06:53 +01002672 return 0;
2673}
2674
2675
2676/**
2677 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2678 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01002679 *
2680 * This is the second phase of the normal nand_scan() function. It
2681 * fills out all the uninitialized function pointers with the defaults
2682 * and scans for a bad block table if appropriate.
2683 */
2684int nand_scan_tail(struct mtd_info *mtd)
2685{
2686 int i;
2687 struct nand_chip *chip = mtd->priv;
2688
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002689 if (!(chip->options & NAND_OWN_BUFFERS))
2690 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2691 if (!chip->buffers)
2692 return -ENOMEM;
2693
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002694 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002695 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002696
2697 /*
2698 * If no default placement scheme is given, select an appropriate one
2699 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002700 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002701 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002703 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 break;
2705 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002706 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
2708 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002709 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002711 case 128:
2712 chip->ecc.layout = &nand_oob_128;
2713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002715 printk(KERN_WARNING "No oob scheme defined for "
2716 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 BUG();
2718 }
2719 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002720
David Woodhouse956e9442006-09-25 17:12:39 +01002721 if (!chip->write_page)
2722 chip->write_page = nand_write_page;
2723
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002724 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002725 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2726 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002727 */
David Woodhouse956e9442006-09-25 17:12:39 +01002728
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002729 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002730 case NAND_ECC_HW_OOB_FIRST:
2731 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2732 if (!chip->ecc.calculate || !chip->ecc.correct ||
2733 !chip->ecc.hwctl) {
2734 printk(KERN_WARNING "No ECC functions supplied; "
2735 "Hardware ECC not possible\n");
2736 BUG();
2737 }
2738 if (!chip->ecc.read_page)
2739 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2740
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002741 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002742 /* Use standard hwecc read page function ? */
2743 if (!chip->ecc.read_page)
2744 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002745 if (!chip->ecc.write_page)
2746 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002747 if (!chip->ecc.read_page_raw)
2748 chip->ecc.read_page_raw = nand_read_page_raw;
2749 if (!chip->ecc.write_page_raw)
2750 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002751 if (!chip->ecc.read_oob)
2752 chip->ecc.read_oob = nand_read_oob_std;
2753 if (!chip->ecc.write_oob)
2754 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002755
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002756 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06002757 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2758 !chip->ecc.hwctl) &&
2759 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002760 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06002761 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002762 chip->ecc.write_page == nand_write_page_hwecc)) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002763 printk(KERN_WARNING "No ECC functions supplied; "
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002764 "Hardware ECC not possible\n");
2765 BUG();
2766 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002767 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002768 if (!chip->ecc.read_page)
2769 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002770 if (!chip->ecc.write_page)
2771 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08002772 if (!chip->ecc.read_page_raw)
2773 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2774 if (!chip->ecc.write_page_raw)
2775 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002776 if (!chip->ecc.read_oob)
2777 chip->ecc.read_oob = nand_read_oob_syndrome;
2778 if (!chip->ecc.write_oob)
2779 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002780
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002781 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002782 break;
2783 printk(KERN_WARNING "%d byte HW ECC not possible on "
2784 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002785 chip->ecc.size, mtd->writesize);
2786 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002788 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002789 chip->ecc.calculate = nand_calculate_ecc;
2790 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002791 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01002792 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002793 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002794 chip->ecc.read_page_raw = nand_read_page_raw;
2795 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002796 chip->ecc.read_oob = nand_read_oob_std;
2797 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00002798 if (!chip->ecc.size)
2799 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002800 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002802
2803 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002804 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2805 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002806 chip->ecc.read_page = nand_read_page_raw;
2807 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002808 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08002809 chip->ecc.read_page_raw = nand_read_page_raw;
2810 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002811 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002812 chip->ecc.size = mtd->writesize;
2813 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002817 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002818 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002819 BUG();
2820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002822 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002823 * The number of bytes available for a client to place data into
2824 * the out of band area
2825 */
2826 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07002827 for (i = 0; chip->ecc.layout->oobfree[i].length
2828 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002829 chip->ecc.layout->oobavail +=
2830 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002831 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002832
2833 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002834 * Set the number of read / write steps for one page depending on ECC
2835 * mode
2836 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002837 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2838 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002839 printk(KERN_WARNING "Invalid ecc parameters\n");
2840 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002842 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002843
Thomas Gleixner29072b92006-09-28 15:38:36 +02002844 /*
2845 * Allow subpage writes up to ecc.steps. Not possible for MLC
2846 * FLASH.
2847 */
2848 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2849 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2850 switch(chip->ecc.steps) {
2851 case 2:
2852 mtd->subpage_sft = 1;
2853 break;
2854 case 4:
2855 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002856 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02002857 mtd->subpage_sft = 2;
2858 break;
2859 }
2860 }
2861 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2862
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002863 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002864 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002867 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002870 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 /* Fill in remaining MTD driver data */
2873 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002874 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 mtd->erase = nand_erase;
2876 mtd->point = NULL;
2877 mtd->unpoint = NULL;
2878 mtd->read = nand_read;
2879 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 mtd->read_oob = nand_read_oob;
2881 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 mtd->sync = nand_sync;
2883 mtd->lock = NULL;
2884 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002885 mtd->suspend = nand_suspend;
2886 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 mtd->block_isbad = nand_block_isbad;
2888 mtd->block_markbad = nand_block_markbad;
2889
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002890 /* propagate ecc.layout to mtd_info */
2891 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002893 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002894 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002898 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
Rusty Russella6e6abd2009-03-31 13:05:31 -06002901/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01002902 test if this is a module _anyway_ -- they'd have to try _really_ hard
2903 to call us from in-kernel code if the core NAND support is modular. */
2904#ifdef MODULE
2905#define caller_is_module() (1)
2906#else
2907#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06002908 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01002909#endif
2910
2911/**
2912 * nand_scan - [NAND Interface] Scan for the NAND device
2913 * @mtd: MTD device structure
2914 * @maxchips: Number of chips to scan for
2915 *
2916 * This fills out all the uninitialized function pointers
2917 * with the defaults.
2918 * The flash ID is read and the mtd/chip structures are
2919 * filled with the appropriate values.
2920 * The mtd->owner field must be set to the module of the caller
2921 *
2922 */
2923int nand_scan(struct mtd_info *mtd, int maxchips)
2924{
2925 int ret;
2926
2927 /* Many callers got this wrong, so check for it for a while... */
2928 if (!mtd->owner && caller_is_module()) {
vimal singh20d8e242009-07-07 15:49:49 +05302929 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
2930 __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01002931 BUG();
2932 }
2933
2934 ret = nand_scan_ident(mtd, maxchips);
2935 if (!ret)
2936 ret = nand_scan_tail(mtd);
2937 return ret;
2938}
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002941 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 * @mtd: MTD device structure
2943*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002944void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002946 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948#ifdef CONFIG_MTD_PARTITIONS
2949 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002950 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951#endif
2952 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002953 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Jesper Juhlfa671642005-11-07 01:01:27 -08002955 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002956 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002957 if (!(chip->options & NAND_OWN_BUFFERS))
2958 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
David Woodhousee0c7d762006-05-13 18:07:53 +01002961EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01002962EXPORT_SYMBOL_GPL(nand_scan_ident);
2963EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01002964EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002965
2966static int __init nand_base_init(void)
2967{
2968 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2969 return 0;
2970}
2971
2972static void __exit nand_base_exit(void)
2973{
2974 led_trigger_unregister_simple(nand_led_trigger);
2975}
2976
2977module_init(nand_base_init);
2978module_exit(nand_base_exit);
2979
David Woodhousee0c7d762006-05-13 18:07:53 +01002980MODULE_LICENSE("GPL");
2981MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2982MODULE_DESCRIPTION("Generic NAND flash driver code");