blob: cf032a622fb178c63f7fa705394468ff663554e7 [file] [log] [blame]
Wolfgang Denk932394a2005-08-17 12:55:25 +02001/*
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.
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02008 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02009 * Additional technical information is available on
Scott Woodc45912d2008-10-24 16:20:43 -050010 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020011 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juulcfa460a2007-10-31 13:53:06 +010013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk932394a2005-08-17 12:55:25 +020014 *
William Juulcfa460a2007-10-31 13:53:06 +010015 * Credits:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020016 * David Woodhouse for adding multichip support
17 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
William Juulcfa460a2007-10-31 13:53:06 +010021 * TODO:
Wolfgang Denk932394a2005-08-17 12:55:25 +020022 * 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.
Scott Woodc45912d2008-10-24 16:20:43 -050027 * BBT table is not serialized, has to be fixed
Wolfgang Denk932394a2005-08-17 12:55:25 +020028 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020029 * 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
35/* XXX U-BOOT XXX */
36#if 0
William Juulcfa460a2007-10-31 13:53:06 +010037#include <linux/module.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020038#include <linux/delay.h>
39#include <linux/errno.h>
William Juulcfa460a2007-10-31 13:53:06 +010040#include <linux/err.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020041#include <linux/sched.h>
42#include <linux/slab.h>
43#include <linux/types.h>
44#include <linux/mtd/mtd.h>
45#include <linux/mtd/nand.h>
46#include <linux/mtd/nand_ecc.h>
47#include <linux/mtd/compatmac.h>
48#include <linux/interrupt.h>
49#include <linux/bitops.h>
William Juulcfa460a2007-10-31 13:53:06 +010050#include <linux/leds.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020051#include <asm/io.h>
52
53#ifdef CONFIG_MTD_PARTITIONS
54#include <linux/mtd/partitions.h>
55#endif
56
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020057#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020058
59#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010060
William Juulcfa460a2007-10-31 13:53:06 +010061#define ENOTSUPP 524 /* Operation is not supported */
62
Wolfgang Denk932394a2005-08-17 12:55:25 +020063#include <malloc.h>
64#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010065#include <linux/err.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020066#include <linux/mtd/compat.h>
67#include <linux/mtd/mtd.h>
68#include <linux/mtd/nand.h>
69#include <linux/mtd/nand_ecc.h>
70
Stefan Roese10bb62d2009-04-24 15:58:33 +020071#ifdef CONFIG_MTD_PARTITIONS
72#include <linux/mtd/partitions.h>
73#endif
74
Wolfgang Denk932394a2005-08-17 12:55:25 +020075#include <asm/io.h>
76#include <asm/errno.h>
77
78#ifdef CONFIG_JFFS2_NAND
79#include <jffs2/jffs2.h>
80#endif
81
Peter Tyser8da60122009-02-04 13:47:22 -060082/*
83 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
84 * a flash. NAND flash is initialized prior to interrupts so standard timers
85 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
86 * which is greater than (max NAND reset time / NAND status read time).
87 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
88 */
89#ifndef CONFIG_SYS_NAND_RESET_CNT
90#define CONFIG_SYS_NAND_RESET_CNT 200000
91#endif
92
Wolfgang Denk932394a2005-08-17 12:55:25 +020093/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010094static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020095 .eccbytes = 3,
96 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010097 .oobfree = {
98 {.offset = 3,
99 .length = 2},
100 {.offset = 6,
101 .length = 2}}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200102};
103
William Juulcfa460a2007-10-31 13:53:06 +0100104static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200105 .eccbytes = 6,
106 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +0100107 .oobfree = {
108 {.offset = 8,
109 . length = 8}}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200110};
111
William Juulcfa460a2007-10-31 13:53:06 +0100112static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200113 .eccbytes = 24,
114 .eccpos = {
William Juulcfa460a2007-10-31 13:53:06 +0100115 40, 41, 42, 43, 44, 45, 46, 47,
116 48, 49, 50, 51, 52, 53, 54, 55,
117 56, 57, 58, 59, 60, 61, 62, 63},
118 .oobfree = {
119 {.offset = 2,
120 .length = 38}}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200121};
122
William Juulcfa460a2007-10-31 13:53:06 +0100123static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200124 .eccbytes = 48,
125 .eccpos = {
William Juulcfa460a2007-10-31 13:53:06 +0100126 80, 81, 82, 83, 84, 85, 86, 87,
127 88, 89, 90, 91, 92, 93, 94, 95,
128 96, 97, 98, 99, 100, 101, 102, 103,
129 104, 105, 106, 107, 108, 109, 110, 111,
130 112, 113, 114, 115, 116, 117, 118, 119,
131 120, 121, 122, 123, 124, 125, 126, 127},
132 .oobfree = {
133 {.offset = 2,
134 .length = 78}}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200135};
136
William Juulcfa460a2007-10-31 13:53:06 +0100137
138static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
139 int new_state);
140
141static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
142 struct mtd_oob_ops *ops);
143
144static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200145
Wolfgang Denk932394a2005-08-17 12:55:25 +0200146/*
Scott Woodc45912d2008-10-24 16:20:43 -0500147 * For devices which display every fart in the system on a separate LED. Is
William Juulcfa460a2007-10-31 13:53:06 +0100148 * compiled away when LED support is disabled.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200149 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200150/* XXX U-BOOT XXX */
151#if 0
William Juulcfa460a2007-10-31 13:53:06 +0100152DEFINE_LED_TRIGGER(nand_led_trigger);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200153#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200154
155/**
156 * nand_release_device - [GENERIC] release chip
157 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200158 *
159 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk932394a2005-08-17 12:55:25 +0200160 */
161/* XXX U-BOOT XXX */
162#if 0
William Juulcfa460a2007-10-31 13:53:06 +0100163static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200164{
William Juulcfa460a2007-10-31 13:53:06 +0100165 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200166
167 /* De-select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100168 chip->select_chip(mtd, -1);
169
170 /* Release the controller and the chip */
171 spin_lock(&chip->controller->lock);
172 chip->controller->active = NULL;
173 chip->state = FL_READY;
174 wake_up(&chip->controller->wq);
175 spin_unlock(&chip->controller->lock);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200176}
177#else
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100178static void nand_release_device (struct mtd_info *mtd)
179{
180 struct nand_chip *this = mtd->priv;
181 this->select_chip(mtd, -1); /* De-select the NAND device */
182}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200183#endif
184
185/**
186 * nand_read_byte - [DEFAULT] read one byte from the chip
187 * @mtd: MTD device structure
188 *
189 * Default read function for 8bit buswith
190 */
William Juulcfa460a2007-10-31 13:53:06 +0100191static uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200192{
William Juulcfa460a2007-10-31 13:53:06 +0100193 struct nand_chip *chip = mtd->priv;
194 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200195}
196
197/**
198 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
199 * @mtd: MTD device structure
200 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200201 * Default read function for 16bit buswith with
Wolfgang Denk932394a2005-08-17 12:55:25 +0200202 * endianess conversion
203 */
William Juulcfa460a2007-10-31 13:53:06 +0100204static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200205{
William Juulcfa460a2007-10-31 13:53:06 +0100206 struct nand_chip *chip = mtd->priv;
207 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200208}
209
210/**
211 * nand_read_word - [DEFAULT] read one word from the chip
212 * @mtd: MTD device structure
213 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200214 * Default read function for 16bit buswith without
Wolfgang Denk932394a2005-08-17 12:55:25 +0200215 * endianess conversion
216 */
217static u16 nand_read_word(struct mtd_info *mtd)
218{
William Juulcfa460a2007-10-31 13:53:06 +0100219 struct nand_chip *chip = mtd->priv;
220 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200221}
222
223/**
224 * nand_select_chip - [DEFAULT] control CE line
225 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +0100226 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200227 *
228 * Default select function for 1 chip devices.
229 */
William Juulcfa460a2007-10-31 13:53:06 +0100230static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200231{
William Juulcfa460a2007-10-31 13:53:06 +0100232 struct nand_chip *chip = mtd->priv;
233
234 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200235 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100236 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200237 break;
238 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200239 break;
240
241 default:
242 BUG();
243 }
244}
245
246/**
247 * nand_write_buf - [DEFAULT] write buffer to chip
248 * @mtd: MTD device structure
249 * @buf: data buffer
250 * @len: number of bytes to write
251 *
252 * Default write function for 8bit buswith
253 */
William Juulcfa460a2007-10-31 13:53:06 +0100254static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200255{
256 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100257 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200258
William Juulcfa460a2007-10-31 13:53:06 +0100259 for (i = 0; i < len; i++)
260 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200261}
262
263/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200264 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200265 * @mtd: MTD device structure
266 * @buf: buffer to store date
267 * @len: number of bytes to read
268 *
269 * Default read function for 8bit buswith
270 */
William Juulcfa460a2007-10-31 13:53:06 +0100271static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200272{
273 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100274 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200275
William Juulcfa460a2007-10-31 13:53:06 +0100276 for (i = 0; i < len; i++)
277 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200278}
279
280/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200281 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200282 * @mtd: MTD device structure
283 * @buf: buffer containing the data to compare
284 * @len: number of bytes to compare
285 *
286 * Default verify function for 8bit buswith
287 */
William Juulcfa460a2007-10-31 13:53:06 +0100288static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200289{
290 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100291 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200292
William Juulcfa460a2007-10-31 13:53:06 +0100293 for (i = 0; i < len; i++)
294 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200295 return -EFAULT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200296 return 0;
297}
298
299/**
300 * nand_write_buf16 - [DEFAULT] write buffer to chip
301 * @mtd: MTD device structure
302 * @buf: data buffer
303 * @len: number of bytes to write
304 *
305 * Default write function for 16bit buswith
306 */
William Juulcfa460a2007-10-31 13:53:06 +0100307static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200308{
309 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100310 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200311 u16 *p = (u16 *) buf;
312 len >>= 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200313
William Juulcfa460a2007-10-31 13:53:06 +0100314 for (i = 0; i < len; i++)
315 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200316
Wolfgang Denk932394a2005-08-17 12:55:25 +0200317}
318
319/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200320 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200321 * @mtd: MTD device structure
322 * @buf: buffer to store date
323 * @len: number of bytes to read
324 *
325 * Default read function for 16bit buswith
326 */
William Juulcfa460a2007-10-31 13:53:06 +0100327static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200328{
329 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100330 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200331 u16 *p = (u16 *) buf;
332 len >>= 1;
333
William Juulcfa460a2007-10-31 13:53:06 +0100334 for (i = 0; i < len; i++)
335 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200336}
337
338/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200339 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200340 * @mtd: MTD device structure
341 * @buf: buffer containing the data to compare
342 * @len: number of bytes to compare
343 *
344 * Default verify function for 16bit buswith
345 */
William Juulcfa460a2007-10-31 13:53:06 +0100346static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200347{
348 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100349 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200350 u16 *p = (u16 *) buf;
351 len >>= 1;
352
William Juulcfa460a2007-10-31 13:53:06 +0100353 for (i = 0; i < len; i++)
354 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200355 return -EFAULT;
356
357 return 0;
358}
359
360/**
361 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
362 * @mtd: MTD device structure
363 * @ofs: offset from device start
364 * @getchip: 0, if the chip is already selected
365 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200366 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200367 */
368static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
369{
370 int page, chipnr, res = 0;
William Juulcfa460a2007-10-31 13:53:06 +0100371 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200372 u16 bad;
373
William Juulcfa460a2007-10-31 13:53:06 +0100374 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200375
Wolfgang Denk932394a2005-08-17 12:55:25 +0200376 if (getchip) {
William Juulcfa460a2007-10-31 13:53:06 +0100377 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200378
William Juulcfa460a2007-10-31 13:53:06 +0100379 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200380
381 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100382 chip->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200383 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200384
William Juulcfa460a2007-10-31 13:53:06 +0100385 if (chip->options & NAND_BUSWIDTH_16) {
386 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
387 page);
388 bad = cpu_to_le16(chip->read_word(mtd));
389 if (chip->badblockpos & 0x1)
390 bad >>= 8;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200391 if ((bad & 0xFF) != 0xff)
392 res = 1;
393 } else {
William Juulcfa460a2007-10-31 13:53:06 +0100394 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
395 if (chip->read_byte(mtd) != 0xff)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200396 res = 1;
397 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200398
William Juulcfa460a2007-10-31 13:53:06 +0100399 if (getchip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200400 nand_release_device(mtd);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200401
Wolfgang Denk932394a2005-08-17 12:55:25 +0200402 return res;
403}
404
405/**
406 * nand_default_block_markbad - [DEFAULT] mark a block bad
407 * @mtd: MTD device structure
408 * @ofs: offset from device start
409 *
410 * This is the default implementation, which can be overridden by
411 * a hardware specific driver.
412*/
413static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
414{
William Juulcfa460a2007-10-31 13:53:06 +0100415 struct nand_chip *chip = mtd->priv;
416 uint8_t buf[2] = { 0, 0 };
417 int block, ret;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200418
Wolfgang Denk932394a2005-08-17 12:55:25 +0200419 /* Get block number */
William Juulcfa460a2007-10-31 13:53:06 +0100420 block = (int)(ofs >> chip->bbt_erase_shift);
421 if (chip->bbt)
422 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200423
424 /* Do we have a flash based bad block table ? */
William Juulcfa460a2007-10-31 13:53:06 +0100425 if (chip->options & NAND_USE_FLASH_BBT)
426 ret = nand_update_bbt(mtd, ofs);
427 else {
428 /* We write two bytes, so we dont have to mess with 16 bit
429 * access
430 */
Scott Woodc45912d2008-10-24 16:20:43 -0500431 nand_get_device(chip, mtd, FL_WRITING);
William Juulcfa460a2007-10-31 13:53:06 +0100432 ofs += mtd->oobsize;
433 chip->ops.len = chip->ops.ooblen = 2;
434 chip->ops.datbuf = NULL;
435 chip->ops.oobbuf = buf;
436 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200437
William Juulcfa460a2007-10-31 13:53:06 +0100438 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Scott Woodc45912d2008-10-24 16:20:43 -0500439 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100440 }
441 if (!ret)
442 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500443
William Juulcfa460a2007-10-31 13:53:06 +0100444 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200445}
446
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200447/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200448 * nand_check_wp - [GENERIC] check if the chip is write protected
449 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200450 * Check, if the device is write protected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200451 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200452 * The function expects, that the device is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200453 */
William Juulcfa460a2007-10-31 13:53:06 +0100454static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200455{
William Juulcfa460a2007-10-31 13:53:06 +0100456 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200457 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100458 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
459 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200460}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100461
Wolfgang Denk932394a2005-08-17 12:55:25 +0200462/**
463 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
464 * @mtd: MTD device structure
465 * @ofs: offset from device start
466 * @getchip: 0, if the chip is already selected
467 * @allowbbt: 1, if its allowed to access the bbt area
468 *
469 * Check, if the block is bad. Either by reading the bad block table or
470 * calling of the scan function.
471 */
William Juulcfa460a2007-10-31 13:53:06 +0100472static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
473 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200474{
William Juulcfa460a2007-10-31 13:53:06 +0100475 struct nand_chip *chip = mtd->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200476
Ilya Yanok13f0fd92008-06-30 15:34:40 +0200477 if (!(chip->options & NAND_BBT_SCANNED)) {
Ilya Yanok13f0fd92008-06-30 15:34:40 +0200478 chip->options |= NAND_BBT_SCANNED;
Scott Woodff49ea82008-12-16 14:24:16 -0600479 chip->scan_bbt(mtd);
Ilya Yanok13f0fd92008-06-30 15:34:40 +0200480 }
481
William Juulcfa460a2007-10-31 13:53:06 +0100482 if (!chip->bbt)
483 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200484
Wolfgang Denk932394a2005-08-17 12:55:25 +0200485 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100486 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200487}
488
William Juulcfa460a2007-10-31 13:53:06 +0100489/*
490 * Wait for the ready pin, after a command
491 * The timeout is catched later.
492 */
493/* XXX U-BOOT XXX */
494#if 0
495void nand_wait_ready(struct mtd_info *mtd)
496{
497 struct nand_chip *chip = mtd->priv;
498 unsigned long timeo = jiffies + 2;
499
500 led_trigger_event(nand_led_trigger, LED_FULL);
501 /* wait until command is processed or timeout occures */
502 do {
503 if (chip->dev_ready(mtd))
504 break;
505 touch_softlockup_watchdog();
506 } while (time_before(jiffies, timeo));
507 led_trigger_event(nand_led_trigger, LED_OFF);
508}
509EXPORT_SYMBOL_GPL(nand_wait_ready);
510#else
511void nand_wait_ready(struct mtd_info *mtd)
512{
513 struct nand_chip *chip = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200514 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Stefan Roese12072262008-01-05 16:43:25 +0100515
516 reset_timer();
517
518 /* wait until command is processed or timeout occures */
519 while (get_timer(0) < timeo) {
520 if (chip->dev_ready)
521 if (chip->dev_ready(mtd))
522 break;
523 }
William Juulcfa460a2007-10-31 13:53:06 +0100524}
525#endif
526
Wolfgang Denk932394a2005-08-17 12:55:25 +0200527/**
528 * nand_command - [DEFAULT] Send command to NAND device
529 * @mtd: MTD device structure
530 * @command: the command to be sent
531 * @column: the column address for this command, -1 if none
532 * @page_addr: the page address for this command, -1 if none
533 *
534 * Send command to NAND device. This function is used for small page
535 * devices (256/512 Bytes per page)
536 */
William Juulcfa460a2007-10-31 13:53:06 +0100537static void nand_command(struct mtd_info *mtd, unsigned int command,
538 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200539{
William Juulcfa460a2007-10-31 13:53:06 +0100540 register struct nand_chip *chip = mtd->priv;
541 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyser8da60122009-02-04 13:47:22 -0600542 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200543
Wolfgang Denk932394a2005-08-17 12:55:25 +0200544 /*
545 * Write out the command to the device.
546 */
547 if (command == NAND_CMD_SEQIN) {
548 int readcmd;
549
William Juulcfa460a2007-10-31 13:53:06 +0100550 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200551 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100552 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200553 readcmd = NAND_CMD_READOOB;
554 } else if (column < 256) {
555 /* First 256 bytes --> READ0 */
556 readcmd = NAND_CMD_READ0;
557 } else {
558 column -= 256;
559 readcmd = NAND_CMD_READ1;
560 }
William Juulcfa460a2007-10-31 13:53:06 +0100561 chip->cmd_ctrl(mtd, readcmd, ctrl);
562 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200563 }
William Juulcfa460a2007-10-31 13:53:06 +0100564 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200565
William Juulcfa460a2007-10-31 13:53:06 +0100566 /*
567 * Address cycle, when necessary
568 */
569 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
570 /* Serially input address */
571 if (column != -1) {
572 /* Adjust columns for 16 bit buswidth */
573 if (chip->options & NAND_BUSWIDTH_16)
574 column >>= 1;
575 chip->cmd_ctrl(mtd, column, ctrl);
576 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200577 }
William Juulcfa460a2007-10-31 13:53:06 +0100578 if (page_addr != -1) {
579 chip->cmd_ctrl(mtd, page_addr, ctrl);
580 ctrl &= ~NAND_CTRL_CHANGE;
581 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
582 /* One more address cycle for devices > 32MiB */
583 if (chip->chipsize > (32 << 20))
584 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
585 }
586 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200587
588 /*
589 * program and erase have their own busy handlers
Wolfgang Denk932394a2005-08-17 12:55:25 +0200590 * status and sequential in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100591 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200592 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200593
Wolfgang Denk932394a2005-08-17 12:55:25 +0200594 case NAND_CMD_PAGEPROG:
595 case NAND_CMD_ERASE1:
596 case NAND_CMD_ERASE2:
597 case NAND_CMD_SEQIN:
598 case NAND_CMD_STATUS:
599 return;
600
601 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100602 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200603 break;
William Juulcfa460a2007-10-31 13:53:06 +0100604 udelay(chip->chip_delay);
605 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
606 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
607 chip->cmd_ctrl(mtd,
608 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600609 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
610 (rst_sts_cnt--));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200611 return;
612
William Juulcfa460a2007-10-31 13:53:06 +0100613 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200614 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200615 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200616 * If we don't have access to the busy pin, we apply the given
617 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100618 */
619 if (!chip->dev_ready) {
620 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200621 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200622 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200623 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200624 /* Apply this short delay always to ensure that we do wait tWB in
625 * any case on any machine. */
William Juulcfa460a2007-10-31 13:53:06 +0100626 ndelay(100);
627
628 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200629}
630
631/**
632 * nand_command_lp - [DEFAULT] Send command to NAND large page device
633 * @mtd: MTD device structure
634 * @command: the command to be sent
635 * @column: the column address for this command, -1 if none
636 * @page_addr: the page address for this command, -1 if none
637 *
William Juulcfa460a2007-10-31 13:53:06 +0100638 * Send command to NAND device. This is the version for the new large page
639 * devices We dont have the separate regions as we have in the small page
640 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200641 */
William Juulcfa460a2007-10-31 13:53:06 +0100642static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
643 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200644{
William Juulcfa460a2007-10-31 13:53:06 +0100645 register struct nand_chip *chip = mtd->priv;
Peter Tyser8da60122009-02-04 13:47:22 -0600646 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200647
648 /* Emulate NAND_CMD_READOOB */
649 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100650 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200651 command = NAND_CMD_READ0;
652 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200653
William Juulcfa460a2007-10-31 13:53:06 +0100654 /* Command latch cycle */
655 chip->cmd_ctrl(mtd, command & 0xff,
656 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200657
658 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100659 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200660
661 /* Serially input address */
662 if (column != -1) {
663 /* Adjust columns for 16 bit buswidth */
William Juulcfa460a2007-10-31 13:53:06 +0100664 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200665 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100666 chip->cmd_ctrl(mtd, column, ctrl);
667 ctrl &= ~NAND_CTRL_CHANGE;
668 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200669 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200670 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100671 chip->cmd_ctrl(mtd, page_addr, ctrl);
672 chip->cmd_ctrl(mtd, page_addr >> 8,
673 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200674 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100675 if (chip->chipsize > (128 << 20))
676 chip->cmd_ctrl(mtd, page_addr >> 16,
677 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200678 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200679 }
William Juulcfa460a2007-10-31 13:53:06 +0100680 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200681
682 /*
683 * program and erase have their own busy handlers
William Juulcfa460a2007-10-31 13:53:06 +0100684 * status, sequential in, and deplete1 need no delay
685 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200686 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200687
Wolfgang Denk932394a2005-08-17 12:55:25 +0200688 case NAND_CMD_CACHEDPROG:
689 case NAND_CMD_PAGEPROG:
690 case NAND_CMD_ERASE1:
691 case NAND_CMD_ERASE2:
692 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100693 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200694 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100695 case NAND_CMD_DEPLETE1:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200696 return;
697
William Juulcfa460a2007-10-31 13:53:06 +0100698 /*
699 * read error status commands require only a short delay
700 */
701 case NAND_CMD_STATUS_ERROR:
702 case NAND_CMD_STATUS_ERROR0:
703 case NAND_CMD_STATUS_ERROR1:
704 case NAND_CMD_STATUS_ERROR2:
705 case NAND_CMD_STATUS_ERROR3:
706 udelay(chip->chip_delay);
707 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200708
709 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100710 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200711 break;
William Juulcfa460a2007-10-31 13:53:06 +0100712 udelay(chip->chip_delay);
713 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
714 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
715 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
716 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600717 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
718 (rst_sts_cnt--));
William Juulcfa460a2007-10-31 13:53:06 +0100719 return;
720
721 case NAND_CMD_RNDOUT:
722 /* No ready / busy check necessary */
723 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
724 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
725 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
726 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200727 return;
728
729 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100730 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
731 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
732 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
733 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200734
William Juulcfa460a2007-10-31 13:53:06 +0100735 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200736 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200737 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200738 * If we don't have access to the busy pin, we apply the given
739 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100740 */
741 if (!chip->dev_ready) {
742 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200743 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200744 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200745 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200746
Wolfgang Denk932394a2005-08-17 12:55:25 +0200747 /* Apply this short delay always to ensure that we do wait tWB in
748 * any case on any machine. */
William Juulcfa460a2007-10-31 13:53:06 +0100749 ndelay(100);
750
751 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200752}
753
754/**
755 * nand_get_device - [GENERIC] Get chip for selected access
William Juulcfa460a2007-10-31 13:53:06 +0100756 * @chip: the nand chip descriptor
Wolfgang Denk932394a2005-08-17 12:55:25 +0200757 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200758 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200759 *
760 * Get the device and lock it for exclusive access
761 */
762/* XXX U-BOOT XXX */
763#if 0
William Juulcfa460a2007-10-31 13:53:06 +0100764static int
765nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200766{
William Juulcfa460a2007-10-31 13:53:06 +0100767 spinlock_t *lock = &chip->controller->lock;
768 wait_queue_head_t *wq = &chip->controller->wq;
769 DECLARE_WAITQUEUE(wait, current);
770 retry:
771 spin_lock(lock);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200772
Wolfgang Denk932394a2005-08-17 12:55:25 +0200773 /* Hardware controller shared among independend devices */
William Juulcfa460a2007-10-31 13:53:06 +0100774 /* Hardware controller shared among independend devices */
775 if (!chip->controller->active)
776 chip->controller->active = chip;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200777
William Juulcfa460a2007-10-31 13:53:06 +0100778 if (chip->controller->active == chip && chip->state == FL_READY) {
779 chip->state = new_state;
780 spin_unlock(lock);
781 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200782 }
William Juulcfa460a2007-10-31 13:53:06 +0100783 if (new_state == FL_PM_SUSPENDED) {
784 spin_unlock(lock);
785 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
786 }
787 set_current_state(TASK_UNINTERRUPTIBLE);
788 add_wait_queue(wq, &wait);
789 spin_unlock(lock);
790 schedule();
791 remove_wait_queue(wq, &wait);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200792 goto retry;
793}
794#else
William Juulcfa460a2007-10-31 13:53:06 +0100795static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
796{
Marcel Ziswilereafcabd2008-06-22 16:30:06 +0200797 this->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100798 return 0;
799}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200800#endif
801
802/**
803 * nand_wait - [DEFAULT] wait until the command is done
804 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +0100805 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200806 *
807 * Wait for command done. This applies to erase and program only
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200808 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk932394a2005-08-17 12:55:25 +0200809 * general NAND and SmartMedia specs
William Juulcfa460a2007-10-31 13:53:06 +0100810 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200811/* XXX U-BOOT XXX */
812#if 0
William Juulcfa460a2007-10-31 13:53:06 +0100813static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200814{
William Juulcfa460a2007-10-31 13:53:06 +0100815
816 unsigned long timeo = jiffies;
817 int status, state = chip->state;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200818
Wolfgang Denk932394a2005-08-17 12:55:25 +0200819 if (state == FL_ERASING)
William Juulcfa460a2007-10-31 13:53:06 +0100820 timeo += (HZ * 400) / 1000;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200821 else
William Juulcfa460a2007-10-31 13:53:06 +0100822 timeo += (HZ * 20) / 1000;
823
824 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200825
826 /* Apply this short delay always to ensure that we do wait tWB in
827 * any case on any machine. */
William Juulcfa460a2007-10-31 13:53:06 +0100828 ndelay(100);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200829
William Juulcfa460a2007-10-31 13:53:06 +0100830 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
831 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200832 else
William Juulcfa460a2007-10-31 13:53:06 +0100833 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200834
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200835 while (time_before(jiffies, timeo)) {
William Juulcfa460a2007-10-31 13:53:06 +0100836 if (chip->dev_ready) {
837 if (chip->dev_ready(mtd))
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200838 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200839 } else {
William Juulcfa460a2007-10-31 13:53:06 +0100840 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200841 break;
842 }
William Juulcfa460a2007-10-31 13:53:06 +0100843 cond_resched();
Wolfgang Denk932394a2005-08-17 12:55:25 +0200844 }
William Juulcfa460a2007-10-31 13:53:06 +0100845 led_trigger_event(nand_led_trigger, LED_OFF);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200846
William Juulcfa460a2007-10-31 13:53:06 +0100847 status = (int)chip->read_byte(mtd);
848 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200849}
850#else
William Juulcfa460a2007-10-31 13:53:06 +0100851static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200852{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100853 unsigned long timeo;
William Juulcfa460a2007-10-31 13:53:06 +0100854 int state = this->state;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100855
856 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200857 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100858 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200859 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100860
861 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
862 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
863 else
864 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
865
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100866 reset_timer();
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100867
868 while (1) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100869 if (get_timer(0) > timeo) {
870 printf("Timeout!");
Stefan Roese15784862006-11-27 17:22:19 +0100871 return 0x01;
872 }
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100873
874 if (this->dev_ready) {
875 if (this->dev_ready(mtd))
876 break;
877 } else {
878 if (this->read_byte(mtd) & NAND_STATUS_READY)
879 break;
880 }
881 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100882#ifdef PPCHAMELON_NAND_TIMER_HACK
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100883 reset_timer();
884 while (get_timer(0) < 10);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100885#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100886
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100887 return this->read_byte(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200888}
889#endif
890
891/**
William Juulcfa460a2007-10-31 13:53:06 +0100892 * nand_read_page_raw - [Intern] read raw page data without ecc
893 * @mtd: mtd info structure
894 * @chip: nand chip info structure
895 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -0500896 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200897 */
William Juulcfa460a2007-10-31 13:53:06 +0100898static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraja2c65b42009-08-10 13:27:46 -0400899 uint8_t *buf, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200900{
William Juulcfa460a2007-10-31 13:53:06 +0100901 chip->read_buf(mtd, buf, mtd->writesize);
902 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
903 return 0;
904}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200905
William Juulcfa460a2007-10-31 13:53:06 +0100906/**
907 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
908 * @mtd: mtd info structure
909 * @chip: nand chip info structure
910 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -0500911 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +0100912 */
913static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraja2c65b42009-08-10 13:27:46 -0400914 uint8_t *buf, int page)
William Juulcfa460a2007-10-31 13:53:06 +0100915{
916 int i, eccsize = chip->ecc.size;
917 int eccbytes = chip->ecc.bytes;
918 int eccsteps = chip->ecc.steps;
919 uint8_t *p = buf;
920 uint8_t *ecc_calc = chip->buffers->ecccalc;
921 uint8_t *ecc_code = chip->buffers->ecccode;
922 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200923
Sandeep Paulraja2c65b42009-08-10 13:27:46 -0400924 chip->ecc.read_page_raw(mtd, chip, buf, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200925
William Juulcfa460a2007-10-31 13:53:06 +0100926 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
927 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200928
William Juulcfa460a2007-10-31 13:53:06 +0100929 for (i = 0; i < chip->ecc.total; i++)
930 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200931
William Juulcfa460a2007-10-31 13:53:06 +0100932 eccsteps = chip->ecc.steps;
933 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200934
William Juulcfa460a2007-10-31 13:53:06 +0100935 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
936 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200937
William Juulcfa460a2007-10-31 13:53:06 +0100938 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Woodc45912d2008-10-24 16:20:43 -0500939 if (stat < 0)
940 mtd->ecc_stats.failed++;
941 else
942 mtd->ecc_stats.corrected += stat;
943 }
944 return 0;
945}
946
947/**
948 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
949 * @mtd: mtd info structure
950 * @chip: nand chip info structure
Sandeep Paulraje25ee032009-11-07 14:24:50 -0500951 * @data_offs: offset of requested data within the page
952 * @readlen: data length
953 * @bufpoi: buffer to store read data
Scott Woodc45912d2008-10-24 16:20:43 -0500954 */
955static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
956{
957 int start_step, end_step, num_steps;
958 uint32_t *eccpos = chip->ecc.layout->eccpos;
959 uint8_t *p;
960 int data_col_addr, i, gaps = 0;
961 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
962 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
963
964 /* Column address wihin the page aligned to ECC size (256bytes). */
965 start_step = data_offs / chip->ecc.size;
966 end_step = (data_offs + readlen - 1) / chip->ecc.size;
967 num_steps = end_step - start_step + 1;
968
969 /* Data size aligned to ECC ecc.size*/
970 datafrag_len = num_steps * chip->ecc.size;
971 eccfrag_len = num_steps * chip->ecc.bytes;
972
973 data_col_addr = start_step * chip->ecc.size;
974 /* If we read not a page aligned data */
975 if (data_col_addr != 0)
976 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
977
978 p = bufpoi + data_col_addr;
979 chip->read_buf(mtd, p, datafrag_len);
980
981 /* Calculate ECC */
982 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
983 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
984
985 /* The performance is faster if to position offsets
986 according to ecc.pos. Let make sure here that
987 there are no gaps in ecc positions */
988 for (i = 0; i < eccfrag_len - 1; i++) {
989 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
990 eccpos[i + start_step * chip->ecc.bytes + 1]) {
991 gaps = 1;
992 break;
993 }
994 }
995 if (gaps) {
996 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
997 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
998 } else {
999 /* send the command to read the particular ecc bytes */
1000 /* take care about buswidth alignment in read_buf */
1001 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
1002 aligned_len = eccfrag_len;
1003 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
1004 aligned_len++;
1005 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1006 aligned_len++;
1007
1008 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1009 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1010 }
1011
1012 for (i = 0; i < eccfrag_len; i++)
1013 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1014
1015 p = bufpoi + data_col_addr;
1016 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1017 int stat;
1018
1019 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1020 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001021 mtd->ecc_stats.failed++;
1022 else
1023 mtd->ecc_stats.corrected += stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001024 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001025 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001026}
1027
Wolfgang Denk932394a2005-08-17 12:55:25 +02001028/**
William Juulcfa460a2007-10-31 13:53:06 +01001029 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
1030 * @mtd: mtd info structure
1031 * @chip: nand chip info structure
1032 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -05001033 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001034 *
William Juulcfa460a2007-10-31 13:53:06 +01001035 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk932394a2005-08-17 12:55:25 +02001036 */
William Juulcfa460a2007-10-31 13:53:06 +01001037static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001038 uint8_t *buf, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001039{
William Juulcfa460a2007-10-31 13:53:06 +01001040 int i, eccsize = chip->ecc.size;
1041 int eccbytes = chip->ecc.bytes;
1042 int eccsteps = chip->ecc.steps;
1043 uint8_t *p = buf;
1044 uint8_t *ecc_calc = chip->buffers->ecccalc;
1045 uint8_t *ecc_code = chip->buffers->ecccode;
1046 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001047
William Juulcfa460a2007-10-31 13:53:06 +01001048 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1049 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1050 chip->read_buf(mtd, p, eccsize);
1051 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1052 }
1053 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001054
William Juulcfa460a2007-10-31 13:53:06 +01001055 for (i = 0; i < chip->ecc.total; i++)
1056 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001057
William Juulcfa460a2007-10-31 13:53:06 +01001058 eccsteps = chip->ecc.steps;
1059 p = buf;
1060
1061 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1062 int stat;
1063
1064 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Sandeep Paulraj18b5a4b2009-11-07 14:25:03 -05001065 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001066 mtd->ecc_stats.failed++;
1067 else
1068 mtd->ecc_stats.corrected += stat;
1069 }
1070 return 0;
1071}
1072
1073/**
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001074 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1075 * @mtd: mtd info structure
1076 * @chip: nand chip info structure
1077 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -05001078 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001079 *
1080 * Hardware ECC for large page chips, require OOB to be read first.
1081 * For this ECC mode, the write_page method is re-used from ECC_HW.
1082 * These methods read/write ECC from the OOB area, unlike the
1083 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1084 * "infix ECC" scheme and reads/writes ECC from the data area, by
1085 * overwriting the NAND manufacturer bad block markings.
1086 */
1087static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1088 struct nand_chip *chip, uint8_t *buf, int page)
1089{
1090 int i, eccsize = chip->ecc.size;
1091 int eccbytes = chip->ecc.bytes;
1092 int eccsteps = chip->ecc.steps;
1093 uint8_t *p = buf;
1094 uint8_t *ecc_code = chip->buffers->ecccode;
1095 uint32_t *eccpos = chip->ecc.layout->eccpos;
1096 uint8_t *ecc_calc = chip->buffers->ecccalc;
1097
1098 /* Read the OOB area first */
1099 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1100 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1101 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1102
1103 for (i = 0; i < chip->ecc.total; i++)
1104 ecc_code[i] = chip->oob_poi[eccpos[i]];
1105
1106 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1107 int stat;
1108
1109 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1110 chip->read_buf(mtd, p, eccsize);
1111 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1112
1113 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1114 if (stat < 0)
1115 mtd->ecc_stats.failed++;
1116 else
1117 mtd->ecc_stats.corrected += stat;
1118 }
1119 return 0;
1120}
1121
1122/**
William Juulcfa460a2007-10-31 13:53:06 +01001123 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1124 * @mtd: mtd info structure
1125 * @chip: nand chip info structure
1126 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -05001127 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001128 *
1129 * The hw generator calculates the error syndrome automatically. Therefor
1130 * we need a special oob layout and handling.
1131 */
1132static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001133 uint8_t *buf, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001134{
1135 int i, eccsize = chip->ecc.size;
1136 int eccbytes = chip->ecc.bytes;
1137 int eccsteps = chip->ecc.steps;
1138 uint8_t *p = buf;
1139 uint8_t *oob = chip->oob_poi;
1140
1141 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1142 int stat;
1143
1144 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1145 chip->read_buf(mtd, p, eccsize);
1146
1147 if (chip->ecc.prepad) {
1148 chip->read_buf(mtd, oob, chip->ecc.prepad);
1149 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001150 }
1151
William Juulcfa460a2007-10-31 13:53:06 +01001152 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1153 chip->read_buf(mtd, oob, eccbytes);
1154 stat = chip->ecc.correct(mtd, p, oob, NULL);
1155
Scott Woodc45912d2008-10-24 16:20:43 -05001156 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001157 mtd->ecc_stats.failed++;
1158 else
1159 mtd->ecc_stats.corrected += stat;
1160
1161 oob += eccbytes;
1162
1163 if (chip->ecc.postpad) {
1164 chip->read_buf(mtd, oob, chip->ecc.postpad);
1165 oob += chip->ecc.postpad;
1166 }
1167 }
1168
1169 /* Calculate remaining oob bytes */
1170 i = mtd->oobsize - (oob - chip->oob_poi);
1171 if (i)
1172 chip->read_buf(mtd, oob, i);
1173
1174 return 0;
1175}
1176
1177/**
1178 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1179 * @chip: nand chip structure
1180 * @oob: oob destination address
1181 * @ops: oob ops structure
1182 * @len: size of oob to transfer
1183 */
1184static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1185 struct mtd_oob_ops *ops, size_t len)
1186{
1187 switch(ops->mode) {
1188
1189 case MTD_OOB_PLACE:
1190 case MTD_OOB_RAW:
1191 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1192 return oob + len;
1193
1194 case MTD_OOB_AUTO: {
1195 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1196 uint32_t boffs = 0, roffs = ops->ooboffs;
1197 size_t bytes = 0;
1198
1199 for(; free->length && len; free++, len -= bytes) {
1200 /* Read request not from offset 0 ? */
1201 if (unlikely(roffs)) {
1202 if (roffs >= free->length) {
1203 roffs -= free->length;
1204 continue;
1205 }
1206 boffs = free->offset + roffs;
1207 bytes = min_t(size_t, len,
1208 (free->length - roffs));
1209 roffs = 0;
1210 } else {
1211 bytes = min_t(size_t, len, free->length);
1212 boffs = free->offset;
1213 }
1214 memcpy(oob, chip->oob_poi + boffs, bytes);
1215 oob += bytes;
1216 }
1217 return oob;
1218 }
1219 default:
1220 BUG();
1221 }
1222 return NULL;
1223}
1224
1225/**
1226 * nand_do_read_ops - [Internal] Read data with ECC
1227 *
1228 * @mtd: MTD device structure
1229 * @from: offset to read from
1230 * @ops: oob ops structure
1231 *
1232 * Internal function. Called with chip held.
1233 */
1234static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1235 struct mtd_oob_ops *ops)
1236{
1237 int chipnr, page, realpage, col, bytes, aligned;
1238 struct nand_chip *chip = mtd->priv;
1239 struct mtd_ecc_stats stats;
1240 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1241 int sndcmd = 1;
1242 int ret = 0;
1243 uint32_t readlen = ops->len;
1244 uint32_t oobreadlen = ops->ooblen;
1245 uint8_t *bufpoi, *oob, *buf;
1246
1247 stats = mtd->ecc_stats;
1248
1249 chipnr = (int)(from >> chip->chip_shift);
1250 chip->select_chip(mtd, chipnr);
1251
1252 realpage = (int)(from >> chip->page_shift);
1253 page = realpage & chip->pagemask;
1254
1255 col = (int)(from & (mtd->writesize - 1));
1256
1257 buf = ops->datbuf;
1258 oob = ops->oobbuf;
1259
1260 while(1) {
1261 bytes = min(mtd->writesize - col, readlen);
1262 aligned = (bytes == mtd->writesize);
1263
1264 /* Is the current page in the buffer ? */
1265 if (realpage != chip->pagebuf || oob) {
1266 bufpoi = aligned ? buf : chip->buffers->databuf;
1267
1268 if (likely(sndcmd)) {
1269 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1270 sndcmd = 0;
1271 }
1272
1273 /* Now read the page into the buffer */
1274 if (unlikely(ops->mode == MTD_OOB_RAW))
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001275 ret = chip->ecc.read_page_raw(mtd, chip,
1276 bufpoi, page);
Scott Woodc45912d2008-10-24 16:20:43 -05001277 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1278 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
William Juulcfa460a2007-10-31 13:53:06 +01001279 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001280 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1281 page);
William Juulcfa460a2007-10-31 13:53:06 +01001282 if (ret < 0)
1283 break;
1284
1285 /* Transfer not aligned data */
1286 if (!aligned) {
Scott Woodc45912d2008-10-24 16:20:43 -05001287 if (!NAND_SUBPAGE_READ(chip) && !oob)
1288 chip->pagebuf = realpage;
William Juulcfa460a2007-10-31 13:53:06 +01001289 memcpy(buf, chip->buffers->databuf + col, bytes);
1290 }
1291
1292 buf += bytes;
1293
1294 if (unlikely(oob)) {
1295 /* Raw mode does data:oob:data:oob */
1296 if (ops->mode != MTD_OOB_RAW) {
1297 int toread = min(oobreadlen,
1298 chip->ecc.layout->oobavail);
1299 if (toread) {
1300 oob = nand_transfer_oob(chip,
1301 oob, ops, toread);
1302 oobreadlen -= toread;
1303 }
1304 } else
1305 buf = nand_transfer_oob(chip,
1306 buf, ops, mtd->oobsize);
1307 }
1308
1309 if (!(chip->options & NAND_NO_READRDY)) {
1310 /*
1311 * Apply delay or wait for ready/busy pin. Do
1312 * this before the AUTOINCR check, so no
1313 * problems arise if a chip which does auto
1314 * increment is marked as NOAUTOINCR by the
1315 * board driver.
1316 */
1317 if (!chip->dev_ready)
1318 udelay(chip->chip_delay);
1319 else
1320 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001321 }
1322 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001323 memcpy(buf, chip->buffers->databuf + col, bytes);
1324 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001325 }
1326
William Juulcfa460a2007-10-31 13:53:06 +01001327 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001328
William Juulcfa460a2007-10-31 13:53:06 +01001329 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001330 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001331
1332 /* For subsequent reads align to page boundary. */
1333 col = 0;
1334 /* Increment page address */
1335 realpage++;
1336
William Juulcfa460a2007-10-31 13:53:06 +01001337 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001338 /* Check, if we cross a chip boundary */
1339 if (!page) {
1340 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001341 chip->select_chip(mtd, -1);
1342 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001343 }
William Juulcfa460a2007-10-31 13:53:06 +01001344
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001345 /* Check, if the chip supports auto page increment
1346 * or if we have hit a block boundary.
William Juulcfa460a2007-10-31 13:53:06 +01001347 */
1348 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001349 sndcmd = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001350 }
1351
William Juulcfa460a2007-10-31 13:53:06 +01001352 ops->retlen = ops->len - (size_t) readlen;
1353 if (oob)
1354 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001355
William Juulcfa460a2007-10-31 13:53:06 +01001356 if (ret)
1357 return ret;
1358
1359 if (mtd->ecc_stats.failed - stats.failed)
1360 return -EBADMSG;
1361
1362 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001363}
1364
1365/**
William Juulcfa460a2007-10-31 13:53:06 +01001366 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
Wolfgang Denk932394a2005-08-17 12:55:25 +02001367 * @mtd: MTD device structure
1368 * @from: offset to read from
1369 * @len: number of bytes to read
1370 * @retlen: pointer to variable to store the number of read bytes
1371 * @buf: the databuffer to put data
1372 *
William Juulcfa460a2007-10-31 13:53:06 +01001373 * Get hold of the chip and call nand_do_read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001374 */
William Juulcfa460a2007-10-31 13:53:06 +01001375static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1376 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001377{
William Juulcfa460a2007-10-31 13:53:06 +01001378 struct nand_chip *chip = mtd->priv;
1379 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001380
1381 /* Do not allow reads past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01001382 if ((from + len) > mtd->size)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001383 return -EINVAL;
William Juulcfa460a2007-10-31 13:53:06 +01001384 if (!len)
1385 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001386
William Juulcfa460a2007-10-31 13:53:06 +01001387 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001388
William Juulcfa460a2007-10-31 13:53:06 +01001389 chip->ops.len = len;
1390 chip->ops.datbuf = buf;
1391 chip->ops.oobbuf = NULL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001392
William Juulcfa460a2007-10-31 13:53:06 +01001393 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001394
William Juulcfa460a2007-10-31 13:53:06 +01001395 *retlen = chip->ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001396
Wolfgang Denk932394a2005-08-17 12:55:25 +02001397 nand_release_device(mtd);
1398
1399 return ret;
1400}
1401
William Juulcfa460a2007-10-31 13:53:06 +01001402/**
1403 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1404 * @mtd: mtd info structure
1405 * @chip: nand chip info structure
1406 * @page: page number to read
1407 * @sndcmd: flag whether to issue read command or not
1408 */
1409static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1410 int page, int sndcmd)
1411{
1412 if (sndcmd) {
1413 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1414 sndcmd = 0;
1415 }
1416 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1417 return sndcmd;
1418}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001419
1420/**
William Juulcfa460a2007-10-31 13:53:06 +01001421 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1422 * with syndromes
1423 * @mtd: mtd info structure
1424 * @chip: nand chip info structure
1425 * @page: page number to read
1426 * @sndcmd: flag whether to issue read command or not
1427 */
1428static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1429 int page, int sndcmd)
1430{
1431 uint8_t *buf = chip->oob_poi;
1432 int length = mtd->oobsize;
1433 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1434 int eccsize = chip->ecc.size;
1435 uint8_t *bufpoi = buf;
1436 int i, toread, sndrnd = 0, pos;
1437
1438 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1439 for (i = 0; i < chip->ecc.steps; i++) {
1440 if (sndrnd) {
1441 pos = eccsize + i * (eccsize + chunk);
1442 if (mtd->writesize > 512)
1443 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1444 else
1445 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1446 } else
1447 sndrnd = 1;
1448 toread = min_t(int, length, chunk);
1449 chip->read_buf(mtd, bufpoi, toread);
1450 bufpoi += toread;
1451 length -= toread;
1452 }
1453 if (length > 0)
1454 chip->read_buf(mtd, bufpoi, length);
1455
1456 return 1;
1457}
1458
1459/**
1460 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1461 * @mtd: mtd info structure
1462 * @chip: nand chip info structure
1463 * @page: page number to write
1464 */
1465static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1466 int page)
1467{
1468 int status = 0;
1469 const uint8_t *buf = chip->oob_poi;
1470 int length = mtd->oobsize;
1471
1472 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1473 chip->write_buf(mtd, buf, length);
1474 /* Send command to program the OOB data */
1475 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1476
1477 status = chip->waitfunc(mtd, chip);
1478
1479 return status & NAND_STATUS_FAIL ? -EIO : 0;
1480}
1481
1482/**
1483 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1484 * with syndrome - only for large page flash !
1485 * @mtd: mtd info structure
1486 * @chip: nand chip info structure
1487 * @page: page number to write
1488 */
1489static int nand_write_oob_syndrome(struct mtd_info *mtd,
1490 struct nand_chip *chip, int page)
1491{
1492 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1493 int eccsize = chip->ecc.size, length = mtd->oobsize;
1494 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1495 const uint8_t *bufpoi = chip->oob_poi;
1496
1497 /*
1498 * data-ecc-data-ecc ... ecc-oob
1499 * or
1500 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1501 */
1502 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1503 pos = steps * (eccsize + chunk);
1504 steps = 0;
1505 } else
1506 pos = eccsize;
1507
1508 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1509 for (i = 0; i < steps; i++) {
1510 if (sndcmd) {
1511 if (mtd->writesize <= 512) {
1512 uint32_t fill = 0xFFFFFFFF;
1513
1514 len = eccsize;
1515 while (len > 0) {
1516 int num = min_t(int, len, 4);
1517 chip->write_buf(mtd, (uint8_t *)&fill,
1518 num);
1519 len -= num;
1520 }
1521 } else {
1522 pos = eccsize + i * (eccsize + chunk);
1523 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1524 }
1525 } else
1526 sndcmd = 1;
1527 len = min_t(int, length, chunk);
1528 chip->write_buf(mtd, bufpoi, len);
1529 bufpoi += len;
1530 length -= len;
1531 }
1532 if (length > 0)
1533 chip->write_buf(mtd, bufpoi, length);
1534
1535 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1536 status = chip->waitfunc(mtd, chip);
1537
1538 return status & NAND_STATUS_FAIL ? -EIO : 0;
1539}
1540
1541/**
1542 * nand_do_read_oob - [Intern] NAND read out-of-band
1543 * @mtd: MTD device structure
1544 * @from: offset to read from
1545 * @ops: oob operations description structure
1546 *
1547 * NAND read out-of-band data from the spare area
1548 */
1549static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1550 struct mtd_oob_ops *ops)
1551{
1552 int page, realpage, chipnr, sndcmd = 1;
1553 struct nand_chip *chip = mtd->priv;
1554 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1555 int readlen = ops->ooblen;
1556 int len;
1557 uint8_t *buf = ops->oobbuf;
1558
1559 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1560 (unsigned long long)from, readlen);
1561
1562 if (ops->mode == MTD_OOB_AUTO)
1563 len = chip->ecc.layout->oobavail;
1564 else
1565 len = mtd->oobsize;
1566
1567 if (unlikely(ops->ooboffs >= len)) {
1568 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1569 "Attempt to start read outside oob\n");
1570 return -EINVAL;
1571 }
1572
1573 /* Do not allow reads past end of device */
1574 if (unlikely(from >= mtd->size ||
1575 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1576 (from >> chip->page_shift)) * len)) {
1577 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1578 "Attempt read beyond end of device\n");
1579 return -EINVAL;
1580 }
1581
1582 chipnr = (int)(from >> chip->chip_shift);
1583 chip->select_chip(mtd, chipnr);
1584
1585 /* Shift to get page */
1586 realpage = (int)(from >> chip->page_shift);
1587 page = realpage & chip->pagemask;
1588
1589 while(1) {
1590 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1591
1592 len = min(len, readlen);
1593 buf = nand_transfer_oob(chip, buf, ops, len);
1594
1595 if (!(chip->options & NAND_NO_READRDY)) {
1596 /*
1597 * Apply delay or wait for ready/busy pin. Do this
1598 * before the AUTOINCR check, so no problems arise if a
1599 * chip which does auto increment is marked as
1600 * NOAUTOINCR by the board driver.
1601 */
1602 if (!chip->dev_ready)
1603 udelay(chip->chip_delay);
1604 else
1605 nand_wait_ready(mtd);
1606 }
1607
1608 readlen -= len;
1609 if (!readlen)
1610 break;
1611
1612 /* Increment page address */
1613 realpage++;
1614
1615 page = realpage & chip->pagemask;
1616 /* Check, if we cross a chip boundary */
1617 if (!page) {
1618 chipnr++;
1619 chip->select_chip(mtd, -1);
1620 chip->select_chip(mtd, chipnr);
1621 }
1622
1623 /* Check, if the chip supports auto page increment
1624 * or if we have hit a block boundary.
1625 */
1626 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1627 sndcmd = 1;
1628 }
1629
1630 ops->oobretlen = ops->ooblen;
1631 return 0;
1632}
1633
1634/**
1635 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1636 * @mtd: MTD device structure
1637 * @from: offset to read from
1638 * @ops: oob operation description structure
1639 *
1640 * NAND read data and/or out-of-band data
1641 */
1642static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1643 struct mtd_oob_ops *ops)
1644{
1645 struct nand_chip *chip = mtd->priv;
1646 int ret = -ENOTSUPP;
1647
1648 ops->retlen = 0;
1649
1650 /* Do not allow reads past end of device */
1651 if (ops->datbuf && (from + ops->len) > mtd->size) {
1652 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1653 "Attempt read beyond end of device\n");
1654 return -EINVAL;
1655 }
1656
1657 nand_get_device(chip, mtd, FL_READING);
1658
1659 switch(ops->mode) {
1660 case MTD_OOB_PLACE:
1661 case MTD_OOB_AUTO:
1662 case MTD_OOB_RAW:
1663 break;
1664
1665 default:
1666 goto out;
1667 }
1668
1669 if (!ops->datbuf)
1670 ret = nand_do_read_oob(mtd, from, ops);
1671 else
1672 ret = nand_do_read_ops(mtd, from, ops);
1673
1674 out:
1675 nand_release_device(mtd);
1676 return ret;
1677}
1678
1679
1680/**
1681 * nand_write_page_raw - [Intern] raw page write function
1682 * @mtd: mtd info structure
1683 * @chip: nand chip info structure
1684 * @buf: data buffer
1685 */
1686static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1687 const uint8_t *buf)
1688{
1689 chip->write_buf(mtd, buf, mtd->writesize);
1690 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1691}
1692
1693/**
1694 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1695 * @mtd: mtd info structure
1696 * @chip: nand chip info structure
1697 * @buf: data buffer
1698 */
1699static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1700 const uint8_t *buf)
1701{
1702 int i, eccsize = chip->ecc.size;
1703 int eccbytes = chip->ecc.bytes;
1704 int eccsteps = chip->ecc.steps;
1705 uint8_t *ecc_calc = chip->buffers->ecccalc;
1706 const uint8_t *p = buf;
1707 uint32_t *eccpos = chip->ecc.layout->eccpos;
1708
1709 /* Software ecc calculation */
1710 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1711 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1712
1713 for (i = 0; i < chip->ecc.total; i++)
1714 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1715
1716 chip->ecc.write_page_raw(mtd, chip, buf);
1717}
1718
1719/**
1720 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1721 * @mtd: mtd info structure
1722 * @chip: nand chip info structure
1723 * @buf: data buffer
1724 */
1725static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1726 const uint8_t *buf)
1727{
1728 int i, eccsize = chip->ecc.size;
1729 int eccbytes = chip->ecc.bytes;
1730 int eccsteps = chip->ecc.steps;
1731 uint8_t *ecc_calc = chip->buffers->ecccalc;
1732 const uint8_t *p = buf;
1733 uint32_t *eccpos = chip->ecc.layout->eccpos;
1734
1735 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1736 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1737 chip->write_buf(mtd, p, eccsize);
1738 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1739 }
1740
1741 for (i = 0; i < chip->ecc.total; i++)
1742 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1743
1744 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1745}
1746
1747/**
1748 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1749 * @mtd: mtd info structure
1750 * @chip: nand chip info structure
1751 * @buf: data buffer
1752 *
1753 * The hw generator calculates the error syndrome automatically. Therefor
1754 * we need a special oob layout and handling.
1755 */
1756static void nand_write_page_syndrome(struct mtd_info *mtd,
1757 struct nand_chip *chip, const uint8_t *buf)
1758{
1759 int i, eccsize = chip->ecc.size;
1760 int eccbytes = chip->ecc.bytes;
1761 int eccsteps = chip->ecc.steps;
1762 const uint8_t *p = buf;
1763 uint8_t *oob = chip->oob_poi;
1764
1765 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1766
1767 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1768 chip->write_buf(mtd, p, eccsize);
1769
1770 if (chip->ecc.prepad) {
1771 chip->write_buf(mtd, oob, chip->ecc.prepad);
1772 oob += chip->ecc.prepad;
1773 }
1774
1775 chip->ecc.calculate(mtd, p, oob);
1776 chip->write_buf(mtd, oob, eccbytes);
1777 oob += eccbytes;
1778
1779 if (chip->ecc.postpad) {
1780 chip->write_buf(mtd, oob, chip->ecc.postpad);
1781 oob += chip->ecc.postpad;
1782 }
1783 }
1784
1785 /* Calculate remaining oob bytes */
1786 i = mtd->oobsize - (oob - chip->oob_poi);
1787 if (i)
1788 chip->write_buf(mtd, oob, i);
1789}
1790
1791/**
1792 * nand_write_page - [REPLACEABLE] write one page
1793 * @mtd: MTD device structure
1794 * @chip: NAND chip descriptor
1795 * @buf: the data to write
1796 * @page: page number to write
1797 * @cached: cached programming
1798 * @raw: use _raw version of write_page
1799 */
1800static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1801 const uint8_t *buf, int page, int cached, int raw)
1802{
1803 int status;
1804
1805 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1806
1807 if (unlikely(raw))
1808 chip->ecc.write_page_raw(mtd, chip, buf);
1809 else
1810 chip->ecc.write_page(mtd, chip, buf);
1811
1812 /*
1813 * Cached progamming disabled for now, Not sure if its worth the
1814 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1815 */
1816 cached = 0;
1817
1818 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1819
1820 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1821 status = chip->waitfunc(mtd, chip);
1822 /*
1823 * See if operation failed and additional status checks are
1824 * available
1825 */
1826 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1827 status = chip->errstat(mtd, chip, FL_WRITING, status,
1828 page);
1829
1830 if (status & NAND_STATUS_FAIL)
1831 return -EIO;
1832 } else {
1833 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1834 status = chip->waitfunc(mtd, chip);
1835 }
1836
1837#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1838 /* Send command to read back the data */
1839 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1840
1841 if (chip->verify_buf(mtd, buf, mtd->writesize))
1842 return -EIO;
1843#endif
1844 return 0;
1845}
1846
1847/**
1848 * nand_fill_oob - [Internal] Transfer client buffer to oob
1849 * @chip: nand chip structure
1850 * @oob: oob data buffer
1851 * @ops: oob ops structure
1852 */
1853static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1854 struct mtd_oob_ops *ops)
1855{
1856 size_t len = ops->ooblen;
1857
1858 switch(ops->mode) {
1859
1860 case MTD_OOB_PLACE:
1861 case MTD_OOB_RAW:
1862 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1863 return oob + len;
1864
1865 case MTD_OOB_AUTO: {
1866 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1867 uint32_t boffs = 0, woffs = ops->ooboffs;
1868 size_t bytes = 0;
1869
1870 for(; free->length && len; free++, len -= bytes) {
1871 /* Write request not from offset 0 ? */
1872 if (unlikely(woffs)) {
1873 if (woffs >= free->length) {
1874 woffs -= free->length;
1875 continue;
1876 }
1877 boffs = free->offset + woffs;
1878 bytes = min_t(size_t, len,
1879 (free->length - woffs));
1880 woffs = 0;
1881 } else {
1882 bytes = min_t(size_t, len, free->length);
1883 boffs = free->offset;
1884 }
1885 memcpy(chip->oob_poi + boffs, oob, bytes);
1886 oob += bytes;
1887 }
1888 return oob;
1889 }
1890 default:
1891 BUG();
1892 }
1893 return NULL;
1894}
1895
1896#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
1897
1898/**
1899 * nand_do_write_ops - [Internal] NAND write with ECC
1900 * @mtd: MTD device structure
1901 * @to: offset to write to
1902 * @ops: oob operations description structure
1903 *
1904 * NAND write with ECC
1905 */
1906static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1907 struct mtd_oob_ops *ops)
1908{
1909 int chipnr, realpage, page, blockmask, column;
1910 struct nand_chip *chip = mtd->priv;
1911 uint32_t writelen = ops->len;
1912 uint8_t *oob = ops->oobbuf;
1913 uint8_t *buf = ops->datbuf;
1914 int ret, subpage;
1915
1916 ops->retlen = 0;
1917 if (!writelen)
1918 return 0;
1919
1920 /* reject writes, which are not page aligned */
1921 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
1922 printk(KERN_NOTICE "nand_write: "
1923 "Attempt to write not page aligned data\n");
1924 return -EINVAL;
1925 }
1926
1927 column = to & (mtd->writesize - 1);
1928 subpage = column || (writelen & (mtd->writesize - 1));
1929
1930 if (subpage && oob)
1931 return -EINVAL;
1932
1933 chipnr = (int)(to >> chip->chip_shift);
1934 chip->select_chip(mtd, chipnr);
1935
1936 /* Check, if it is write protected */
1937 if (nand_check_wp(mtd)) {
1938 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1939 return -EIO;
1940 }
1941
1942 realpage = (int)(to >> chip->page_shift);
1943 page = realpage & chip->pagemask;
1944 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1945
1946 /* Invalidate the page cache, when we write to the cached page */
1947 if (to <= (chip->pagebuf << chip->page_shift) &&
1948 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1949 chip->pagebuf = -1;
1950
1951 /* If we're not given explicit OOB data, let it be 0xFF */
1952 if (likely(!oob))
1953 memset(chip->oob_poi, 0xff, mtd->oobsize);
1954
1955 while(1) {
1956 int bytes = mtd->writesize;
1957 int cached = writelen > bytes && page != blockmask;
1958 uint8_t *wbuf = buf;
1959
1960 /* Partial page write ? */
1961 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1962 cached = 0;
1963 bytes = min_t(int, bytes - column, (int) writelen);
1964 chip->pagebuf = -1;
1965 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1966 memcpy(&chip->buffers->databuf[column], buf, bytes);
1967 wbuf = chip->buffers->databuf;
1968 }
1969
1970 if (unlikely(oob))
1971 oob = nand_fill_oob(chip, oob, ops);
1972
1973 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1974 (ops->mode == MTD_OOB_RAW));
1975 if (ret)
1976 break;
1977
1978 writelen -= bytes;
1979 if (!writelen)
1980 break;
1981
1982 column = 0;
1983 buf += bytes;
1984 realpage++;
1985
1986 page = realpage & chip->pagemask;
1987 /* Check, if we cross a chip boundary */
1988 if (!page) {
1989 chipnr++;
1990 chip->select_chip(mtd, -1);
1991 chip->select_chip(mtd, chipnr);
1992 }
1993 }
1994
1995 ops->retlen = ops->len - writelen;
1996 if (unlikely(oob))
1997 ops->oobretlen = ops->ooblen;
1998 return ret;
1999}
2000
2001/**
2002 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk932394a2005-08-17 12:55:25 +02002003 * @mtd: MTD device structure
2004 * @to: offset to write to
2005 * @len: number of bytes to write
2006 * @retlen: pointer to variable to store the number of written bytes
2007 * @buf: the data to write
2008 *
William Juulcfa460a2007-10-31 13:53:06 +01002009 * NAND write with ECC
2010 */
2011static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2012 size_t *retlen, const uint8_t *buf)
2013{
2014 struct nand_chip *chip = mtd->priv;
2015 int ret;
2016
2017 /* Do not allow reads past end of device */
2018 if ((to + len) > mtd->size)
2019 return -EINVAL;
2020 if (!len)
2021 return 0;
2022
2023 nand_get_device(chip, mtd, FL_WRITING);
2024
2025 chip->ops.len = len;
2026 chip->ops.datbuf = (uint8_t *)buf;
2027 chip->ops.oobbuf = NULL;
2028
2029 ret = nand_do_write_ops(mtd, to, &chip->ops);
2030
2031 *retlen = chip->ops.retlen;
2032
2033 nand_release_device(mtd);
2034
2035 return ret;
2036}
2037
2038/**
2039 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2040 * @mtd: MTD device structure
2041 * @to: offset to write to
2042 * @ops: oob operation description structure
2043 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02002044 * NAND write out-of-band
2045 */
William Juulcfa460a2007-10-31 13:53:06 +01002046static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2047 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002048{
William Juulcfa460a2007-10-31 13:53:06 +01002049 int chipnr, page, status, len;
2050 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002051
Scott Wood3167c532008-06-20 12:38:57 -05002052 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
William Juulcfa460a2007-10-31 13:53:06 +01002053 (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002054
William Juulcfa460a2007-10-31 13:53:06 +01002055 if (ops->mode == MTD_OOB_AUTO)
2056 len = chip->ecc.layout->oobavail;
2057 else
2058 len = mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002059
2060 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002061 if ((ops->ooboffs + ops->ooblen) > len) {
Scott Wood3167c532008-06-20 12:38:57 -05002062 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
2063 "Attempt to write past end of page\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002064 return -EINVAL;
2065 }
2066
William Juulcfa460a2007-10-31 13:53:06 +01002067 if (unlikely(ops->ooboffs >= len)) {
2068 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2069 "Attempt to start write outside oob\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002070 return -EINVAL;
2071 }
2072
William Juulcfa460a2007-10-31 13:53:06 +01002073 /* Do not allow reads past end of device */
2074 if (unlikely(to >= mtd->size ||
2075 ops->ooboffs + ops->ooblen >
2076 ((mtd->size >> chip->page_shift) -
2077 (to >> chip->page_shift)) * len)) {
2078 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2079 "Attempt write beyond end of device\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002080 return -EINVAL;
2081 }
2082
William Juulcfa460a2007-10-31 13:53:06 +01002083 chipnr = (int)(to >> chip->chip_shift);
2084 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002085
William Juulcfa460a2007-10-31 13:53:06 +01002086 /* Shift to get page */
2087 page = (int)(to >> chip->page_shift);
2088
2089 /*
2090 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2091 * of my DiskOnChip 2000 test units) will clear the whole data page too
2092 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2093 * it in the doc2000 driver in August 1999. dwmw2.
2094 */
2095 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002096
2097 /* Check, if it is write protected */
2098 if (nand_check_wp(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01002099 return -EROFS;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002100
Wolfgang Denk932394a2005-08-17 12:55:25 +02002101 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002102 if (page == chip->pagebuf)
2103 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002104
William Juulcfa460a2007-10-31 13:53:06 +01002105 memset(chip->oob_poi, 0xff, mtd->oobsize);
2106 nand_fill_oob(chip, ops->oobbuf, ops);
2107 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2108 memset(chip->oob_poi, 0xff, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002109
William Juulcfa460a2007-10-31 13:53:06 +01002110 if (status)
2111 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002112
William Juulcfa460a2007-10-31 13:53:06 +01002113 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002114
William Juulcfa460a2007-10-31 13:53:06 +01002115 return 0;
2116}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002117
William Juulcfa460a2007-10-31 13:53:06 +01002118/**
2119 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2120 * @mtd: MTD device structure
2121 * @to: offset to write to
2122 * @ops: oob operation description structure
2123 */
2124static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2125 struct mtd_oob_ops *ops)
2126{
2127 struct nand_chip *chip = mtd->priv;
2128 int ret = -ENOTSUPP;
2129
2130 ops->retlen = 0;
2131
2132 /* Do not allow writes past end of device */
2133 if (ops->datbuf && (to + ops->len) > mtd->size) {
2134 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2135 "Attempt read beyond end of device\n");
2136 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002137 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002138
William Juulcfa460a2007-10-31 13:53:06 +01002139 nand_get_device(chip, mtd, FL_WRITING);
2140
2141 switch(ops->mode) {
2142 case MTD_OOB_PLACE:
2143 case MTD_OOB_AUTO:
2144 case MTD_OOB_RAW:
2145 break;
2146
2147 default:
2148 goto out;
2149 }
2150
2151 if (!ops->datbuf)
2152 ret = nand_do_write_oob(mtd, to, ops);
2153 else
2154 ret = nand_do_write_ops(mtd, to, ops);
2155
2156 out:
2157 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002158 return ret;
2159}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002160
2161/**
2162 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2163 * @mtd: MTD device structure
2164 * @page: the page address of the block which will be erased
2165 *
2166 * Standard erase command for NAND chips
2167 */
William Juulcfa460a2007-10-31 13:53:06 +01002168static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002169{
William Juulcfa460a2007-10-31 13:53:06 +01002170 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002171 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002172 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2173 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002174}
2175
2176/**
2177 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2178 * @mtd: MTD device structure
2179 * @page: the page address of the block which will be erased
2180 *
2181 * AND multi block erase command function
2182 * Erase 4 consecutive blocks
2183 */
William Juulcfa460a2007-10-31 13:53:06 +01002184static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002185{
William Juulcfa460a2007-10-31 13:53:06 +01002186 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002187 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002188 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2189 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2190 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2191 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2192 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002193}
2194
2195/**
2196 * nand_erase - [MTD Interface] erase block(s)
2197 * @mtd: MTD device structure
2198 * @instr: erase instruction
2199 *
2200 * Erase one ore more blocks
2201 */
William Juulcfa460a2007-10-31 13:53:06 +01002202static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002203{
William Juulcfa460a2007-10-31 13:53:06 +01002204 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002205}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002206
William Juulcfa460a2007-10-31 13:53:06 +01002207#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk932394a2005-08-17 12:55:25 +02002208/**
William Juulcfa460a2007-10-31 13:53:06 +01002209 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002210 * @mtd: MTD device structure
2211 * @instr: erase instruction
2212 * @allowbbt: allow erasing the bbt area
2213 *
2214 * Erase one ore more blocks
2215 */
William Juulcfa460a2007-10-31 13:53:06 +01002216int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2217 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002218{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002219 int page, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002220 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002221 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
William Juulcfa460a2007-10-31 13:53:06 +01002222 unsigned int bbt_masked_page = 0xffffffff;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002223 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002224
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002225 MTDDEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%012llx, "
2226 "len = %llu\n", (unsigned long long) instr->addr,
2227 (unsigned long long) instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002228
2229 /* Start address must align on block boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002230 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wood3167c532008-06-20 12:38:57 -05002231 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002232 return -EINVAL;
2233 }
2234
2235 /* Length must align on block boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002236 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wood3167c532008-06-20 12:38:57 -05002237 MTDDEBUG (MTD_DEBUG_LEVEL0,
2238 "nand_erase: Length not block aligned\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002239 return -EINVAL;
2240 }
2241
2242 /* Do not allow erase past end of device */
2243 if ((instr->len + instr->addr) > mtd->size) {
Scott Wood3167c532008-06-20 12:38:57 -05002244 MTDDEBUG (MTD_DEBUG_LEVEL0,
2245 "nand_erase: Erase past end of device\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002246 return -EINVAL;
2247 }
2248
2249 instr->fail_addr = 0xffffffff;
2250
2251 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002252 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002253
2254 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002255 page = (int)(instr->addr >> chip->page_shift);
2256 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002257
2258 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002259 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002260
Wolfgang Denk932394a2005-08-17 12:55:25 +02002261 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002262 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002263
Wolfgang Denk932394a2005-08-17 12:55:25 +02002264 /* Check, if it is write protected */
2265 if (nand_check_wp(mtd)) {
Scott Wood3167c532008-06-20 12:38:57 -05002266 MTDDEBUG (MTD_DEBUG_LEVEL0,
2267 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002268 instr->state = MTD_ERASE_FAILED;
2269 goto erase_exit;
2270 }
2271
William Juulcfa460a2007-10-31 13:53:06 +01002272 /*
2273 * If BBT requires refresh, set the BBT page mask to see if the BBT
2274 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2275 * can not be matched. This is also done when the bbt is actually
2276 * erased to avoid recusrsive updates
2277 */
2278 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2279 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2280
Wolfgang Denk932394a2005-08-17 12:55:25 +02002281 /* Loop through the pages */
2282 len = instr->len;
2283
2284 instr->state = MTD_ERASING;
2285
2286 while (len) {
William Juulcfa460a2007-10-31 13:53:06 +01002287 /*
2288 * heck if we have a bad block, we do not erase bad blocks !
2289 */
2290 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2291 chip->page_shift, 0, allowbbt)) {
2292 printk(KERN_WARNING "nand_erase: attempt to erase a "
2293 "bad block at page 0x%08x\n", page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002294 instr->state = MTD_ERASE_FAILED;
2295 goto erase_exit;
2296 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002297
William Juulcfa460a2007-10-31 13:53:06 +01002298 /*
2299 * Invalidate the page cache, if we erase the block which
2300 * contains the current cached page
2301 */
2302 if (page <= chip->pagebuf && chip->pagebuf <
2303 (page + pages_per_block))
2304 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002305
William Juulcfa460a2007-10-31 13:53:06 +01002306 chip->erase_cmd(mtd, page & chip->pagemask);
2307
2308 status = chip->waitfunc(mtd, chip);
2309
2310 /*
2311 * See if operation failed and additional status checks are
2312 * available
2313 */
2314 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2315 status = chip->errstat(mtd, chip, FL_ERASING,
2316 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002317
2318 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002319 if (status & NAND_STATUS_FAIL) {
Scott Wood3167c532008-06-20 12:38:57 -05002320 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2321 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002322 instr->state = MTD_ERASE_FAILED;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002323 instr->fail_addr = ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002324 goto erase_exit;
2325 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002326
William Juulcfa460a2007-10-31 13:53:06 +01002327 /*
2328 * If BBT requires refresh, set the BBT rewrite flag to the
2329 * page being erased
2330 */
2331 if (bbt_masked_page != 0xffffffff &&
2332 (page & BBT_PAGE_MASK) == bbt_masked_page)
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002333 rewrite_bbt[chipnr] =
2334 ((loff_t)page << chip->page_shift);
William Juulcfa460a2007-10-31 13:53:06 +01002335
Wolfgang Denk932394a2005-08-17 12:55:25 +02002336 /* Increment page address and decrement length */
William Juulcfa460a2007-10-31 13:53:06 +01002337 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002338 page += pages_per_block;
2339
2340 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002341 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002342 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002343 chip->select_chip(mtd, -1);
2344 chip->select_chip(mtd, chipnr);
2345
2346 /*
2347 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2348 * page mask to see if this BBT should be rewritten
2349 */
2350 if (bbt_masked_page != 0xffffffff &&
2351 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2352 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2353 BBT_PAGE_MASK;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002354 }
2355 }
2356 instr->state = MTD_ERASE_DONE;
2357
William Juulcfa460a2007-10-31 13:53:06 +01002358 erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002359
2360 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002361
2362 /* Deselect and wake up anyone waiting on the device */
2363 nand_release_device(mtd);
2364
Scott Woodc45912d2008-10-24 16:20:43 -05002365 /* Do call back function */
2366 if (!ret)
2367 mtd_erase_callback(instr);
2368
William Juulcfa460a2007-10-31 13:53:06 +01002369 /*
2370 * If BBT requires refresh and erase was successful, rewrite any
2371 * selected bad block tables
2372 */
2373 if (bbt_masked_page == 0xffffffff || ret)
2374 return ret;
2375
2376 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2377 if (!rewrite_bbt[chipnr])
2378 continue;
2379 /* update the BBT for chip */
2380 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002381 "(%d:0x%0llx 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2382 chip->bbt_td->pages[chipnr]);
William Juulcfa460a2007-10-31 13:53:06 +01002383 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2384 }
2385
Wolfgang Denk932394a2005-08-17 12:55:25 +02002386 /* Return more or less happy */
2387 return ret;
2388}
2389
2390/**
2391 * nand_sync - [MTD Interface] sync
2392 * @mtd: MTD device structure
2393 *
2394 * Sync is actually a wait for chip ready function
2395 */
William Juulcfa460a2007-10-31 13:53:06 +01002396static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002397{
William Juulcfa460a2007-10-31 13:53:06 +01002398 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002399
Scott Wood3167c532008-06-20 12:38:57 -05002400 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002401
2402 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002403 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002404 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002405 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002406}
2407
Wolfgang Denk932394a2005-08-17 12:55:25 +02002408/**
William Juulcfa460a2007-10-31 13:53:06 +01002409 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk932394a2005-08-17 12:55:25 +02002410 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01002411 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002412 */
William Juulcfa460a2007-10-31 13:53:06 +01002413static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002414{
2415 /* Check for invalid offset */
William Juulcfa460a2007-10-31 13:53:06 +01002416 if (offs > mtd->size)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002417 return -EINVAL;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002418
William Juulcfa460a2007-10-31 13:53:06 +01002419 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002420}
2421
2422/**
William Juulcfa460a2007-10-31 13:53:06 +01002423 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk932394a2005-08-17 12:55:25 +02002424 * @mtd: MTD device structure
2425 * @ofs: offset relative to mtd start
2426 */
William Juulcfa460a2007-10-31 13:53:06 +01002427static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002428{
William Juulcfa460a2007-10-31 13:53:06 +01002429 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002430 int ret;
2431
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002432 if ((ret = nand_block_isbad(mtd, ofs))) {
2433 /* If it was bad already, return success and do nothing. */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002434 if (ret > 0)
2435 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002436 return ret;
2437 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002438
William Juulcfa460a2007-10-31 13:53:06 +01002439 return chip->block_markbad(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002440}
2441
2442/**
William Juulcfa460a2007-10-31 13:53:06 +01002443 * nand_suspend - [MTD Interface] Suspend the NAND flash
2444 * @mtd: MTD device structure
2445 */
2446static int nand_suspend(struct mtd_info *mtd)
2447{
2448 struct nand_chip *chip = mtd->priv;
2449
2450 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2451}
2452
2453/**
2454 * nand_resume - [MTD Interface] Resume the NAND flash
2455 * @mtd: MTD device structure
2456 */
2457static void nand_resume(struct mtd_info *mtd)
2458{
2459 struct nand_chip *chip = mtd->priv;
2460
2461 if (chip->state == FL_PM_SUSPENDED)
2462 nand_release_device(mtd);
2463 else
2464 printk(KERN_ERR "nand_resume() called for a chip which is not "
2465 "in suspended state\n");
2466}
2467
2468/*
2469 * Set default functions
2470 */
2471static void nand_set_defaults(struct nand_chip *chip, int busw)
2472{
2473 /* check for proper chip_delay setup, set 20us if not */
2474 if (!chip->chip_delay)
2475 chip->chip_delay = 20;
2476
2477 /* check, if a user supplied command function given */
2478 if (chip->cmdfunc == NULL)
2479 chip->cmdfunc = nand_command;
2480
2481 /* check, if a user supplied wait function given */
2482 if (chip->waitfunc == NULL)
2483 chip->waitfunc = nand_wait;
2484
2485 if (!chip->select_chip)
2486 chip->select_chip = nand_select_chip;
2487 if (!chip->read_byte)
2488 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2489 if (!chip->read_word)
2490 chip->read_word = nand_read_word;
2491 if (!chip->block_bad)
2492 chip->block_bad = nand_block_bad;
2493 if (!chip->block_markbad)
2494 chip->block_markbad = nand_default_block_markbad;
2495 if (!chip->write_buf)
2496 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2497 if (!chip->read_buf)
2498 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2499 if (!chip->verify_buf)
2500 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2501 if (!chip->scan_bbt)
2502 chip->scan_bbt = nand_default_bbt;
2503
2504 if (!chip->controller) {
2505 chip->controller = &chip->hwcontrol;
2506
2507 /* XXX U-BOOT XXX */
2508#if 0
2509 spin_lock_init(&chip->controller->lock);
2510 init_waitqueue_head(&chip->controller->wq);
2511#endif
2512 }
2513
2514}
2515
2516/*
2517 * Get the flash and manufacturer id and lookup if the type is supported
2518 */
2519static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2520 struct nand_chip *chip,
2521 int busw, int *maf_id)
2522{
2523 struct nand_flash_dev *type = NULL;
2524 int i, dev_id, maf_idx;
Scott Woodc45912d2008-10-24 16:20:43 -05002525 int tmp_id, tmp_manf;
William Juulcfa460a2007-10-31 13:53:06 +01002526
2527 /* Select the device */
2528 chip->select_chip(mtd, 0);
2529
Karl Beldan33efde52008-09-15 16:08:03 +02002530 /*
2531 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2532 * after power-up
2533 */
2534 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2535
William Juulcfa460a2007-10-31 13:53:06 +01002536 /* Send the command for reading device ID */
2537 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2538
2539 /* Read manufacturer and device IDs */
2540 *maf_id = chip->read_byte(mtd);
2541 dev_id = chip->read_byte(mtd);
2542
Scott Woodc45912d2008-10-24 16:20:43 -05002543 /* Try again to make sure, as some systems the bus-hold or other
2544 * interface concerns can cause random data which looks like a
2545 * possibly credible NAND flash to appear. If the two results do
2546 * not match, ignore the device completely.
2547 */
2548
2549 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2550
2551 /* Read manufacturer and device IDs */
2552
2553 tmp_manf = chip->read_byte(mtd);
2554 tmp_id = chip->read_byte(mtd);
2555
2556 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2557 printk(KERN_INFO "%s: second ID read did not match "
2558 "%02x,%02x against %02x,%02x\n", __func__,
2559 *maf_id, dev_id, tmp_manf, tmp_id);
2560 return ERR_PTR(-ENODEV);
2561 }
2562
William Juulcfa460a2007-10-31 13:53:06 +01002563 /* Lookup the flash id */
2564 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2565 if (dev_id == nand_flash_ids[i].id) {
2566 type = &nand_flash_ids[i];
2567 break;
2568 }
2569 }
2570
2571 if (!type)
2572 return ERR_PTR(-ENODEV);
2573
2574 if (!mtd->name)
2575 mtd->name = type->name;
2576
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002577 chip->chipsize = (uint64_t)type->chipsize << 20;
William Juulcfa460a2007-10-31 13:53:06 +01002578
2579 /* Newer devices have all the information in additional id bytes */
2580 if (!type->pagesize) {
2581 int extid;
2582 /* The 3rd id byte holds MLC / multichip data */
2583 chip->cellinfo = chip->read_byte(mtd);
2584 /* The 4th id byte is the important one */
2585 extid = chip->read_byte(mtd);
2586 /* Calc pagesize */
2587 mtd->writesize = 1024 << (extid & 0x3);
2588 extid >>= 2;
2589 /* Calc oobsize */
2590 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2591 extid >>= 2;
2592 /* Calc blocksize. Blocksize is multiples of 64KiB */
2593 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2594 extid >>= 2;
2595 /* Get buswidth information */
2596 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2597
2598 } else {
2599 /*
2600 * Old devices have chip data hardcoded in the device id table
2601 */
2602 mtd->erasesize = type->erasesize;
2603 mtd->writesize = type->pagesize;
2604 mtd->oobsize = mtd->writesize / 32;
2605 busw = type->options & NAND_BUSWIDTH_16;
2606 }
2607
2608 /* Try to identify manufacturer */
2609 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2610 if (nand_manuf_ids[maf_idx].id == *maf_id)
2611 break;
2612 }
2613
2614 /*
2615 * Check, if buswidth is correct. Hardware drivers should set
2616 * chip correct !
2617 */
2618 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2619 printk(KERN_INFO "NAND device: Manufacturer ID:"
2620 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2621 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2622 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2623 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2624 busw ? 16 : 8);
2625 return ERR_PTR(-EINVAL);
2626 }
2627
2628 /* Calculate the address shift from the page size */
2629 chip->page_shift = ffs(mtd->writesize) - 1;
2630 /* Convert chipsize to number of pages per chip -1. */
2631 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2632
2633 chip->bbt_erase_shift = chip->phys_erase_shift =
2634 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002635 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05002636 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002637 else
2638 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 31;
William Juulcfa460a2007-10-31 13:53:06 +01002639
2640 /* Set the bad block position */
2641 chip->badblockpos = mtd->writesize > 512 ?
2642 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2643
2644 /* Get chip options, preserve non chip based options */
2645 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2646 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
2647
2648 /*
2649 * Set chip as a default. Board drivers can override it, if necessary
2650 */
2651 chip->options |= NAND_NO_AUTOINCR;
2652
2653 /* Check if chip is a not a samsung device. Do not clear the
2654 * options for chips which are not having an extended id.
2655 */
2656 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2657 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2658
2659 /* Check for AND chips with 4 page planes */
2660 if (chip->options & NAND_4PAGE_ARRAY)
2661 chip->erase_cmd = multi_erase_cmd;
2662 else
2663 chip->erase_cmd = single_erase_cmd;
2664
2665 /* Do not replace user supplied command function ! */
2666 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2667 chip->cmdfunc = nand_command_lp;
2668
Stefan Roesee52b34d2008-01-10 18:47:33 +01002669 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2670 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2671 nand_manuf_ids[maf_idx].name, type->name);
William Juulcfa460a2007-10-31 13:53:06 +01002672
2673 return type;
2674}
2675
2676/**
2677 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2678 * @mtd: MTD device structure
2679 * @maxchips: Number of chips to scan for
2680 *
2681 * This is the first phase of the normal nand_scan() function. It
2682 * reads the flash ID and sets up MTD fields accordingly.
2683 *
2684 * The mtd->owner field must be set to the module of the caller.
2685 */
2686int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2687{
2688 int i, busw, nand_maf_id;
2689 struct nand_chip *chip = mtd->priv;
2690 struct nand_flash_dev *type;
2691
2692 /* Get buswidth to select the correct functions */
2693 busw = chip->options & NAND_BUSWIDTH_16;
2694 /* Set the default functions */
2695 nand_set_defaults(chip, busw);
2696
2697 /* Read the flash type */
2698 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2699
2700 if (IS_ERR(type)) {
Peter Tyser10dc6a92009-02-04 13:39:40 -06002701#ifndef CONFIG_SYS_NAND_QUIET_TEST
William Juulcfa460a2007-10-31 13:53:06 +01002702 printk(KERN_WARNING "No NAND device found!!!\n");
Peter Tyser10dc6a92009-02-04 13:39:40 -06002703#endif
William Juulcfa460a2007-10-31 13:53:06 +01002704 chip->select_chip(mtd, -1);
2705 return PTR_ERR(type);
2706 }
2707
2708 /* Check for a chip array */
2709 for (i = 1; i < maxchips; i++) {
2710 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02002711 /* See comment in nand_get_flash_type for reset */
2712 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002713 /* Send the command for reading device ID */
2714 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2715 /* Read manufacturer and device IDs */
2716 if (nand_maf_id != chip->read_byte(mtd) ||
2717 type->id != chip->read_byte(mtd))
2718 break;
2719 }
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01002720#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01002721 if (i > 1)
2722 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01002723#endif
William Juulcfa460a2007-10-31 13:53:06 +01002724
2725 /* Store the number of chips and calc total size for mtd */
2726 chip->numchips = i;
2727 mtd->size = i * chip->chipsize;
2728
2729 return 0;
2730}
2731
2732
2733/**
2734 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2735 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01002736 *
2737 * This is the second phase of the normal nand_scan() function. It
2738 * fills out all the uninitialized function pointers with the defaults
2739 * and scans for a bad block table if appropriate.
2740 */
2741int nand_scan_tail(struct mtd_info *mtd)
2742{
2743 int i;
2744 struct nand_chip *chip = mtd->priv;
2745
2746 if (!(chip->options & NAND_OWN_BUFFERS))
2747 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2748 if (!chip->buffers)
2749 return -ENOMEM;
2750
2751 /* Set the internal oob buffer location, just after the page data */
2752 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2753
2754 /*
2755 * If no default placement scheme is given, select an appropriate one
2756 */
2757 if (!chip->ecc.layout) {
2758 switch (mtd->oobsize) {
2759 case 8:
2760 chip->ecc.layout = &nand_oob_8;
2761 break;
2762 case 16:
2763 chip->ecc.layout = &nand_oob_16;
2764 break;
2765 case 64:
2766 chip->ecc.layout = &nand_oob_64;
2767 break;
2768 case 128:
2769 chip->ecc.layout = &nand_oob_128;
2770 break;
2771 default:
2772 printk(KERN_WARNING "No oob scheme defined for "
2773 "oobsize %d\n", mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01002774 }
2775 }
2776
2777 if (!chip->write_page)
2778 chip->write_page = nand_write_page;
2779
2780 /*
2781 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2782 * selected and we have 256 byte pagesize fallback to software ECC
2783 */
2784 if (!chip->ecc.read_page_raw)
2785 chip->ecc.read_page_raw = nand_read_page_raw;
2786 if (!chip->ecc.write_page_raw)
2787 chip->ecc.write_page_raw = nand_write_page_raw;
2788
2789 switch (chip->ecc.mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04002790 case NAND_ECC_HW_OOB_FIRST:
2791 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2792 if (!chip->ecc.calculate || !chip->ecc.correct ||
2793 !chip->ecc.hwctl) {
2794 printk(KERN_WARNING "No ECC functions supplied, "
2795 "Hardware ECC not possible\n");
2796 BUG();
2797 }
2798 if (!chip->ecc.read_page)
2799 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2800
William Juulcfa460a2007-10-31 13:53:06 +01002801 case NAND_ECC_HW:
2802 /* Use standard hwecc read page function ? */
2803 if (!chip->ecc.read_page)
2804 chip->ecc.read_page = nand_read_page_hwecc;
2805 if (!chip->ecc.write_page)
2806 chip->ecc.write_page = nand_write_page_hwecc;
2807 if (!chip->ecc.read_oob)
2808 chip->ecc.read_oob = nand_read_oob_std;
2809 if (!chip->ecc.write_oob)
2810 chip->ecc.write_oob = nand_write_oob_std;
2811
2812 case NAND_ECC_HW_SYNDROME:
Scott Wood41ef8c72008-03-18 15:29:14 -05002813 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2814 !chip->ecc.hwctl) &&
2815 (!chip->ecc.read_page ||
2816 chip->ecc.read_page == nand_read_page_hwecc ||
2817 !chip->ecc.write_page ||
2818 chip->ecc.write_page == nand_write_page_hwecc)) {
William Juulcfa460a2007-10-31 13:53:06 +01002819 printk(KERN_WARNING "No ECC functions supplied, "
2820 "Hardware ECC not possible\n");
2821 BUG();
2822 }
2823 /* Use standard syndrome read/write page function ? */
2824 if (!chip->ecc.read_page)
2825 chip->ecc.read_page = nand_read_page_syndrome;
2826 if (!chip->ecc.write_page)
2827 chip->ecc.write_page = nand_write_page_syndrome;
2828 if (!chip->ecc.read_oob)
2829 chip->ecc.read_oob = nand_read_oob_syndrome;
2830 if (!chip->ecc.write_oob)
2831 chip->ecc.write_oob = nand_write_oob_syndrome;
2832
2833 if (mtd->writesize >= chip->ecc.size)
2834 break;
2835 printk(KERN_WARNING "%d byte HW ECC not possible on "
2836 "%d byte page size, fallback to SW ECC\n",
2837 chip->ecc.size, mtd->writesize);
2838 chip->ecc.mode = NAND_ECC_SOFT;
2839
2840 case NAND_ECC_SOFT:
2841 chip->ecc.calculate = nand_calculate_ecc;
2842 chip->ecc.correct = nand_correct_data;
2843 chip->ecc.read_page = nand_read_page_swecc;
Scott Woodc45912d2008-10-24 16:20:43 -05002844 chip->ecc.read_subpage = nand_read_subpage;
William Juulcfa460a2007-10-31 13:53:06 +01002845 chip->ecc.write_page = nand_write_page_swecc;
2846 chip->ecc.read_oob = nand_read_oob_std;
2847 chip->ecc.write_oob = nand_write_oob_std;
2848 chip->ecc.size = 256;
2849 chip->ecc.bytes = 3;
2850 break;
2851
2852 case NAND_ECC_NONE:
2853 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2854 "This is not recommended !!\n");
2855 chip->ecc.read_page = nand_read_page_raw;
2856 chip->ecc.write_page = nand_write_page_raw;
2857 chip->ecc.read_oob = nand_read_oob_std;
2858 chip->ecc.write_oob = nand_write_oob_std;
2859 chip->ecc.size = mtd->writesize;
2860 chip->ecc.bytes = 0;
2861 break;
2862
2863 default:
2864 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2865 chip->ecc.mode);
2866 BUG();
2867 }
2868
2869 /*
2870 * The number of bytes available for a client to place data into
2871 * the out of band area
2872 */
2873 chip->ecc.layout->oobavail = 0;
Sandeep Paulraj5df3c2b2009-11-07 14:25:18 -05002874 for (i = 0; chip->ecc.layout->oobfree[i].length
2875 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
William Juulcfa460a2007-10-31 13:53:06 +01002876 chip->ecc.layout->oobavail +=
2877 chip->ecc.layout->oobfree[i].length;
2878 mtd->oobavail = chip->ecc.layout->oobavail;
2879
2880 /*
2881 * Set the number of read / write steps for one page depending on ECC
2882 * mode
2883 */
2884 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2885 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2886 printk(KERN_WARNING "Invalid ecc parameters\n");
2887 BUG();
2888 }
2889 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
2890
2891 /*
2892 * Allow subpage writes up to ecc.steps. Not possible for MLC
2893 * FLASH.
2894 */
2895 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2896 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2897 switch(chip->ecc.steps) {
2898 case 2:
2899 mtd->subpage_sft = 1;
2900 break;
2901 case 4:
2902 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05002903 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01002904 mtd->subpage_sft = 2;
2905 break;
2906 }
2907 }
2908 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2909
2910 /* Initialize state */
2911 chip->state = FL_READY;
2912
2913 /* De-select the device */
2914 chip->select_chip(mtd, -1);
2915
2916 /* Invalidate the pagebuffer reference */
2917 chip->pagebuf = -1;
2918
2919 /* Fill in remaining MTD driver data */
2920 mtd->type = MTD_NANDFLASH;
2921 mtd->flags = MTD_CAP_NANDFLASH;
2922 mtd->erase = nand_erase;
2923 mtd->point = NULL;
2924 mtd->unpoint = NULL;
2925 mtd->read = nand_read;
2926 mtd->write = nand_write;
2927 mtd->read_oob = nand_read_oob;
2928 mtd->write_oob = nand_write_oob;
2929 mtd->sync = nand_sync;
2930 mtd->lock = NULL;
2931 mtd->unlock = NULL;
2932 mtd->suspend = nand_suspend;
2933 mtd->resume = nand_resume;
2934 mtd->block_isbad = nand_block_isbad;
2935 mtd->block_markbad = nand_block_markbad;
2936
2937 /* propagate ecc.layout to mtd_info */
2938 mtd->ecclayout = chip->ecc.layout;
2939
2940 /* Check, if we should skip the bad block table scan */
2941 if (chip->options & NAND_SKIP_BBTSCAN)
Ilya Yanok13f0fd92008-06-30 15:34:40 +02002942 chip->options |= NAND_BBT_SCANNED;
William Juulcfa460a2007-10-31 13:53:06 +01002943
Ilya Yanok13f0fd92008-06-30 15:34:40 +02002944 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002945}
2946
2947/* module_text_address() isn't exported, and it's mostly a pointless
2948 test if this is a module _anyway_ -- they'd have to try _really_ hard
2949 to call us from in-kernel code if the core NAND support is modular. */
2950#ifdef MODULE
2951#define caller_is_module() (1)
2952#else
2953#define caller_is_module() \
2954 module_text_address((unsigned long)__builtin_return_address(0))
2955#endif
2956
2957/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02002958 * nand_scan - [NAND Interface] Scan for the NAND device
2959 * @mtd: MTD device structure
2960 * @maxchips: Number of chips to scan for
2961 *
William Juulcfa460a2007-10-31 13:53:06 +01002962 * This fills out all the uninitialized function pointers
Wolfgang Denk932394a2005-08-17 12:55:25 +02002963 * with the defaults.
2964 * The flash ID is read and the mtd/chip structures are
William Juulcfa460a2007-10-31 13:53:06 +01002965 * filled with the appropriate values.
2966 * The mtd->owner field must be set to the module of the caller
Wolfgang Denk932394a2005-08-17 12:55:25 +02002967 *
2968 */
William Juulcfa460a2007-10-31 13:53:06 +01002969int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002970{
William Juulcfa460a2007-10-31 13:53:06 +01002971 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002972
William Juulcfa460a2007-10-31 13:53:06 +01002973 /* Many callers got this wrong, so check for it for a while... */
2974 /* XXX U-BOOT XXX */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002975#if 0
William Juulcfa460a2007-10-31 13:53:06 +01002976 if (!mtd->owner && caller_is_module()) {
2977 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2978 BUG();
2979 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002980#endif
William Juul4cbb6512007-11-08 10:39:53 +01002981
William Juulcfa460a2007-10-31 13:53:06 +01002982 ret = nand_scan_ident(mtd, maxchips);
2983 if (!ret)
2984 ret = nand_scan_tail(mtd);
2985 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002986}
2987
2988/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002989 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk932394a2005-08-17 12:55:25 +02002990 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01002991*/
2992void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002993{
William Juulcfa460a2007-10-31 13:53:06 +01002994 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002995
2996#ifdef CONFIG_MTD_PARTITIONS
2997 /* Deregister partitions */
William Juulcfa460a2007-10-31 13:53:06 +01002998 del_mtd_partitions(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002999#endif
3000 /* Deregister the device */
William Juulcfa460a2007-10-31 13:53:06 +01003001 /* XXX U-BOOT XXX */
Wolfgang Denk932394a2005-08-17 12:55:25 +02003002#if 0
William Juulcfa460a2007-10-31 13:53:06 +01003003 del_mtd_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003004#endif
William Juulcfa460a2007-10-31 13:53:06 +01003005
3006 /* Free bad block table memory */
3007 kfree(chip->bbt);
3008 if (!(chip->options & NAND_OWN_BUFFERS))
3009 kfree(chip->buffers);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003010}
3011
William Juulcfa460a2007-10-31 13:53:06 +01003012/* XXX U-BOOT XXX */
3013#if 0
3014EXPORT_SYMBOL_GPL(nand_scan);
3015EXPORT_SYMBOL_GPL(nand_scan_ident);
3016EXPORT_SYMBOL_GPL(nand_scan_tail);
3017EXPORT_SYMBOL_GPL(nand_release);
3018
3019static int __init nand_base_init(void)
3020{
3021 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3022 return 0;
3023}
3024
3025static void __exit nand_base_exit(void)
3026{
3027 led_trigger_unregister_simple(nand_led_trigger);
3028}
3029
3030module_init(nand_base_init);
3031module_exit(nand_base_exit);
3032
3033MODULE_LICENSE("GPL");
3034MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3035MODULE_DESCRIPTION("Generic NAND flash driver code");
Wolfgang Denk932394a2005-08-17 12:55:25 +02003036#endif