blob: d902370df3e0dddfb208c55066319f90d0fe56dd [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 Gleixnerace4dfe2006-05-24 12:07:37 +020085static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +020086 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020088static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
89 struct mtd_oob_ops *ops);
90
Thomas Gleixnerd470a972006-05-23 23:48:57 +020091/*
Joe Perches8e87d782008-02-03 17:22:34 +020092 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +020093 * compiled away when LED support is disabled.
94 */
95DEFINE_LED_TRIGGER(nand_led_trigger);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/**
98 * nand_release_device - [GENERIC] release chip
99 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000100 *
101 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100103static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200105 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200108 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100109
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200110 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200111 spin_lock(&chip->controller->lock);
112 chip->controller->active = NULL;
113 chip->state = FL_READY;
114 wake_up(&chip->controller->wq);
115 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/**
119 * nand_read_byte - [DEFAULT] read one byte from the chip
120 * @mtd: MTD device structure
121 *
122 * Default read function for 8bit buswith
123 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200124static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200126 struct nand_chip *chip = mtd->priv;
127 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
132 * @mtd: MTD device structure
133 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000134 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * endianess conversion
136 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200137static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200139 struct nand_chip *chip = mtd->priv;
140 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * nand_read_word - [DEFAULT] read one word from the chip
145 * @mtd: MTD device structure
146 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000147 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * endianess conversion
149 */
150static u16 nand_read_word(struct mtd_info *mtd)
151{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200152 struct nand_chip *chip = mtd->priv;
153 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * nand_select_chip - [DEFAULT] control CE line
158 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700159 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
161 * Default select function for 1 chip devices.
162 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200163static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200165 struct nand_chip *chip = mtd->priv;
166
167 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200169 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 break;
171 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
173
174 default:
175 BUG();
176 }
177}
178
179/**
180 * nand_write_buf - [DEFAULT] write buffer to chip
181 * @mtd: MTD device structure
182 * @buf: data buffer
183 * @len: number of bytes to write
184 *
185 * Default write function for 8bit buswith
186 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200187static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200190 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David Woodhousee0c7d762006-05-13 18:07:53 +0100192 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200193 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000197 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * @mtd: MTD device structure
199 * @buf: buffer to store date
200 * @len: number of bytes to read
201 *
202 * Default read function for 8bit buswith
203 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200204static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200207 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David Woodhousee0c7d762006-05-13 18:07:53 +0100209 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200210 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000214 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * @mtd: MTD device structure
216 * @buf: buffer containing the data to compare
217 * @len: number of bytes to compare
218 *
219 * Default verify function for 8bit buswith
220 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200221static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200224 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
David Woodhousee0c7d762006-05-13 18:07:53 +0100226 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200227 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230}
231
232/**
233 * nand_write_buf16 - [DEFAULT] write buffer to chip
234 * @mtd: MTD device structure
235 * @buf: data buffer
236 * @len: number of bytes to write
237 *
238 * Default write function for 16bit buswith
239 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200240static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200243 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 u16 *p = (u16 *) buf;
245 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000246
David Woodhousee0c7d762006-05-13 18:07:53 +0100247 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000253 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * @mtd: MTD device structure
255 * @buf: buffer to store date
256 * @len: number of bytes to read
257 *
258 * Default read function for 16bit buswith
259 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200260static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200263 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 u16 *p = (u16 *) buf;
265 len >>= 1;
266
David Woodhousee0c7d762006-05-13 18:07:53 +0100267 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200268 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000272 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * @mtd: MTD device structure
274 * @buf: buffer containing the data to compare
275 * @len: number of bytes to compare
276 *
277 * Default verify function for 16bit buswith
278 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200279static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200282 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u16 *p = (u16 *) buf;
284 len >>= 1;
285
David Woodhousee0c7d762006-05-13 18:07:53 +0100286 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200287 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -EFAULT;
289
290 return 0;
291}
292
293/**
294 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
295 * @mtd: MTD device structure
296 * @ofs: offset from device start
297 * @getchip: 0, if the chip is already selected
298 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000299 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
301static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
302{
303 int page, chipnr, res = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200304 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u16 bad;
306
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100307 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200310 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200312 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200315 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200318 if (chip->options & NAND_BUSWIDTH_16) {
319 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100320 page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200321 bad = cpu_to_le16(chip->read_word(mtd));
322 if (chip->badblockpos & 0x1)
Vitaly Wool49196f32005-11-02 16:54:46 +0000323 bad >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if ((bad & 0xFF) != 0xff)
325 res = 1;
326 } else {
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100327 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200328 if (chip->read_byte(mtd) != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 res = 1;
330 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000331
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200332 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return res;
336}
337
338/**
339 * nand_default_block_markbad - [DEFAULT] mark a block bad
340 * @mtd: MTD device structure
341 * @ofs: offset from device start
342 *
343 * This is the default implementation, which can be overridden by
344 * a hardware specific driver.
345*/
346static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
347{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200348 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200349 uint8_t buf[2] = { 0, 0 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200350 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400353 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200354 if (chip->bbt)
355 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200358 if (chip->options & NAND_USE_FLASH_BBT)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200359 ret = nand_update_bbt(mtd, ofs);
360 else {
361 /* We write two bytes, so we dont have to mess with 16 bit
362 * access
363 */
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300364 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200365 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200366 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200367 chip->ops.datbuf = NULL;
368 chip->ops.oobbuf = buf;
369 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000370
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200371 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300372 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200373 }
374 if (!ret)
375 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300376
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200377 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000380/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * nand_check_wp - [GENERIC] check if the chip is write protected
382 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000383 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100387static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200389 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200391 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
392 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/**
396 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
397 * @mtd: MTD device structure
398 * @ofs: offset from device start
399 * @getchip: 0, if the chip is already selected
400 * @allowbbt: 1, if its allowed to access the bbt area
401 *
402 * Check, if the block is bad. Either by reading the bad block table or
403 * calling of the scan function.
404 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200405static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
406 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200408 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000409
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200410 if (!chip->bbt)
411 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100414 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000417/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000418 * Wait for the ready pin, after a command
419 * The timeout is catched later.
420 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100421void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000422{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200423 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100424 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000425
Richard Purdie8fe833c2006-03-31 02:31:14 -0800426 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000427 /* wait until command is processed or timeout occures */
428 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200429 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800430 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700431 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000432 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800433 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000434}
David Woodhouse4b648b02006-09-25 17:05:24 +0100435EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/**
438 * nand_command - [DEFAULT] Send command to NAND device
439 * @mtd: MTD device structure
440 * @command: the command to be sent
441 * @column: the column address for this command, -1 if none
442 * @page_addr: the page address for this command, -1 if none
443 *
444 * Send command to NAND device. This function is used for small page
445 * devices (256/512 Bytes per page)
446 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200447static void nand_command(struct mtd_info *mtd, unsigned int command,
448 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200450 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200451 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
454 * Write out the command to the device.
455 */
456 if (command == NAND_CMD_SEQIN) {
457 int readcmd;
458
Joern Engel28318772006-05-22 23:18:05 +0200459 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200461 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 readcmd = NAND_CMD_READOOB;
463 } else if (column < 256) {
464 /* First 256 bytes --> READ0 */
465 readcmd = NAND_CMD_READ0;
466 } else {
467 column -= 256;
468 readcmd = NAND_CMD_READ1;
469 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200470 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200471 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200473 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200475 /*
476 * Address cycle, when necessary
477 */
478 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
479 /* Serially input address */
480 if (column != -1) {
481 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200482 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200483 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200484 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200485 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200487 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200488 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200489 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200490 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200491 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200492 if (chip->chipsize > (32 << 20))
493 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200494 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200495 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000496
497 /*
498 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 case NAND_CMD_PAGEPROG:
504 case NAND_CMD_ERASE1:
505 case NAND_CMD_ERASE2:
506 case NAND_CMD_SEQIN:
507 case NAND_CMD_STATUS:
508 return;
509
510 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200511 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200513 udelay(chip->chip_delay);
514 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200515 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200516 chip->cmd_ctrl(mtd,
517 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200518 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return;
520
David Woodhousee0c7d762006-05-13 18:07:53 +0100521 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000523 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * If we don't have access to the busy pin, we apply the given
525 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100526 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200527 if (!chip->dev_ready) {
528 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Apply this short delay always to ensure that we do wait tWB in
533 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100534 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000535
536 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/**
540 * nand_command_lp - [DEFAULT] Send command to NAND large page device
541 * @mtd: MTD device structure
542 * @command: the command to be sent
543 * @column: the column address for this command, -1 if none
544 * @page_addr: the page address for this command, -1 if none
545 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200546 * Send command to NAND device. This is the version for the new large page
547 * devices We dont have the separate regions as we have in the small page
548 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200550static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
551 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200553 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* Emulate NAND_CMD_READOOB */
556 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200557 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 command = NAND_CMD_READ0;
559 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000560
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200561 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200562 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200563 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200566 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* Serially input address */
569 if (column != -1) {
570 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200571 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200573 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200574 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200575 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200578 chip->cmd_ctrl(mtd, page_addr, ctrl);
579 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200580 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 if (chip->chipsize > (128 << 20))
583 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200584 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200587 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000588
589 /*
590 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000591 * status, sequential in, and deplete1 need no delay
592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 case NAND_CMD_CACHEDPROG:
596 case NAND_CMD_PAGEPROG:
597 case NAND_CMD_ERASE1:
598 case NAND_CMD_ERASE2:
599 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200600 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000602 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return;
604
David Woodhousee0c7d762006-05-13 18:07:53 +0100605 /*
606 * read error status commands require only a short delay
607 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000608 case NAND_CMD_STATUS_ERROR:
609 case NAND_CMD_STATUS_ERROR0:
610 case NAND_CMD_STATUS_ERROR1:
611 case NAND_CMD_STATUS_ERROR2:
612 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200613 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000614 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200617 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200619 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200620 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
621 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
622 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
623 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200624 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return;
626
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200627 case NAND_CMD_RNDOUT:
628 /* No ready / busy check necessary */
629 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
630 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
631 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
632 NAND_NCE | NAND_CTRL_CHANGE);
633 return;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200636 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
637 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
638 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
639 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000640
David Woodhousee0c7d762006-05-13 18:07:53 +0100641 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000643 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * If we don't have access to the busy pin, we apply the given
645 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100646 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200647 if (!chip->dev_ready) {
648 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* Apply this short delay always to ensure that we do wait tWB in
654 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100655 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000656
657 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/**
661 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700662 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000664 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 *
666 * Get the device and lock it for exclusive access
667 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200668static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200669nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200671 spinlock_t *lock = &chip->controller->lock;
672 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100673 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100674 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100675 spin_lock(lock);
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* Hardware controller shared among independend devices */
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200678 /* Hardware controller shared among independend devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200679 if (!chip->controller->active)
680 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200681
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200682 if (chip->controller->active == chip && chip->state == FL_READY) {
683 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100684 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100685 return 0;
686 }
687 if (new_state == FL_PM_SUSPENDED) {
688 spin_unlock(lock);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200689 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100690 }
691 set_current_state(TASK_UNINTERRUPTIBLE);
692 add_wait_queue(wq, &wait);
693 spin_unlock(lock);
694 schedule();
695 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto retry;
697}
698
699/**
700 * nand_wait - [DEFAULT] wait until the command is done
701 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700702 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *
704 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000705 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700707 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200708static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710
David Woodhousee0c7d762006-05-13 18:07:53 +0100711 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200712 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100715 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100717 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Richard Purdie8fe833c2006-03-31 02:31:14 -0800719 led_trigger_event(nand_led_trigger, LED_FULL);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* Apply this short delay always to ensure that we do wait tWB in
722 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100723 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200725 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
726 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000727 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200728 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000730 while (time_before(jiffies, timeo)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (chip->dev_ready) {
732 if (chip->dev_ready(mtd))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 } else {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200735 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
737 }
Thomas Gleixner20a6c212005-03-01 09:32:48 +0000738 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800740 led_trigger_event(nand_led_trigger, LED_OFF);
741
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200742 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return status;
744}
745
746/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200747 * nand_read_page_raw - [Intern] read raw page data without ecc
748 * @mtd: mtd info structure
749 * @chip: nand chip info structure
750 * @buf: buffer to store read data
David Brownell52ff49d2009-03-04 12:01:36 -0800751 *
752 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200753 */
754static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
755 uint8_t *buf)
756{
757 chip->read_buf(mtd, buf, mtd->writesize);
758 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
759 return 0;
760}
761
762/**
David Brownell52ff49d2009-03-04 12:01:36 -0800763 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
764 * @mtd: mtd info structure
765 * @chip: nand chip info structure
766 * @buf: buffer to store read data
767 *
768 * We need a special oob layout and handling even when OOB isn't used.
769 */
770static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
771 uint8_t *buf)
772{
773 int eccsize = chip->ecc.size;
774 int eccbytes = chip->ecc.bytes;
775 uint8_t *oob = chip->oob_poi;
776 int steps, size;
777
778 for (steps = chip->ecc.steps; steps > 0; steps--) {
779 chip->read_buf(mtd, buf, eccsize);
780 buf += eccsize;
781
782 if (chip->ecc.prepad) {
783 chip->read_buf(mtd, oob, chip->ecc.prepad);
784 oob += chip->ecc.prepad;
785 }
786
787 chip->read_buf(mtd, oob, eccbytes);
788 oob += eccbytes;
789
790 if (chip->ecc.postpad) {
791 chip->read_buf(mtd, oob, chip->ecc.postpad);
792 oob += chip->ecc.postpad;
793 }
794 }
795
796 size = mtd->oobsize - (oob - chip->oob_poi);
797 if (size)
798 chip->read_buf(mtd, oob, size);
799
800 return 0;
801}
802
803/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300804 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200805 * @mtd: mtd info structure
806 * @chip: nand chip info structure
807 * @buf: buffer to store read data
David A. Marlin068e3c02005-01-24 03:07:46 +0000808 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200809static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
810 uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200812 int i, eccsize = chip->ecc.size;
813 int eccbytes = chip->ecc.bytes;
814 int eccsteps = chip->ecc.steps;
815 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100816 uint8_t *ecc_calc = chip->buffers->ecccalc;
817 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100818 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200819
Thomas Gleixner90424de2007-04-05 11:44:05 +0200820 chip->ecc.read_page_raw(mtd, chip, buf);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200821
822 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
823 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
824
825 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200826 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200827
828 eccsteps = chip->ecc.steps;
829 p = buf;
830
831 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
832 int stat;
833
834 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700835 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200836 mtd->ecc_stats.failed++;
837 else
838 mtd->ecc_stats.corrected += stat;
839 }
840 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +0100841}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/**
Alexey Korolev3d459552008-05-15 17:23:18 +0100844 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
845 * @mtd: mtd info structure
846 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +0100847 * @data_offs: offset of requested data within the page
848 * @readlen: data length
849 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +0100850 */
851static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
852{
853 int start_step, end_step, num_steps;
854 uint32_t *eccpos = chip->ecc.layout->eccpos;
855 uint8_t *p;
856 int data_col_addr, i, gaps = 0;
857 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
858 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
859
860 /* Column address wihin the page aligned to ECC size (256bytes). */
861 start_step = data_offs / chip->ecc.size;
862 end_step = (data_offs + readlen - 1) / chip->ecc.size;
863 num_steps = end_step - start_step + 1;
864
865 /* Data size aligned to ECC ecc.size*/
866 datafrag_len = num_steps * chip->ecc.size;
867 eccfrag_len = num_steps * chip->ecc.bytes;
868
869 data_col_addr = start_step * chip->ecc.size;
870 /* If we read not a page aligned data */
871 if (data_col_addr != 0)
872 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
873
874 p = bufpoi + data_col_addr;
875 chip->read_buf(mtd, p, datafrag_len);
876
877 /* Calculate ECC */
878 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
879 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
880
881 /* The performance is faster if to position offsets
882 according to ecc.pos. Let make sure here that
883 there are no gaps in ecc positions */
884 for (i = 0; i < eccfrag_len - 1; i++) {
885 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
886 eccpos[i + start_step * chip->ecc.bytes + 1]) {
887 gaps = 1;
888 break;
889 }
890 }
891 if (gaps) {
892 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
893 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
894 } else {
895 /* send the command to read the particular ecc bytes */
896 /* take care about buswidth alignment in read_buf */
897 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
898 aligned_len = eccfrag_len;
899 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
900 aligned_len++;
901 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
902 aligned_len++;
903
904 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
905 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
906 }
907
908 for (i = 0; i < eccfrag_len; i++)
909 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
910
911 p = bufpoi + data_col_addr;
912 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
913 int stat;
914
915 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
916 if (stat == -1)
917 mtd->ecc_stats.failed++;
918 else
919 mtd->ecc_stats.corrected += stat;
920 }
921 return 0;
922}
923
924/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300925 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200926 * @mtd: mtd info structure
927 * @chip: nand chip info structure
928 * @buf: buffer to store read data
929 *
930 * Not for syndrome calculating ecc controllers which need a special oob layout
931 */
932static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
933 uint8_t *buf)
934{
935 int i, eccsize = chip->ecc.size;
936 int eccbytes = chip->ecc.bytes;
937 int eccsteps = chip->ecc.steps;
938 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100939 uint8_t *ecc_calc = chip->buffers->ecccalc;
940 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +0100941 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200942
943 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
944 chip->ecc.hwctl(mtd, NAND_ECC_READ);
945 chip->read_buf(mtd, p, eccsize);
946 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
947 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200948 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200949
950 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200951 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200952
953 eccsteps = chip->ecc.steps;
954 p = buf;
955
956 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
957 int stat;
958
959 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -0700960 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200961 mtd->ecc_stats.failed++;
962 else
963 mtd->ecc_stats.corrected += stat;
964 }
965 return 0;
966}
967
968/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +0300969 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200970 * @mtd: mtd info structure
971 * @chip: nand chip info structure
972 * @buf: buffer to store read data
973 *
974 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200975 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200976 */
977static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
978 uint8_t *buf)
979{
980 int i, eccsize = chip->ecc.size;
981 int eccbytes = chip->ecc.bytes;
982 int eccsteps = chip->ecc.steps;
983 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200984 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200985
986 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
987 int stat;
988
989 chip->ecc.hwctl(mtd, NAND_ECC_READ);
990 chip->read_buf(mtd, p, eccsize);
991
992 if (chip->ecc.prepad) {
993 chip->read_buf(mtd, oob, chip->ecc.prepad);
994 oob += chip->ecc.prepad;
995 }
996
997 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
998 chip->read_buf(mtd, oob, eccbytes);
999 stat = chip->ecc.correct(mtd, p, oob, NULL);
1000
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001001 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001002 mtd->ecc_stats.failed++;
1003 else
1004 mtd->ecc_stats.corrected += stat;
1005
1006 oob += eccbytes;
1007
1008 if (chip->ecc.postpad) {
1009 chip->read_buf(mtd, oob, chip->ecc.postpad);
1010 oob += chip->ecc.postpad;
1011 }
1012 }
1013
1014 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001015 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001016 if (i)
1017 chip->read_buf(mtd, oob, i);
1018
1019 return 0;
1020}
1021
1022/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001023 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1024 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001025 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001026 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001027 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001028 */
1029static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001030 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001031{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001032 switch(ops->mode) {
1033
1034 case MTD_OOB_PLACE:
1035 case MTD_OOB_RAW:
1036 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1037 return oob + len;
1038
1039 case MTD_OOB_AUTO: {
1040 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001041 uint32_t boffs = 0, roffs = ops->ooboffs;
1042 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001043
1044 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001045 /* Read request not from offset 0 ? */
1046 if (unlikely(roffs)) {
1047 if (roffs >= free->length) {
1048 roffs -= free->length;
1049 continue;
1050 }
1051 boffs = free->offset + roffs;
1052 bytes = min_t(size_t, len,
1053 (free->length - roffs));
1054 roffs = 0;
1055 } else {
1056 bytes = min_t(size_t, len, free->length);
1057 boffs = free->offset;
1058 }
1059 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001060 oob += bytes;
1061 }
1062 return oob;
1063 }
1064 default:
1065 BUG();
1066 }
1067 return NULL;
1068}
1069
1070/**
1071 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001072 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001073 * @mtd: MTD device structure
1074 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001075 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001076 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001077 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001078 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001079static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1080 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001081{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001082 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001083 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001084 struct mtd_ecc_stats stats;
1085 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1086 int sndcmd = 1;
1087 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001088 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001089 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001090 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001092 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001094 chipnr = (int)(from >> chip->chip_shift);
1095 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001097 realpage = (int)(from >> chip->page_shift);
1098 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001100 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001102 buf = ops->datbuf;
1103 oob = ops->oobbuf;
1104
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001105 while(1) {
1106 bytes = min(mtd->writesize - col, readlen);
1107 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001108
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001109 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001110 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001111 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001113 if (likely(sndcmd)) {
1114 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1115 sndcmd = 0;
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001118 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001119 if (unlikely(ops->mode == MTD_OOB_RAW))
1120 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
Alexey Korolev3d459552008-05-15 17:23:18 +01001121 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1122 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001123 else
1124 ret = chip->ecc.read_page(mtd, chip, bufpoi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001125 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001126 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001127
1128 /* Transfer not aligned data */
1129 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001130 if (!NAND_SUBPAGE_READ(chip) && !oob)
1131 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001132 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001134
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001135 buf += bytes;
1136
1137 if (unlikely(oob)) {
1138 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001139 if (ops->mode != MTD_OOB_RAW) {
1140 int toread = min(oobreadlen,
1141 chip->ecc.layout->oobavail);
1142 if (toread) {
1143 oob = nand_transfer_oob(chip,
1144 oob, ops, toread);
1145 oobreadlen -= toread;
1146 }
1147 } else
1148 buf = nand_transfer_oob(chip,
1149 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001150 }
1151
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001152 if (!(chip->options & NAND_NO_READRDY)) {
1153 /*
1154 * Apply delay or wait for ready/busy pin. Do
1155 * this before the AUTOINCR check, so no
1156 * problems arise if a chip which does auto
1157 * increment is marked as NOAUTOINCR by the
1158 * board driver.
1159 */
1160 if (!chip->dev_ready)
1161 udelay(chip->chip_delay);
1162 else
1163 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001165 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001166 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001167 buf += bytes;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001170 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001171
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001172 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /* For subsequent reads align to page boundary. */
1176 col = 0;
1177 /* Increment page address */
1178 realpage++;
1179
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001180 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* Check, if we cross a chip boundary */
1182 if (!page) {
1183 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001184 chip->select_chip(mtd, -1);
1185 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001187
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001188 /* Check, if the chip supports auto page increment
1189 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001190 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001191 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001192 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001195 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001196 if (oob)
1197 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001199 if (ret)
1200 return ret;
1201
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001202 if (mtd->ecc_stats.failed - stats.failed)
1203 return -EBADMSG;
1204
1205 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001206}
1207
1208/**
1209 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1210 * @mtd: MTD device structure
1211 * @from: offset to read from
1212 * @len: number of bytes to read
1213 * @retlen: pointer to variable to store the number of read bytes
1214 * @buf: the databuffer to put data
1215 *
1216 * Get hold of the chip and call nand_do_read
1217 */
1218static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1219 size_t *retlen, uint8_t *buf)
1220{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001221 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001222 int ret;
1223
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001224 /* Do not allow reads past end of device */
1225 if ((from + len) > mtd->size)
1226 return -EINVAL;
1227 if (!len)
1228 return 0;
1229
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001230 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001231
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001232 chip->ops.len = len;
1233 chip->ops.datbuf = buf;
1234 chip->ops.oobbuf = NULL;
1235
1236 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001237
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001238 *retlen = chip->ops.retlen;
1239
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001240 nand_release_device(mtd);
1241
1242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001246 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1247 * @mtd: mtd info structure
1248 * @chip: nand chip info structure
1249 * @page: page number to read
1250 * @sndcmd: flag whether to issue read command or not
1251 */
1252static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1253 int page, int sndcmd)
1254{
1255 if (sndcmd) {
1256 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1257 sndcmd = 0;
1258 }
1259 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1260 return sndcmd;
1261}
1262
1263/**
1264 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1265 * with syndromes
1266 * @mtd: mtd info structure
1267 * @chip: nand chip info structure
1268 * @page: page number to read
1269 * @sndcmd: flag whether to issue read command or not
1270 */
1271static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1272 int page, int sndcmd)
1273{
1274 uint8_t *buf = chip->oob_poi;
1275 int length = mtd->oobsize;
1276 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1277 int eccsize = chip->ecc.size;
1278 uint8_t *bufpoi = buf;
1279 int i, toread, sndrnd = 0, pos;
1280
1281 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1282 for (i = 0; i < chip->ecc.steps; i++) {
1283 if (sndrnd) {
1284 pos = eccsize + i * (eccsize + chunk);
1285 if (mtd->writesize > 512)
1286 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1287 else
1288 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1289 } else
1290 sndrnd = 1;
1291 toread = min_t(int, length, chunk);
1292 chip->read_buf(mtd, bufpoi, toread);
1293 bufpoi += toread;
1294 length -= toread;
1295 }
1296 if (length > 0)
1297 chip->read_buf(mtd, bufpoi, length);
1298
1299 return 1;
1300}
1301
1302/**
1303 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1304 * @mtd: mtd info structure
1305 * @chip: nand chip info structure
1306 * @page: page number to write
1307 */
1308static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1309 int page)
1310{
1311 int status = 0;
1312 const uint8_t *buf = chip->oob_poi;
1313 int length = mtd->oobsize;
1314
1315 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1316 chip->write_buf(mtd, buf, length);
1317 /* Send command to program the OOB data */
1318 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1319
1320 status = chip->waitfunc(mtd, chip);
1321
Savin Zlobec0d420f92006-06-21 11:51:20 +02001322 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001323}
1324
1325/**
1326 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1327 * with syndrome - only for large page flash !
1328 * @mtd: mtd info structure
1329 * @chip: nand chip info structure
1330 * @page: page number to write
1331 */
1332static int nand_write_oob_syndrome(struct mtd_info *mtd,
1333 struct nand_chip *chip, int page)
1334{
1335 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1336 int eccsize = chip->ecc.size, length = mtd->oobsize;
1337 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1338 const uint8_t *bufpoi = chip->oob_poi;
1339
1340 /*
1341 * data-ecc-data-ecc ... ecc-oob
1342 * or
1343 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1344 */
1345 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1346 pos = steps * (eccsize + chunk);
1347 steps = 0;
1348 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001349 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001350
1351 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1352 for (i = 0; i < steps; i++) {
1353 if (sndcmd) {
1354 if (mtd->writesize <= 512) {
1355 uint32_t fill = 0xFFFFFFFF;
1356
1357 len = eccsize;
1358 while (len > 0) {
1359 int num = min_t(int, len, 4);
1360 chip->write_buf(mtd, (uint8_t *)&fill,
1361 num);
1362 len -= num;
1363 }
1364 } else {
1365 pos = eccsize + i * (eccsize + chunk);
1366 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1367 }
1368 } else
1369 sndcmd = 1;
1370 len = min_t(int, length, chunk);
1371 chip->write_buf(mtd, bufpoi, len);
1372 bufpoi += len;
1373 length -= len;
1374 }
1375 if (length > 0)
1376 chip->write_buf(mtd, bufpoi, length);
1377
1378 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1379 status = chip->waitfunc(mtd, chip);
1380
1381 return status & NAND_STATUS_FAIL ? -EIO : 0;
1382}
1383
1384/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001385 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * @mtd: MTD device structure
1387 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001388 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *
1390 * NAND read out-of-band data from the spare area
1391 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001392static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1393 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001395 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001396 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001397 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001398 int readlen = ops->ooblen;
1399 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001400 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Andrew Morton7e9a0bb2006-05-30 09:06:41 +01001402 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1403 (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Adrian Hunter03736152007-01-31 17:58:29 +02001405 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001406 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001407 else
1408 len = mtd->oobsize;
1409
1410 if (unlikely(ops->ooboffs >= len)) {
1411 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1412 "Attempt to start read outside oob\n");
1413 return -EINVAL;
1414 }
1415
1416 /* Do not allow reads past end of device */
1417 if (unlikely(from >= mtd->size ||
1418 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1419 (from >> chip->page_shift)) * len)) {
1420 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1421 "Attempt read beyond end of device\n");
1422 return -EINVAL;
1423 }
Vitaly Wool70145682006-11-03 18:20:38 +03001424
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001425 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001426 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001428 /* Shift to get page */
1429 realpage = (int)(from >> chip->page_shift);
1430 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001432 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001433 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001434
1435 len = min(len, readlen);
1436 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001437
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001438 if (!(chip->options & NAND_NO_READRDY)) {
1439 /*
1440 * Apply delay or wait for ready/busy pin. Do this
1441 * before the AUTOINCR check, so no problems arise if a
1442 * chip which does auto increment is marked as
1443 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001444 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001445 if (!chip->dev_ready)
1446 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001447 else
1448 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001450
Vitaly Wool70145682006-11-03 18:20:38 +03001451 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001452 if (!readlen)
1453 break;
1454
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001455 /* Increment page address */
1456 realpage++;
1457
1458 page = realpage & chip->pagemask;
1459 /* Check, if we cross a chip boundary */
1460 if (!page) {
1461 chipnr++;
1462 chip->select_chip(mtd, -1);
1463 chip->select_chip(mtd, chipnr);
1464 }
1465
1466 /* Check, if the chip supports auto page increment
1467 * or if we have hit a block boundary.
1468 */
1469 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1470 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
Vitaly Wool70145682006-11-03 18:20:38 +03001473 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
1475}
1476
1477/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001478 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001481 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001483 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001485static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1486 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001488 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001489 int ret = -ENOTSUPP;
1490
1491 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001494 if (ops->datbuf && (from + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001495 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001496 "Attempt read beyond end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return -EINVAL;
1498 }
1499
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001500 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001502 switch(ops->mode) {
1503 case MTD_OOB_PLACE:
1504 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001505 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001506 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001507
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001508 default:
1509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001512 if (!ops->datbuf)
1513 ret = nand_do_read_oob(mtd, from, ops);
1514 else
1515 ret = nand_do_read_ops(mtd, from, ops);
1516
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001517 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001519 return ret;
1520}
1521
1522
1523/**
1524 * nand_write_page_raw - [Intern] raw page write function
1525 * @mtd: mtd info structure
1526 * @chip: nand chip info structure
1527 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001528 *
1529 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001530 */
1531static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1532 const uint8_t *buf)
1533{
1534 chip->write_buf(mtd, buf, mtd->writesize);
1535 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001538/**
David Brownell52ff49d2009-03-04 12:01:36 -08001539 * nand_write_page_raw_syndrome - [Intern] raw page write function
1540 * @mtd: mtd info structure
1541 * @chip: nand chip info structure
1542 * @buf: data buffer
1543 *
1544 * We need a special oob layout and handling even when ECC isn't checked.
1545 */
1546static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1547 const uint8_t *buf)
1548{
1549 int eccsize = chip->ecc.size;
1550 int eccbytes = chip->ecc.bytes;
1551 uint8_t *oob = chip->oob_poi;
1552 int steps, size;
1553
1554 for (steps = chip->ecc.steps; steps > 0; steps--) {
1555 chip->write_buf(mtd, buf, eccsize);
1556 buf += eccsize;
1557
1558 if (chip->ecc.prepad) {
1559 chip->write_buf(mtd, oob, chip->ecc.prepad);
1560 oob += chip->ecc.prepad;
1561 }
1562
1563 chip->read_buf(mtd, oob, eccbytes);
1564 oob += eccbytes;
1565
1566 if (chip->ecc.postpad) {
1567 chip->write_buf(mtd, oob, chip->ecc.postpad);
1568 oob += chip->ecc.postpad;
1569 }
1570 }
1571
1572 size = mtd->oobsize - (oob - chip->oob_poi);
1573 if (size)
1574 chip->write_buf(mtd, oob, size);
1575}
1576/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001577 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001578 * @mtd: mtd info structure
1579 * @chip: nand chip info structure
1580 * @buf: data buffer
1581 */
1582static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1583 const uint8_t *buf)
1584{
1585 int i, eccsize = chip->ecc.size;
1586 int eccbytes = chip->ecc.bytes;
1587 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001588 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001589 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001590 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001591
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001592 /* Software ecc calculation */
1593 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1594 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001595
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001596 for (i = 0; i < chip->ecc.total; i++)
1597 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001598
Thomas Gleixner90424de2007-04-05 11:44:05 +02001599 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001600}
1601
1602/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001603 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001604 * @mtd: mtd info structure
1605 * @chip: nand chip info structure
1606 * @buf: data buffer
1607 */
1608static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1609 const uint8_t *buf)
1610{
1611 int i, eccsize = chip->ecc.size;
1612 int eccbytes = chip->ecc.bytes;
1613 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001614 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001615 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001616 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001617
1618 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1619 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001620 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001621 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1622 }
1623
1624 for (i = 0; i < chip->ecc.total; i++)
1625 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1626
1627 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1628}
1629
1630/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001631 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001632 * @mtd: mtd info structure
1633 * @chip: nand chip info structure
1634 * @buf: data buffer
1635 *
1636 * The hw generator calculates the error syndrome automatically. Therefor
1637 * we need a special oob layout and handling.
1638 */
1639static void nand_write_page_syndrome(struct mtd_info *mtd,
1640 struct nand_chip *chip, const uint8_t *buf)
1641{
1642 int i, eccsize = chip->ecc.size;
1643 int eccbytes = chip->ecc.bytes;
1644 int eccsteps = chip->ecc.steps;
1645 const uint8_t *p = buf;
1646 uint8_t *oob = chip->oob_poi;
1647
1648 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1649
1650 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1651 chip->write_buf(mtd, p, eccsize);
1652
1653 if (chip->ecc.prepad) {
1654 chip->write_buf(mtd, oob, chip->ecc.prepad);
1655 oob += chip->ecc.prepad;
1656 }
1657
1658 chip->ecc.calculate(mtd, p, oob);
1659 chip->write_buf(mtd, oob, eccbytes);
1660 oob += eccbytes;
1661
1662 if (chip->ecc.postpad) {
1663 chip->write_buf(mtd, oob, chip->ecc.postpad);
1664 oob += chip->ecc.postpad;
1665 }
1666 }
1667
1668 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001669 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001670 if (i)
1671 chip->write_buf(mtd, oob, i);
1672}
1673
1674/**
David Woodhouse956e9442006-09-25 17:12:39 +01001675 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001676 * @mtd: MTD device structure
1677 * @chip: NAND chip descriptor
1678 * @buf: the data to write
1679 * @page: page number to write
1680 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02001681 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001682 */
1683static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01001684 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001685{
1686 int status;
1687
1688 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1689
David Woodhouse956e9442006-09-25 17:12:39 +01001690 if (unlikely(raw))
1691 chip->ecc.write_page_raw(mtd, chip, buf);
1692 else
1693 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001694
1695 /*
1696 * Cached progamming disabled for now, Not sure if its worth the
1697 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1698 */
1699 cached = 0;
1700
1701 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1702
1703 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001704 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001705 /*
1706 * See if operation failed and additional status checks are
1707 * available
1708 */
1709 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1710 status = chip->errstat(mtd, chip, FL_WRITING, status,
1711 page);
1712
1713 if (status & NAND_STATUS_FAIL)
1714 return -EIO;
1715 } else {
1716 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001717 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001718 }
1719
1720#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1721 /* Send command to read back the data */
1722 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1723
1724 if (chip->verify_buf(mtd, buf, mtd->writesize))
1725 return -EIO;
1726#endif
1727 return 0;
1728}
1729
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001730/**
1731 * nand_fill_oob - [Internal] Transfer client buffer to oob
1732 * @chip: nand chip structure
1733 * @oob: oob data buffer
1734 * @ops: oob ops structure
1735 */
1736static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1737 struct mtd_oob_ops *ops)
1738{
1739 size_t len = ops->ooblen;
1740
1741 switch(ops->mode) {
1742
1743 case MTD_OOB_PLACE:
1744 case MTD_OOB_RAW:
1745 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1746 return oob + len;
1747
1748 case MTD_OOB_AUTO: {
1749 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001750 uint32_t boffs = 0, woffs = ops->ooboffs;
1751 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001752
1753 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001754 /* Write request not from offset 0 ? */
1755 if (unlikely(woffs)) {
1756 if (woffs >= free->length) {
1757 woffs -= free->length;
1758 continue;
1759 }
1760 boffs = free->offset + woffs;
1761 bytes = min_t(size_t, len,
1762 (free->length - woffs));
1763 woffs = 0;
1764 } else {
1765 bytes = min_t(size_t, len, free->length);
1766 boffs = free->offset;
1767 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001768 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001769 oob += bytes;
1770 }
1771 return oob;
1772 }
1773 default:
1774 BUG();
1775 }
1776 return NULL;
1777}
1778
Thomas Gleixner29072b92006-09-28 15:38:36 +02001779#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001780
1781/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001782 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001783 * @mtd: MTD device structure
1784 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001785 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001786 *
1787 * NAND write with ECC
1788 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001789static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1790 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001791{
Thomas Gleixner29072b92006-09-28 15:38:36 +02001792 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001793 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001794 uint32_t writelen = ops->len;
1795 uint8_t *oob = ops->oobbuf;
1796 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001797 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001798
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001799 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001800 if (!writelen)
1801 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001802
1803 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001804 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001805 printk(KERN_NOTICE "nand_write: "
1806 "Attempt to write not page aligned data\n");
1807 return -EINVAL;
1808 }
1809
Thomas Gleixner29072b92006-09-28 15:38:36 +02001810 column = to & (mtd->writesize - 1);
1811 subpage = column || (writelen & (mtd->writesize - 1));
1812
1813 if (subpage && oob)
1814 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001815
Thomas Gleixner6a930962006-06-28 00:11:45 +02001816 chipnr = (int)(to >> chip->chip_shift);
1817 chip->select_chip(mtd, chipnr);
1818
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001819 /* Check, if it is write protected */
1820 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001821 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001822
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001823 realpage = (int)(to >> chip->page_shift);
1824 page = realpage & chip->pagemask;
1825 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1826
1827 /* Invalidate the page cache, when we write to the cached page */
1828 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001829 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001830 chip->pagebuf = -1;
1831
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01001832 /* If we're not given explicit OOB data, let it be 0xFF */
1833 if (likely(!oob))
1834 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001835
1836 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02001837 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001838 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02001839 uint8_t *wbuf = buf;
1840
1841 /* Partial page write ? */
1842 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1843 cached = 0;
1844 bytes = min_t(int, bytes - column, (int) writelen);
1845 chip->pagebuf = -1;
1846 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1847 memcpy(&chip->buffers->databuf[column], buf, bytes);
1848 wbuf = chip->buffers->databuf;
1849 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001850
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001851 if (unlikely(oob))
1852 oob = nand_fill_oob(chip, oob, ops);
1853
Thomas Gleixner29072b92006-09-28 15:38:36 +02001854 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01001855 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001856 if (ret)
1857 break;
1858
1859 writelen -= bytes;
1860 if (!writelen)
1861 break;
1862
Thomas Gleixner29072b92006-09-28 15:38:36 +02001863 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001864 buf += bytes;
1865 realpage++;
1866
1867 page = realpage & chip->pagemask;
1868 /* Check, if we cross a chip boundary */
1869 if (!page) {
1870 chipnr++;
1871 chip->select_chip(mtd, -1);
1872 chip->select_chip(mtd, chipnr);
1873 }
1874 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001875
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001876 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03001877 if (unlikely(oob))
1878 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001879 return ret;
1880}
1881
1882/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001883 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 * @mtd: MTD device structure
1885 * @to: offset to write to
1886 * @len: number of bytes to write
1887 * @retlen: pointer to variable to store the number of written bytes
1888 * @buf: the data to write
1889 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001890 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001892static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001893 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001895 struct nand_chip *chip = mtd->priv;
1896 int ret;
1897
1898 /* Do not allow reads past end of device */
1899 if ((to + len) > mtd->size)
1900 return -EINVAL;
1901 if (!len)
1902 return 0;
1903
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001904 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001905
1906 chip->ops.len = len;
1907 chip->ops.datbuf = (uint8_t *)buf;
1908 chip->ops.oobbuf = NULL;
1909
1910 ret = nand_do_write_ops(mtd, to, &chip->ops);
1911
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001912 *retlen = chip->ops.retlen;
1913
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001914 nand_release_device(mtd);
1915
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001916 return ret;
1917}
1918
1919/**
1920 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1921 * @mtd: MTD device structure
1922 * @to: offset to write to
1923 * @ops: oob operation description structure
1924 *
1925 * NAND write out-of-band
1926 */
1927static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1928 struct mtd_oob_ops *ops)
1929{
Adrian Hunter03736152007-01-31 17:58:29 +02001930 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001931 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001933 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
Vitaly Wool70145682006-11-03 18:20:38 +03001934 (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Adrian Hunter03736152007-01-31 17:58:29 +02001936 if (ops->mode == MTD_OOB_AUTO)
1937 len = chip->ecc.layout->oobavail;
1938 else
1939 len = mtd->oobsize;
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02001942 if ((ops->ooboffs + ops->ooblen) > len) {
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001943 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1944 "Attempt to write past end of page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return -EINVAL;
1946 }
1947
Adrian Hunter03736152007-01-31 17:58:29 +02001948 if (unlikely(ops->ooboffs >= len)) {
1949 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1950 "Attempt to start write outside oob\n");
1951 return -EINVAL;
1952 }
1953
1954 /* Do not allow reads past end of device */
1955 if (unlikely(to >= mtd->size ||
1956 ops->ooboffs + ops->ooblen >
1957 ((mtd->size >> chip->page_shift) -
1958 (to >> chip->page_shift)) * len)) {
1959 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1960 "Attempt write beyond end of device\n");
1961 return -EINVAL;
1962 }
1963
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001964 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001965 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001967 /* Shift to get page */
1968 page = (int)(to >> chip->page_shift);
1969
1970 /*
1971 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1972 * of my DiskOnChip 2000 test units) will clear the whole data page too
1973 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1974 * it in the doc2000 driver in August 1999. dwmw2.
1975 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001976 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /* Check, if it is write protected */
1979 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001980 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001983 if (page == chip->pagebuf)
1984 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001986 memset(chip->oob_poi, 0xff, mtd->oobsize);
1987 nand_fill_oob(chip, ops->oobbuf, ops);
1988 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1989 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001990
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001991 if (status)
1992 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Vitaly Wool70145682006-11-03 18:20:38 +03001994 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001996 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001999/**
2000 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2001 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002002 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002003 * @ops: oob operation description structure
2004 */
2005static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2006 struct mtd_oob_ops *ops)
2007{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002008 struct nand_chip *chip = mtd->priv;
2009 int ret = -ENOTSUPP;
2010
2011 ops->retlen = 0;
2012
2013 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002014 if (ops->datbuf && (to + ops->len) > mtd->size) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002015 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
2016 "Attempt read beyond end of device\n");
2017 return -EINVAL;
2018 }
2019
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002020 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002021
2022 switch(ops->mode) {
2023 case MTD_OOB_PLACE:
2024 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002025 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002026 break;
2027
2028 default:
2029 goto out;
2030 }
2031
2032 if (!ops->datbuf)
2033 ret = nand_do_write_oob(mtd, to, ops);
2034 else
2035 ret = nand_do_write_ops(mtd, to, ops);
2036
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002037 out:
2038 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return ret;
2040}
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2044 * @mtd: MTD device structure
2045 * @page: the page address of the block which will be erased
2046 *
2047 * Standard erase command for NAND chips
2048 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002049static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002051 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002053 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2054 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057/**
2058 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2059 * @mtd: MTD device structure
2060 * @page: the page address of the block which will be erased
2061 *
2062 * AND multi block erase command function
2063 * Erase 4 consecutive blocks
2064 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002065static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002067 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002069 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2070 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2071 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2072 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2073 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
2076/**
2077 * nand_erase - [MTD Interface] erase block(s)
2078 * @mtd: MTD device structure
2079 * @instr: erase instruction
2080 *
2081 * Erase one ore more blocks
2082 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002083static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
David Woodhousee0c7d762006-05-13 18:07:53 +01002085 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002087
David A. Marlin30f464b2005-01-17 18:35:25 +00002088#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002090 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 * @mtd: MTD device structure
2092 * @instr: erase instruction
2093 * @allowbbt: allow erasing the bbt area
2094 *
2095 * Erase one ore more blocks
2096 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002097int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2098 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Adrian Hunter69423d92008-12-10 13:37:21 +00002100 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002101 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002102 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002103 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002104 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Adrian Hunter69423d92008-12-10 13:37:21 +00002106 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%012llx, len = %llu\n",
2107 (unsigned long long)instr->addr, (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 /* Start address must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002110 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002111 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return -EINVAL;
2113 }
2114
2115 /* Length must align on block boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002116 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
2117 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2118 "Length not block aligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return -EINVAL;
2120 }
2121
2122 /* Do not allow erase past end of device */
2123 if ((instr->len + instr->addr) > mtd->size) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002124 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2125 "Erase past end of device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return -EINVAL;
2127 }
2128
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002129 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002132 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002135 page = (int)(instr->addr >> chip->page_shift);
2136 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002139 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002142 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /* Check, if it is write protected */
2145 if (nand_check_wp(mtd)) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002146 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2147 "Device is write protected!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 instr->state = MTD_ERASE_FAILED;
2149 goto erase_exit;
2150 }
2151
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002152 /*
2153 * If BBT requires refresh, set the BBT page mask to see if the BBT
2154 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2155 * can not be matched. This is also done when the bbt is actually
2156 * erased to avoid recusrsive updates
2157 */
2158 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2159 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /* Loop through the pages */
2162 len = instr->len;
2163
2164 instr->state = MTD_ERASING;
2165
2166 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002167 /*
2168 * heck if we have a bad block, we do not erase bad blocks !
2169 */
2170 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2171 chip->page_shift, 0, allowbbt)) {
2172 printk(KERN_WARNING "nand_erase: attempt to erase a "
2173 "bad block at page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 instr->state = MTD_ERASE_FAILED;
2175 goto erase_exit;
2176 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002177
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002178 /*
2179 * Invalidate the page cache, if we erase the block which
2180 * contains the current cached page
2181 */
2182 if (page <= chip->pagebuf && chip->pagebuf <
2183 (page + pages_per_block))
2184 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002186 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002187
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002188 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002190 /*
2191 * See if operation failed and additional status checks are
2192 * available
2193 */
2194 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2195 status = chip->errstat(mtd, chip, FL_ERASING,
2196 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002199 if (status & NAND_STATUS_FAIL) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002200 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2201 "Failed erase, page 0x%08x\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002203 instr->fail_addr =
2204 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto erase_exit;
2206 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002207
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002208 /*
2209 * If BBT requires refresh, set the BBT rewrite flag to the
2210 * page being erased
2211 */
2212 if (bbt_masked_page != 0xffffffff &&
2213 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002214 rewrite_bbt[chipnr] =
2215 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002218 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 page += pages_per_block;
2220
2221 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002222 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002224 chip->select_chip(mtd, -1);
2225 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002226
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002227 /*
2228 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2229 * page mask to see if this BBT should be rewritten
2230 */
2231 if (bbt_masked_page != 0xffffffff &&
2232 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2233 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2234 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 }
2237 instr->state = MTD_ERASE_DONE;
2238
David Woodhousee0c7d762006-05-13 18:07:53 +01002239 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 /* Deselect and wake up anyone waiting on the device */
2244 nand_release_device(mtd);
2245
David Woodhouse49defc02007-10-06 15:01:59 -04002246 /* Do call back function */
2247 if (!ret)
2248 mtd_erase_callback(instr);
2249
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002250 /*
2251 * If BBT requires refresh and erase was successful, rewrite any
2252 * selected bad block tables
2253 */
2254 if (bbt_masked_page == 0xffffffff || ret)
2255 return ret;
2256
2257 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2258 if (!rewrite_bbt[chipnr])
2259 continue;
2260 /* update the BBT for chip */
2261 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
Adrian Hunter69423d92008-12-10 13:37:21 +00002262 "(%d:0x%0llx 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002263 chip->bbt_td->pages[chipnr]);
2264 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002265 }
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* Return more or less happy */
2268 return ret;
2269}
2270
2271/**
2272 * nand_sync - [MTD Interface] sync
2273 * @mtd: MTD device structure
2274 *
2275 * Sync is actually a wait for chip ready function
2276 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002277static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002279 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
David Woodhousee0c7d762006-05-13 18:07:53 +01002281 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002284 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002286 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002290 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002292 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002294static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002297 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002299
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002300 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002304 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 * @mtd: MTD device structure
2306 * @ofs: offset relative to mtd start
2307 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002308static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002310 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 int ret;
2312
David Woodhousee0c7d762006-05-13 18:07:53 +01002313 if ((ret = nand_block_isbad(mtd, ofs))) {
2314 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (ret > 0)
2316 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002317 return ret;
2318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002320 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
2323/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002324 * nand_suspend - [MTD Interface] Suspend the NAND flash
2325 * @mtd: MTD device structure
2326 */
2327static int nand_suspend(struct mtd_info *mtd)
2328{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002329 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002330
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002331 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002332}
2333
2334/**
2335 * nand_resume - [MTD Interface] Resume the NAND flash
2336 * @mtd: MTD device structure
2337 */
2338static void nand_resume(struct mtd_info *mtd)
2339{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002340 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002341
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002342 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002343 nand_release_device(mtd);
2344 else
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02002345 printk(KERN_ERR "nand_resume() called for a chip which is not "
2346 "in suspended state\n");
Vitaly Wool962034f2005-09-15 14:58:53 +01002347}
2348
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002349/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002350 * Set default functions
2351 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002352static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002355 if (!chip->chip_delay)
2356 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002359 if (chip->cmdfunc == NULL)
2360 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002363 if (chip->waitfunc == NULL)
2364 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002366 if (!chip->select_chip)
2367 chip->select_chip = nand_select_chip;
2368 if (!chip->read_byte)
2369 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2370 if (!chip->read_word)
2371 chip->read_word = nand_read_word;
2372 if (!chip->block_bad)
2373 chip->block_bad = nand_block_bad;
2374 if (!chip->block_markbad)
2375 chip->block_markbad = nand_default_block_markbad;
2376 if (!chip->write_buf)
2377 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2378 if (!chip->read_buf)
2379 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2380 if (!chip->verify_buf)
2381 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2382 if (!chip->scan_bbt)
2383 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002384
2385 if (!chip->controller) {
2386 chip->controller = &chip->hwcontrol;
2387 spin_lock_init(&chip->controller->lock);
2388 init_waitqueue_head(&chip->controller->wq);
2389 }
2390
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002391}
2392
2393/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002394 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002395 */
2396static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002397 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002398 int busw, int *maf_id)
2399{
2400 struct nand_flash_dev *type = NULL;
2401 int i, dev_id, maf_idx;
Ben Dooksed8165c2008-04-14 14:58:58 +01002402 int tmp_id, tmp_manf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002405 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Karl Beldanef89a882008-09-15 14:37:29 +02002407 /*
2408 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2409 * after power-up
2410 */
2411 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002414 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002417 *maf_id = chip->read_byte(mtd);
2418 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Ben Dooksed8165c2008-04-14 14:58:58 +01002420 /* Try again to make sure, as some systems the bus-hold or other
2421 * interface concerns can cause random data which looks like a
2422 * possibly credible NAND flash to appear. If the two results do
2423 * not match, ignore the device completely.
2424 */
2425
2426 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2427
2428 /* Read manufacturer and device IDs */
2429
2430 tmp_manf = chip->read_byte(mtd);
2431 tmp_id = chip->read_byte(mtd);
2432
2433 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2434 printk(KERN_INFO "%s: second ID read did not match "
2435 "%02x,%02x against %02x,%02x\n", __func__,
2436 *maf_id, dev_id, tmp_manf, tmp_id);
2437 return ERR_PTR(-ENODEV);
2438 }
2439
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002440 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002442 if (dev_id == nand_flash_ids[i].id) {
2443 type = &nand_flash_ids[i];
2444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002448 if (!type)
2449 return ERR_PTR(-ENODEV);
2450
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002451 if (!mtd->name)
2452 mtd->name = type->name;
2453
Adrian Hunter69423d92008-12-10 13:37:21 +00002454 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002455
2456 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002457 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002458 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002459 /* The 3rd id byte holds MLC / multichip data */
2460 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002461 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002462 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002463 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002464 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002465 extid >>= 2;
2466 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002467 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002468 extid >>= 2;
2469 /* Calc blocksize. Blocksize is multiples of 64KiB */
2470 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2471 extid >>= 2;
2472 /* Get buswidth information */
2473 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2474
2475 } else {
2476 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002477 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002478 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002479 mtd->erasesize = type->erasesize;
2480 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002481 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002482 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002483 }
2484
2485 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002486 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002487 if (nand_manuf_ids[maf_idx].id == *maf_id)
2488 break;
2489 }
2490
2491 /*
2492 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002493 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002494 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002495 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002496 printk(KERN_INFO "NAND device: Manufacturer ID:"
2497 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2498 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2499 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002500 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002501 busw ? 16 : 8);
2502 return ERR_PTR(-EINVAL);
2503 }
2504
2505 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002506 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002507 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002508 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002509
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002510 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002511 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002512 if (chip->chipsize & 0xffffffff)
2513 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2514 else
2515 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002516
2517 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002518 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002519 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2520
2521 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002522 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002523 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002524
2525 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002526 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002527 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002528 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002529
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002530 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002531 * options for chips which are not having an extended id.
2532 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002533 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002534 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002535
2536 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002537 if (chip->options & NAND_4PAGE_ARRAY)
2538 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002539 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002540 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002541
2542 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002543 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2544 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002545
2546 printk(KERN_INFO "NAND device: Manufacturer ID:"
2547 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2548 nand_manuf_ids[maf_idx].name, type->name);
2549
2550 return type;
2551}
2552
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002553/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002554 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2555 * @mtd: MTD device structure
2556 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002557 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002558 * This is the first phase of the normal nand_scan() function. It
2559 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002560 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002561 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002562 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002563int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002564{
2565 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002566 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002567 struct nand_flash_dev *type;
2568
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002569 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002570 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002571 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002572 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002573
2574 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002575 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002576
2577 if (IS_ERR(type)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01002578 printk(KERN_WARNING "No NAND device found!!!\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002579 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002580 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002583 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002584 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002585 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02002586 /* See comment in nand_get_flash_type for reset */
2587 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002589 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002591 if (nand_maf_id != chip->read_byte(mtd) ||
2592 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 break;
2594 }
2595 if (i > 1)
2596 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002599 chip->numchips = i;
2600 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
David Woodhouse3b85c322006-09-25 17:06:53 +01002602 return 0;
2603}
2604
2605
2606/**
2607 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2608 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01002609 *
2610 * This is the second phase of the normal nand_scan() function. It
2611 * fills out all the uninitialized function pointers with the defaults
2612 * and scans for a bad block table if appropriate.
2613 */
2614int nand_scan_tail(struct mtd_info *mtd)
2615{
2616 int i;
2617 struct nand_chip *chip = mtd->priv;
2618
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002619 if (!(chip->options & NAND_OWN_BUFFERS))
2620 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2621 if (!chip->buffers)
2622 return -ENOMEM;
2623
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002624 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002625 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002626
2627 /*
2628 * If no default placement scheme is given, select an appropriate one
2629 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002630 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002631 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002633 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 break;
2635 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002636 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 break;
2638 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002639 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 break;
2641 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002642 printk(KERN_WARNING "No oob scheme defined for "
2643 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 BUG();
2645 }
2646 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002647
David Woodhouse956e9442006-09-25 17:12:39 +01002648 if (!chip->write_page)
2649 chip->write_page = nand_write_page;
2650
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002651 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002652 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2653 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01002654 */
David Woodhouse956e9442006-09-25 17:12:39 +01002655
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002656 switch (chip->ecc.mode) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002657 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002658 /* Use standard hwecc read page function ? */
2659 if (!chip->ecc.read_page)
2660 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002661 if (!chip->ecc.write_page)
2662 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002663 if (!chip->ecc.read_page_raw)
2664 chip->ecc.read_page_raw = nand_read_page_raw;
2665 if (!chip->ecc.write_page_raw)
2666 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002667 if (!chip->ecc.read_oob)
2668 chip->ecc.read_oob = nand_read_oob_std;
2669 if (!chip->ecc.write_oob)
2670 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002671
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002672 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06002673 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2674 !chip->ecc.hwctl) &&
2675 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002676 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06002677 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06002678 chip->ecc.write_page == nand_write_page_hwecc)) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002679 printk(KERN_WARNING "No ECC functions supplied, "
2680 "Hardware ECC not possible\n");
2681 BUG();
2682 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002683 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002684 if (!chip->ecc.read_page)
2685 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002686 if (!chip->ecc.write_page)
2687 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08002688 if (!chip->ecc.read_page_raw)
2689 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2690 if (!chip->ecc.write_page_raw)
2691 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002692 if (!chip->ecc.read_oob)
2693 chip->ecc.read_oob = nand_read_oob_syndrome;
2694 if (!chip->ecc.write_oob)
2695 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002696
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002697 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002698 break;
2699 printk(KERN_WARNING "%d byte HW ECC not possible on "
2700 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002701 chip->ecc.size, mtd->writesize);
2702 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002704 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002705 chip->ecc.calculate = nand_calculate_ecc;
2706 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002707 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01002708 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002709 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08002710 chip->ecc.read_page_raw = nand_read_page_raw;
2711 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002712 chip->ecc.read_oob = nand_read_oob_std;
2713 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002714 chip->ecc.size = 256;
2715 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002717
2718 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002719 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2720 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002721 chip->ecc.read_page = nand_read_page_raw;
2722 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002723 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08002724 chip->ecc.read_page_raw = nand_read_page_raw;
2725 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002726 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002727 chip->ecc.size = mtd->writesize;
2728 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 break;
David Woodhouse956e9442006-09-25 17:12:39 +01002730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002732 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002733 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002734 BUG();
2735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002737 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002738 * The number of bytes available for a client to place data into
2739 * the out of band area
2740 */
2741 chip->ecc.layout->oobavail = 0;
2742 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2743 chip->ecc.layout->oobavail +=
2744 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002745 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002746
2747 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002748 * Set the number of read / write steps for one page depending on ECC
2749 * mode
2750 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002751 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2752 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002753 printk(KERN_WARNING "Invalid ecc parameters\n");
2754 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002756 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002757
Thomas Gleixner29072b92006-09-28 15:38:36 +02002758 /*
2759 * Allow subpage writes up to ecc.steps. Not possible for MLC
2760 * FLASH.
2761 */
2762 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2763 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2764 switch(chip->ecc.steps) {
2765 case 2:
2766 mtd->subpage_sft = 1;
2767 break;
2768 case 4:
2769 case 8:
2770 mtd->subpage_sft = 2;
2771 break;
2772 }
2773 }
2774 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2775
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02002776 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002777 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002780 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002783 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 /* Fill in remaining MTD driver data */
2786 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002787 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 mtd->erase = nand_erase;
2789 mtd->point = NULL;
2790 mtd->unpoint = NULL;
2791 mtd->read = nand_read;
2792 mtd->write = nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 mtd->read_oob = nand_read_oob;
2794 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 mtd->sync = nand_sync;
2796 mtd->lock = NULL;
2797 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01002798 mtd->suspend = nand_suspend;
2799 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 mtd->block_isbad = nand_block_isbad;
2801 mtd->block_markbad = nand_block_markbad;
2802
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002803 /* propagate ecc.layout to mtd_info */
2804 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002806 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002807 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00002808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002811 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
David Woodhouse3b85c322006-09-25 17:06:53 +01002814/* module_text_address() isn't exported, and it's mostly a pointless
2815 test if this is a module _anyway_ -- they'd have to try _really_ hard
2816 to call us from in-kernel code if the core NAND support is modular. */
2817#ifdef MODULE
2818#define caller_is_module() (1)
2819#else
2820#define caller_is_module() \
2821 module_text_address((unsigned long)__builtin_return_address(0))
2822#endif
2823
2824/**
2825 * nand_scan - [NAND Interface] Scan for the NAND device
2826 * @mtd: MTD device structure
2827 * @maxchips: Number of chips to scan for
2828 *
2829 * This fills out all the uninitialized function pointers
2830 * with the defaults.
2831 * The flash ID is read and the mtd/chip structures are
2832 * filled with the appropriate values.
2833 * The mtd->owner field must be set to the module of the caller
2834 *
2835 */
2836int nand_scan(struct mtd_info *mtd, int maxchips)
2837{
2838 int ret;
2839
2840 /* Many callers got this wrong, so check for it for a while... */
2841 if (!mtd->owner && caller_is_module()) {
2842 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2843 BUG();
2844 }
2845
2846 ret = nand_scan_ident(mtd, maxchips);
2847 if (!ret)
2848 ret = nand_scan_tail(mtd);
2849 return ret;
2850}
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002853 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 * @mtd: MTD device structure
2855*/
David Woodhousee0c7d762006-05-13 18:07:53 +01002856void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002858 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860#ifdef CONFIG_MTD_PARTITIONS
2861 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01002862 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863#endif
2864 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01002865 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Jesper Juhlfa671642005-11-07 01:01:27 -08002867 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002868 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002869 if (!(chip->options & NAND_OWN_BUFFERS))
2870 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
David Woodhousee0c7d762006-05-13 18:07:53 +01002873EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01002874EXPORT_SYMBOL_GPL(nand_scan_ident);
2875EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01002876EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08002877
2878static int __init nand_base_init(void)
2879{
2880 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2881 return 0;
2882}
2883
2884static void __exit nand_base_exit(void)
2885{
2886 led_trigger_unregister_simple(nand_led_trigger);
2887}
2888
2889module_init(nand_base_init);
2890module_exit(nand_base_exit);
2891
David Woodhousee0c7d762006-05-13 18:07:53 +01002892MODULE_LICENSE("GPL");
2893MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2894MODULE_DESCRIPTION("Generic NAND flash driver code");