blob: 22113865438b83eab930c9aa839a127c6a5c8de3 [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
David Brownell52ff49d2009-03-04 12:01:36 -0800764 *
765 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200766 */
767static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700768 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200769{
770 chip->read_buf(mtd, buf, mtd->writesize);
771 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
772 return 0;
773}
774
775/**
David Brownell52ff49d2009-03-04 12:01:36 -0800776 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
777 * @mtd: mtd info structure
778 * @chip: nand chip info structure
779 * @buf: buffer to store read data
780 *
781 * We need a special oob layout and handling even when OOB isn't used.
782 */
783static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700784 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -0800785{
786 int eccsize = chip->ecc.size;
787 int eccbytes = chip->ecc.bytes;
788 uint8_t *oob = chip->oob_poi;
789 int steps, size;
790
791 for (steps = chip->ecc.steps; steps > 0; steps--) {
792 chip->read_buf(mtd, buf, eccsize);
793 buf += eccsize;
794
795 if (chip->ecc.prepad) {
796 chip->read_buf(mtd, oob, chip->ecc.prepad);
797 oob += chip->ecc.prepad;
798 }
799
800 chip->read_buf(mtd, oob, eccbytes);
801 oob += eccbytes;
802
803 if (chip->ecc.postpad) {
804 chip->read_buf(mtd, oob, chip->ecc.postpad);
805 oob += chip->ecc.postpad;
806 }
807 }
808
809 size = mtd->oobsize - (oob - chip->oob_poi);
810 if (size)
811 chip->read_buf(mtd, oob, size);
812
813 return 0;
814}
815
816/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300817 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200818 * @mtd: mtd info structure
819 * @chip: nand chip info structure
820 * @buf: buffer to store read data
David A. Marlin068e3c02005-01-24 03:07:46 +0000821 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200822static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700823 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200825 int i, eccsize = chip->ecc.size;
826 int eccbytes = chip->ecc.bytes;
827 int eccsteps = chip->ecc.steps;
828 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100829 uint8_t *ecc_calc = chip->buffers->ecccalc;
830 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100831 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200832
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700833 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200834
835 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
836 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
837
838 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200839 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200840
841 eccsteps = chip->ecc.steps;
842 p = buf;
843
844 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
845 int stat;
846
847 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700848 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200849 mtd->ecc_stats.failed++;
850 else
851 mtd->ecc_stats.corrected += stat;
852 }
853 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100854}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/**
Alexey Korolev3d459552008-05-15 17:23:18 +0100857 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
858 * @mtd: mtd info structure
859 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +0100860 * @data_offs: offset of requested data within the page
861 * @readlen: data length
862 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +0100863 */
864static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
865{
866 int start_step, end_step, num_steps;
867 uint32_t *eccpos = chip->ecc.layout->eccpos;
868 uint8_t *p;
869 int data_col_addr, i, gaps = 0;
870 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
871 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
872
873 /* Column address wihin the page aligned to ECC size (256bytes). */
874 start_step = data_offs / chip->ecc.size;
875 end_step = (data_offs + readlen - 1) / chip->ecc.size;
876 num_steps = end_step - start_step + 1;
877
878 /* Data size aligned to ECC ecc.size*/
879 datafrag_len = num_steps * chip->ecc.size;
880 eccfrag_len = num_steps * chip->ecc.bytes;
881
882 data_col_addr = start_step * chip->ecc.size;
883 /* If we read not a page aligned data */
884 if (data_col_addr != 0)
885 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
886
887 p = bufpoi + data_col_addr;
888 chip->read_buf(mtd, p, datafrag_len);
889
890 /* Calculate ECC */
891 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
892 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
893
894 /* The performance is faster if to position offsets
895 according to ecc.pos. Let make sure here that
896 there are no gaps in ecc positions */
897 for (i = 0; i < eccfrag_len - 1; i++) {
898 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
899 eccpos[i + start_step * chip->ecc.bytes + 1]) {
900 gaps = 1;
901 break;
902 }
903 }
904 if (gaps) {
905 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
906 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
907 } else {
908 /* send the command to read the particular ecc bytes */
909 /* take care about buswidth alignment in read_buf */
910 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
911 aligned_len = eccfrag_len;
912 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
913 aligned_len++;
914 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
915 aligned_len++;
916
917 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
918 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
919 }
920
921 for (i = 0; i < eccfrag_len; i++)
922 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
923
924 p = bufpoi + data_col_addr;
925 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
926 int stat;
927
928 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
929 if (stat == -1)
930 mtd->ecc_stats.failed++;
931 else
932 mtd->ecc_stats.corrected += stat;
933 }
934 return 0;
935}
936
937/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300938 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200939 * @mtd: mtd info structure
940 * @chip: nand chip info structure
941 * @buf: buffer to store read data
942 *
943 * Not for syndrome calculating ecc controllers which need a special oob layout
944 */
945static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700946 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200947{
948 int i, eccsize = chip->ecc.size;
949 int eccbytes = chip->ecc.bytes;
950 int eccsteps = chip->ecc.steps;
951 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100952 uint8_t *ecc_calc = chip->buffers->ecccalc;
953 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100954 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200955
956 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
957 chip->ecc.hwctl(mtd, NAND_ECC_READ);
958 chip->read_buf(mtd, p, eccsize);
959 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
960 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200961 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200962
963 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200964 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200965
966 eccsteps = chip->ecc.steps;
967 p = buf;
968
969 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
970 int stat;
971
972 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700973 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200974 mtd->ecc_stats.failed++;
975 else
976 mtd->ecc_stats.corrected += stat;
977 }
978 return 0;
979}
980
981/**
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -0700982 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
983 * @mtd: mtd info structure
984 * @chip: nand chip info structure
985 * @buf: buffer to store read data
986 *
987 * Hardware ECC for large page chips, require OOB to be read first.
988 * For this ECC mode, the write_page method is re-used from ECC_HW.
989 * These methods read/write ECC from the OOB area, unlike the
990 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
991 * "infix ECC" scheme and reads/writes ECC from the data area, by
992 * overwriting the NAND manufacturer bad block markings.
993 */
994static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
995 struct nand_chip *chip, uint8_t *buf, int page)
996{
997 int i, eccsize = chip->ecc.size;
998 int eccbytes = chip->ecc.bytes;
999 int eccsteps = chip->ecc.steps;
1000 uint8_t *p = buf;
1001 uint8_t *ecc_code = chip->buffers->ecccode;
1002 uint32_t *eccpos = chip->ecc.layout->eccpos;
1003 uint8_t *ecc_calc = chip->buffers->ecccalc;
1004
1005 /* Read the OOB area first */
1006 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1007 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1008 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1009
1010 for (i = 0; i < chip->ecc.total; i++)
1011 ecc_code[i] = chip->oob_poi[eccpos[i]];
1012
1013 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1014 int stat;
1015
1016 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1017 chip->read_buf(mtd, p, eccsize);
1018 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1019
1020 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1021 if (stat < 0)
1022 mtd->ecc_stats.failed++;
1023 else
1024 mtd->ecc_stats.corrected += stat;
1025 }
1026 return 0;
1027}
1028
1029/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001030 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001031 * @mtd: mtd info structure
1032 * @chip: nand chip info structure
1033 * @buf: buffer to store read data
1034 *
1035 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001036 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001037 */
1038static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001039 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001040{
1041 int i, eccsize = chip->ecc.size;
1042 int eccbytes = chip->ecc.bytes;
1043 int eccsteps = chip->ecc.steps;
1044 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001045 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001046
1047 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1048 int stat;
1049
1050 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1051 chip->read_buf(mtd, p, eccsize);
1052
1053 if (chip->ecc.prepad) {
1054 chip->read_buf(mtd, oob, chip->ecc.prepad);
1055 oob += chip->ecc.prepad;
1056 }
1057
1058 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1059 chip->read_buf(mtd, oob, eccbytes);
1060 stat = chip->ecc.correct(mtd, p, oob, NULL);
1061
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001062 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001063 mtd->ecc_stats.failed++;
1064 else
1065 mtd->ecc_stats.corrected += stat;
1066
1067 oob += eccbytes;
1068
1069 if (chip->ecc.postpad) {
1070 chip->read_buf(mtd, oob, chip->ecc.postpad);
1071 oob += chip->ecc.postpad;
1072 }
1073 }
1074
1075 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001076 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001077 if (i)
1078 chip->read_buf(mtd, oob, i);
1079
1080 return 0;
1081}
1082
1083/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001084 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1085 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001086 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001087 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001088 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001089 */
1090static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001091 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001092{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001093 switch(ops->mode) {
1094
1095 case MTD_OOB_PLACE:
1096 case MTD_OOB_RAW:
1097 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1098 return oob + len;
1099
1100 case MTD_OOB_AUTO: {
1101 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001102 uint32_t boffs = 0, roffs = ops->ooboffs;
1103 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001104
1105 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001106 /* Read request not from offset 0 ? */
1107 if (unlikely(roffs)) {
1108 if (roffs >= free->length) {
1109 roffs -= free->length;
1110 continue;
1111 }
1112 boffs = free->offset + roffs;
1113 bytes = min_t(size_t, len,
1114 (free->length - roffs));
1115 roffs = 0;
1116 } else {
1117 bytes = min_t(size_t, len, free->length);
1118 boffs = free->offset;
1119 }
1120 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001121 oob += bytes;
1122 }
1123 return oob;
1124 }
1125 default:
1126 BUG();
1127 }
1128 return NULL;
1129}
1130
1131/**
1132 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001133 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001134 * @mtd: MTD device structure
1135 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001136 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001137 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001138 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001139 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001140static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1141 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001142{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001143 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001144 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001145 struct mtd_ecc_stats stats;
1146 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1147 int sndcmd = 1;
1148 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001149 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001150 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001151 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001153 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001155 chipnr = (int)(from >> chip->chip_shift);
1156 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001158 realpage = (int)(from >> chip->page_shift);
1159 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001161 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001163 buf = ops->datbuf;
1164 oob = ops->oobbuf;
1165
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001166 while(1) {
1167 bytes = min(mtd->writesize - col, readlen);
1168 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001169
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001170 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001171 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001172 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001174 if (likely(sndcmd)) {
1175 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1176 sndcmd = 0;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001179 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001180 if (unlikely(ops->mode == MTD_OOB_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001181 ret = chip->ecc.read_page_raw(mtd, chip,
1182 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001183 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1184 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001185 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001186 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1187 page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001188 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001189 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001190
1191 /* Transfer not aligned data */
1192 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001193 if (!NAND_SUBPAGE_READ(chip) && !oob)
1194 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001195 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001197
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001198 buf += bytes;
1199
1200 if (unlikely(oob)) {
1201 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001202 if (ops->mode != MTD_OOB_RAW) {
1203 int toread = min(oobreadlen,
1204 chip->ecc.layout->oobavail);
1205 if (toread) {
1206 oob = nand_transfer_oob(chip,
1207 oob, ops, toread);
1208 oobreadlen -= toread;
1209 }
1210 } else
1211 buf = nand_transfer_oob(chip,
1212 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001213 }
1214
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001215 if (!(chip->options & NAND_NO_READRDY)) {
1216 /*
1217 * Apply delay or wait for ready/busy pin. Do
1218 * this before the AUTOINCR check, so no
1219 * problems arise if a chip which does auto
1220 * increment is marked as NOAUTOINCR by the
1221 * board driver.
1222 */
1223 if (!chip->dev_ready)
1224 udelay(chip->chip_delay);
1225 else
1226 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001228 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001229 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001230 buf += bytes;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001233 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001234
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001235 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /* For subsequent reads align to page boundary. */
1239 col = 0;
1240 /* Increment page address */
1241 realpage++;
1242
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001243 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* Check, if we cross a chip boundary */
1245 if (!page) {
1246 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001247 chip->select_chip(mtd, -1);
1248 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001250
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001251 /* Check, if the chip supports auto page increment
1252 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001253 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001254 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001255 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001258 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001259 if (oob)
1260 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001262 if (ret)
1263 return ret;
1264
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001265 if (mtd->ecc_stats.failed - stats.failed)
1266 return -EBADMSG;
1267
1268 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001269}
1270
1271/**
1272 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1273 * @mtd: MTD device structure
1274 * @from: offset to read from
1275 * @len: number of bytes to read
1276 * @retlen: pointer to variable to store the number of read bytes
1277 * @buf: the databuffer to put data
1278 *
1279 * Get hold of the chip and call nand_do_read
1280 */
1281static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1282 size_t *retlen, uint8_t *buf)
1283{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001284 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001285 int ret;
1286
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001287 /* Do not allow reads past end of device */
1288 if ((from + len) > mtd->size)
1289 return -EINVAL;
1290 if (!len)
1291 return 0;
1292
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001293 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001294
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001295 chip->ops.len = len;
1296 chip->ops.datbuf = buf;
1297 chip->ops.oobbuf = NULL;
1298
1299 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001300
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001301 *retlen = chip->ops.retlen;
1302
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001303 nand_release_device(mtd);
1304
1305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001309 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1310 * @mtd: mtd info structure
1311 * @chip: nand chip info structure
1312 * @page: page number to read
1313 * @sndcmd: flag whether to issue read command or not
1314 */
1315static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1316 int page, int sndcmd)
1317{
1318 if (sndcmd) {
1319 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1320 sndcmd = 0;
1321 }
1322 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1323 return sndcmd;
1324}
1325
1326/**
1327 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1328 * with syndromes
1329 * @mtd: mtd info structure
1330 * @chip: nand chip info structure
1331 * @page: page number to read
1332 * @sndcmd: flag whether to issue read command or not
1333 */
1334static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1335 int page, int sndcmd)
1336{
1337 uint8_t *buf = chip->oob_poi;
1338 int length = mtd->oobsize;
1339 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1340 int eccsize = chip->ecc.size;
1341 uint8_t *bufpoi = buf;
1342 int i, toread, sndrnd = 0, pos;
1343
1344 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1345 for (i = 0; i < chip->ecc.steps; i++) {
1346 if (sndrnd) {
1347 pos = eccsize + i * (eccsize + chunk);
1348 if (mtd->writesize > 512)
1349 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1350 else
1351 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1352 } else
1353 sndrnd = 1;
1354 toread = min_t(int, length, chunk);
1355 chip->read_buf(mtd, bufpoi, toread);
1356 bufpoi += toread;
1357 length -= toread;
1358 }
1359 if (length > 0)
1360 chip->read_buf(mtd, bufpoi, length);
1361
1362 return 1;
1363}
1364
1365/**
1366 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1367 * @mtd: mtd info structure
1368 * @chip: nand chip info structure
1369 * @page: page number to write
1370 */
1371static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1372 int page)
1373{
1374 int status = 0;
1375 const uint8_t *buf = chip->oob_poi;
1376 int length = mtd->oobsize;
1377
1378 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1379 chip->write_buf(mtd, buf, length);
1380 /* Send command to program the OOB data */
1381 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1382
1383 status = chip->waitfunc(mtd, chip);
1384
Savin Zlobec0d420f92006-06-21 11:51:20 +02001385 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001386}
1387
1388/**
1389 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1390 * with syndrome - only for large page flash !
1391 * @mtd: mtd info structure
1392 * @chip: nand chip info structure
1393 * @page: page number to write
1394 */
1395static int nand_write_oob_syndrome(struct mtd_info *mtd,
1396 struct nand_chip *chip, int page)
1397{
1398 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1399 int eccsize = chip->ecc.size, length = mtd->oobsize;
1400 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1401 const uint8_t *bufpoi = chip->oob_poi;
1402
1403 /*
1404 * data-ecc-data-ecc ... ecc-oob
1405 * or
1406 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1407 */
1408 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1409 pos = steps * (eccsize + chunk);
1410 steps = 0;
1411 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001412 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001413
1414 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1415 for (i = 0; i < steps; i++) {
1416 if (sndcmd) {
1417 if (mtd->writesize <= 512) {
1418 uint32_t fill = 0xFFFFFFFF;
1419
1420 len = eccsize;
1421 while (len > 0) {
1422 int num = min_t(int, len, 4);
1423 chip->write_buf(mtd, (uint8_t *)&fill,
1424 num);
1425 len -= num;
1426 }
1427 } else {
1428 pos = eccsize + i * (eccsize + chunk);
1429 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1430 }
1431 } else
1432 sndcmd = 1;
1433 len = min_t(int, length, chunk);
1434 chip->write_buf(mtd, bufpoi, len);
1435 bufpoi += len;
1436 length -= len;
1437 }
1438 if (length > 0)
1439 chip->write_buf(mtd, bufpoi, length);
1440
1441 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1442 status = chip->waitfunc(mtd, chip);
1443
1444 return status & NAND_STATUS_FAIL ? -EIO : 0;
1445}
1446
1447/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001448 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 * @mtd: MTD device structure
1450 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001451 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 *
1453 * NAND read out-of-band data from the spare area
1454 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001455static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1456 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001458 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001459 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001460 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001461 int readlen = ops->ooblen;
1462 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001463 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
vimal singh20d8e242009-07-07 15:49:49 +05301465 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1466 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Adrian Hunter03736152007-01-31 17:58:29 +02001468 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001469 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001470 else
1471 len = mtd->oobsize;
1472
1473 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301474 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1475 "outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001476 return -EINVAL;
1477 }
1478
1479 /* Do not allow reads past end of device */
1480 if (unlikely(from >= mtd->size ||
1481 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1482 (from >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301483 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1484 "of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001485 return -EINVAL;
1486 }
Vitaly Wool70145682006-11-03 18:20:38 +03001487
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001488 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001489 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001491 /* Shift to get page */
1492 realpage = (int)(from >> chip->page_shift);
1493 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001495 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001496 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001497
1498 len = min(len, readlen);
1499 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001500
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001501 if (!(chip->options & NAND_NO_READRDY)) {
1502 /*
1503 * Apply delay or wait for ready/busy pin. Do this
1504 * before the AUTOINCR check, so no problems arise if a
1505 * chip which does auto increment is marked as
1506 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001507 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001508 if (!chip->dev_ready)
1509 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001510 else
1511 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001513
Vitaly Wool70145682006-11-03 18:20:38 +03001514 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001515 if (!readlen)
1516 break;
1517
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001518 /* Increment page address */
1519 realpage++;
1520
1521 page = realpage & chip->pagemask;
1522 /* Check, if we cross a chip boundary */
1523 if (!page) {
1524 chipnr++;
1525 chip->select_chip(mtd, -1);
1526 chip->select_chip(mtd, chipnr);
1527 }
1528
1529 /* Check, if the chip supports auto page increment
1530 * or if we have hit a block boundary.
1531 */
1532 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1533 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
Vitaly Wool70145682006-11-03 18:20:38 +03001536 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return 0;
1538}
1539
1540/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001541 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001544 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001546 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001548static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1549 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001551 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001552 int ret = -ENOTSUPP;
1553
1554 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001557 if (ops->datbuf && (from + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05301558 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1559 "beyond end of device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -EINVAL;
1561 }
1562
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001563 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001565 switch(ops->mode) {
1566 case MTD_OOB_PLACE:
1567 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001568 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001569 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001570
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001571 default:
1572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001575 if (!ops->datbuf)
1576 ret = nand_do_read_oob(mtd, from, ops);
1577 else
1578 ret = nand_do_read_ops(mtd, from, ops);
1579
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001580 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001582 return ret;
1583}
1584
1585
1586/**
1587 * nand_write_page_raw - [Intern] raw page write function
1588 * @mtd: mtd info structure
1589 * @chip: nand chip info structure
1590 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001591 *
1592 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001593 */
1594static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1595 const uint8_t *buf)
1596{
1597 chip->write_buf(mtd, buf, mtd->writesize);
1598 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001601/**
David Brownell52ff49d2009-03-04 12:01:36 -08001602 * nand_write_page_raw_syndrome - [Intern] raw page write function
1603 * @mtd: mtd info structure
1604 * @chip: nand chip info structure
1605 * @buf: data buffer
1606 *
1607 * We need a special oob layout and handling even when ECC isn't checked.
1608 */
1609static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1610 const uint8_t *buf)
1611{
1612 int eccsize = chip->ecc.size;
1613 int eccbytes = chip->ecc.bytes;
1614 uint8_t *oob = chip->oob_poi;
1615 int steps, size;
1616
1617 for (steps = chip->ecc.steps; steps > 0; steps--) {
1618 chip->write_buf(mtd, buf, eccsize);
1619 buf += eccsize;
1620
1621 if (chip->ecc.prepad) {
1622 chip->write_buf(mtd, oob, chip->ecc.prepad);
1623 oob += chip->ecc.prepad;
1624 }
1625
1626 chip->read_buf(mtd, oob, eccbytes);
1627 oob += eccbytes;
1628
1629 if (chip->ecc.postpad) {
1630 chip->write_buf(mtd, oob, chip->ecc.postpad);
1631 oob += chip->ecc.postpad;
1632 }
1633 }
1634
1635 size = mtd->oobsize - (oob - chip->oob_poi);
1636 if (size)
1637 chip->write_buf(mtd, oob, size);
1638}
1639/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001640 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001641 * @mtd: mtd info structure
1642 * @chip: nand chip info structure
1643 * @buf: data buffer
1644 */
1645static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1646 const uint8_t *buf)
1647{
1648 int i, eccsize = chip->ecc.size;
1649 int eccbytes = chip->ecc.bytes;
1650 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001651 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001652 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001653 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001654
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001655 /* Software ecc calculation */
1656 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1657 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001658
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001659 for (i = 0; i < chip->ecc.total; i++)
1660 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001661
Thomas Gleixner90424de2007-04-05 11:44:05 +02001662 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001663}
1664
1665/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001666 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001667 * @mtd: mtd info structure
1668 * @chip: nand chip info structure
1669 * @buf: data buffer
1670 */
1671static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1672 const uint8_t *buf)
1673{
1674 int i, eccsize = chip->ecc.size;
1675 int eccbytes = chip->ecc.bytes;
1676 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001677 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001678 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001679 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001680
1681 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1682 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001683 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001684 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1685 }
1686
1687 for (i = 0; i < chip->ecc.total; i++)
1688 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1689
1690 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1691}
1692
1693/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001694 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001695 * @mtd: mtd info structure
1696 * @chip: nand chip info structure
1697 * @buf: data buffer
1698 *
1699 * The hw generator calculates the error syndrome automatically. Therefor
1700 * we need a special oob layout and handling.
1701 */
1702static void nand_write_page_syndrome(struct mtd_info *mtd,
1703 struct nand_chip *chip, const uint8_t *buf)
1704{
1705 int i, eccsize = chip->ecc.size;
1706 int eccbytes = chip->ecc.bytes;
1707 int eccsteps = chip->ecc.steps;
1708 const uint8_t *p = buf;
1709 uint8_t *oob = chip->oob_poi;
1710
1711 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1712
1713 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1714 chip->write_buf(mtd, p, eccsize);
1715
1716 if (chip->ecc.prepad) {
1717 chip->write_buf(mtd, oob, chip->ecc.prepad);
1718 oob += chip->ecc.prepad;
1719 }
1720
1721 chip->ecc.calculate(mtd, p, oob);
1722 chip->write_buf(mtd, oob, eccbytes);
1723 oob += eccbytes;
1724
1725 if (chip->ecc.postpad) {
1726 chip->write_buf(mtd, oob, chip->ecc.postpad);
1727 oob += chip->ecc.postpad;
1728 }
1729 }
1730
1731 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001732 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001733 if (i)
1734 chip->write_buf(mtd, oob, i);
1735}
1736
1737/**
David Woodhouse956e9442006-09-25 17:12:39 +01001738 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001739 * @mtd: MTD device structure
1740 * @chip: NAND chip descriptor
1741 * @buf: the data to write
1742 * @page: page number to write
1743 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02001744 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001745 */
1746static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001747 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001748{
1749 int status;
1750
1751 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1752
David Woodhouse956e9442006-09-25 17:12:39 +01001753 if (unlikely(raw))
1754 chip->ecc.write_page_raw(mtd, chip, buf);
1755 else
1756 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001757
1758 /*
1759 * Cached progamming disabled for now, Not sure if its worth the
1760 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1761 */
1762 cached = 0;
1763
1764 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1765
1766 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001767 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001768 /*
1769 * See if operation failed and additional status checks are
1770 * available
1771 */
1772 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1773 status = chip->errstat(mtd, chip, FL_WRITING, status,
1774 page);
1775
1776 if (status & NAND_STATUS_FAIL)
1777 return -EIO;
1778 } else {
1779 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001780 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001781 }
1782
1783#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1784 /* Send command to read back the data */
1785 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1786
1787 if (chip->verify_buf(mtd, buf, mtd->writesize))
1788 return -EIO;
1789#endif
1790 return 0;
1791}
1792
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001793/**
1794 * nand_fill_oob - [Internal] Transfer client buffer to oob
1795 * @chip: nand chip structure
1796 * @oob: oob data buffer
1797 * @ops: oob ops structure
1798 */
1799static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1800 struct mtd_oob_ops *ops)
1801{
1802 size_t len = ops->ooblen;
1803
1804 switch(ops->mode) {
1805
1806 case MTD_OOB_PLACE:
1807 case MTD_OOB_RAW:
1808 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1809 return oob + len;
1810
1811 case MTD_OOB_AUTO: {
1812 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001813 uint32_t boffs = 0, woffs = ops->ooboffs;
1814 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001815
1816 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001817 /* Write request not from offset 0 ? */
1818 if (unlikely(woffs)) {
1819 if (woffs >= free->length) {
1820 woffs -= free->length;
1821 continue;
1822 }
1823 boffs = free->offset + woffs;
1824 bytes = min_t(size_t, len,
1825 (free->length - woffs));
1826 woffs = 0;
1827 } else {
1828 bytes = min_t(size_t, len, free->length);
1829 boffs = free->offset;
1830 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001831 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001832 oob += bytes;
1833 }
1834 return oob;
1835 }
1836 default:
1837 BUG();
1838 }
1839 return NULL;
1840}
1841
Thomas Gleixner29072b92006-09-28 15:38:36 +02001842#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001843
1844/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001845 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001846 * @mtd: MTD device structure
1847 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001848 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001849 *
1850 * NAND write with ECC
1851 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001852static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1853 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001854{
Thomas Gleixner29072b92006-09-28 15:38:36 +02001855 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001856 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001857 uint32_t writelen = ops->len;
1858 uint8_t *oob = ops->oobbuf;
1859 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001860 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001861
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001862 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001863 if (!writelen)
1864 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001865
1866 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001867 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301868 printk(KERN_NOTICE "%s: Attempt to write not "
1869 "page aligned data\n", __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001870 return -EINVAL;
1871 }
1872
Thomas Gleixner29072b92006-09-28 15:38:36 +02001873 column = to & (mtd->writesize - 1);
1874 subpage = column || (writelen & (mtd->writesize - 1));
1875
1876 if (subpage && oob)
1877 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001878
Thomas Gleixner6a930962006-06-28 00:11:45 +02001879 chipnr = (int)(to >> chip->chip_shift);
1880 chip->select_chip(mtd, chipnr);
1881
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001882 /* Check, if it is write protected */
1883 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001884 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001885
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001886 realpage = (int)(to >> chip->page_shift);
1887 page = realpage & chip->pagemask;
1888 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1889
1890 /* Invalidate the page cache, when we write to the cached page */
1891 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001892 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001893 chip->pagebuf = -1;
1894
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001895 /* If we're not given explicit OOB data, let it be 0xFF */
1896 if (likely(!oob))
1897 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001898
1899 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02001900 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001901 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001902 uint8_t *wbuf = buf;
1903
1904 /* Partial page write ? */
1905 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1906 cached = 0;
1907 bytes = min_t(int, bytes - column, (int) writelen);
1908 chip->pagebuf = -1;
1909 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1910 memcpy(&chip->buffers->databuf[column], buf, bytes);
1911 wbuf = chip->buffers->databuf;
1912 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001913
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001914 if (unlikely(oob))
1915 oob = nand_fill_oob(chip, oob, ops);
1916
Thomas Gleixner29072b92006-09-28 15:38:36 +02001917 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01001918 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001919 if (ret)
1920 break;
1921
1922 writelen -= bytes;
1923 if (!writelen)
1924 break;
1925
Thomas Gleixner29072b92006-09-28 15:38:36 +02001926 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001927 buf += bytes;
1928 realpage++;
1929
1930 page = realpage & chip->pagemask;
1931 /* Check, if we cross a chip boundary */
1932 if (!page) {
1933 chipnr++;
1934 chip->select_chip(mtd, -1);
1935 chip->select_chip(mtd, chipnr);
1936 }
1937 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001938
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001939 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03001940 if (unlikely(oob))
1941 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001942 return ret;
1943}
1944
1945/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001946 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 * @mtd: MTD device structure
1948 * @to: offset to write to
1949 * @len: number of bytes to write
1950 * @retlen: pointer to variable to store the number of written bytes
1951 * @buf: the data to write
1952 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001953 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001955static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001956 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001958 struct nand_chip *chip = mtd->priv;
1959 int ret;
1960
1961 /* Do not allow reads past end of device */
1962 if ((to + len) > mtd->size)
1963 return -EINVAL;
1964 if (!len)
1965 return 0;
1966
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001967 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001968
1969 chip->ops.len = len;
1970 chip->ops.datbuf = (uint8_t *)buf;
1971 chip->ops.oobbuf = NULL;
1972
1973 ret = nand_do_write_ops(mtd, to, &chip->ops);
1974
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001975 *retlen = chip->ops.retlen;
1976
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001977 nand_release_device(mtd);
1978
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001979 return ret;
1980}
1981
1982/**
1983 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1984 * @mtd: MTD device structure
1985 * @to: offset to write to
1986 * @ops: oob operation description structure
1987 *
1988 * NAND write out-of-band
1989 */
1990static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1991 struct mtd_oob_ops *ops)
1992{
Adrian Hunter03736152007-01-31 17:58:29 +02001993 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001994 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
vimal singh20d8e242009-07-07 15:49:49 +05301996 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
1997 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Adrian Hunter03736152007-01-31 17:58:29 +02001999 if (ops->mode == MTD_OOB_AUTO)
2000 len = chip->ecc.layout->oobavail;
2001 else
2002 len = mtd->oobsize;
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002005 if ((ops->ooboffs + ops->ooblen) > len) {
vimal singh20d8e242009-07-07 15:49:49 +05302006 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2007 "past end of page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -EINVAL;
2009 }
2010
Adrian Hunter03736152007-01-31 17:58:29 +02002011 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302012 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2013 "write outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002014 return -EINVAL;
2015 }
2016
2017 /* Do not allow reads past end of device */
2018 if (unlikely(to >= mtd->size ||
2019 ops->ooboffs + ops->ooblen >
2020 ((mtd->size >> chip->page_shift) -
2021 (to >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302022 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2023 "end of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002024 return -EINVAL;
2025 }
2026
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002027 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002028 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002030 /* Shift to get page */
2031 page = (int)(to >> chip->page_shift);
2032
2033 /*
2034 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2035 * of my DiskOnChip 2000 test units) will clear the whole data page too
2036 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2037 * it in the doc2000 driver in August 1999. dwmw2.
2038 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002039 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 /* Check, if it is write protected */
2042 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002043 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002046 if (page == chip->pagebuf)
2047 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002049 memset(chip->oob_poi, 0xff, mtd->oobsize);
2050 nand_fill_oob(chip, ops->oobbuf, ops);
2051 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2052 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002053
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002054 if (status)
2055 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Vitaly Wool70145682006-11-03 18:20:38 +03002057 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002059 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002062/**
2063 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2064 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002065 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002066 * @ops: oob operation description structure
2067 */
2068static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2069 struct mtd_oob_ops *ops)
2070{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002071 struct nand_chip *chip = mtd->priv;
2072 int ret = -ENOTSUPP;
2073
2074 ops->retlen = 0;
2075
2076 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002077 if (ops->datbuf && (to + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302078 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2079 "end of device\n", __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002080 return -EINVAL;
2081 }
2082
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002083 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002084
2085 switch(ops->mode) {
2086 case MTD_OOB_PLACE:
2087 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002088 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002089 break;
2090
2091 default:
2092 goto out;
2093 }
2094
2095 if (!ops->datbuf)
2096 ret = nand_do_write_oob(mtd, to, ops);
2097 else
2098 ret = nand_do_write_ops(mtd, to, ops);
2099
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002100 out:
2101 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return ret;
2103}
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2107 * @mtd: MTD device structure
2108 * @page: the page address of the block which will be erased
2109 *
2110 * Standard erase command for NAND chips
2111 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002112static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002114 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002116 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2117 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120/**
2121 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2122 * @mtd: MTD device structure
2123 * @page: the page address of the block which will be erased
2124 *
2125 * AND multi block erase command function
2126 * Erase 4 consecutive blocks
2127 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002128static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002130 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002132 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2133 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2134 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2135 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2136 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
2139/**
2140 * nand_erase - [MTD Interface] erase block(s)
2141 * @mtd: MTD device structure
2142 * @instr: erase instruction
2143 *
2144 * Erase one ore more blocks
2145 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002146static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
David Woodhousee0c7d762006-05-13 18:07:53 +01002148 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002150
David A. Marlin30f464b2005-01-17 18:35:25 +00002151#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002153 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 * @mtd: MTD device structure
2155 * @instr: erase instruction
2156 * @allowbbt: allow erasing the bbt area
2157 *
2158 * Erase one ore more blocks
2159 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002160int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2161 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Adrian Hunter69423d92008-12-10 13:37:21 +00002163 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002164 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002165 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002166 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002167 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
vimal singh20d8e242009-07-07 15:49:49 +05302169 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2170 __func__, (unsigned long long)instr->addr,
2171 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002174 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
vimal singh20d8e242009-07-07 15:49:49 +05302175 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return -EINVAL;
2177 }
2178
2179 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002180 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
vimal singh20d8e242009-07-07 15:49:49 +05302181 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
2182 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -EINVAL;
2184 }
2185
2186 /* Do not allow erase past end of device */
2187 if ((instr->len + instr->addr) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302188 DEBUG(MTD_DEBUG_LEVEL0, "%s: Erase past end of device\n",
2189 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return -EINVAL;
2191 }
2192
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002193 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002196 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002199 page = (int)(instr->addr >> chip->page_shift);
2200 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002203 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002206 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /* Check, if it is write protected */
2209 if (nand_check_wp(mtd)) {
vimal singh20d8e242009-07-07 15:49:49 +05302210 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2211 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 instr->state = MTD_ERASE_FAILED;
2213 goto erase_exit;
2214 }
2215
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002216 /*
2217 * If BBT requires refresh, set the BBT page mask to see if the BBT
2218 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2219 * can not be matched. This is also done when the bbt is actually
2220 * erased to avoid recusrsive updates
2221 */
2222 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2223 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /* Loop through the pages */
2226 len = instr->len;
2227
2228 instr->state = MTD_ERASING;
2229
2230 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002231 /*
2232 * heck if we have a bad block, we do not erase bad blocks !
2233 */
2234 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2235 chip->page_shift, 0, allowbbt)) {
vimal singh20d8e242009-07-07 15:49:49 +05302236 printk(KERN_WARNING "%s: attempt to erase a bad block "
2237 "at page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 instr->state = MTD_ERASE_FAILED;
2239 goto erase_exit;
2240 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002241
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002242 /*
2243 * Invalidate the page cache, if we erase the block which
2244 * contains the current cached page
2245 */
2246 if (page <= chip->pagebuf && chip->pagebuf <
2247 (page + pages_per_block))
2248 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002250 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002251
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002252 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002254 /*
2255 * See if operation failed and additional status checks are
2256 * available
2257 */
2258 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2259 status = chip->errstat(mtd, chip, FL_ERASING,
2260 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002263 if (status & NAND_STATUS_FAIL) {
vimal singh20d8e242009-07-07 15:49:49 +05302264 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2265 "page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002267 instr->fail_addr =
2268 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto erase_exit;
2270 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002271
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002272 /*
2273 * If BBT requires refresh, set the BBT rewrite flag to the
2274 * page being erased
2275 */
2276 if (bbt_masked_page != 0xffffffff &&
2277 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002278 rewrite_bbt[chipnr] =
2279 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002282 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 page += pages_per_block;
2284
2285 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002286 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002288 chip->select_chip(mtd, -1);
2289 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002290
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002291 /*
2292 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2293 * page mask to see if this BBT should be rewritten
2294 */
2295 if (bbt_masked_page != 0xffffffff &&
2296 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2297 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2298 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 }
2301 instr->state = MTD_ERASE_DONE;
2302
David Woodhousee0c7d762006-05-13 18:07:53 +01002303 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 /* Deselect and wake up anyone waiting on the device */
2308 nand_release_device(mtd);
2309
David Woodhouse49defc02007-10-06 15:01:59 -04002310 /* Do call back function */
2311 if (!ret)
2312 mtd_erase_callback(instr);
2313
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002314 /*
2315 * If BBT requires refresh and erase was successful, rewrite any
2316 * selected bad block tables
2317 */
2318 if (bbt_masked_page == 0xffffffff || ret)
2319 return ret;
2320
2321 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2322 if (!rewrite_bbt[chipnr])
2323 continue;
2324 /* update the BBT for chip */
vimal singh20d8e242009-07-07 15:49:49 +05302325 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2326 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2327 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002328 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002329 }
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 /* Return more or less happy */
2332 return ret;
2333}
2334
2335/**
2336 * nand_sync - [MTD Interface] sync
2337 * @mtd: MTD device structure
2338 *
2339 * Sync is actually a wait for chip ready function
2340 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002341static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002343 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
vimal singh20d8e242009-07-07 15:49:49 +05302345 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002348 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002350 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002354 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002356 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002358static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002361 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002363
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002364 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
2367/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002368 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 * @mtd: MTD device structure
2370 * @ofs: offset relative to mtd start
2371 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002372static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002374 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 int ret;
2376
David Woodhousee0c7d762006-05-13 18:07:53 +01002377 if ((ret = nand_block_isbad(mtd, ofs))) {
2378 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (ret > 0)
2380 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002381 return ret;
2382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002384 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
2387/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002388 * nand_suspend - [MTD Interface] Suspend the NAND flash
2389 * @mtd: MTD device structure
2390 */
2391static int nand_suspend(struct mtd_info *mtd)
2392{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002393 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002394
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002395 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002396}
2397
2398/**
2399 * nand_resume - [MTD Interface] Resume the NAND flash
2400 * @mtd: MTD device structure
2401 */
2402static void nand_resume(struct mtd_info *mtd)
2403{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002404 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002405
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002406 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002407 nand_release_device(mtd);
2408 else
vimal singh20d8e242009-07-07 15:49:49 +05302409 printk(KERN_ERR "%s called for a chip which is not "
2410 "in suspended state\n", __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002411}
2412
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002413/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002414 * Set default functions
2415 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002416static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002419 if (!chip->chip_delay)
2420 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002423 if (chip->cmdfunc == NULL)
2424 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002427 if (chip->waitfunc == NULL)
2428 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002430 if (!chip->select_chip)
2431 chip->select_chip = nand_select_chip;
2432 if (!chip->read_byte)
2433 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2434 if (!chip->read_word)
2435 chip->read_word = nand_read_word;
2436 if (!chip->block_bad)
2437 chip->block_bad = nand_block_bad;
2438 if (!chip->block_markbad)
2439 chip->block_markbad = nand_default_block_markbad;
2440 if (!chip->write_buf)
2441 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2442 if (!chip->read_buf)
2443 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2444 if (!chip->verify_buf)
2445 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2446 if (!chip->scan_bbt)
2447 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002448
2449 if (!chip->controller) {
2450 chip->controller = &chip->hwcontrol;
2451 spin_lock_init(&chip->controller->lock);
2452 init_waitqueue_head(&chip->controller->wq);
2453 }
2454
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002455}
2456
2457/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002458 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002459 */
2460static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002461 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002462 int busw, int *maf_id)
2463{
2464 struct nand_flash_dev *type = NULL;
2465 int i, dev_id, maf_idx;
Ben Dooksed8165c2008-04-14 14:58:58 +01002466 int tmp_id, tmp_manf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002469 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Karl Beldanef89a882008-09-15 14:37:29 +02002471 /*
2472 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2473 * after power-up
2474 */
2475 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002478 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002481 *maf_id = chip->read_byte(mtd);
2482 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Ben Dooksed8165c2008-04-14 14:58:58 +01002484 /* Try again to make sure, as some systems the bus-hold or other
2485 * interface concerns can cause random data which looks like a
2486 * possibly credible NAND flash to appear. If the two results do
2487 * not match, ignore the device completely.
2488 */
2489
2490 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2491
2492 /* Read manufacturer and device IDs */
2493
2494 tmp_manf = chip->read_byte(mtd);
2495 tmp_id = chip->read_byte(mtd);
2496
2497 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2498 printk(KERN_INFO "%s: second ID read did not match "
2499 "%02x,%02x against %02x,%02x\n", __func__,
2500 *maf_id, dev_id, tmp_manf, tmp_id);
2501 return ERR_PTR(-ENODEV);
2502 }
2503
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002504 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002506 if (dev_id == nand_flash_ids[i].id) {
2507 type = &nand_flash_ids[i];
2508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 }
2511
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002512 if (!type)
2513 return ERR_PTR(-ENODEV);
2514
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002515 if (!mtd->name)
2516 mtd->name = type->name;
2517
Adrian Hunter69423d92008-12-10 13:37:21 +00002518 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002519
2520 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002521 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002522 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002523 /* The 3rd id byte holds MLC / multichip data */
2524 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002525 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002526 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002527 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002528 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002529 extid >>= 2;
2530 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002531 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002532 extid >>= 2;
2533 /* Calc blocksize. Blocksize is multiples of 64KiB */
2534 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2535 extid >>= 2;
2536 /* Get buswidth information */
2537 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2538
2539 } else {
2540 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002541 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002542 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002543 mtd->erasesize = type->erasesize;
2544 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002545 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002546 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002547 }
2548
2549 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002550 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002551 if (nand_manuf_ids[maf_idx].id == *maf_id)
2552 break;
2553 }
2554
2555 /*
2556 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002557 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002558 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002559 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002560 printk(KERN_INFO "NAND device: Manufacturer ID:"
2561 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2562 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2563 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002564 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002565 busw ? 16 : 8);
2566 return ERR_PTR(-EINVAL);
2567 }
2568
2569 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002570 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002571 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002572 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002573
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002574 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002575 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002576 if (chip->chipsize & 0xffffffff)
2577 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2578 else
2579 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002580
2581 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002582 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002583 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2584
2585 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002586 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002587 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002588
2589 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002590 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002591 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002592 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002593
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002594 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002595 * options for chips which are not having an extended id.
2596 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002597 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002598 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002599
2600 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002601 if (chip->options & NAND_4PAGE_ARRAY)
2602 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002603 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002604 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002605
2606 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002607 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2608 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002609
2610 printk(KERN_INFO "NAND device: Manufacturer ID:"
2611 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2612 nand_manuf_ids[maf_idx].name, type->name);
2613
2614 return type;
2615}
2616
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002617/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002618 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2619 * @mtd: MTD device structure
2620 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002621 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002622 * This is the first phase of the normal nand_scan() function. It
2623 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002624 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002625 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002626 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002627int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002628{
2629 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002630 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002631 struct nand_flash_dev *type;
2632
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002633 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002634 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002635 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002636 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002637
2638 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002639 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002640
2641 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002642 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002643 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002644 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
2646
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002647 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002648 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002649 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02002650 /* See comment in nand_get_flash_type for reset */
2651 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002653 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002655 if (nand_maf_id != chip->read_byte(mtd) ||
2656 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 break;
2658 }
2659 if (i > 1)
2660 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002663 chip->numchips = i;
2664 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
David Woodhouse3b85c322006-09-25 17:06:53 +01002666 return 0;
2667}
2668
2669
2670/**
2671 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2672 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01002673 *
2674 * This is the second phase of the normal nand_scan() function. It
2675 * fills out all the uninitialized function pointers with the defaults
2676 * and scans for a bad block table if appropriate.
2677 */
2678int nand_scan_tail(struct mtd_info *mtd)
2679{
2680 int i;
2681 struct nand_chip *chip = mtd->priv;
2682
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002683 if (!(chip->options & NAND_OWN_BUFFERS))
2684 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2685 if (!chip->buffers)
2686 return -ENOMEM;
2687
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002688 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002689 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002690
2691 /*
2692 * If no default placement scheme is given, select an appropriate one
2693 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002694 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002695 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002697 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 break;
2699 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002700 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 break;
2702 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002703 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002705 case 128:
2706 chip->ecc.layout = &nand_oob_128;
2707 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002709 printk(KERN_WARNING "No oob scheme defined for "
2710 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 BUG();
2712 }
2713 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002714
David Woodhouse956e9442006-09-25 17:12:39 +01002715 if (!chip->write_page)
2716 chip->write_page = nand_write_page;
2717
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002718 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002719 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2720 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002721 */
David Woodhouse956e9442006-09-25 17:12:39 +01002722
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002723 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002724 case NAND_ECC_HW_OOB_FIRST:
2725 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2726 if (!chip->ecc.calculate || !chip->ecc.correct ||
2727 !chip->ecc.hwctl) {
2728 printk(KERN_WARNING "No ECC functions supplied; "
2729 "Hardware ECC not possible\n");
2730 BUG();
2731 }
2732 if (!chip->ecc.read_page)
2733 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2734
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002735 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002736 /* Use standard hwecc read page function ? */
2737 if (!chip->ecc.read_page)
2738 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002739 if (!chip->ecc.write_page)
2740 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002741 if (!chip->ecc.read_page_raw)
2742 chip->ecc.read_page_raw = nand_read_page_raw;
2743 if (!chip->ecc.write_page_raw)
2744 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002745 if (!chip->ecc.read_oob)
2746 chip->ecc.read_oob = nand_read_oob_std;
2747 if (!chip->ecc.write_oob)
2748 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002749
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002750 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06002751 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2752 !chip->ecc.hwctl) &&
2753 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002754 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06002755 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002756 chip->ecc.write_page == nand_write_page_hwecc)) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002757 printk(KERN_WARNING "No ECC functions supplied; "
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002758 "Hardware ECC not possible\n");
2759 BUG();
2760 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002761 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002762 if (!chip->ecc.read_page)
2763 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002764 if (!chip->ecc.write_page)
2765 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08002766 if (!chip->ecc.read_page_raw)
2767 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2768 if (!chip->ecc.write_page_raw)
2769 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002770 if (!chip->ecc.read_oob)
2771 chip->ecc.read_oob = nand_read_oob_syndrome;
2772 if (!chip->ecc.write_oob)
2773 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002774
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002775 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002776 break;
2777 printk(KERN_WARNING "%d byte HW ECC not possible on "
2778 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002779 chip->ecc.size, mtd->writesize);
2780 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002782 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002783 chip->ecc.calculate = nand_calculate_ecc;
2784 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002785 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01002786 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002787 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002788 chip->ecc.read_page_raw = nand_read_page_raw;
2789 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002790 chip->ecc.read_oob = nand_read_oob_std;
2791 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00002792 if (!chip->ecc.size)
2793 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002794 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002796
2797 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002798 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2799 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002800 chip->ecc.read_page = nand_read_page_raw;
2801 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002802 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08002803 chip->ecc.read_page_raw = nand_read_page_raw;
2804 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002805 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002806 chip->ecc.size = mtd->writesize;
2807 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002811 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002812 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002813 BUG();
2814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002816 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002817 * The number of bytes available for a client to place data into
2818 * the out of band area
2819 */
2820 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07002821 for (i = 0; chip->ecc.layout->oobfree[i].length
2822 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002823 chip->ecc.layout->oobavail +=
2824 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002825 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002826
2827 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002828 * Set the number of read / write steps for one page depending on ECC
2829 * mode
2830 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002831 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2832 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002833 printk(KERN_WARNING "Invalid ecc parameters\n");
2834 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002836 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002837
Thomas Gleixner29072b92006-09-28 15:38:36 +02002838 /*
2839 * Allow subpage writes up to ecc.steps. Not possible for MLC
2840 * FLASH.
2841 */
2842 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2843 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2844 switch(chip->ecc.steps) {
2845 case 2:
2846 mtd->subpage_sft = 1;
2847 break;
2848 case 4:
2849 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01002850 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02002851 mtd->subpage_sft = 2;
2852 break;
2853 }
2854 }
2855 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2856
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002857 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002858 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002861 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002864 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 /* Fill in remaining MTD driver data */
2867 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002868 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 mtd->erase = nand_erase;
2870 mtd->point = NULL;
2871 mtd->unpoint = NULL;
2872 mtd->read = nand_read;
2873 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 mtd->read_oob = nand_read_oob;
2875 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 mtd->sync = nand_sync;
2877 mtd->lock = NULL;
2878 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002879 mtd->suspend = nand_suspend;
2880 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 mtd->block_isbad = nand_block_isbad;
2882 mtd->block_markbad = nand_block_markbad;
2883
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002884 /* propagate ecc.layout to mtd_info */
2885 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002887 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002888 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002892 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
2894
Rusty Russella6e6abd2009-03-31 13:05:31 -06002895/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01002896 test if this is a module _anyway_ -- they'd have to try _really_ hard
2897 to call us from in-kernel code if the core NAND support is modular. */
2898#ifdef MODULE
2899#define caller_is_module() (1)
2900#else
2901#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06002902 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01002903#endif
2904
2905/**
2906 * nand_scan - [NAND Interface] Scan for the NAND device
2907 * @mtd: MTD device structure
2908 * @maxchips: Number of chips to scan for
2909 *
2910 * This fills out all the uninitialized function pointers
2911 * with the defaults.
2912 * The flash ID is read and the mtd/chip structures are
2913 * filled with the appropriate values.
2914 * The mtd->owner field must be set to the module of the caller
2915 *
2916 */
2917int nand_scan(struct mtd_info *mtd, int maxchips)
2918{
2919 int ret;
2920
2921 /* Many callers got this wrong, so check for it for a while... */
2922 if (!mtd->owner && caller_is_module()) {
vimal singh20d8e242009-07-07 15:49:49 +05302923 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
2924 __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01002925 BUG();
2926 }
2927
2928 ret = nand_scan_ident(mtd, maxchips);
2929 if (!ret)
2930 ret = nand_scan_tail(mtd);
2931 return ret;
2932}
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002935 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 * @mtd: MTD device structure
2937*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002938void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002940 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942#ifdef CONFIG_MTD_PARTITIONS
2943 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002944 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945#endif
2946 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002947 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Jesper Juhlfa671642005-11-07 01:01:27 -08002949 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002950 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002951 if (!(chip->options & NAND_OWN_BUFFERS))
2952 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
2954
David Woodhousee0c7d762006-05-13 18:07:53 +01002955EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01002956EXPORT_SYMBOL_GPL(nand_scan_ident);
2957EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01002958EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002959
2960static int __init nand_base_init(void)
2961{
2962 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2963 return 0;
2964}
2965
2966static void __exit nand_base_exit(void)
2967{
2968 led_trigger_unregister_simple(nand_led_trigger);
2969}
2970
2971module_init(nand_base_init);
2972module_exit(nand_base_exit);
2973
David Woodhousee0c7d762006-05-13 18:07:53 +01002974MODULE_LICENSE("GPL");
2975MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2976MODULE_DESCRIPTION("Generic NAND flash driver code");