blob: ed1c9c9a88aa906b70dbd83698f7406e010dd774 [file] [log] [blame]
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +02008 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * Additional technical information is available on
Scott Wood3628f002008-10-24 16:20:43 -050010 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020011 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juul52c07962007-10-31 13:53:06 +010013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020014 *
William Juul52c07962007-10-31 13:53:06 +010015 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020016 * David Woodhouse for adding multichip support
17 *
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +010021 * TODO:
Wolfgang Denk2f9b7e42005-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 Wood3628f002008-10-24 16:20:43 -050027 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020028 *
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +010037#include <linux/module.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020038#include <linux/delay.h>
39#include <linux/errno.h>
William Juul52c07962007-10-31 13:53:06 +010040#include <linux/err.h>
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +010050#include <linux/leds.h>
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +020057#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020058
59#include <common.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010060
William Juul52c07962007-10-31 13:53:06 +010061#define ENOTSUPP 524 /* Operation is not supported */
62
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020063#include <malloc.h>
64#include <watchdog.h>
William Juul52c07962007-10-31 13:53:06 +010065#include <linux/err.h>
Wolfgang Denk2f9b7e42005-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 Roesefa252ea2009-04-24 15:58:33 +020071#ifdef CONFIG_MTD_PARTITIONS
72#include <linux/mtd/partitions.h>
73#endif
74
Wolfgang Denk2f9b7e42005-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 Tyserf9f36222009-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 Denk2f9b7e42005-08-17 12:55:25 +020093/* Define default oob placement schemes for large and small page devices */
William Juul52c07962007-10-31 13:53:06 +010094static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020095 .eccbytes = 3,
96 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010097 .oobfree = {
98 {.offset = 3,
99 .length = 2},
100 {.offset = 6,
101 .length = 2}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200102};
103
William Juul52c07962007-10-31 13:53:06 +0100104static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200105 .eccbytes = 6,
106 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +0100107 .oobfree = {
108 {.offset = 8,
109 . length = 8}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200110};
111
William Juul52c07962007-10-31 13:53:06 +0100112static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200113 .eccbytes = 24,
114 .eccpos = {
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200121};
122
William Juul52c07962007-10-31 13:53:06 +0100123static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200124 .eccbytes = 48,
125 .eccpos = {
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200135};
136
William Juul52c07962007-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 Poselenov04fbaa02008-06-06 15:42:43 +0200145
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146/*
Scott Wood3628f002008-10-24 16:20:43 -0500147 * For devices which display every fart in the system on a separate LED. Is
William Juul52c07962007-10-31 13:53:06 +0100148 * compiled away when LED support is disabled.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200149 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200150/* XXX U-BOOT XXX */
151#if 0
William Juul52c07962007-10-31 13:53:06 +0100152DEFINE_LED_TRIGGER(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200153#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200154
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200155/**
156 * nand_release_device - [GENERIC] release chip
157 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200158 *
159 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160 */
161/* XXX U-BOOT XXX */
162#if 0
William Juul52c07962007-10-31 13:53:06 +0100163static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200164{
William Juul52c07962007-10-31 13:53:06 +0100165 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200166
167 /* De-select the NAND device */
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200176}
177#else
Wolfgang Denk9a08dd12005-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 Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100191static uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192{
William Juul52c07962007-10-31 13:53:06 +0100193 struct nand_chip *chip = mtd->priv;
194 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200201 * Default read function for 16bit buswith with
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200202 * endianess conversion
203 */
William Juul52c07962007-10-31 13:53:06 +0100204static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200205{
William Juul52c07962007-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 Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200214 * Default read function for 16bit buswith without
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200215 * endianess conversion
216 */
217static u16 nand_read_word(struct mtd_info *mtd)
218{
William Juul52c07962007-10-31 13:53:06 +0100219 struct nand_chip *chip = mtd->priv;
220 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200221}
222
223/**
224 * nand_select_chip - [DEFAULT] control CE line
225 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100226 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200227 *
228 * Default select function for 1 chip devices.
229 */
William Juul52c07962007-10-31 13:53:06 +0100230static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200231{
William Juul52c07962007-10-31 13:53:06 +0100232 struct nand_chip *chip = mtd->priv;
233
234 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200235 case -1:
William Juul52c07962007-10-31 13:53:06 +0100236 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200237 break;
238 case 0:
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100254static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200255{
256 int i;
William Juul52c07962007-10-31 13:53:06 +0100257 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200258
William Juul52c07962007-10-31 13:53:06 +0100259 for (i = 0; i < len; i++)
260 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200261}
262
263/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200264 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100271static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200272{
273 int i;
William Juul52c07962007-10-31 13:53:06 +0100274 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200275
William Juul52c07962007-10-31 13:53:06 +0100276 for (i = 0; i < len; i++)
277 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200278}
279
280/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200281 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100288static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200289{
290 int i;
William Juul52c07962007-10-31 13:53:06 +0100291 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200292
William Juul52c07962007-10-31 13:53:06 +0100293 for (i = 0; i < len; i++)
294 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200295 return -EFAULT;
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100307static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200308{
309 int i;
William Juul52c07962007-10-31 13:53:06 +0100310 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200311 u16 *p = (u16 *) buf;
312 len >>= 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200313
William Juul52c07962007-10-31 13:53:06 +0100314 for (i = 0; i < len; i++)
315 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200316
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200317}
318
319/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200320 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100327static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200328{
329 int i;
William Juul52c07962007-10-31 13:53:06 +0100330 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200331 u16 *p = (u16 *) buf;
332 len >>= 1;
333
William Juul52c07962007-10-31 13:53:06 +0100334 for (i = 0; i < len; i++)
335 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200336}
337
338/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200339 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100346static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200347{
348 int i;
William Juul52c07962007-10-31 13:53:06 +0100349 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200350 u16 *p = (u16 *) buf;
351 len >>= 1;
352
William Juul52c07962007-10-31 13:53:06 +0100353 for (i = 0; i < len; i++)
354 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +0200366 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100371 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200372 u16 bad;
373
William Juul52c07962007-10-31 13:53:06 +0100374 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200375
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200376 if (getchip) {
William Juul52c07962007-10-31 13:53:06 +0100377 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200378
William Juul52c07962007-10-31 13:53:06 +0100379 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200380
381 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100382 chip->select_chip(mtd, chipnr);
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200383 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200384
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200391 if ((bad & 0xFF) != 0xff)
392 res = 1;
393 } else {
William Juul52c07962007-10-31 13:53:06 +0100394 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
395 if (chip->read_byte(mtd) != 0xff)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200396 res = 1;
397 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200398
William Juul52c07962007-10-31 13:53:06 +0100399 if (getchip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200400 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200401
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100415 struct nand_chip *chip = mtd->priv;
416 uint8_t buf[2] = { 0, 0 };
417 int block, ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200418
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200419 /* Get block number */
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200423
424 /* Do we have a flash based bad block table ? */
William Juul52c07962007-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 Wood3628f002008-10-24 16:20:43 -0500431 nand_get_device(chip, mtd, FL_WRITING);
William Juul52c07962007-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 Denk7b9bc3a2005-09-14 23:53:32 +0200437
William Juul52c07962007-10-31 13:53:06 +0100438 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Scott Wood3628f002008-10-24 16:20:43 -0500439 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100440 }
441 if (!ret)
442 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500443
William Juul52c07962007-10-31 13:53:06 +0100444 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200445}
446
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200447/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200448 * nand_check_wp - [GENERIC] check if the chip is write protected
449 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200450 * Check, if the device is write protected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200451 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200452 * The function expects, that the device is already selected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200453 */
William Juul52c07962007-10-31 13:53:06 +0100454static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200455{
William Juul52c07962007-10-31 13:53:06 +0100456 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200457 /* Check the WP bit */
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200460}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100461
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100472static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
473 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200474{
William Juul52c07962007-10-31 13:53:06 +0100475 struct nand_chip *chip = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200476
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200477 if (!(chip->options & NAND_BBT_SCANNED)) {
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200478 chip->options |= NAND_BBT_SCANNED;
Scott Wood11b12a32008-12-16 14:24:16 -0600479 chip->scan_bbt(mtd);
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200480 }
481
William Juul52c07962007-10-31 13:53:06 +0100482 if (!chip->bbt)
483 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200484
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200485 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100486 return nand_isbad_bbt(mtd, ofs, allowbbt);
487}
488
489/*
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-VILLARD03836942008-10-16 15:01:15 +0200514 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Stefan Roesea5c312c2008-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 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200524}
William Juul52c07962007-10-31 13:53:06 +0100525#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200526
527/**
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 Juul52c07962007-10-31 13:53:06 +0100537static void nand_command(struct mtd_info *mtd, unsigned int command,
538 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200539{
William Juul52c07962007-10-31 13:53:06 +0100540 register struct nand_chip *chip = mtd->priv;
541 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyserf9f36222009-02-04 13:47:22 -0600542 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200543
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100550 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200551 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100552 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100561 chip->cmd_ctrl(mtd, readcmd, ctrl);
562 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200563 }
William Juul52c07962007-10-31 13:53:06 +0100564 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200565
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200577 }
William Juul52c07962007-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 Denk7b9bc3a2005-09-14 23:53:32 +0200587
588 /*
589 * program and erase have their own busy handlers
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200590 * status and sequential in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100591 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200592 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200593
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100602 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 break;
William Juul52c07962007-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 Tyserf9f36222009-02-04 13:47:22 -0600609 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
610 (rst_sts_cnt--));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200611 return;
612
William Juul52c07962007-10-31 13:53:06 +0100613 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200614 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200615 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200616 * If we don't have access to the busy pin, we apply the given
617 * command delay
William Juul52c07962007-10-31 13:53:06 +0100618 */
619 if (!chip->dev_ready) {
620 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200621 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200622 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200623 }
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100626 ndelay(100);
627
628 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200641 */
William Juul52c07962007-10-31 13:53:06 +0100642static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
643 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200644{
William Juul52c07962007-10-31 13:53:06 +0100645 register struct nand_chip *chip = mtd->priv;
Peter Tyserf9f36222009-02-04 13:47:22 -0600646 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200647
648 /* Emulate NAND_CMD_READOOB */
649 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100650 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200651 command = NAND_CMD_READ0;
652 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200653
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200657
658 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100659 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200660
661 /* Serially input address */
662 if (column != -1) {
663 /* Adjust columns for 16 bit buswidth */
William Juul52c07962007-10-31 13:53:06 +0100664 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200665 column >>= 1;
William Juul52c07962007-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 Denk7b9bc3a2005-09-14 23:53:32 +0200669 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200670 if (page_addr != -1) {
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200674 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200678 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200679 }
William Juul52c07962007-10-31 13:53:06 +0100680 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200681
682 /*
683 * program and erase have their own busy handlers
William Juul52c07962007-10-31 13:53:06 +0100684 * status, sequential in, and deplete1 need no delay
685 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200686 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200687
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100693 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200694 case NAND_CMD_STATUS:
William Juul52c07962007-10-31 13:53:06 +0100695 case NAND_CMD_DEPLETE1:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200696 return;
697
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200708
709 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100710 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200711 break;
William Juul52c07962007-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 Tyserf9f36222009-02-04 13:47:22 -0600717 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
718 (rst_sts_cnt--));
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200727 return;
728
729 case NAND_CMD_READ0:
William Juul52c07962007-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 Denk7b9bc3a2005-09-14 23:53:32 +0200734
William Juul52c07962007-10-31 13:53:06 +0100735 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200736 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200737 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200738 * If we don't have access to the busy pin, we apply the given
739 * command delay
William Juul52c07962007-10-31 13:53:06 +0100740 */
741 if (!chip->dev_ready) {
742 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200743 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200744 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200745 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200746
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100749 ndelay(100);
750
751 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200752}
753
754/**
755 * nand_get_device - [GENERIC] Get chip for selected access
William Juul52c07962007-10-31 13:53:06 +0100756 * @chip: the nand chip descriptor
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200757 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200758 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100764static int
765nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200766{
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200772
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200773 /* Hardware controller shared among independend devices */
William Juul52c07962007-10-31 13:53:06 +0100774 /* Hardware controller shared among independend devices */
775 if (!chip->controller->active)
776 chip->controller->active = chip;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200777
William Juul52c07962007-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;
782 }
783 if (new_state == FL_PM_SUSPENDED) {
784 spin_unlock(lock);
785 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200786 }
William Juul52c07962007-10-31 13:53:06 +0100787 set_current_state(TASK_UNINTERRUPTIBLE);
788 add_wait_queue(wq, &wait);
789 spin_unlock(lock);
790 schedule();
791 remove_wait_queue(wq, &wait);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200792 goto retry;
793}
794#else
William Juul52c07962007-10-31 13:53:06 +0100795static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
796{
Marcel Ziswilerf30a2aa2008-06-22 16:30:06 +0200797 this->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100798 return 0;
799}
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100805 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200806 *
807 * Wait for command done. This applies to erase and program only
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200808 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200809 * general NAND and SmartMedia specs
William Juul52c07962007-10-31 13:53:06 +0100810 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200811/* XXX U-BOOT XXX */
812#if 0
William Juul52c07962007-10-31 13:53:06 +0100813static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200814{
William Juul52c07962007-10-31 13:53:06 +0100815
816 unsigned long timeo = jiffies;
817 int status, state = chip->state;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200818
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200819 if (state == FL_ERASING)
William Juul52c07962007-10-31 13:53:06 +0100820 timeo += (HZ * 400) / 1000;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200821 else
William Juul52c07962007-10-31 13:53:06 +0100822 timeo += (HZ * 20) / 1000;
823
824 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +0100828 ndelay(100);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200829
William Juul52c07962007-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 Denk7b9bc3a2005-09-14 23:53:32 +0200832 else
William Juul52c07962007-10-31 13:53:06 +0100833 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200834
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200835 while (time_before(jiffies, timeo)) {
William Juul52c07962007-10-31 13:53:06 +0100836 if (chip->dev_ready) {
837 if (chip->dev_ready(mtd))
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200838 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200839 } else {
William Juul52c07962007-10-31 13:53:06 +0100840 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200841 break;
842 }
William Juul52c07962007-10-31 13:53:06 +0100843 cond_resched();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200844 }
William Juul52c07962007-10-31 13:53:06 +0100845 led_trigger_event(nand_led_trigger, LED_OFF);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200846
William Juul52c07962007-10-31 13:53:06 +0100847 status = (int)chip->read_byte(mtd);
848 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200849}
850#else
William Juul52c07962007-10-31 13:53:06 +0100851static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200852{
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100853 unsigned long timeo;
William Juul52c07962007-10-31 13:53:06 +0100854 int state = this->state;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100855
856 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200857 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100858 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200859 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk9a08dd12005-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 Sieka6eed2ab2006-02-24 09:37:22 +0100866 reset_timer();
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100867
868 while (1) {
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100869 if (get_timer(0) > timeo) {
870 printf("Timeout!");
Stefan Roeseef26d242006-11-27 17:22:19 +0100871 return 0x01;
872 }
Wolfgang Denk9a08dd12005-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 Sieka582f1a32006-03-05 18:57:33 +0100882#ifdef PPCHAMELON_NAND_TIMER_HACK
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100883 reset_timer();
884 while (get_timer(0) < 10);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100885#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100886
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100887 return this->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200888}
889#endif
890
891/**
William Juul52c07962007-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 Paulraj22bc7ba2009-11-07 14:24:50 -0500896 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -0500897 *
898 * Not for syndrome calculating ecc controllers, which use a special oob layout
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200899 */
William Juul52c07962007-10-31 13:53:06 +0100900static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400901 uint8_t *buf, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200902{
William Juul52c07962007-10-31 13:53:06 +0100903 chip->read_buf(mtd, buf, mtd->writesize);
904 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
905 return 0;
906}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200907
William Juul52c07962007-10-31 13:53:06 +0100908/**
David Brownellee86b8d2009-11-07 16:27:01 -0500909 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
910 * @mtd: mtd info structure
911 * @chip: nand chip info structure
912 * @buf: buffer to store read data
913 * @page: page number to read
914 *
915 * We need a special oob layout and handling even when OOB isn't used.
916 */
917static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
918 uint8_t *buf, int page)
919{
920 int eccsize = chip->ecc.size;
921 int eccbytes = chip->ecc.bytes;
922 uint8_t *oob = chip->oob_poi;
923 int steps, size;
924
925 for (steps = chip->ecc.steps; steps > 0; steps--) {
926 chip->read_buf(mtd, buf, eccsize);
927 buf += eccsize;
928
929 if (chip->ecc.prepad) {
930 chip->read_buf(mtd, oob, chip->ecc.prepad);
931 oob += chip->ecc.prepad;
932 }
933
934 chip->read_buf(mtd, oob, eccbytes);
935 oob += eccbytes;
936
937 if (chip->ecc.postpad) {
938 chip->read_buf(mtd, oob, chip->ecc.postpad);
939 oob += chip->ecc.postpad;
940 }
941 }
942
943 size = mtd->oobsize - (oob - chip->oob_poi);
944 if (size)
945 chip->read_buf(mtd, oob, size);
946
947 return 0;
948}
949
950/**
William Juul52c07962007-10-31 13:53:06 +0100951 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
952 * @mtd: mtd info structure
953 * @chip: nand chip info structure
954 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -0500955 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +0100956 */
957static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400958 uint8_t *buf, int page)
William Juul52c07962007-10-31 13:53:06 +0100959{
960 int i, eccsize = chip->ecc.size;
961 int eccbytes = chip->ecc.bytes;
962 int eccsteps = chip->ecc.steps;
963 uint8_t *p = buf;
964 uint8_t *ecc_calc = chip->buffers->ecccalc;
965 uint8_t *ecc_code = chip->buffers->ecccode;
966 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200967
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400968 chip->ecc.read_page_raw(mtd, chip, buf, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200969
William Juul52c07962007-10-31 13:53:06 +0100970 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
971 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200972
William Juul52c07962007-10-31 13:53:06 +0100973 for (i = 0; i < chip->ecc.total; i++)
974 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200975
William Juul52c07962007-10-31 13:53:06 +0100976 eccsteps = chip->ecc.steps;
977 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200978
William Juul52c07962007-10-31 13:53:06 +0100979 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
980 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200981
William Juul52c07962007-10-31 13:53:06 +0100982 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood3628f002008-10-24 16:20:43 -0500983 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +0100984 mtd->ecc_stats.failed++;
985 else
986 mtd->ecc_stats.corrected += stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200987 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200988 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200989}
990
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200991/**
Scott Wood3628f002008-10-24 16:20:43 -0500992 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
993 * @mtd: mtd info structure
994 * @chip: nand chip info structure
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -0500995 * @data_offs: offset of requested data within the page
996 * @readlen: data length
997 * @bufpoi: buffer to store read data
Scott Wood3628f002008-10-24 16:20:43 -0500998 */
999static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
1000{
1001 int start_step, end_step, num_steps;
1002 uint32_t *eccpos = chip->ecc.layout->eccpos;
1003 uint8_t *p;
1004 int data_col_addr, i, gaps = 0;
1005 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1006 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1007
1008 /* Column address wihin the page aligned to ECC size (256bytes). */
1009 start_step = data_offs / chip->ecc.size;
1010 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1011 num_steps = end_step - start_step + 1;
1012
1013 /* Data size aligned to ECC ecc.size*/
1014 datafrag_len = num_steps * chip->ecc.size;
1015 eccfrag_len = num_steps * chip->ecc.bytes;
1016
1017 data_col_addr = start_step * chip->ecc.size;
1018 /* If we read not a page aligned data */
1019 if (data_col_addr != 0)
1020 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1021
1022 p = bufpoi + data_col_addr;
1023 chip->read_buf(mtd, p, datafrag_len);
1024
1025 /* Calculate ECC */
1026 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1027 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1028
1029 /* The performance is faster if to position offsets
1030 according to ecc.pos. Let make sure here that
1031 there are no gaps in ecc positions */
1032 for (i = 0; i < eccfrag_len - 1; i++) {
1033 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1034 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1035 gaps = 1;
1036 break;
1037 }
1038 }
1039 if (gaps) {
1040 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1041 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1042 } else {
1043 /* send the command to read the particular ecc bytes */
1044 /* take care about buswidth alignment in read_buf */
1045 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
1046 aligned_len = eccfrag_len;
1047 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
1048 aligned_len++;
1049 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1050 aligned_len++;
1051
1052 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1053 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1054 }
1055
1056 for (i = 0; i < eccfrag_len; i++)
1057 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1058
1059 p = bufpoi + data_col_addr;
1060 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1061 int stat;
1062
1063 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Sandeep Paulraj9656fe22009-11-16 13:32:01 -05001064 if (stat == -1)
Scott Wood3628f002008-10-24 16:20:43 -05001065 mtd->ecc_stats.failed++;
1066 else
1067 mtd->ecc_stats.corrected += stat;
1068 }
1069 return 0;
1070}
1071
1072/**
William Juul52c07962007-10-31 13:53:06 +01001073 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
1074 * @mtd: mtd info structure
1075 * @chip: nand chip info structure
1076 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -05001077 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001078 *
William Juul52c07962007-10-31 13:53:06 +01001079 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001080 */
William Juul52c07962007-10-31 13:53:06 +01001081static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001082 uint8_t *buf, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001083{
William Juul52c07962007-10-31 13:53:06 +01001084 int i, eccsize = chip->ecc.size;
1085 int eccbytes = chip->ecc.bytes;
1086 int eccsteps = chip->ecc.steps;
1087 uint8_t *p = buf;
1088 uint8_t *ecc_calc = chip->buffers->ecccalc;
1089 uint8_t *ecc_code = chip->buffers->ecccode;
1090 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001091
William Juul52c07962007-10-31 13:53:06 +01001092 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1093 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1094 chip->read_buf(mtd, p, eccsize);
1095 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1096 }
1097 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001098
William Juul52c07962007-10-31 13:53:06 +01001099 for (i = 0; i < chip->ecc.total; i++)
1100 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001101
William Juul52c07962007-10-31 13:53:06 +01001102 eccsteps = chip->ecc.steps;
1103 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001104
William Juul52c07962007-10-31 13:53:06 +01001105 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1106 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001107
William Juul52c07962007-10-31 13:53:06 +01001108 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Sandeep Paulraj16add232009-11-07 14:25:03 -05001109 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001110 mtd->ecc_stats.failed++;
1111 else
1112 mtd->ecc_stats.corrected += stat;
1113 }
1114 return 0;
1115}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001116
William Juul52c07962007-10-31 13:53:06 +01001117/**
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001118 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1119 * @mtd: mtd info structure
1120 * @chip: nand chip info structure
1121 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -05001122 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001123 *
1124 * Hardware ECC for large page chips, require OOB to be read first.
1125 * For this ECC mode, the write_page method is re-used from ECC_HW.
1126 * These methods read/write ECC from the OOB area, unlike the
1127 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1128 * "infix ECC" scheme and reads/writes ECC from the data area, by
1129 * overwriting the NAND manufacturer bad block markings.
1130 */
1131static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1132 struct nand_chip *chip, uint8_t *buf, int page)
1133{
1134 int i, eccsize = chip->ecc.size;
1135 int eccbytes = chip->ecc.bytes;
1136 int eccsteps = chip->ecc.steps;
1137 uint8_t *p = buf;
1138 uint8_t *ecc_code = chip->buffers->ecccode;
1139 uint32_t *eccpos = chip->ecc.layout->eccpos;
1140 uint8_t *ecc_calc = chip->buffers->ecccalc;
1141
1142 /* Read the OOB area first */
1143 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1144 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1145 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1146
1147 for (i = 0; i < chip->ecc.total; i++)
1148 ecc_code[i] = chip->oob_poi[eccpos[i]];
1149
1150 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1151 int stat;
1152
1153 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1154 chip->read_buf(mtd, p, eccsize);
1155 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1156
1157 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1158 if (stat < 0)
1159 mtd->ecc_stats.failed++;
1160 else
1161 mtd->ecc_stats.corrected += stat;
1162 }
1163 return 0;
1164}
1165
1166/**
William Juul52c07962007-10-31 13:53:06 +01001167 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1168 * @mtd: mtd info structure
1169 * @chip: nand chip info structure
1170 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -05001171 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001172 *
1173 * The hw generator calculates the error syndrome automatically. Therefor
1174 * we need a special oob layout and handling.
1175 */
1176static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001177 uint8_t *buf, int page)
William Juul52c07962007-10-31 13:53:06 +01001178{
1179 int i, eccsize = chip->ecc.size;
1180 int eccbytes = chip->ecc.bytes;
1181 int eccsteps = chip->ecc.steps;
1182 uint8_t *p = buf;
1183 uint8_t *oob = chip->oob_poi;
1184
1185 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1186 int stat;
1187
1188 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1189 chip->read_buf(mtd, p, eccsize);
1190
1191 if (chip->ecc.prepad) {
1192 chip->read_buf(mtd, oob, chip->ecc.prepad);
1193 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001194 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001195
William Juul52c07962007-10-31 13:53:06 +01001196 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1197 chip->read_buf(mtd, oob, eccbytes);
1198 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001199
Scott Wood3628f002008-10-24 16:20:43 -05001200 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001201 mtd->ecc_stats.failed++;
1202 else
1203 mtd->ecc_stats.corrected += stat;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001204
William Juul52c07962007-10-31 13:53:06 +01001205 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001206
William Juul52c07962007-10-31 13:53:06 +01001207 if (chip->ecc.postpad) {
1208 chip->read_buf(mtd, oob, chip->ecc.postpad);
1209 oob += chip->ecc.postpad;
1210 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001211 }
William Juul52c07962007-10-31 13:53:06 +01001212
1213 /* Calculate remaining oob bytes */
1214 i = mtd->oobsize - (oob - chip->oob_poi);
1215 if (i)
1216 chip->read_buf(mtd, oob, i);
1217
1218 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001219}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001220
1221/**
William Juul52c07962007-10-31 13:53:06 +01001222 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1223 * @chip: nand chip structure
1224 * @oob: oob destination address
1225 * @ops: oob ops structure
1226 * @len: size of oob to transfer
1227 */
1228static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1229 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001230{
William Juul52c07962007-10-31 13:53:06 +01001231 switch(ops->mode) {
1232
1233 case MTD_OOB_PLACE:
1234 case MTD_OOB_RAW:
1235 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1236 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001237
William Juul52c07962007-10-31 13:53:06 +01001238 case MTD_OOB_AUTO: {
1239 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1240 uint32_t boffs = 0, roffs = ops->ooboffs;
1241 size_t bytes = 0;
1242
1243 for(; free->length && len; free++, len -= bytes) {
1244 /* Read request not from offset 0 ? */
1245 if (unlikely(roffs)) {
1246 if (roffs >= free->length) {
1247 roffs -= free->length;
1248 continue;
1249 }
1250 boffs = free->offset + roffs;
1251 bytes = min_t(size_t, len,
1252 (free->length - roffs));
1253 roffs = 0;
1254 } else {
1255 bytes = min_t(size_t, len, free->length);
1256 boffs = free->offset;
1257 }
1258 memcpy(oob, chip->oob_poi + boffs, bytes);
1259 oob += bytes;
1260 }
1261 return oob;
1262 }
1263 default:
1264 BUG();
1265 }
1266 return NULL;
1267}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001268
1269/**
William Juul52c07962007-10-31 13:53:06 +01001270 * nand_do_read_ops - [Internal] Read data with ECC
1271 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001272 * @mtd: MTD device structure
1273 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001274 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001275 *
William Juul52c07962007-10-31 13:53:06 +01001276 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001277 */
William Juul52c07962007-10-31 13:53:06 +01001278static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1279 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001280{
William Juul52c07962007-10-31 13:53:06 +01001281 int chipnr, page, realpage, col, bytes, aligned;
1282 struct nand_chip *chip = mtd->priv;
1283 struct mtd_ecc_stats stats;
1284 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1285 int sndcmd = 1;
1286 int ret = 0;
1287 uint32_t readlen = ops->len;
1288 uint32_t oobreadlen = ops->ooblen;
1289 uint8_t *bufpoi, *oob, *buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001290
William Juul52c07962007-10-31 13:53:06 +01001291 stats = mtd->ecc_stats;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001292
William Juul52c07962007-10-31 13:53:06 +01001293 chipnr = (int)(from >> chip->chip_shift);
1294 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001295
William Juul52c07962007-10-31 13:53:06 +01001296 realpage = (int)(from >> chip->page_shift);
1297 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001298
William Juul52c07962007-10-31 13:53:06 +01001299 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001300
William Juul52c07962007-10-31 13:53:06 +01001301 buf = ops->datbuf;
1302 oob = ops->oobbuf;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001303
William Juul52c07962007-10-31 13:53:06 +01001304 while(1) {
1305 bytes = min(mtd->writesize - col, readlen);
1306 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001307
William Juul52c07962007-10-31 13:53:06 +01001308 /* Is the current page in the buffer ? */
1309 if (realpage != chip->pagebuf || oob) {
1310 bufpoi = aligned ? buf : chip->buffers->databuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001311
William Juul52c07962007-10-31 13:53:06 +01001312 if (likely(sndcmd)) {
1313 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1314 sndcmd = 0;
1315 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001316
William Juul52c07962007-10-31 13:53:06 +01001317 /* Now read the page into the buffer */
1318 if (unlikely(ops->mode == MTD_OOB_RAW))
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001319 ret = chip->ecc.read_page_raw(mtd, chip,
1320 bufpoi, page);
Scott Wood3628f002008-10-24 16:20:43 -05001321 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1322 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
William Juul52c07962007-10-31 13:53:06 +01001323 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001324 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1325 page);
William Juul52c07962007-10-31 13:53:06 +01001326 if (ret < 0)
1327 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001328
William Juul52c07962007-10-31 13:53:06 +01001329 /* Transfer not aligned data */
1330 if (!aligned) {
Scott Wood3628f002008-10-24 16:20:43 -05001331 if (!NAND_SUBPAGE_READ(chip) && !oob)
1332 chip->pagebuf = realpage;
William Juul52c07962007-10-31 13:53:06 +01001333 memcpy(buf, chip->buffers->databuf + col, bytes);
1334 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001335
William Juul52c07962007-10-31 13:53:06 +01001336 buf += bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001337
William Juul52c07962007-10-31 13:53:06 +01001338 if (unlikely(oob)) {
1339 /* Raw mode does data:oob:data:oob */
1340 if (ops->mode != MTD_OOB_RAW) {
1341 int toread = min(oobreadlen,
1342 chip->ecc.layout->oobavail);
1343 if (toread) {
1344 oob = nand_transfer_oob(chip,
1345 oob, ops, toread);
1346 oobreadlen -= toread;
1347 }
1348 } else
1349 buf = nand_transfer_oob(chip,
1350 buf, ops, mtd->oobsize);
1351 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001352
William Juul52c07962007-10-31 13:53:06 +01001353 if (!(chip->options & NAND_NO_READRDY)) {
1354 /*
1355 * Apply delay or wait for ready/busy pin. Do
1356 * this before the AUTOINCR check, so no
1357 * problems arise if a chip which does auto
1358 * increment is marked as NOAUTOINCR by the
1359 * board driver.
1360 */
1361 if (!chip->dev_ready)
1362 udelay(chip->chip_delay);
1363 else
1364 nand_wait_ready(mtd);
1365 }
1366 } else {
1367 memcpy(buf, chip->buffers->databuf + col, bytes);
1368 buf += bytes;
1369 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001370
William Juul52c07962007-10-31 13:53:06 +01001371 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001372
William Juul52c07962007-10-31 13:53:06 +01001373 if (!readlen)
1374 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001375
William Juul52c07962007-10-31 13:53:06 +01001376 /* For subsequent reads align to page boundary. */
1377 col = 0;
1378 /* Increment page address */
1379 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001380
William Juul52c07962007-10-31 13:53:06 +01001381 page = realpage & chip->pagemask;
1382 /* Check, if we cross a chip boundary */
1383 if (!page) {
1384 chipnr++;
1385 chip->select_chip(mtd, -1);
1386 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001387 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001388
William Juul52c07962007-10-31 13:53:06 +01001389 /* Check, if the chip supports auto page increment
1390 * or if we have hit a block boundary.
1391 */
1392 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1393 sndcmd = 1;
1394 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001395
William Juul52c07962007-10-31 13:53:06 +01001396 ops->retlen = ops->len - (size_t) readlen;
1397 if (oob)
1398 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001399
William Juul52c07962007-10-31 13:53:06 +01001400 if (ret)
1401 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001402
William Juul52c07962007-10-31 13:53:06 +01001403 if (mtd->ecc_stats.failed - stats.failed)
1404 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001405
William Juul52c07962007-10-31 13:53:06 +01001406 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1407}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001408
William Juul52c07962007-10-31 13:53:06 +01001409/**
1410 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1411 * @mtd: MTD device structure
1412 * @from: offset to read from
1413 * @len: number of bytes to read
1414 * @retlen: pointer to variable to store the number of read bytes
1415 * @buf: the databuffer to put data
1416 *
1417 * Get hold of the chip and call nand_do_read
1418 */
1419static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1420 size_t *retlen, uint8_t *buf)
1421{
1422 struct nand_chip *chip = mtd->priv;
1423 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001424
William Juul52c07962007-10-31 13:53:06 +01001425 /* Do not allow reads past end of device */
1426 if ((from + len) > mtd->size)
1427 return -EINVAL;
1428 if (!len)
1429 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001430
William Juul52c07962007-10-31 13:53:06 +01001431 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001432
William Juul52c07962007-10-31 13:53:06 +01001433 chip->ops.len = len;
1434 chip->ops.datbuf = buf;
1435 chip->ops.oobbuf = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001436
William Juul52c07962007-10-31 13:53:06 +01001437 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001438
William Juul52c07962007-10-31 13:53:06 +01001439 *retlen = chip->ops.retlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001440
William Juul52c07962007-10-31 13:53:06 +01001441 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001442
William Juul52c07962007-10-31 13:53:06 +01001443 return ret;
1444}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001445
William Juul52c07962007-10-31 13:53:06 +01001446/**
1447 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1448 * @mtd: mtd info structure
1449 * @chip: nand chip info structure
1450 * @page: page number to read
1451 * @sndcmd: flag whether to issue read command or not
1452 */
1453static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1454 int page, int sndcmd)
1455{
1456 if (sndcmd) {
1457 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1458 sndcmd = 0;
1459 }
1460 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1461 return sndcmd;
1462}
1463
1464/**
1465 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1466 * with syndromes
1467 * @mtd: mtd info structure
1468 * @chip: nand chip info structure
1469 * @page: page number to read
1470 * @sndcmd: flag whether to issue read command or not
1471 */
1472static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1473 int page, int sndcmd)
1474{
1475 uint8_t *buf = chip->oob_poi;
1476 int length = mtd->oobsize;
1477 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1478 int eccsize = chip->ecc.size;
1479 uint8_t *bufpoi = buf;
1480 int i, toread, sndrnd = 0, pos;
1481
1482 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1483 for (i = 0; i < chip->ecc.steps; i++) {
1484 if (sndrnd) {
1485 pos = eccsize + i * (eccsize + chunk);
1486 if (mtd->writesize > 512)
1487 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1488 else
1489 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001490 } else
William Juul52c07962007-10-31 13:53:06 +01001491 sndrnd = 1;
1492 toread = min_t(int, length, chunk);
1493 chip->read_buf(mtd, bufpoi, toread);
1494 bufpoi += toread;
1495 length -= toread;
1496 }
1497 if (length > 0)
1498 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001499
William Juul52c07962007-10-31 13:53:06 +01001500 return 1;
1501}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001502
William Juul52c07962007-10-31 13:53:06 +01001503/**
1504 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1505 * @mtd: mtd info structure
1506 * @chip: nand chip info structure
1507 * @page: page number to write
1508 */
1509static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1510 int page)
1511{
1512 int status = 0;
1513 const uint8_t *buf = chip->oob_poi;
1514 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001515
William Juul52c07962007-10-31 13:53:06 +01001516 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1517 chip->write_buf(mtd, buf, length);
1518 /* Send command to program the OOB data */
1519 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001520
William Juul52c07962007-10-31 13:53:06 +01001521 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001522
William Juul52c07962007-10-31 13:53:06 +01001523 return status & NAND_STATUS_FAIL ? -EIO : 0;
1524}
1525
1526/**
1527 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1528 * with syndrome - only for large page flash !
1529 * @mtd: mtd info structure
1530 * @chip: nand chip info structure
1531 * @page: page number to write
1532 */
1533static int nand_write_oob_syndrome(struct mtd_info *mtd,
1534 struct nand_chip *chip, int page)
1535{
1536 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1537 int eccsize = chip->ecc.size, length = mtd->oobsize;
1538 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1539 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001540
1541 /*
William Juul52c07962007-10-31 13:53:06 +01001542 * data-ecc-data-ecc ... ecc-oob
1543 * or
1544 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001545 */
William Juul52c07962007-10-31 13:53:06 +01001546 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1547 pos = steps * (eccsize + chunk);
1548 steps = 0;
1549 } else
1550 pos = eccsize;
1551
1552 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1553 for (i = 0; i < steps; i++) {
1554 if (sndcmd) {
1555 if (mtd->writesize <= 512) {
1556 uint32_t fill = 0xFFFFFFFF;
1557
1558 len = eccsize;
1559 while (len > 0) {
1560 int num = min_t(int, len, 4);
1561 chip->write_buf(mtd, (uint8_t *)&fill,
1562 num);
1563 len -= num;
1564 }
1565 } else {
1566 pos = eccsize + i * (eccsize + chunk);
1567 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1568 }
1569 } else
1570 sndcmd = 1;
1571 len = min_t(int, length, chunk);
1572 chip->write_buf(mtd, bufpoi, len);
1573 bufpoi += len;
1574 length -= len;
1575 }
1576 if (length > 0)
1577 chip->write_buf(mtd, bufpoi, length);
1578
1579 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1580 status = chip->waitfunc(mtd, chip);
1581
1582 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001583}
1584
1585/**
William Juul52c07962007-10-31 13:53:06 +01001586 * nand_do_read_oob - [Intern] NAND read out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001587 * @mtd: MTD device structure
1588 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001589 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001590 *
1591 * NAND read out-of-band data from the spare area
1592 */
William Juul52c07962007-10-31 13:53:06 +01001593static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1594 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001595{
William Juul52c07962007-10-31 13:53:06 +01001596 int page, realpage, chipnr, sndcmd = 1;
1597 struct nand_chip *chip = mtd->priv;
1598 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1599 int readlen = ops->ooblen;
1600 int len;
1601 uint8_t *buf = ops->oobbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001602
William Juul52c07962007-10-31 13:53:06 +01001603 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1604 (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001605
William Juul52c07962007-10-31 13:53:06 +01001606 if (ops->mode == MTD_OOB_AUTO)
1607 len = chip->ecc.layout->oobavail;
1608 else
1609 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001610
William Juul52c07962007-10-31 13:53:06 +01001611 if (unlikely(ops->ooboffs >= len)) {
1612 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1613 "Attempt to start read outside oob\n");
1614 return -EINVAL;
1615 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001616
1617 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001618 if (unlikely(from >= mtd->size ||
1619 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1620 (from >> chip->page_shift)) * len)) {
1621 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1622 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001623 return -EINVAL;
1624 }
1625
William Juul52c07962007-10-31 13:53:06 +01001626 chipnr = (int)(from >> chip->chip_shift);
1627 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001628
William Juul52c07962007-10-31 13:53:06 +01001629 /* Shift to get page */
1630 realpage = (int)(from >> chip->page_shift);
1631 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001632
William Juul52c07962007-10-31 13:53:06 +01001633 while(1) {
1634 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001635
William Juul52c07962007-10-31 13:53:06 +01001636 len = min(len, readlen);
1637 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001638
William Juul52c07962007-10-31 13:53:06 +01001639 if (!(chip->options & NAND_NO_READRDY)) {
1640 /*
1641 * Apply delay or wait for ready/busy pin. Do this
1642 * before the AUTOINCR check, so no problems arise if a
1643 * chip which does auto increment is marked as
1644 * NOAUTOINCR by the board driver.
1645 */
1646 if (!chip->dev_ready)
1647 udelay(chip->chip_delay);
1648 else
1649 nand_wait_ready(mtd);
1650 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001651
William Juul52c07962007-10-31 13:53:06 +01001652 readlen -= len;
1653 if (!readlen)
1654 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001655
William Juul52c07962007-10-31 13:53:06 +01001656 /* Increment page address */
1657 realpage++;
1658
1659 page = realpage & chip->pagemask;
1660 /* Check, if we cross a chip boundary */
1661 if (!page) {
1662 chipnr++;
1663 chip->select_chip(mtd, -1);
1664 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001665 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001666
William Juul52c07962007-10-31 13:53:06 +01001667 /* Check, if the chip supports auto page increment
1668 * or if we have hit a block boundary.
1669 */
1670 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1671 sndcmd = 1;
1672 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001673
William Juul52c07962007-10-31 13:53:06 +01001674 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001675 return 0;
1676}
1677
1678/**
William Juul52c07962007-10-31 13:53:06 +01001679 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001680 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001681 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001682 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001683 *
William Juul52c07962007-10-31 13:53:06 +01001684 * NAND read data and/or out-of-band data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001685 */
William Juul52c07962007-10-31 13:53:06 +01001686static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1687 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001688{
William Juul52c07962007-10-31 13:53:06 +01001689 struct nand_chip *chip = mtd->priv;
1690 int ret = -ENOTSUPP;
1691
1692 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001693
1694 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001695 if (ops->datbuf && (from + ops->len) > mtd->size) {
1696 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1697 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001698 return -EINVAL;
1699 }
1700
William Juul52c07962007-10-31 13:53:06 +01001701 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001702
William Juul52c07962007-10-31 13:53:06 +01001703 switch(ops->mode) {
1704 case MTD_OOB_PLACE:
1705 case MTD_OOB_AUTO:
1706 case MTD_OOB_RAW:
1707 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001708
William Juul52c07962007-10-31 13:53:06 +01001709 default:
1710 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001711 }
1712
William Juul52c07962007-10-31 13:53:06 +01001713 if (!ops->datbuf)
1714 ret = nand_do_read_oob(mtd, from, ops);
1715 else
1716 ret = nand_do_read_ops(mtd, from, ops);
1717
1718 out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001719 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001720 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001721}
1722
1723
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001724/**
William Juul52c07962007-10-31 13:53:06 +01001725 * nand_write_page_raw - [Intern] raw page write function
1726 * @mtd: mtd info structure
1727 * @chip: nand chip info structure
1728 * @buf: data buffer
David Brownellee86b8d2009-11-07 16:27:01 -05001729 *
1730 * Not for syndrome calculating ecc controllers, which use a special oob layout
William Juul52c07962007-10-31 13:53:06 +01001731 */
1732static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1733 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001734{
William Juul52c07962007-10-31 13:53:06 +01001735 chip->write_buf(mtd, buf, mtd->writesize);
1736 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1737}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001738
William Juul52c07962007-10-31 13:53:06 +01001739/**
David Brownellee86b8d2009-11-07 16:27:01 -05001740 * nand_write_page_raw_syndrome - [Intern] raw page write function
1741 * @mtd: mtd info structure
1742 * @chip: nand chip info structure
1743 * @buf: data buffer
1744 *
1745 * We need a special oob layout and handling even when ECC isn't checked.
1746 */
1747static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1748 const uint8_t *buf)
1749{
1750 int eccsize = chip->ecc.size;
1751 int eccbytes = chip->ecc.bytes;
1752 uint8_t *oob = chip->oob_poi;
1753 int steps, size;
1754
1755 for (steps = chip->ecc.steps; steps > 0; steps--) {
1756 chip->write_buf(mtd, buf, eccsize);
1757 buf += eccsize;
1758
1759 if (chip->ecc.prepad) {
1760 chip->write_buf(mtd, oob, chip->ecc.prepad);
1761 oob += chip->ecc.prepad;
1762 }
1763
1764 chip->read_buf(mtd, oob, eccbytes);
1765 oob += eccbytes;
1766
1767 if (chip->ecc.postpad) {
1768 chip->write_buf(mtd, oob, chip->ecc.postpad);
1769 oob += chip->ecc.postpad;
1770 }
1771 }
1772
1773 size = mtd->oobsize - (oob - chip->oob_poi);
1774 if (size)
1775 chip->write_buf(mtd, oob, size);
1776}
1777/**
William Juul52c07962007-10-31 13:53:06 +01001778 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1779 * @mtd: mtd info structure
1780 * @chip: nand chip info structure
1781 * @buf: data buffer
1782 */
1783static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1784 const uint8_t *buf)
1785{
1786 int i, eccsize = chip->ecc.size;
1787 int eccbytes = chip->ecc.bytes;
1788 int eccsteps = chip->ecc.steps;
1789 uint8_t *ecc_calc = chip->buffers->ecccalc;
1790 const uint8_t *p = buf;
1791 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001792
William Juul52c07962007-10-31 13:53:06 +01001793 /* Software ecc calculation */
1794 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1795 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001796
William Juul52c07962007-10-31 13:53:06 +01001797 for (i = 0; i < chip->ecc.total; i++)
1798 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001799
William Juul52c07962007-10-31 13:53:06 +01001800 chip->ecc.write_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001801}
1802
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001803/**
William Juul52c07962007-10-31 13:53:06 +01001804 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1805 * @mtd: mtd info structure
1806 * @chip: nand chip info structure
1807 * @buf: data buffer
1808 */
1809static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1810 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001811{
William Juul52c07962007-10-31 13:53:06 +01001812 int i, eccsize = chip->ecc.size;
1813 int eccbytes = chip->ecc.bytes;
1814 int eccsteps = chip->ecc.steps;
1815 uint8_t *ecc_calc = chip->buffers->ecccalc;
1816 const uint8_t *p = buf;
1817 uint32_t *eccpos = chip->ecc.layout->eccpos;
1818
1819 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1820 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1821 chip->write_buf(mtd, p, eccsize);
1822 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1823 }
1824
1825 for (i = 0; i < chip->ecc.total; i++)
1826 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1827
1828 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001829}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001830
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001831/**
William Juul52c07962007-10-31 13:53:06 +01001832 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1833 * @mtd: mtd info structure
1834 * @chip: nand chip info structure
1835 * @buf: data buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001836 *
William Juul52c07962007-10-31 13:53:06 +01001837 * The hw generator calculates the error syndrome automatically. Therefor
1838 * we need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001839 */
William Juul52c07962007-10-31 13:53:06 +01001840static void nand_write_page_syndrome(struct mtd_info *mtd,
1841 struct nand_chip *chip, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001842{
William Juul52c07962007-10-31 13:53:06 +01001843 int i, eccsize = chip->ecc.size;
1844 int eccbytes = chip->ecc.bytes;
1845 int eccsteps = chip->ecc.steps;
1846 const uint8_t *p = buf;
1847 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001848
William Juul52c07962007-10-31 13:53:06 +01001849 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001850
William Juul52c07962007-10-31 13:53:06 +01001851 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1852 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001853
William Juul52c07962007-10-31 13:53:06 +01001854 if (chip->ecc.prepad) {
1855 chip->write_buf(mtd, oob, chip->ecc.prepad);
1856 oob += chip->ecc.prepad;
1857 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001858
William Juul52c07962007-10-31 13:53:06 +01001859 chip->ecc.calculate(mtd, p, oob);
1860 chip->write_buf(mtd, oob, eccbytes);
1861 oob += eccbytes;
1862
1863 if (chip->ecc.postpad) {
1864 chip->write_buf(mtd, oob, chip->ecc.postpad);
1865 oob += chip->ecc.postpad;
1866 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001867 }
1868
William Juul52c07962007-10-31 13:53:06 +01001869 /* Calculate remaining oob bytes */
1870 i = mtd->oobsize - (oob - chip->oob_poi);
1871 if (i)
1872 chip->write_buf(mtd, oob, i);
1873}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001874
William Juul52c07962007-10-31 13:53:06 +01001875/**
1876 * nand_write_page - [REPLACEABLE] write one page
1877 * @mtd: MTD device structure
1878 * @chip: NAND chip descriptor
1879 * @buf: the data to write
1880 * @page: page number to write
1881 * @cached: cached programming
1882 * @raw: use _raw version of write_page
1883 */
1884static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1885 const uint8_t *buf, int page, int cached, int raw)
1886{
1887 int status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001888
William Juul52c07962007-10-31 13:53:06 +01001889 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1890
1891 if (unlikely(raw))
1892 chip->ecc.write_page_raw(mtd, chip, buf);
1893 else
1894 chip->ecc.write_page(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001895
William Juul52c07962007-10-31 13:53:06 +01001896 /*
1897 * Cached progamming disabled for now, Not sure if its worth the
1898 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1899 */
1900 cached = 0;
1901
1902 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1903
1904 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1905 status = chip->waitfunc(mtd, chip);
1906 /*
1907 * See if operation failed and additional status checks are
1908 * available
1909 */
1910 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1911 status = chip->errstat(mtd, chip, FL_WRITING, status,
1912 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001913
William Juul52c07962007-10-31 13:53:06 +01001914 if (status & NAND_STATUS_FAIL)
1915 return -EIO;
1916 } else {
1917 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1918 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001919 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001920
William Juul52c07962007-10-31 13:53:06 +01001921#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1922 /* Send command to read back the data */
1923 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001924
William Juul52c07962007-10-31 13:53:06 +01001925 if (chip->verify_buf(mtd, buf, mtd->writesize))
1926 return -EIO;
1927#endif
1928 return 0;
1929}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001930
William Juul52c07962007-10-31 13:53:06 +01001931/**
1932 * nand_fill_oob - [Internal] Transfer client buffer to oob
1933 * @chip: nand chip structure
1934 * @oob: oob data buffer
1935 * @ops: oob ops structure
1936 */
1937static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1938 struct mtd_oob_ops *ops)
1939{
1940 size_t len = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001941
William Juul52c07962007-10-31 13:53:06 +01001942 switch(ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001943
William Juul52c07962007-10-31 13:53:06 +01001944 case MTD_OOB_PLACE:
1945 case MTD_OOB_RAW:
1946 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1947 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001948
William Juul52c07962007-10-31 13:53:06 +01001949 case MTD_OOB_AUTO: {
1950 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1951 uint32_t boffs = 0, woffs = ops->ooboffs;
1952 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001953
William Juul52c07962007-10-31 13:53:06 +01001954 for(; free->length && len; free++, len -= bytes) {
1955 /* Write request not from offset 0 ? */
1956 if (unlikely(woffs)) {
1957 if (woffs >= free->length) {
1958 woffs -= free->length;
1959 continue;
1960 }
1961 boffs = free->offset + woffs;
1962 bytes = min_t(size_t, len,
1963 (free->length - woffs));
1964 woffs = 0;
1965 } else {
1966 bytes = min_t(size_t, len, free->length);
1967 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001968 }
William Juul52c07962007-10-31 13:53:06 +01001969 memcpy(chip->oob_poi + boffs, oob, bytes);
1970 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001971 }
William Juul52c07962007-10-31 13:53:06 +01001972 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001973 }
William Juul52c07962007-10-31 13:53:06 +01001974 default:
1975 BUG();
1976 }
1977 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001978}
1979
William Juul52c07962007-10-31 13:53:06 +01001980#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001981
1982/**
William Juul52c07962007-10-31 13:53:06 +01001983 * nand_do_write_ops - [Internal] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001984 * @mtd: MTD device structure
1985 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001986 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001987 *
William Juul52c07962007-10-31 13:53:06 +01001988 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001989 */
William Juul52c07962007-10-31 13:53:06 +01001990static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1991 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001992{
William Juul52c07962007-10-31 13:53:06 +01001993 int chipnr, realpage, page, blockmask, column;
1994 struct nand_chip *chip = mtd->priv;
1995 uint32_t writelen = ops->len;
1996 uint8_t *oob = ops->oobbuf;
1997 uint8_t *buf = ops->datbuf;
1998 int ret, subpage;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001999
William Juul52c07962007-10-31 13:53:06 +01002000 ops->retlen = 0;
2001 if (!writelen)
2002 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002003
William Juul52c07962007-10-31 13:53:06 +01002004 /* reject writes, which are not page aligned */
2005 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2006 printk(KERN_NOTICE "nand_write: "
2007 "Attempt to write not page aligned data\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002008 return -EINVAL;
2009 }
2010
William Juul52c07962007-10-31 13:53:06 +01002011 column = to & (mtd->writesize - 1);
2012 subpage = column || (writelen & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002013
William Juul52c07962007-10-31 13:53:06 +01002014 if (subpage && oob)
2015 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002016
William Juul52c07962007-10-31 13:53:06 +01002017 chipnr = (int)(to >> chip->chip_shift);
2018 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002019
2020 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01002021 if (nand_check_wp(mtd)) {
2022 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
2023 return -EIO;
2024 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002025
William Juul52c07962007-10-31 13:53:06 +01002026 realpage = (int)(to >> chip->page_shift);
2027 page = realpage & chip->pagemask;
2028 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002029
William Juul52c07962007-10-31 13:53:06 +01002030 /* Invalidate the page cache, when we write to the cached page */
2031 if (to <= (chip->pagebuf << chip->page_shift) &&
2032 (chip->pagebuf << chip->page_shift) < (to + ops->len))
2033 chip->pagebuf = -1;
2034
2035 /* If we're not given explicit OOB data, let it be 0xFF */
2036 if (likely(!oob))
2037 memset(chip->oob_poi, 0xff, mtd->oobsize);
2038
2039 while(1) {
2040 int bytes = mtd->writesize;
2041 int cached = writelen > bytes && page != blockmask;
2042 uint8_t *wbuf = buf;
2043
2044 /* Partial page write ? */
2045 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2046 cached = 0;
2047 bytes = min_t(int, bytes - column, (int) writelen);
2048 chip->pagebuf = -1;
2049 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2050 memcpy(&chip->buffers->databuf[column], buf, bytes);
2051 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002052 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002053
William Juul52c07962007-10-31 13:53:06 +01002054 if (unlikely(oob))
2055 oob = nand_fill_oob(chip, oob, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002056
William Juul52c07962007-10-31 13:53:06 +01002057 ret = chip->write_page(mtd, chip, wbuf, page, cached,
2058 (ops->mode == MTD_OOB_RAW));
2059 if (ret)
2060 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002061
William Juul52c07962007-10-31 13:53:06 +01002062 writelen -= bytes;
2063 if (!writelen)
2064 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002065
William Juul52c07962007-10-31 13:53:06 +01002066 column = 0;
2067 buf += bytes;
2068 realpage++;
2069
2070 page = realpage & chip->pagemask;
2071 /* Check, if we cross a chip boundary */
2072 if (!page) {
2073 chipnr++;
2074 chip->select_chip(mtd, -1);
2075 chip->select_chip(mtd, chipnr);
2076 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002077 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002078
William Juul52c07962007-10-31 13:53:06 +01002079 ops->retlen = ops->len - writelen;
2080 if (unlikely(oob))
2081 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002082 return ret;
2083}
2084
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002085/**
William Juul52c07962007-10-31 13:53:06 +01002086 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002087 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002088 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01002089 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002090 * @retlen: pointer to variable to store the number of written bytes
William Juul52c07962007-10-31 13:53:06 +01002091 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002092 *
William Juul52c07962007-10-31 13:53:06 +01002093 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002094 */
William Juul52c07962007-10-31 13:53:06 +01002095static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2096 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002097{
William Juul52c07962007-10-31 13:53:06 +01002098 struct nand_chip *chip = mtd->priv;
2099 int ret;
2100
2101 /* Do not allow reads past end of device */
2102 if ((to + len) > mtd->size)
2103 return -EINVAL;
2104 if (!len)
2105 return 0;
2106
2107 nand_get_device(chip, mtd, FL_WRITING);
2108
2109 chip->ops.len = len;
2110 chip->ops.datbuf = (uint8_t *)buf;
2111 chip->ops.oobbuf = NULL;
2112
2113 ret = nand_do_write_ops(mtd, to, &chip->ops);
2114
2115 *retlen = chip->ops.retlen;
2116
2117 nand_release_device(mtd);
2118
2119 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002120}
2121
2122/**
William Juul52c07962007-10-31 13:53:06 +01002123 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002124 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002125 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01002126 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002127 *
William Juul52c07962007-10-31 13:53:06 +01002128 * NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002129 */
William Juul52c07962007-10-31 13:53:06 +01002130static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2131 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002132{
William Juul52c07962007-10-31 13:53:06 +01002133 int chipnr, page, status, len;
2134 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002135
William Juul52c07962007-10-31 13:53:06 +01002136 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
2137 (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002138
William Juul52c07962007-10-31 13:53:06 +01002139 if (ops->mode == MTD_OOB_AUTO)
2140 len = chip->ecc.layout->oobavail;
2141 else
2142 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002143
2144 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002145 if ((ops->ooboffs + ops->ooblen) > len) {
2146 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
2147 "Attempt to write past end of page\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002148 return -EINVAL;
2149 }
2150
William Juul52c07962007-10-31 13:53:06 +01002151 if (unlikely(ops->ooboffs >= len)) {
2152 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2153 "Attempt to start write outside oob\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002154 return -EINVAL;
2155 }
2156
William Juul52c07962007-10-31 13:53:06 +01002157 /* Do not allow reads past end of device */
2158 if (unlikely(to >= mtd->size ||
2159 ops->ooboffs + ops->ooblen >
2160 ((mtd->size >> chip->page_shift) -
2161 (to >> chip->page_shift)) * len)) {
2162 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2163 "Attempt write beyond end of device\n");
2164 return -EINVAL;
2165 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002166
William Juul52c07962007-10-31 13:53:06 +01002167 chipnr = (int)(to >> chip->chip_shift);
2168 chip->select_chip(mtd, chipnr);
2169
2170 /* Shift to get page */
2171 page = (int)(to >> chip->page_shift);
2172
2173 /*
2174 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2175 * of my DiskOnChip 2000 test units) will clear the whole data page too
2176 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2177 * it in the doc2000 driver in August 1999. dwmw2.
2178 */
2179 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002180
2181 /* Check, if it is write protected */
2182 if (nand_check_wp(mtd))
William Juul52c07962007-10-31 13:53:06 +01002183 return -EROFS;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002184
William Juul52c07962007-10-31 13:53:06 +01002185 /* Invalidate the page cache, if we write to the cached page */
2186 if (page == chip->pagebuf)
2187 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002188
William Juul52c07962007-10-31 13:53:06 +01002189 memset(chip->oob_poi, 0xff, mtd->oobsize);
2190 nand_fill_oob(chip, ops->oobbuf, ops);
2191 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2192 memset(chip->oob_poi, 0xff, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002193
William Juul52c07962007-10-31 13:53:06 +01002194 if (status)
2195 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002196
William Juul52c07962007-10-31 13:53:06 +01002197 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002198
William Juul52c07962007-10-31 13:53:06 +01002199 return 0;
2200}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002201
William Juul52c07962007-10-31 13:53:06 +01002202/**
2203 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2204 * @mtd: MTD device structure
2205 * @to: offset to write to
2206 * @ops: oob operation description structure
2207 */
2208static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2209 struct mtd_oob_ops *ops)
2210{
2211 struct nand_chip *chip = mtd->priv;
2212 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002213
William Juul52c07962007-10-31 13:53:06 +01002214 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002215
William Juul52c07962007-10-31 13:53:06 +01002216 /* Do not allow writes past end of device */
2217 if (ops->datbuf && (to + ops->len) > mtd->size) {
2218 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2219 "Attempt read beyond end of device\n");
2220 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002221 }
William Juul52c07962007-10-31 13:53:06 +01002222
2223 nand_get_device(chip, mtd, FL_WRITING);
2224
2225 switch(ops->mode) {
2226 case MTD_OOB_PLACE:
2227 case MTD_OOB_AUTO:
2228 case MTD_OOB_RAW:
2229 break;
2230
2231 default:
2232 goto out;
2233 }
2234
2235 if (!ops->datbuf)
2236 ret = nand_do_write_oob(mtd, to, ops);
2237 else
2238 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002239
William Juul52c07962007-10-31 13:53:06 +01002240 out:
2241 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002242 return ret;
2243}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002244
2245/**
2246 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2247 * @mtd: MTD device structure
2248 * @page: the page address of the block which will be erased
2249 *
2250 * Standard erase command for NAND chips
2251 */
William Juul52c07962007-10-31 13:53:06 +01002252static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002253{
William Juul52c07962007-10-31 13:53:06 +01002254 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002255 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002256 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2257 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002258}
2259
2260/**
2261 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2262 * @mtd: MTD device structure
2263 * @page: the page address of the block which will be erased
2264 *
2265 * AND multi block erase command function
2266 * Erase 4 consecutive blocks
2267 */
William Juul52c07962007-10-31 13:53:06 +01002268static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002269{
William Juul52c07962007-10-31 13:53:06 +01002270 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002271 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002272 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2273 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2274 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2275 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2276 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002277}
2278
2279/**
2280 * nand_erase - [MTD Interface] erase block(s)
2281 * @mtd: MTD device structure
2282 * @instr: erase instruction
2283 *
2284 * Erase one ore more blocks
2285 */
William Juul52c07962007-10-31 13:53:06 +01002286static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002287{
William Juul52c07962007-10-31 13:53:06 +01002288 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002289}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002290
William Juul52c07962007-10-31 13:53:06 +01002291#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002292/**
William Juul52c07962007-10-31 13:53:06 +01002293 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002294 * @mtd: MTD device structure
2295 * @instr: erase instruction
2296 * @allowbbt: allow erasing the bbt area
2297 *
2298 * Erase one ore more blocks
2299 */
William Juul52c07962007-10-31 13:53:06 +01002300int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2301 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002302{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002303 int page, status, pages_per_block, ret, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002304 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002305 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
William Juul52c07962007-10-31 13:53:06 +01002306 unsigned int bbt_masked_page = 0xffffffff;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002307 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002308
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002309 MTDDEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%012llx, "
2310 "len = %llu\n", (unsigned long long) instr->addr,
2311 (unsigned long long) instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002312
2313 /* Start address must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002314 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002315 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002316 return -EINVAL;
2317 }
2318
2319 /* Length must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002320 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002321 MTDDEBUG (MTD_DEBUG_LEVEL0,
2322 "nand_erase: Length not block aligned\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002323 return -EINVAL;
2324 }
2325
2326 /* Do not allow erase past end of device */
2327 if ((instr->len + instr->addr) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05002328 MTDDEBUG (MTD_DEBUG_LEVEL0,
2329 "nand_erase: Erase past end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002330 return -EINVAL;
2331 }
2332
2333 instr->fail_addr = 0xffffffff;
2334
2335 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002336 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002337
2338 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002339 page = (int)(instr->addr >> chip->page_shift);
2340 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002341
2342 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002343 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002344
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002345 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002346 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002347
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002348 /* Check, if it is write protected */
2349 if (nand_check_wp(mtd)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002350 MTDDEBUG (MTD_DEBUG_LEVEL0,
2351 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002352 instr->state = MTD_ERASE_FAILED;
2353 goto erase_exit;
2354 }
2355
William Juul52c07962007-10-31 13:53:06 +01002356 /*
2357 * If BBT requires refresh, set the BBT page mask to see if the BBT
2358 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2359 * can not be matched. This is also done when the bbt is actually
2360 * erased to avoid recusrsive updates
2361 */
2362 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2363 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2364
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002365 /* Loop through the pages */
2366 len = instr->len;
2367
2368 instr->state = MTD_ERASING;
2369
2370 while (len) {
William Juul52c07962007-10-31 13:53:06 +01002371 /*
2372 * heck if we have a bad block, we do not erase bad blocks !
2373 */
2374 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2375 chip->page_shift, 0, allowbbt)) {
2376 printk(KERN_WARNING "nand_erase: attempt to erase a "
2377 "bad block at page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002378 instr->state = MTD_ERASE_FAILED;
2379 goto erase_exit;
2380 }
William Juul52c07962007-10-31 13:53:06 +01002381
2382 /*
2383 * Invalidate the page cache, if we erase the block which
2384 * contains the current cached page
2385 */
2386 if (page <= chip->pagebuf && chip->pagebuf <
2387 (page + pages_per_block))
2388 chip->pagebuf = -1;
2389
2390 chip->erase_cmd(mtd, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002391
William Juul52c07962007-10-31 13:53:06 +01002392 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002393
William Juul52c07962007-10-31 13:53:06 +01002394 /*
2395 * See if operation failed and additional status checks are
2396 * available
2397 */
2398 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2399 status = chip->errstat(mtd, chip, FL_ERASING,
2400 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002401
2402 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002403 if (status & NAND_STATUS_FAIL) {
Scott Wooddf83c472008-06-20 12:38:57 -05002404 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2405 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002406 instr->state = MTD_ERASE_FAILED;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002407 instr->fail_addr = ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002408 goto erase_exit;
2409 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002410
William Juul52c07962007-10-31 13:53:06 +01002411 /*
2412 * If BBT requires refresh, set the BBT rewrite flag to the
2413 * page being erased
2414 */
2415 if (bbt_masked_page != 0xffffffff &&
2416 (page & BBT_PAGE_MASK) == bbt_masked_page)
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002417 rewrite_bbt[chipnr] =
2418 ((loff_t)page << chip->page_shift);
William Juul52c07962007-10-31 13:53:06 +01002419
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002420 /* Increment page address and decrement length */
William Juul52c07962007-10-31 13:53:06 +01002421 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002422 page += pages_per_block;
2423
2424 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002425 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002426 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002427 chip->select_chip(mtd, -1);
2428 chip->select_chip(mtd, chipnr);
2429
2430 /*
2431 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2432 * page mask to see if this BBT should be rewritten
2433 */
2434 if (bbt_masked_page != 0xffffffff &&
2435 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2436 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2437 BBT_PAGE_MASK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002438 }
2439 }
2440 instr->state = MTD_ERASE_DONE;
2441
William Juul52c07962007-10-31 13:53:06 +01002442 erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002443
2444 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002445
2446 /* Deselect and wake up anyone waiting on the device */
2447 nand_release_device(mtd);
2448
Scott Wood3628f002008-10-24 16:20:43 -05002449 /* Do call back function */
2450 if (!ret)
2451 mtd_erase_callback(instr);
2452
William Juul52c07962007-10-31 13:53:06 +01002453 /*
2454 * If BBT requires refresh and erase was successful, rewrite any
2455 * selected bad block tables
2456 */
2457 if (bbt_masked_page == 0xffffffff || ret)
2458 return ret;
2459
2460 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2461 if (!rewrite_bbt[chipnr])
2462 continue;
2463 /* update the BBT for chip */
2464 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002465 "(%d:0x%0llx 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2466 chip->bbt_td->pages[chipnr]);
William Juul52c07962007-10-31 13:53:06 +01002467 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2468 }
2469
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002470 /* Return more or less happy */
2471 return ret;
2472}
2473
2474/**
2475 * nand_sync - [MTD Interface] sync
2476 * @mtd: MTD device structure
2477 *
2478 * Sync is actually a wait for chip ready function
2479 */
William Juul52c07962007-10-31 13:53:06 +01002480static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002481{
William Juul52c07962007-10-31 13:53:06 +01002482 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002483
Scott Wooddf83c472008-06-20 12:38:57 -05002484 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002485
2486 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002487 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002488 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002489 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002490}
2491
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002492/**
William Juul52c07962007-10-31 13:53:06 +01002493 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002494 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002495 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002496 */
William Juul52c07962007-10-31 13:53:06 +01002497static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002498{
2499 /* Check for invalid offset */
William Juul52c07962007-10-31 13:53:06 +01002500 if (offs > mtd->size)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002501 return -EINVAL;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002502
William Juul52c07962007-10-31 13:53:06 +01002503 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002504}
2505
2506/**
William Juul52c07962007-10-31 13:53:06 +01002507 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002508 * @mtd: MTD device structure
2509 * @ofs: offset relative to mtd start
2510 */
William Juul52c07962007-10-31 13:53:06 +01002511static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002512{
William Juul52c07962007-10-31 13:53:06 +01002513 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002514 int ret;
2515
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002516 if ((ret = nand_block_isbad(mtd, ofs))) {
2517 /* If it was bad already, return success and do nothing. */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002518 if (ret > 0)
2519 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002520 return ret;
2521 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002522
William Juul52c07962007-10-31 13:53:06 +01002523 return chip->block_markbad(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002524}
2525
2526/**
William Juul52c07962007-10-31 13:53:06 +01002527 * nand_suspend - [MTD Interface] Suspend the NAND flash
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002528 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002529 */
William Juul52c07962007-10-31 13:53:06 +01002530static int nand_suspend(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002531{
William Juul52c07962007-10-31 13:53:06 +01002532 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002533
William Juul52c07962007-10-31 13:53:06 +01002534 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2535}
2536
2537/**
2538 * nand_resume - [MTD Interface] Resume the NAND flash
2539 * @mtd: MTD device structure
2540 */
2541static void nand_resume(struct mtd_info *mtd)
2542{
2543 struct nand_chip *chip = mtd->priv;
2544
2545 if (chip->state == FL_PM_SUSPENDED)
2546 nand_release_device(mtd);
2547 else
2548 printk(KERN_ERR "nand_resume() called for a chip which is not "
2549 "in suspended state\n");
2550}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002551
William Juul52c07962007-10-31 13:53:06 +01002552/*
2553 * Set default functions
2554 */
2555static void nand_set_defaults(struct nand_chip *chip, int busw)
2556{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002557 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01002558 if (!chip->chip_delay)
2559 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002560
2561 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01002562 if (chip->cmdfunc == NULL)
2563 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002564
2565 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01002566 if (chip->waitfunc == NULL)
2567 chip->waitfunc = nand_wait;
2568
2569 if (!chip->select_chip)
2570 chip->select_chip = nand_select_chip;
2571 if (!chip->read_byte)
2572 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2573 if (!chip->read_word)
2574 chip->read_word = nand_read_word;
2575 if (!chip->block_bad)
2576 chip->block_bad = nand_block_bad;
2577 if (!chip->block_markbad)
2578 chip->block_markbad = nand_default_block_markbad;
2579 if (!chip->write_buf)
2580 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2581 if (!chip->read_buf)
2582 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2583 if (!chip->verify_buf)
2584 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2585 if (!chip->scan_bbt)
2586 chip->scan_bbt = nand_default_bbt;
2587
2588 if (!chip->controller) {
2589 chip->controller = &chip->hwcontrol;
2590
2591 /* XXX U-BOOT XXX */
2592#if 0
2593 spin_lock_init(&chip->controller->lock);
2594 init_waitqueue_head(&chip->controller->wq);
2595#endif
2596 }
2597
2598}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002599
William Juul52c07962007-10-31 13:53:06 +01002600/*
2601 * Get the flash and manufacturer id and lookup if the type is supported
2602 */
2603static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2604 struct nand_chip *chip,
2605 int busw, int *maf_id)
2606{
2607 struct nand_flash_dev *type = NULL;
2608 int i, dev_id, maf_idx;
Scott Wood3628f002008-10-24 16:20:43 -05002609 int tmp_id, tmp_manf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002610
2611 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01002612 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002613
Karl Beldanb6322fc2008-09-15 16:08:03 +02002614 /*
2615 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2616 * after power-up
2617 */
2618 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2619
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002620 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01002621 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002622
2623 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002624 *maf_id = chip->read_byte(mtd);
2625 dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002626
Scott Wood3628f002008-10-24 16:20:43 -05002627 /* Try again to make sure, as some systems the bus-hold or other
2628 * interface concerns can cause random data which looks like a
2629 * possibly credible NAND flash to appear. If the two results do
2630 * not match, ignore the device completely.
2631 */
2632
2633 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2634
2635 /* Read manufacturer and device IDs */
2636
2637 tmp_manf = chip->read_byte(mtd);
2638 tmp_id = chip->read_byte(mtd);
2639
2640 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2641 printk(KERN_INFO "%s: second ID read did not match "
2642 "%02x,%02x against %02x,%02x\n", __func__,
2643 *maf_id, dev_id, tmp_manf, tmp_id);
2644 return ERR_PTR(-ENODEV);
2645 }
2646
William Juul52c07962007-10-31 13:53:06 +01002647 /* Lookup the flash id */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002648 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
William Juul52c07962007-10-31 13:53:06 +01002649 if (dev_id == nand_flash_ids[i].id) {
2650 type = &nand_flash_ids[i];
2651 break;
2652 }
2653 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002654
Florian Fainellid6191892010-06-12 20:59:25 +02002655 if (!type) {
2656 printk(KERN_INFO "%s: unknown NAND device: Manufacturer ID:"
2657 " 0x%02x, Chip ID: 0x%02x\n", __func__,
2658 *maf_id, dev_id);
William Juul52c07962007-10-31 13:53:06 +01002659 return ERR_PTR(-ENODEV);
Florian Fainellid6191892010-06-12 20:59:25 +02002660 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002661
William Juul52c07962007-10-31 13:53:06 +01002662 if (!mtd->name)
2663 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002664
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002665 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002666
William Juul52c07962007-10-31 13:53:06 +01002667 /* Newer devices have all the information in additional id bytes */
2668 if (!type->pagesize) {
2669 int extid;
2670 /* The 3rd id byte holds MLC / multichip data */
2671 chip->cellinfo = chip->read_byte(mtd);
2672 /* The 4th id byte is the important one */
2673 extid = chip->read_byte(mtd);
2674 /* Calc pagesize */
2675 mtd->writesize = 1024 << (extid & 0x3);
2676 extid >>= 2;
2677 /* Calc oobsize */
2678 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2679 extid >>= 2;
2680 /* Calc blocksize. Blocksize is multiples of 64KiB */
2681 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2682 extid >>= 2;
2683 /* Get buswidth information */
2684 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002685
William Juul52c07962007-10-31 13:53:06 +01002686 } else {
2687 /*
2688 * Old devices have chip data hardcoded in the device id table
2689 */
2690 mtd->erasesize = type->erasesize;
2691 mtd->writesize = type->pagesize;
2692 mtd->oobsize = mtd->writesize / 32;
2693 busw = type->options & NAND_BUSWIDTH_16;
2694 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002695
William Juul52c07962007-10-31 13:53:06 +01002696 /* Try to identify manufacturer */
2697 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2698 if (nand_manuf_ids[maf_idx].id == *maf_id)
2699 break;
2700 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002701
William Juul52c07962007-10-31 13:53:06 +01002702 /*
2703 * Check, if buswidth is correct. Hardware drivers should set
2704 * chip correct !
2705 */
2706 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2707 printk(KERN_INFO "NAND device: Manufacturer ID:"
2708 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2709 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2710 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2711 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2712 busw ? 16 : 8);
2713 return ERR_PTR(-EINVAL);
2714 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002715
William Juul52c07962007-10-31 13:53:06 +01002716 /* Calculate the address shift from the page size */
2717 chip->page_shift = ffs(mtd->writesize) - 1;
2718 /* Convert chipsize to number of pages per chip -1. */
2719 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002720
William Juul52c07962007-10-31 13:53:06 +01002721 chip->bbt_erase_shift = chip->phys_erase_shift =
2722 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002723 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05002724 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002725 else
2726 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 31;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002727
William Juul52c07962007-10-31 13:53:06 +01002728 /* Set the bad block position */
2729 chip->badblockpos = mtd->writesize > 512 ?
2730 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002731
William Juul52c07962007-10-31 13:53:06 +01002732 /* Get chip options, preserve non chip based options */
2733 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2734 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002735
William Juul52c07962007-10-31 13:53:06 +01002736 /*
2737 * Set chip as a default. Board drivers can override it, if necessary
2738 */
2739 chip->options |= NAND_NO_AUTOINCR;
2740
2741 /* Check if chip is a not a samsung device. Do not clear the
2742 * options for chips which are not having an extended id.
2743 */
2744 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2745 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2746
2747 /* Check for AND chips with 4 page planes */
2748 if (chip->options & NAND_4PAGE_ARRAY)
2749 chip->erase_cmd = multi_erase_cmd;
2750 else
2751 chip->erase_cmd = single_erase_cmd;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002752
William Juul52c07962007-10-31 13:53:06 +01002753 /* Do not replace user supplied command function ! */
2754 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2755 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002756
Stefan Roese4fe71432008-01-10 18:47:33 +01002757 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2758 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2759 nand_manuf_ids[maf_idx].name, type->name);
William Juul52c07962007-10-31 13:53:06 +01002760
2761 return type;
2762}
2763
2764/**
2765 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2766 * @mtd: MTD device structure
2767 * @maxchips: Number of chips to scan for
2768 *
2769 * This is the first phase of the normal nand_scan() function. It
2770 * reads the flash ID and sets up MTD fields accordingly.
2771 *
2772 * The mtd->owner field must be set to the module of the caller.
2773 */
2774int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2775{
2776 int i, busw, nand_maf_id;
2777 struct nand_chip *chip = mtd->priv;
2778 struct nand_flash_dev *type;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002779
William Juul52c07962007-10-31 13:53:06 +01002780 /* Get buswidth to select the correct functions */
2781 busw = chip->options & NAND_BUSWIDTH_16;
2782 /* Set the default functions */
2783 nand_set_defaults(chip, busw);
2784
2785 /* Read the flash type */
2786 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2787
2788 if (IS_ERR(type)) {
Peter Tyserf96047a2009-02-04 13:39:40 -06002789#ifndef CONFIG_SYS_NAND_QUIET_TEST
William Juul52c07962007-10-31 13:53:06 +01002790 printk(KERN_WARNING "No NAND device found!!!\n");
Peter Tyserf96047a2009-02-04 13:39:40 -06002791#endif
William Juul52c07962007-10-31 13:53:06 +01002792 chip->select_chip(mtd, -1);
2793 return PTR_ERR(type);
2794 }
2795
2796 /* Check for a chip array */
2797 for (i = 1; i < maxchips; i++) {
2798 chip->select_chip(mtd, i);
Karl Beldanb6322fc2008-09-15 16:08:03 +02002799 /* See comment in nand_get_flash_type for reset */
2800 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002801 /* Send the command for reading device ID */
2802 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002803 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002804 if (nand_maf_id != chip->read_byte(mtd) ||
2805 type->id != chip->read_byte(mtd))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002806 break;
2807 }
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01002808#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002809 if (i > 1)
2810 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01002811#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002812
William Juul52c07962007-10-31 13:53:06 +01002813 /* Store the number of chips and calc total size for mtd */
2814 chip->numchips = i;
2815 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002816
William Juul52c07962007-10-31 13:53:06 +01002817 return 0;
2818}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002819
William Juul52c07962007-10-31 13:53:06 +01002820
2821/**
2822 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2823 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002824 *
2825 * This is the second phase of the normal nand_scan() function. It
2826 * fills out all the uninitialized function pointers with the defaults
2827 * and scans for a bad block table if appropriate.
2828 */
2829int nand_scan_tail(struct mtd_info *mtd)
2830{
2831 int i;
2832 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002833
William Juul52c07962007-10-31 13:53:06 +01002834 if (!(chip->options & NAND_OWN_BUFFERS))
2835 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2836 if (!chip->buffers)
2837 return -ENOMEM;
2838
2839 /* Set the internal oob buffer location, just after the page data */
2840 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2841
2842 /*
2843 * If no default placement scheme is given, select an appropriate one
2844 */
2845 if (!chip->ecc.layout) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002846 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002847 case 8:
William Juul52c07962007-10-31 13:53:06 +01002848 chip->ecc.layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002849 break;
2850 case 16:
William Juul52c07962007-10-31 13:53:06 +01002851 chip->ecc.layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002852 break;
2853 case 64:
William Juul52c07962007-10-31 13:53:06 +01002854 chip->ecc.layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002855 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002856 case 128:
William Juul52c07962007-10-31 13:53:06 +01002857 chip->ecc.layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002858 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002859 default:
William Juul52c07962007-10-31 13:53:06 +01002860 printk(KERN_WARNING "No oob scheme defined for "
2861 "oobsize %d\n", mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002862 }
2863 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002864
William Juul52c07962007-10-31 13:53:06 +01002865 if (!chip->write_page)
2866 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002867
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002868 /*
William Juul52c07962007-10-31 13:53:06 +01002869 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2870 * selected and we have 256 byte pagesize fallback to software ECC
2871 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002872
William Juul52c07962007-10-31 13:53:06 +01002873 switch (chip->ecc.mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002874 case NAND_ECC_HW_OOB_FIRST:
2875 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2876 if (!chip->ecc.calculate || !chip->ecc.correct ||
2877 !chip->ecc.hwctl) {
2878 printk(KERN_WARNING "No ECC functions supplied, "
2879 "Hardware ECC not possible\n");
2880 BUG();
2881 }
2882 if (!chip->ecc.read_page)
2883 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2884
William Juul52c07962007-10-31 13:53:06 +01002885 case NAND_ECC_HW:
2886 /* Use standard hwecc read page function ? */
2887 if (!chip->ecc.read_page)
2888 chip->ecc.read_page = nand_read_page_hwecc;
2889 if (!chip->ecc.write_page)
2890 chip->ecc.write_page = nand_write_page_hwecc;
David Brownellee86b8d2009-11-07 16:27:01 -05002891 if (!chip->ecc.read_page_raw)
2892 chip->ecc.read_page_raw = nand_read_page_raw;
2893 if (!chip->ecc.write_page_raw)
2894 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01002895 if (!chip->ecc.read_oob)
2896 chip->ecc.read_oob = nand_read_oob_std;
2897 if (!chip->ecc.write_oob)
2898 chip->ecc.write_oob = nand_write_oob_std;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002899
William Juul52c07962007-10-31 13:53:06 +01002900 case NAND_ECC_HW_SYNDROME:
Scott Wood5fd2c302008-03-18 15:29:14 -05002901 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2902 !chip->ecc.hwctl) &&
2903 (!chip->ecc.read_page ||
2904 chip->ecc.read_page == nand_read_page_hwecc ||
2905 !chip->ecc.write_page ||
2906 chip->ecc.write_page == nand_write_page_hwecc)) {
William Juul52c07962007-10-31 13:53:06 +01002907 printk(KERN_WARNING "No ECC functions supplied, "
2908 "Hardware ECC not possible\n");
2909 BUG();
2910 }
2911 /* Use standard syndrome read/write page function ? */
2912 if (!chip->ecc.read_page)
2913 chip->ecc.read_page = nand_read_page_syndrome;
2914 if (!chip->ecc.write_page)
2915 chip->ecc.write_page = nand_write_page_syndrome;
David Brownellee86b8d2009-11-07 16:27:01 -05002916 if (!chip->ecc.read_page_raw)
2917 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2918 if (!chip->ecc.write_page_raw)
2919 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
William Juul52c07962007-10-31 13:53:06 +01002920 if (!chip->ecc.read_oob)
2921 chip->ecc.read_oob = nand_read_oob_syndrome;
2922 if (!chip->ecc.write_oob)
2923 chip->ecc.write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002924
William Juul52c07962007-10-31 13:53:06 +01002925 if (mtd->writesize >= chip->ecc.size)
2926 break;
2927 printk(KERN_WARNING "%d byte HW ECC not possible on "
2928 "%d byte page size, fallback to SW ECC\n",
2929 chip->ecc.size, mtd->writesize);
2930 chip->ecc.mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002931
William Juul52c07962007-10-31 13:53:06 +01002932 case NAND_ECC_SOFT:
2933 chip->ecc.calculate = nand_calculate_ecc;
2934 chip->ecc.correct = nand_correct_data;
2935 chip->ecc.read_page = nand_read_page_swecc;
Scott Wood3628f002008-10-24 16:20:43 -05002936 chip->ecc.read_subpage = nand_read_subpage;
William Juul52c07962007-10-31 13:53:06 +01002937 chip->ecc.write_page = nand_write_page_swecc;
David Brownellee86b8d2009-11-07 16:27:01 -05002938 chip->ecc.read_page_raw = nand_read_page_raw;
2939 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01002940 chip->ecc.read_oob = nand_read_oob_std;
2941 chip->ecc.write_oob = nand_write_oob_std;
2942 chip->ecc.size = 256;
2943 chip->ecc.bytes = 3;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002944 break;
2945
William Juul52c07962007-10-31 13:53:06 +01002946 case NAND_ECC_NONE:
2947 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2948 "This is not recommended !!\n");
2949 chip->ecc.read_page = nand_read_page_raw;
2950 chip->ecc.write_page = nand_write_page_raw;
2951 chip->ecc.read_oob = nand_read_oob_std;
David Brownellee86b8d2009-11-07 16:27:01 -05002952 chip->ecc.read_page_raw = nand_read_page_raw;
2953 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01002954 chip->ecc.write_oob = nand_write_oob_std;
2955 chip->ecc.size = mtd->writesize;
2956 chip->ecc.bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002957 break;
2958
2959 default:
William Juul52c07962007-10-31 13:53:06 +01002960 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2961 chip->ecc.mode);
2962 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002963 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002964
William Juul52c07962007-10-31 13:53:06 +01002965 /*
2966 * The number of bytes available for a client to place data into
2967 * the out of band area
2968 */
2969 chip->ecc.layout->oobavail = 0;
Sandeep Paulrajeec6cb32009-11-07 14:25:18 -05002970 for (i = 0; chip->ecc.layout->oobfree[i].length
2971 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
William Juul52c07962007-10-31 13:53:06 +01002972 chip->ecc.layout->oobavail +=
2973 chip->ecc.layout->oobfree[i].length;
2974 mtd->oobavail = chip->ecc.layout->oobavail;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002975
William Juul52c07962007-10-31 13:53:06 +01002976 /*
2977 * Set the number of read / write steps for one page depending on ECC
2978 * mode
2979 */
2980 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2981 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2982 printk(KERN_WARNING "Invalid ecc parameters\n");
2983 BUG();
2984 }
2985 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002986
William Juul52c07962007-10-31 13:53:06 +01002987 /*
2988 * Allow subpage writes up to ecc.steps. Not possible for MLC
2989 * FLASH.
2990 */
2991 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2992 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2993 switch(chip->ecc.steps) {
2994 case 2:
2995 mtd->subpage_sft = 1;
2996 break;
2997 case 4:
2998 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05002999 case 16:
William Juul52c07962007-10-31 13:53:06 +01003000 mtd->subpage_sft = 2;
3001 break;
3002 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003003 }
William Juul52c07962007-10-31 13:53:06 +01003004 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003005
William Juul52c07962007-10-31 13:53:06 +01003006 /* Initialize state */
3007 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003008
3009 /* De-select the device */
William Juul52c07962007-10-31 13:53:06 +01003010 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003011
3012 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01003013 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003014
3015 /* Fill in remaining MTD driver data */
3016 mtd->type = MTD_NANDFLASH;
William Juul52c07962007-10-31 13:53:06 +01003017 mtd->flags = MTD_CAP_NANDFLASH;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003018 mtd->erase = nand_erase;
3019 mtd->point = NULL;
3020 mtd->unpoint = NULL;
3021 mtd->read = nand_read;
3022 mtd->write = nand_write;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003023 mtd->read_oob = nand_read_oob;
3024 mtd->write_oob = nand_write_oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003025 mtd->sync = nand_sync;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003026 mtd->lock = NULL;
3027 mtd->unlock = NULL;
William Juul52c07962007-10-31 13:53:06 +01003028 mtd->suspend = nand_suspend;
3029 mtd->resume = nand_resume;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003030 mtd->block_isbad = nand_block_isbad;
3031 mtd->block_markbad = nand_block_markbad;
3032
William Juul52c07962007-10-31 13:53:06 +01003033 /* propagate ecc.layout to mtd_info */
3034 mtd->ecclayout = chip->ecc.layout;
3035
3036 /* Check, if we should skip the bad block table scan */
3037 if (chip->options & NAND_SKIP_BBTSCAN)
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02003038 chip->options |= NAND_BBT_SCANNED;
William Juul52c07962007-10-31 13:53:06 +01003039
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02003040 return 0;
William Juul52c07962007-10-31 13:53:06 +01003041}
3042
3043/* module_text_address() isn't exported, and it's mostly a pointless
3044 test if this is a module _anyway_ -- they'd have to try _really_ hard
3045 to call us from in-kernel code if the core NAND support is modular. */
3046#ifdef MODULE
3047#define caller_is_module() (1)
3048#else
3049#define caller_is_module() \
3050 module_text_address((unsigned long)__builtin_return_address(0))
3051#endif
3052
3053/**
3054 * nand_scan - [NAND Interface] Scan for the NAND device
3055 * @mtd: MTD device structure
3056 * @maxchips: Number of chips to scan for
3057 *
3058 * This fills out all the uninitialized function pointers
3059 * with the defaults.
3060 * The flash ID is read and the mtd/chip structures are
3061 * filled with the appropriate values.
3062 * The mtd->owner field must be set to the module of the caller
3063 *
3064 */
3065int nand_scan(struct mtd_info *mtd, int maxchips)
3066{
3067 int ret;
3068
3069 /* Many callers got this wrong, so check for it for a while... */
3070 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003071#if 0
William Juul52c07962007-10-31 13:53:06 +01003072 if (!mtd->owner && caller_is_module()) {
3073 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
3074 BUG();
3075 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003076#endif
William Juulb76ec382007-11-08 10:39:53 +01003077
William Juul52c07962007-10-31 13:53:06 +01003078 ret = nand_scan_ident(mtd, maxchips);
3079 if (!ret)
3080 ret = nand_scan_tail(mtd);
3081 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003082}
3083
3084/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003085 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003086 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01003087*/
3088void nand_release(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003089{
William Juul52c07962007-10-31 13:53:06 +01003090 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003091
3092#ifdef CONFIG_MTD_PARTITIONS
3093 /* Deregister partitions */
William Juul52c07962007-10-31 13:53:06 +01003094 del_mtd_partitions(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003095#endif
3096 /* Deregister the device */
William Juul52c07962007-10-31 13:53:06 +01003097 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003098#if 0
William Juul52c07962007-10-31 13:53:06 +01003099 del_mtd_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003100#endif
William Juul52c07962007-10-31 13:53:06 +01003101
3102 /* Free bad block table memory */
3103 kfree(chip->bbt);
3104 if (!(chip->options & NAND_OWN_BUFFERS))
3105 kfree(chip->buffers);
3106}
3107
3108/* XXX U-BOOT XXX */
3109#if 0
3110EXPORT_SYMBOL_GPL(nand_scan);
3111EXPORT_SYMBOL_GPL(nand_scan_ident);
3112EXPORT_SYMBOL_GPL(nand_scan_tail);
3113EXPORT_SYMBOL_GPL(nand_release);
3114
3115static int __init nand_base_init(void)
3116{
3117 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3118 return 0;
3119}
3120
3121static void __exit nand_base_exit(void)
3122{
3123 led_trigger_unregister_simple(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003124}
3125
William Juul52c07962007-10-31 13:53:06 +01003126module_init(nand_base_init);
3127module_exit(nand_base_exit);
3128
3129MODULE_LICENSE("GPL");
3130MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3131MODULE_DESCRIPTION("Generic NAND flash driver code");
3132#endif