blob: ca026286f20ac562bfe249ebca14aa629998b7fb [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
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200896 */
William Juul52c07962007-10-31 13:53:06 +0100897static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400898 uint8_t *buf, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200899{
William Juul52c07962007-10-31 13:53:06 +0100900 chip->read_buf(mtd, buf, mtd->writesize);
901 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
902 return 0;
903}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200904
William Juul52c07962007-10-31 13:53:06 +0100905/**
906 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
907 * @mtd: mtd info structure
908 * @chip: nand chip info structure
909 * @buf: buffer to store read data
910 */
911static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400912 uint8_t *buf, int page)
William Juul52c07962007-10-31 13:53:06 +0100913{
914 int i, eccsize = chip->ecc.size;
915 int eccbytes = chip->ecc.bytes;
916 int eccsteps = chip->ecc.steps;
917 uint8_t *p = buf;
918 uint8_t *ecc_calc = chip->buffers->ecccalc;
919 uint8_t *ecc_code = chip->buffers->ecccode;
920 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200921
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400922 chip->ecc.read_page_raw(mtd, chip, buf, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200923
William Juul52c07962007-10-31 13:53:06 +0100924 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
925 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200926
William Juul52c07962007-10-31 13:53:06 +0100927 for (i = 0; i < chip->ecc.total; i++)
928 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200929
William Juul52c07962007-10-31 13:53:06 +0100930 eccsteps = chip->ecc.steps;
931 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200932
William Juul52c07962007-10-31 13:53:06 +0100933 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
934 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200935
William Juul52c07962007-10-31 13:53:06 +0100936 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood3628f002008-10-24 16:20:43 -0500937 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +0100938 mtd->ecc_stats.failed++;
939 else
940 mtd->ecc_stats.corrected += stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200941 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200942 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200943}
944
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200945/**
Scott Wood3628f002008-10-24 16:20:43 -0500946 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
947 * @mtd: mtd info structure
948 * @chip: nand chip info structure
949 * @dataofs offset of requested data within the page
950 * @readlen data length
951 * @buf: buffer to store read data
952 */
953static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
954{
955 int start_step, end_step, num_steps;
956 uint32_t *eccpos = chip->ecc.layout->eccpos;
957 uint8_t *p;
958 int data_col_addr, i, gaps = 0;
959 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
960 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
961
962 /* Column address wihin the page aligned to ECC size (256bytes). */
963 start_step = data_offs / chip->ecc.size;
964 end_step = (data_offs + readlen - 1) / chip->ecc.size;
965 num_steps = end_step - start_step + 1;
966
967 /* Data size aligned to ECC ecc.size*/
968 datafrag_len = num_steps * chip->ecc.size;
969 eccfrag_len = num_steps * chip->ecc.bytes;
970
971 data_col_addr = start_step * chip->ecc.size;
972 /* If we read not a page aligned data */
973 if (data_col_addr != 0)
974 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
975
976 p = bufpoi + data_col_addr;
977 chip->read_buf(mtd, p, datafrag_len);
978
979 /* Calculate ECC */
980 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
981 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
982
983 /* The performance is faster if to position offsets
984 according to ecc.pos. Let make sure here that
985 there are no gaps in ecc positions */
986 for (i = 0; i < eccfrag_len - 1; i++) {
987 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
988 eccpos[i + start_step * chip->ecc.bytes + 1]) {
989 gaps = 1;
990 break;
991 }
992 }
993 if (gaps) {
994 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
995 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
996 } else {
997 /* send the command to read the particular ecc bytes */
998 /* take care about buswidth alignment in read_buf */
999 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
1000 aligned_len = eccfrag_len;
1001 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
1002 aligned_len++;
1003 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1004 aligned_len++;
1005
1006 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1007 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1008 }
1009
1010 for (i = 0; i < eccfrag_len; i++)
1011 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1012
1013 p = bufpoi + data_col_addr;
1014 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1015 int stat;
1016
1017 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1018 if (stat < 0)
1019 mtd->ecc_stats.failed++;
1020 else
1021 mtd->ecc_stats.corrected += stat;
1022 }
1023 return 0;
1024}
1025
1026/**
William Juul52c07962007-10-31 13:53:06 +01001027 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
1028 * @mtd: mtd info structure
1029 * @chip: nand chip info structure
1030 * @buf: buffer to store read data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001031 *
William Juul52c07962007-10-31 13:53:06 +01001032 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001033 */
William Juul52c07962007-10-31 13:53:06 +01001034static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001035 uint8_t *buf, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001036{
William Juul52c07962007-10-31 13:53:06 +01001037 int i, eccsize = chip->ecc.size;
1038 int eccbytes = chip->ecc.bytes;
1039 int eccsteps = chip->ecc.steps;
1040 uint8_t *p = buf;
1041 uint8_t *ecc_calc = chip->buffers->ecccalc;
1042 uint8_t *ecc_code = chip->buffers->ecccode;
1043 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001044
William Juul52c07962007-10-31 13:53:06 +01001045 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1046 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1047 chip->read_buf(mtd, p, eccsize);
1048 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1049 }
1050 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001051
William Juul52c07962007-10-31 13:53:06 +01001052 for (i = 0; i < chip->ecc.total; i++)
1053 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001054
William Juul52c07962007-10-31 13:53:06 +01001055 eccsteps = chip->ecc.steps;
1056 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001057
William Juul52c07962007-10-31 13:53:06 +01001058 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1059 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001060
William Juul52c07962007-10-31 13:53:06 +01001061 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1062 if (stat == -1)
1063 mtd->ecc_stats.failed++;
1064 else
1065 mtd->ecc_stats.corrected += stat;
1066 }
1067 return 0;
1068}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001069
William Juul52c07962007-10-31 13:53:06 +01001070/**
1071 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1072 * @mtd: mtd info structure
1073 * @chip: nand chip info structure
1074 * @buf: buffer to store read data
1075 *
1076 * The hw generator calculates the error syndrome automatically. Therefor
1077 * we need a special oob layout and handling.
1078 */
1079static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001080 uint8_t *buf, int page)
William Juul52c07962007-10-31 13:53:06 +01001081{
1082 int i, eccsize = chip->ecc.size;
1083 int eccbytes = chip->ecc.bytes;
1084 int eccsteps = chip->ecc.steps;
1085 uint8_t *p = buf;
1086 uint8_t *oob = chip->oob_poi;
1087
1088 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1089 int stat;
1090
1091 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1092 chip->read_buf(mtd, p, eccsize);
1093
1094 if (chip->ecc.prepad) {
1095 chip->read_buf(mtd, oob, chip->ecc.prepad);
1096 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001097 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001098
William Juul52c07962007-10-31 13:53:06 +01001099 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1100 chip->read_buf(mtd, oob, eccbytes);
1101 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001102
Scott Wood3628f002008-10-24 16:20:43 -05001103 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001104 mtd->ecc_stats.failed++;
1105 else
1106 mtd->ecc_stats.corrected += stat;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001107
William Juul52c07962007-10-31 13:53:06 +01001108 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001109
William Juul52c07962007-10-31 13:53:06 +01001110 if (chip->ecc.postpad) {
1111 chip->read_buf(mtd, oob, chip->ecc.postpad);
1112 oob += chip->ecc.postpad;
1113 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001114 }
William Juul52c07962007-10-31 13:53:06 +01001115
1116 /* Calculate remaining oob bytes */
1117 i = mtd->oobsize - (oob - chip->oob_poi);
1118 if (i)
1119 chip->read_buf(mtd, oob, i);
1120
1121 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001122}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001123
1124/**
William Juul52c07962007-10-31 13:53:06 +01001125 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1126 * @chip: nand chip structure
1127 * @oob: oob destination address
1128 * @ops: oob ops structure
1129 * @len: size of oob to transfer
1130 */
1131static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1132 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001133{
William Juul52c07962007-10-31 13:53:06 +01001134 switch(ops->mode) {
1135
1136 case MTD_OOB_PLACE:
1137 case MTD_OOB_RAW:
1138 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1139 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001140
William Juul52c07962007-10-31 13:53:06 +01001141 case MTD_OOB_AUTO: {
1142 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1143 uint32_t boffs = 0, roffs = ops->ooboffs;
1144 size_t bytes = 0;
1145
1146 for(; free->length && len; free++, len -= bytes) {
1147 /* Read request not from offset 0 ? */
1148 if (unlikely(roffs)) {
1149 if (roffs >= free->length) {
1150 roffs -= free->length;
1151 continue;
1152 }
1153 boffs = free->offset + roffs;
1154 bytes = min_t(size_t, len,
1155 (free->length - roffs));
1156 roffs = 0;
1157 } else {
1158 bytes = min_t(size_t, len, free->length);
1159 boffs = free->offset;
1160 }
1161 memcpy(oob, chip->oob_poi + boffs, bytes);
1162 oob += bytes;
1163 }
1164 return oob;
1165 }
1166 default:
1167 BUG();
1168 }
1169 return NULL;
1170}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001171
1172/**
William Juul52c07962007-10-31 13:53:06 +01001173 * nand_do_read_ops - [Internal] Read data with ECC
1174 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001175 * @mtd: MTD device structure
1176 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001177 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001178 *
William Juul52c07962007-10-31 13:53:06 +01001179 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001180 */
William Juul52c07962007-10-31 13:53:06 +01001181static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1182 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001183{
William Juul52c07962007-10-31 13:53:06 +01001184 int chipnr, page, realpage, col, bytes, aligned;
1185 struct nand_chip *chip = mtd->priv;
1186 struct mtd_ecc_stats stats;
1187 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1188 int sndcmd = 1;
1189 int ret = 0;
1190 uint32_t readlen = ops->len;
1191 uint32_t oobreadlen = ops->ooblen;
1192 uint8_t *bufpoi, *oob, *buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001193
William Juul52c07962007-10-31 13:53:06 +01001194 stats = mtd->ecc_stats;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001195
William Juul52c07962007-10-31 13:53:06 +01001196 chipnr = (int)(from >> chip->chip_shift);
1197 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001198
William Juul52c07962007-10-31 13:53:06 +01001199 realpage = (int)(from >> chip->page_shift);
1200 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001201
William Juul52c07962007-10-31 13:53:06 +01001202 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001203
William Juul52c07962007-10-31 13:53:06 +01001204 buf = ops->datbuf;
1205 oob = ops->oobbuf;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001206
William Juul52c07962007-10-31 13:53:06 +01001207 while(1) {
1208 bytes = min(mtd->writesize - col, readlen);
1209 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001210
William Juul52c07962007-10-31 13:53:06 +01001211 /* Is the current page in the buffer ? */
1212 if (realpage != chip->pagebuf || oob) {
1213 bufpoi = aligned ? buf : chip->buffers->databuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001214
William Juul52c07962007-10-31 13:53:06 +01001215 if (likely(sndcmd)) {
1216 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1217 sndcmd = 0;
1218 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001219
William Juul52c07962007-10-31 13:53:06 +01001220 /* Now read the page into the buffer */
1221 if (unlikely(ops->mode == MTD_OOB_RAW))
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001222 ret = chip->ecc.read_page_raw(mtd, chip,
1223 bufpoi, page);
Scott Wood3628f002008-10-24 16:20:43 -05001224 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1225 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
William Juul52c07962007-10-31 13:53:06 +01001226 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001227 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1228 page);
William Juul52c07962007-10-31 13:53:06 +01001229 if (ret < 0)
1230 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001231
William Juul52c07962007-10-31 13:53:06 +01001232 /* Transfer not aligned data */
1233 if (!aligned) {
Scott Wood3628f002008-10-24 16:20:43 -05001234 if (!NAND_SUBPAGE_READ(chip) && !oob)
1235 chip->pagebuf = realpage;
William Juul52c07962007-10-31 13:53:06 +01001236 memcpy(buf, chip->buffers->databuf + col, bytes);
1237 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001238
William Juul52c07962007-10-31 13:53:06 +01001239 buf += bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001240
William Juul52c07962007-10-31 13:53:06 +01001241 if (unlikely(oob)) {
1242 /* Raw mode does data:oob:data:oob */
1243 if (ops->mode != MTD_OOB_RAW) {
1244 int toread = min(oobreadlen,
1245 chip->ecc.layout->oobavail);
1246 if (toread) {
1247 oob = nand_transfer_oob(chip,
1248 oob, ops, toread);
1249 oobreadlen -= toread;
1250 }
1251 } else
1252 buf = nand_transfer_oob(chip,
1253 buf, ops, mtd->oobsize);
1254 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001255
William Juul52c07962007-10-31 13:53:06 +01001256 if (!(chip->options & NAND_NO_READRDY)) {
1257 /*
1258 * Apply delay or wait for ready/busy pin. Do
1259 * this before the AUTOINCR check, so no
1260 * problems arise if a chip which does auto
1261 * increment is marked as NOAUTOINCR by the
1262 * board driver.
1263 */
1264 if (!chip->dev_ready)
1265 udelay(chip->chip_delay);
1266 else
1267 nand_wait_ready(mtd);
1268 }
1269 } else {
1270 memcpy(buf, chip->buffers->databuf + col, bytes);
1271 buf += bytes;
1272 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001273
William Juul52c07962007-10-31 13:53:06 +01001274 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001275
William Juul52c07962007-10-31 13:53:06 +01001276 if (!readlen)
1277 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001278
William Juul52c07962007-10-31 13:53:06 +01001279 /* For subsequent reads align to page boundary. */
1280 col = 0;
1281 /* Increment page address */
1282 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001283
William Juul52c07962007-10-31 13:53:06 +01001284 page = realpage & chip->pagemask;
1285 /* Check, if we cross a chip boundary */
1286 if (!page) {
1287 chipnr++;
1288 chip->select_chip(mtd, -1);
1289 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001290 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001291
William Juul52c07962007-10-31 13:53:06 +01001292 /* Check, if the chip supports auto page increment
1293 * or if we have hit a block boundary.
1294 */
1295 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1296 sndcmd = 1;
1297 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001298
William Juul52c07962007-10-31 13:53:06 +01001299 ops->retlen = ops->len - (size_t) readlen;
1300 if (oob)
1301 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001302
William Juul52c07962007-10-31 13:53:06 +01001303 if (ret)
1304 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001305
William Juul52c07962007-10-31 13:53:06 +01001306 if (mtd->ecc_stats.failed - stats.failed)
1307 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001308
William Juul52c07962007-10-31 13:53:06 +01001309 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1310}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001311
William Juul52c07962007-10-31 13:53:06 +01001312/**
1313 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1314 * @mtd: MTD device structure
1315 * @from: offset to read from
1316 * @len: number of bytes to read
1317 * @retlen: pointer to variable to store the number of read bytes
1318 * @buf: the databuffer to put data
1319 *
1320 * Get hold of the chip and call nand_do_read
1321 */
1322static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1323 size_t *retlen, uint8_t *buf)
1324{
1325 struct nand_chip *chip = mtd->priv;
1326 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001327
William Juul52c07962007-10-31 13:53:06 +01001328 /* Do not allow reads past end of device */
1329 if ((from + len) > mtd->size)
1330 return -EINVAL;
1331 if (!len)
1332 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001333
William Juul52c07962007-10-31 13:53:06 +01001334 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001335
William Juul52c07962007-10-31 13:53:06 +01001336 chip->ops.len = len;
1337 chip->ops.datbuf = buf;
1338 chip->ops.oobbuf = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001339
William Juul52c07962007-10-31 13:53:06 +01001340 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001341
William Juul52c07962007-10-31 13:53:06 +01001342 *retlen = chip->ops.retlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001343
William Juul52c07962007-10-31 13:53:06 +01001344 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001345
William Juul52c07962007-10-31 13:53:06 +01001346 return ret;
1347}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001348
William Juul52c07962007-10-31 13:53:06 +01001349/**
1350 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1351 * @mtd: mtd info structure
1352 * @chip: nand chip info structure
1353 * @page: page number to read
1354 * @sndcmd: flag whether to issue read command or not
1355 */
1356static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1357 int page, int sndcmd)
1358{
1359 if (sndcmd) {
1360 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1361 sndcmd = 0;
1362 }
1363 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1364 return sndcmd;
1365}
1366
1367/**
1368 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1369 * with syndromes
1370 * @mtd: mtd info structure
1371 * @chip: nand chip info structure
1372 * @page: page number to read
1373 * @sndcmd: flag whether to issue read command or not
1374 */
1375static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1376 int page, int sndcmd)
1377{
1378 uint8_t *buf = chip->oob_poi;
1379 int length = mtd->oobsize;
1380 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1381 int eccsize = chip->ecc.size;
1382 uint8_t *bufpoi = buf;
1383 int i, toread, sndrnd = 0, pos;
1384
1385 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1386 for (i = 0; i < chip->ecc.steps; i++) {
1387 if (sndrnd) {
1388 pos = eccsize + i * (eccsize + chunk);
1389 if (mtd->writesize > 512)
1390 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1391 else
1392 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001393 } else
William Juul52c07962007-10-31 13:53:06 +01001394 sndrnd = 1;
1395 toread = min_t(int, length, chunk);
1396 chip->read_buf(mtd, bufpoi, toread);
1397 bufpoi += toread;
1398 length -= toread;
1399 }
1400 if (length > 0)
1401 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001402
William Juul52c07962007-10-31 13:53:06 +01001403 return 1;
1404}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001405
William Juul52c07962007-10-31 13:53:06 +01001406/**
1407 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1408 * @mtd: mtd info structure
1409 * @chip: nand chip info structure
1410 * @page: page number to write
1411 */
1412static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1413 int page)
1414{
1415 int status = 0;
1416 const uint8_t *buf = chip->oob_poi;
1417 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001418
William Juul52c07962007-10-31 13:53:06 +01001419 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1420 chip->write_buf(mtd, buf, length);
1421 /* Send command to program the OOB data */
1422 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001423
William Juul52c07962007-10-31 13:53:06 +01001424 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001425
William Juul52c07962007-10-31 13:53:06 +01001426 return status & NAND_STATUS_FAIL ? -EIO : 0;
1427}
1428
1429/**
1430 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1431 * with syndrome - only for large page flash !
1432 * @mtd: mtd info structure
1433 * @chip: nand chip info structure
1434 * @page: page number to write
1435 */
1436static int nand_write_oob_syndrome(struct mtd_info *mtd,
1437 struct nand_chip *chip, int page)
1438{
1439 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1440 int eccsize = chip->ecc.size, length = mtd->oobsize;
1441 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1442 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001443
1444 /*
William Juul52c07962007-10-31 13:53:06 +01001445 * data-ecc-data-ecc ... ecc-oob
1446 * or
1447 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001448 */
William Juul52c07962007-10-31 13:53:06 +01001449 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1450 pos = steps * (eccsize + chunk);
1451 steps = 0;
1452 } else
1453 pos = eccsize;
1454
1455 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1456 for (i = 0; i < steps; i++) {
1457 if (sndcmd) {
1458 if (mtd->writesize <= 512) {
1459 uint32_t fill = 0xFFFFFFFF;
1460
1461 len = eccsize;
1462 while (len > 0) {
1463 int num = min_t(int, len, 4);
1464 chip->write_buf(mtd, (uint8_t *)&fill,
1465 num);
1466 len -= num;
1467 }
1468 } else {
1469 pos = eccsize + i * (eccsize + chunk);
1470 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1471 }
1472 } else
1473 sndcmd = 1;
1474 len = min_t(int, length, chunk);
1475 chip->write_buf(mtd, bufpoi, len);
1476 bufpoi += len;
1477 length -= len;
1478 }
1479 if (length > 0)
1480 chip->write_buf(mtd, bufpoi, length);
1481
1482 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1483 status = chip->waitfunc(mtd, chip);
1484
1485 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001486}
1487
1488/**
William Juul52c07962007-10-31 13:53:06 +01001489 * nand_do_read_oob - [Intern] NAND read out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001490 * @mtd: MTD device structure
1491 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001492 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001493 *
1494 * NAND read out-of-band data from the spare area
1495 */
William Juul52c07962007-10-31 13:53:06 +01001496static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1497 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001498{
William Juul52c07962007-10-31 13:53:06 +01001499 int page, realpage, chipnr, sndcmd = 1;
1500 struct nand_chip *chip = mtd->priv;
1501 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1502 int readlen = ops->ooblen;
1503 int len;
1504 uint8_t *buf = ops->oobbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001505
William Juul52c07962007-10-31 13:53:06 +01001506 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1507 (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001508
William Juul52c07962007-10-31 13:53:06 +01001509 if (ops->mode == MTD_OOB_AUTO)
1510 len = chip->ecc.layout->oobavail;
1511 else
1512 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001513
William Juul52c07962007-10-31 13:53:06 +01001514 if (unlikely(ops->ooboffs >= len)) {
1515 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1516 "Attempt to start read outside oob\n");
1517 return -EINVAL;
1518 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001519
1520 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001521 if (unlikely(from >= mtd->size ||
1522 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1523 (from >> chip->page_shift)) * len)) {
1524 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1525 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001526 return -EINVAL;
1527 }
1528
William Juul52c07962007-10-31 13:53:06 +01001529 chipnr = (int)(from >> chip->chip_shift);
1530 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001531
William Juul52c07962007-10-31 13:53:06 +01001532 /* Shift to get page */
1533 realpage = (int)(from >> chip->page_shift);
1534 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001535
William Juul52c07962007-10-31 13:53:06 +01001536 while(1) {
1537 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001538
William Juul52c07962007-10-31 13:53:06 +01001539 len = min(len, readlen);
1540 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001541
William Juul52c07962007-10-31 13:53:06 +01001542 if (!(chip->options & NAND_NO_READRDY)) {
1543 /*
1544 * Apply delay or wait for ready/busy pin. Do this
1545 * before the AUTOINCR check, so no problems arise if a
1546 * chip which does auto increment is marked as
1547 * NOAUTOINCR by the board driver.
1548 */
1549 if (!chip->dev_ready)
1550 udelay(chip->chip_delay);
1551 else
1552 nand_wait_ready(mtd);
1553 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001554
William Juul52c07962007-10-31 13:53:06 +01001555 readlen -= len;
1556 if (!readlen)
1557 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001558
William Juul52c07962007-10-31 13:53:06 +01001559 /* Increment page address */
1560 realpage++;
1561
1562 page = realpage & chip->pagemask;
1563 /* Check, if we cross a chip boundary */
1564 if (!page) {
1565 chipnr++;
1566 chip->select_chip(mtd, -1);
1567 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001568 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001569
William Juul52c07962007-10-31 13:53:06 +01001570 /* Check, if the chip supports auto page increment
1571 * or if we have hit a block boundary.
1572 */
1573 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1574 sndcmd = 1;
1575 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001576
William Juul52c07962007-10-31 13:53:06 +01001577 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001578 return 0;
1579}
1580
1581/**
William Juul52c07962007-10-31 13:53:06 +01001582 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001583 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001584 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001585 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001586 *
William Juul52c07962007-10-31 13:53:06 +01001587 * NAND read data and/or out-of-band data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001588 */
William Juul52c07962007-10-31 13:53:06 +01001589static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1590 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001591{
William Juul52c07962007-10-31 13:53:06 +01001592 struct nand_chip *chip = mtd->priv;
1593 int ret = -ENOTSUPP;
1594
1595 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001596
1597 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001598 if (ops->datbuf && (from + ops->len) > mtd->size) {
1599 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1600 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001601 return -EINVAL;
1602 }
1603
William Juul52c07962007-10-31 13:53:06 +01001604 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001605
William Juul52c07962007-10-31 13:53:06 +01001606 switch(ops->mode) {
1607 case MTD_OOB_PLACE:
1608 case MTD_OOB_AUTO:
1609 case MTD_OOB_RAW:
1610 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001611
William Juul52c07962007-10-31 13:53:06 +01001612 default:
1613 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001614 }
1615
William Juul52c07962007-10-31 13:53:06 +01001616 if (!ops->datbuf)
1617 ret = nand_do_read_oob(mtd, from, ops);
1618 else
1619 ret = nand_do_read_ops(mtd, from, ops);
1620
1621 out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001622 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001623 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001624}
1625
1626
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001627/**
William Juul52c07962007-10-31 13:53:06 +01001628 * nand_write_page_raw - [Intern] raw page write function
1629 * @mtd: mtd info structure
1630 * @chip: nand chip info structure
1631 * @buf: data buffer
1632 */
1633static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1634 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001635{
William Juul52c07962007-10-31 13:53:06 +01001636 chip->write_buf(mtd, buf, mtd->writesize);
1637 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1638}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001639
William Juul52c07962007-10-31 13:53:06 +01001640/**
1641 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1642 * @mtd: mtd info structure
1643 * @chip: nand chip info structure
1644 * @buf: data buffer
1645 */
1646static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1647 const uint8_t *buf)
1648{
1649 int i, eccsize = chip->ecc.size;
1650 int eccbytes = chip->ecc.bytes;
1651 int eccsteps = chip->ecc.steps;
1652 uint8_t *ecc_calc = chip->buffers->ecccalc;
1653 const uint8_t *p = buf;
1654 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001655
William Juul52c07962007-10-31 13:53:06 +01001656 /* Software ecc calculation */
1657 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1658 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001659
William Juul52c07962007-10-31 13:53:06 +01001660 for (i = 0; i < chip->ecc.total; i++)
1661 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001662
William Juul52c07962007-10-31 13:53:06 +01001663 chip->ecc.write_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001664}
1665
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001666/**
William Juul52c07962007-10-31 13:53:06 +01001667 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1668 * @mtd: mtd info structure
1669 * @chip: nand chip info structure
1670 * @buf: data buffer
1671 */
1672static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1673 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001674{
William Juul52c07962007-10-31 13:53:06 +01001675 int i, eccsize = chip->ecc.size;
1676 int eccbytes = chip->ecc.bytes;
1677 int eccsteps = chip->ecc.steps;
1678 uint8_t *ecc_calc = chip->buffers->ecccalc;
1679 const uint8_t *p = buf;
1680 uint32_t *eccpos = chip->ecc.layout->eccpos;
1681
1682 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1683 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1684 chip->write_buf(mtd, p, eccsize);
1685 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1686 }
1687
1688 for (i = 0; i < chip->ecc.total; i++)
1689 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1690
1691 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001692}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001693
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001694/**
William Juul52c07962007-10-31 13:53:06 +01001695 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1696 * @mtd: mtd info structure
1697 * @chip: nand chip info structure
1698 * @buf: data buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001699 *
William Juul52c07962007-10-31 13:53:06 +01001700 * The hw generator calculates the error syndrome automatically. Therefor
1701 * we need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001702 */
William Juul52c07962007-10-31 13:53:06 +01001703static void nand_write_page_syndrome(struct mtd_info *mtd,
1704 struct nand_chip *chip, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001705{
William Juul52c07962007-10-31 13:53:06 +01001706 int i, eccsize = chip->ecc.size;
1707 int eccbytes = chip->ecc.bytes;
1708 int eccsteps = chip->ecc.steps;
1709 const uint8_t *p = buf;
1710 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001711
William Juul52c07962007-10-31 13:53:06 +01001712 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001713
William Juul52c07962007-10-31 13:53:06 +01001714 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1715 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001716
William Juul52c07962007-10-31 13:53:06 +01001717 if (chip->ecc.prepad) {
1718 chip->write_buf(mtd, oob, chip->ecc.prepad);
1719 oob += chip->ecc.prepad;
1720 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001721
William Juul52c07962007-10-31 13:53:06 +01001722 chip->ecc.calculate(mtd, p, oob);
1723 chip->write_buf(mtd, oob, eccbytes);
1724 oob += eccbytes;
1725
1726 if (chip->ecc.postpad) {
1727 chip->write_buf(mtd, oob, chip->ecc.postpad);
1728 oob += chip->ecc.postpad;
1729 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001730 }
1731
William Juul52c07962007-10-31 13:53:06 +01001732 /* Calculate remaining oob bytes */
1733 i = mtd->oobsize - (oob - chip->oob_poi);
1734 if (i)
1735 chip->write_buf(mtd, oob, i);
1736}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001737
William Juul52c07962007-10-31 13:53:06 +01001738/**
1739 * nand_write_page - [REPLACEABLE] write one page
1740 * @mtd: MTD device structure
1741 * @chip: NAND chip descriptor
1742 * @buf: the data to write
1743 * @page: page number to write
1744 * @cached: cached programming
1745 * @raw: use _raw version of write_page
1746 */
1747static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1748 const uint8_t *buf, int page, int cached, int raw)
1749{
1750 int status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001751
William Juul52c07962007-10-31 13:53:06 +01001752 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1753
1754 if (unlikely(raw))
1755 chip->ecc.write_page_raw(mtd, chip, buf);
1756 else
1757 chip->ecc.write_page(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001758
William Juul52c07962007-10-31 13:53:06 +01001759 /*
1760 * Cached progamming disabled for now, Not sure if its worth the
1761 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1762 */
1763 cached = 0;
1764
1765 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1766
1767 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1768 status = chip->waitfunc(mtd, chip);
1769 /*
1770 * See if operation failed and additional status checks are
1771 * available
1772 */
1773 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1774 status = chip->errstat(mtd, chip, FL_WRITING, status,
1775 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001776
William Juul52c07962007-10-31 13:53:06 +01001777 if (status & NAND_STATUS_FAIL)
1778 return -EIO;
1779 } else {
1780 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1781 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001782 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001783
William Juul52c07962007-10-31 13:53:06 +01001784#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1785 /* Send command to read back the data */
1786 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001787
William Juul52c07962007-10-31 13:53:06 +01001788 if (chip->verify_buf(mtd, buf, mtd->writesize))
1789 return -EIO;
1790#endif
1791 return 0;
1792}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001793
William Juul52c07962007-10-31 13:53:06 +01001794/**
1795 * nand_fill_oob - [Internal] Transfer client buffer to oob
1796 * @chip: nand chip structure
1797 * @oob: oob data buffer
1798 * @ops: oob ops structure
1799 */
1800static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1801 struct mtd_oob_ops *ops)
1802{
1803 size_t len = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001804
William Juul52c07962007-10-31 13:53:06 +01001805 switch(ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001806
William Juul52c07962007-10-31 13:53:06 +01001807 case MTD_OOB_PLACE:
1808 case MTD_OOB_RAW:
1809 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1810 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001811
William Juul52c07962007-10-31 13:53:06 +01001812 case MTD_OOB_AUTO: {
1813 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1814 uint32_t boffs = 0, woffs = ops->ooboffs;
1815 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001816
William Juul52c07962007-10-31 13:53:06 +01001817 for(; free->length && len; free++, len -= bytes) {
1818 /* Write request not from offset 0 ? */
1819 if (unlikely(woffs)) {
1820 if (woffs >= free->length) {
1821 woffs -= free->length;
1822 continue;
1823 }
1824 boffs = free->offset + woffs;
1825 bytes = min_t(size_t, len,
1826 (free->length - woffs));
1827 woffs = 0;
1828 } else {
1829 bytes = min_t(size_t, len, free->length);
1830 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001831 }
William Juul52c07962007-10-31 13:53:06 +01001832 memcpy(chip->oob_poi + boffs, oob, bytes);
1833 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001834 }
William Juul52c07962007-10-31 13:53:06 +01001835 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001836 }
William Juul52c07962007-10-31 13:53:06 +01001837 default:
1838 BUG();
1839 }
1840 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001841}
1842
William Juul52c07962007-10-31 13:53:06 +01001843#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001844
1845/**
William Juul52c07962007-10-31 13:53:06 +01001846 * nand_do_write_ops - [Internal] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001847 * @mtd: MTD device structure
1848 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001849 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001850 *
William Juul52c07962007-10-31 13:53:06 +01001851 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001852 */
William Juul52c07962007-10-31 13:53:06 +01001853static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1854 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001855{
William Juul52c07962007-10-31 13:53:06 +01001856 int chipnr, realpage, page, blockmask, column;
1857 struct nand_chip *chip = mtd->priv;
1858 uint32_t writelen = ops->len;
1859 uint8_t *oob = ops->oobbuf;
1860 uint8_t *buf = ops->datbuf;
1861 int ret, subpage;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001862
William Juul52c07962007-10-31 13:53:06 +01001863 ops->retlen = 0;
1864 if (!writelen)
1865 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001866
William Juul52c07962007-10-31 13:53:06 +01001867 /* reject writes, which are not page aligned */
1868 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
1869 printk(KERN_NOTICE "nand_write: "
1870 "Attempt to write not page aligned data\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001871 return -EINVAL;
1872 }
1873
William Juul52c07962007-10-31 13:53:06 +01001874 column = to & (mtd->writesize - 1);
1875 subpage = column || (writelen & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001876
William Juul52c07962007-10-31 13:53:06 +01001877 if (subpage && oob)
1878 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001879
William Juul52c07962007-10-31 13:53:06 +01001880 chipnr = (int)(to >> chip->chip_shift);
1881 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001882
1883 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01001884 if (nand_check_wp(mtd)) {
1885 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1886 return -EIO;
1887 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001888
William Juul52c07962007-10-31 13:53:06 +01001889 realpage = (int)(to >> chip->page_shift);
1890 page = realpage & chip->pagemask;
1891 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001892
William Juul52c07962007-10-31 13:53:06 +01001893 /* Invalidate the page cache, when we write to the cached page */
1894 if (to <= (chip->pagebuf << chip->page_shift) &&
1895 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1896 chip->pagebuf = -1;
1897
1898 /* If we're not given explicit OOB data, let it be 0xFF */
1899 if (likely(!oob))
1900 memset(chip->oob_poi, 0xff, mtd->oobsize);
1901
1902 while(1) {
1903 int bytes = mtd->writesize;
1904 int cached = writelen > bytes && page != blockmask;
1905 uint8_t *wbuf = buf;
1906
1907 /* Partial page write ? */
1908 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1909 cached = 0;
1910 bytes = min_t(int, bytes - column, (int) writelen);
1911 chip->pagebuf = -1;
1912 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1913 memcpy(&chip->buffers->databuf[column], buf, bytes);
1914 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02001915 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001916
William Juul52c07962007-10-31 13:53:06 +01001917 if (unlikely(oob))
1918 oob = nand_fill_oob(chip, oob, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001919
William Juul52c07962007-10-31 13:53:06 +01001920 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1921 (ops->mode == MTD_OOB_RAW));
1922 if (ret)
1923 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001924
William Juul52c07962007-10-31 13:53:06 +01001925 writelen -= bytes;
1926 if (!writelen)
1927 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001928
William Juul52c07962007-10-31 13:53:06 +01001929 column = 0;
1930 buf += bytes;
1931 realpage++;
1932
1933 page = realpage & chip->pagemask;
1934 /* Check, if we cross a chip boundary */
1935 if (!page) {
1936 chipnr++;
1937 chip->select_chip(mtd, -1);
1938 chip->select_chip(mtd, chipnr);
1939 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001940 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001941
William Juul52c07962007-10-31 13:53:06 +01001942 ops->retlen = ops->len - writelen;
1943 if (unlikely(oob))
1944 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001945 return ret;
1946}
1947
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001948/**
William Juul52c07962007-10-31 13:53:06 +01001949 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001950 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001951 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001952 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001953 * @retlen: pointer to variable to store the number of written bytes
William Juul52c07962007-10-31 13:53:06 +01001954 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001955 *
William Juul52c07962007-10-31 13:53:06 +01001956 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001957 */
William Juul52c07962007-10-31 13:53:06 +01001958static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1959 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001960{
William Juul52c07962007-10-31 13:53:06 +01001961 struct nand_chip *chip = mtd->priv;
1962 int ret;
1963
1964 /* Do not allow reads past end of device */
1965 if ((to + len) > mtd->size)
1966 return -EINVAL;
1967 if (!len)
1968 return 0;
1969
1970 nand_get_device(chip, mtd, FL_WRITING);
1971
1972 chip->ops.len = len;
1973 chip->ops.datbuf = (uint8_t *)buf;
1974 chip->ops.oobbuf = NULL;
1975
1976 ret = nand_do_write_ops(mtd, to, &chip->ops);
1977
1978 *retlen = chip->ops.retlen;
1979
1980 nand_release_device(mtd);
1981
1982 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001983}
1984
1985/**
William Juul52c07962007-10-31 13:53:06 +01001986 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001987 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001988 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001989 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001990 *
William Juul52c07962007-10-31 13:53:06 +01001991 * NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001992 */
William Juul52c07962007-10-31 13:53:06 +01001993static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1994 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001995{
William Juul52c07962007-10-31 13:53:06 +01001996 int chipnr, page, status, len;
1997 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001998
William Juul52c07962007-10-31 13:53:06 +01001999 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
2000 (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002001
William Juul52c07962007-10-31 13:53:06 +01002002 if (ops->mode == MTD_OOB_AUTO)
2003 len = chip->ecc.layout->oobavail;
2004 else
2005 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002006
2007 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002008 if ((ops->ooboffs + ops->ooblen) > len) {
2009 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
2010 "Attempt to write past end of page\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002011 return -EINVAL;
2012 }
2013
William Juul52c07962007-10-31 13:53:06 +01002014 if (unlikely(ops->ooboffs >= len)) {
2015 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2016 "Attempt to start write outside oob\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002017 return -EINVAL;
2018 }
2019
William Juul52c07962007-10-31 13:53:06 +01002020 /* Do not allow reads past end of device */
2021 if (unlikely(to >= mtd->size ||
2022 ops->ooboffs + ops->ooblen >
2023 ((mtd->size >> chip->page_shift) -
2024 (to >> chip->page_shift)) * len)) {
2025 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2026 "Attempt write beyond end of device\n");
2027 return -EINVAL;
2028 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002029
William Juul52c07962007-10-31 13:53:06 +01002030 chipnr = (int)(to >> chip->chip_shift);
2031 chip->select_chip(mtd, chipnr);
2032
2033 /* Shift to get page */
2034 page = (int)(to >> chip->page_shift);
2035
2036 /*
2037 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2038 * of my DiskOnChip 2000 test units) will clear the whole data page too
2039 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2040 * it in the doc2000 driver in August 1999. dwmw2.
2041 */
2042 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002043
2044 /* Check, if it is write protected */
2045 if (nand_check_wp(mtd))
William Juul52c07962007-10-31 13:53:06 +01002046 return -EROFS;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002047
William Juul52c07962007-10-31 13:53:06 +01002048 /* Invalidate the page cache, if we write to the cached page */
2049 if (page == chip->pagebuf)
2050 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002051
William Juul52c07962007-10-31 13:53:06 +01002052 memset(chip->oob_poi, 0xff, mtd->oobsize);
2053 nand_fill_oob(chip, ops->oobbuf, ops);
2054 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2055 memset(chip->oob_poi, 0xff, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002056
William Juul52c07962007-10-31 13:53:06 +01002057 if (status)
2058 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002059
William Juul52c07962007-10-31 13:53:06 +01002060 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002061
William Juul52c07962007-10-31 13:53:06 +01002062 return 0;
2063}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002064
William Juul52c07962007-10-31 13:53:06 +01002065/**
2066 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2067 * @mtd: MTD device structure
2068 * @to: offset to write to
2069 * @ops: oob operation description structure
2070 */
2071static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2072 struct mtd_oob_ops *ops)
2073{
2074 struct nand_chip *chip = mtd->priv;
2075 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002076
William Juul52c07962007-10-31 13:53:06 +01002077 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002078
William Juul52c07962007-10-31 13:53:06 +01002079 /* Do not allow writes past end of device */
2080 if (ops->datbuf && (to + ops->len) > mtd->size) {
2081 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2082 "Attempt read beyond end of device\n");
2083 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002084 }
William Juul52c07962007-10-31 13:53:06 +01002085
2086 nand_get_device(chip, mtd, FL_WRITING);
2087
2088 switch(ops->mode) {
2089 case MTD_OOB_PLACE:
2090 case MTD_OOB_AUTO:
2091 case MTD_OOB_RAW:
2092 break;
2093
2094 default:
2095 goto out;
2096 }
2097
2098 if (!ops->datbuf)
2099 ret = nand_do_write_oob(mtd, to, ops);
2100 else
2101 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002102
William Juul52c07962007-10-31 13:53:06 +01002103 out:
2104 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002105 return ret;
2106}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002107
2108/**
2109 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2110 * @mtd: MTD device structure
2111 * @page: the page address of the block which will be erased
2112 *
2113 * Standard erase command for NAND chips
2114 */
William Juul52c07962007-10-31 13:53:06 +01002115static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002116{
William Juul52c07962007-10-31 13:53:06 +01002117 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002118 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002119 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2120 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002121}
2122
2123/**
2124 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2125 * @mtd: MTD device structure
2126 * @page: the page address of the block which will be erased
2127 *
2128 * AND multi block erase command function
2129 * Erase 4 consecutive blocks
2130 */
William Juul52c07962007-10-31 13:53:06 +01002131static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002132{
William Juul52c07962007-10-31 13:53:06 +01002133 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002134 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002135 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2136 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2137 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2138 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2139 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002140}
2141
2142/**
2143 * nand_erase - [MTD Interface] erase block(s)
2144 * @mtd: MTD device structure
2145 * @instr: erase instruction
2146 *
2147 * Erase one ore more blocks
2148 */
William Juul52c07962007-10-31 13:53:06 +01002149static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002150{
William Juul52c07962007-10-31 13:53:06 +01002151 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002152}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002153
William Juul52c07962007-10-31 13:53:06 +01002154#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002155/**
William Juul52c07962007-10-31 13:53:06 +01002156 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002157 * @mtd: MTD device structure
2158 * @instr: erase instruction
2159 * @allowbbt: allow erasing the bbt area
2160 *
2161 * Erase one ore more blocks
2162 */
William Juul52c07962007-10-31 13:53:06 +01002163int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2164 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002165{
2166 int page, len, status, pages_per_block, ret, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002167 struct nand_chip *chip = mtd->priv;
Wolfgang Grandegger03025502009-01-16 18:55:54 +01002168 int rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS]={0};
William Juul52c07962007-10-31 13:53:06 +01002169 unsigned int bbt_masked_page = 0xffffffff;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002170
Scott Wooddf83c472008-06-20 12:38:57 -05002171 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2172 (unsigned int) instr->addr, (unsigned int) instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002173
2174 /* Start address must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002175 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002176 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002177 return -EINVAL;
2178 }
2179
2180 /* Length must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002181 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002182 MTDDEBUG (MTD_DEBUG_LEVEL0,
2183 "nand_erase: Length not block aligned\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002184 return -EINVAL;
2185 }
2186
2187 /* Do not allow erase past end of device */
2188 if ((instr->len + instr->addr) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05002189 MTDDEBUG (MTD_DEBUG_LEVEL0,
2190 "nand_erase: Erase past end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002191 return -EINVAL;
2192 }
2193
2194 instr->fail_addr = 0xffffffff;
2195
2196 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002197 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002198
2199 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002200 page = (int)(instr->addr >> chip->page_shift);
2201 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002202
2203 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002204 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002205
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002206 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002207 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002208
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002209 /* Check, if it is write protected */
2210 if (nand_check_wp(mtd)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002211 MTDDEBUG (MTD_DEBUG_LEVEL0,
2212 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002213 instr->state = MTD_ERASE_FAILED;
2214 goto erase_exit;
2215 }
2216
William Juul52c07962007-10-31 13:53:06 +01002217 /*
2218 * If BBT requires refresh, set the BBT page mask to see if the BBT
2219 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2220 * can not be matched. This is also done when the bbt is actually
2221 * erased to avoid recusrsive updates
2222 */
2223 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2224 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2225
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002226 /* Loop through the pages */
2227 len = instr->len;
2228
2229 instr->state = MTD_ERASING;
2230
2231 while (len) {
William Juul52c07962007-10-31 13:53:06 +01002232 /*
2233 * heck if we have a bad block, we do not erase bad blocks !
2234 */
2235 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2236 chip->page_shift, 0, allowbbt)) {
2237 printk(KERN_WARNING "nand_erase: attempt to erase a "
2238 "bad block at page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002239 instr->state = MTD_ERASE_FAILED;
2240 goto erase_exit;
2241 }
William Juul52c07962007-10-31 13:53:06 +01002242
2243 /*
2244 * Invalidate the page cache, if we erase the block which
2245 * contains the current cached page
2246 */
2247 if (page <= chip->pagebuf && chip->pagebuf <
2248 (page + pages_per_block))
2249 chip->pagebuf = -1;
2250
2251 chip->erase_cmd(mtd, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002252
William Juul52c07962007-10-31 13:53:06 +01002253 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002254
William Juul52c07962007-10-31 13:53:06 +01002255 /*
2256 * See if operation failed and additional status checks are
2257 * available
2258 */
2259 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2260 status = chip->errstat(mtd, chip, FL_ERASING,
2261 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002262
2263 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002264 if (status & NAND_STATUS_FAIL) {
Scott Wooddf83c472008-06-20 12:38:57 -05002265 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2266 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002267 instr->state = MTD_ERASE_FAILED;
William Juul52c07962007-10-31 13:53:06 +01002268 instr->fail_addr = (page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002269 goto erase_exit;
2270 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002271
William Juul52c07962007-10-31 13:53:06 +01002272 /*
2273 * If BBT requires refresh, set the BBT rewrite flag to the
2274 * page being erased
2275 */
2276 if (bbt_masked_page != 0xffffffff &&
2277 (page & BBT_PAGE_MASK) == bbt_masked_page)
2278 rewrite_bbt[chipnr] = (page << chip->page_shift);
2279
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002280 /* Increment page address and decrement length */
William Juul52c07962007-10-31 13:53:06 +01002281 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002282 page += pages_per_block;
2283
2284 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002285 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002286 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002287 chip->select_chip(mtd, -1);
2288 chip->select_chip(mtd, chipnr);
2289
2290 /*
2291 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2292 * page mask to see if this BBT should be rewritten
2293 */
2294 if (bbt_masked_page != 0xffffffff &&
2295 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2296 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2297 BBT_PAGE_MASK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002298 }
2299 }
2300 instr->state = MTD_ERASE_DONE;
2301
William Juul52c07962007-10-31 13:53:06 +01002302 erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002303
2304 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002305
2306 /* Deselect and wake up anyone waiting on the device */
2307 nand_release_device(mtd);
2308
Scott Wood3628f002008-10-24 16:20:43 -05002309 /* Do call back function */
2310 if (!ret)
2311 mtd_erase_callback(instr);
2312
William Juul52c07962007-10-31 13:53:06 +01002313 /*
2314 * If BBT requires refresh and erase was successful, rewrite any
2315 * selected bad block tables
2316 */
2317 if (bbt_masked_page == 0xffffffff || ret)
2318 return ret;
2319
2320 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2321 if (!rewrite_bbt[chipnr])
2322 continue;
2323 /* update the BBT for chip */
2324 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2325 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2326 chip->bbt_td->pages[chipnr]);
2327 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2328 }
2329
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002330 /* Return more or less happy */
2331 return ret;
2332}
2333
2334/**
2335 * nand_sync - [MTD Interface] sync
2336 * @mtd: MTD device structure
2337 *
2338 * Sync is actually a wait for chip ready function
2339 */
William Juul52c07962007-10-31 13:53:06 +01002340static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002341{
William Juul52c07962007-10-31 13:53:06 +01002342 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002343
Scott Wooddf83c472008-06-20 12:38:57 -05002344 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002345
2346 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002347 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002348 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002349 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002350}
2351
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002352/**
William Juul52c07962007-10-31 13:53:06 +01002353 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002354 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002355 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002356 */
William Juul52c07962007-10-31 13:53:06 +01002357static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002358{
2359 /* Check for invalid offset */
William Juul52c07962007-10-31 13:53:06 +01002360 if (offs > mtd->size)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002361 return -EINVAL;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002362
William Juul52c07962007-10-31 13:53:06 +01002363 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002364}
2365
2366/**
William Juul52c07962007-10-31 13:53:06 +01002367 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002368 * @mtd: MTD device structure
2369 * @ofs: offset relative to mtd start
2370 */
William Juul52c07962007-10-31 13:53:06 +01002371static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002372{
William Juul52c07962007-10-31 13:53:06 +01002373 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002374 int ret;
2375
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002376 if ((ret = nand_block_isbad(mtd, ofs))) {
2377 /* If it was bad already, return success and do nothing. */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002378 if (ret > 0)
2379 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002380 return ret;
2381 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002382
William Juul52c07962007-10-31 13:53:06 +01002383 return chip->block_markbad(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002384}
2385
2386/**
William Juul52c07962007-10-31 13:53:06 +01002387 * nand_suspend - [MTD Interface] Suspend the NAND flash
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002388 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002389 */
William Juul52c07962007-10-31 13:53:06 +01002390static int nand_suspend(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002391{
William Juul52c07962007-10-31 13:53:06 +01002392 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002393
William Juul52c07962007-10-31 13:53:06 +01002394 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2395}
2396
2397/**
2398 * nand_resume - [MTD Interface] Resume the NAND flash
2399 * @mtd: MTD device structure
2400 */
2401static void nand_resume(struct mtd_info *mtd)
2402{
2403 struct nand_chip *chip = mtd->priv;
2404
2405 if (chip->state == FL_PM_SUSPENDED)
2406 nand_release_device(mtd);
2407 else
2408 printk(KERN_ERR "nand_resume() called for a chip which is not "
2409 "in suspended state\n");
2410}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002411
William Juul52c07962007-10-31 13:53:06 +01002412/*
2413 * Set default functions
2414 */
2415static void nand_set_defaults(struct nand_chip *chip, int busw)
2416{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002417 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01002418 if (!chip->chip_delay)
2419 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002420
2421 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01002422 if (chip->cmdfunc == NULL)
2423 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002424
2425 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01002426 if (chip->waitfunc == NULL)
2427 chip->waitfunc = nand_wait;
2428
2429 if (!chip->select_chip)
2430 chip->select_chip = nand_select_chip;
2431 if (!chip->read_byte)
2432 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2433 if (!chip->read_word)
2434 chip->read_word = nand_read_word;
2435 if (!chip->block_bad)
2436 chip->block_bad = nand_block_bad;
2437 if (!chip->block_markbad)
2438 chip->block_markbad = nand_default_block_markbad;
2439 if (!chip->write_buf)
2440 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2441 if (!chip->read_buf)
2442 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2443 if (!chip->verify_buf)
2444 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2445 if (!chip->scan_bbt)
2446 chip->scan_bbt = nand_default_bbt;
2447
2448 if (!chip->controller) {
2449 chip->controller = &chip->hwcontrol;
2450
2451 /* XXX U-BOOT XXX */
2452#if 0
2453 spin_lock_init(&chip->controller->lock);
2454 init_waitqueue_head(&chip->controller->wq);
2455#endif
2456 }
2457
2458}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002459
William Juul52c07962007-10-31 13:53:06 +01002460/*
2461 * Get the flash and manufacturer id and lookup if the type is supported
2462 */
2463static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2464 struct nand_chip *chip,
2465 int busw, int *maf_id)
2466{
2467 struct nand_flash_dev *type = NULL;
2468 int i, dev_id, maf_idx;
Scott Wood3628f002008-10-24 16:20:43 -05002469 int tmp_id, tmp_manf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002470
2471 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01002472 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002473
Karl Beldanb6322fc2008-09-15 16:08:03 +02002474 /*
2475 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2476 * after power-up
2477 */
2478 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2479
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002480 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01002481 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002482
2483 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002484 *maf_id = chip->read_byte(mtd);
2485 dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002486
Scott Wood3628f002008-10-24 16:20:43 -05002487 /* Try again to make sure, as some systems the bus-hold or other
2488 * interface concerns can cause random data which looks like a
2489 * possibly credible NAND flash to appear. If the two results do
2490 * not match, ignore the device completely.
2491 */
2492
2493 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2494
2495 /* Read manufacturer and device IDs */
2496
2497 tmp_manf = chip->read_byte(mtd);
2498 tmp_id = chip->read_byte(mtd);
2499
2500 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2501 printk(KERN_INFO "%s: second ID read did not match "
2502 "%02x,%02x against %02x,%02x\n", __func__,
2503 *maf_id, dev_id, tmp_manf, tmp_id);
2504 return ERR_PTR(-ENODEV);
2505 }
2506
William Juul52c07962007-10-31 13:53:06 +01002507 /* Lookup the flash id */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002508 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
William Juul52c07962007-10-31 13:53:06 +01002509 if (dev_id == nand_flash_ids[i].id) {
2510 type = &nand_flash_ids[i];
2511 break;
2512 }
2513 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002514
William Juul52c07962007-10-31 13:53:06 +01002515 if (!type)
2516 return ERR_PTR(-ENODEV);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002517
William Juul52c07962007-10-31 13:53:06 +01002518 if (!mtd->name)
2519 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002520
William Juul52c07962007-10-31 13:53:06 +01002521 chip->chipsize = type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002522
William Juul52c07962007-10-31 13:53:06 +01002523 /* Newer devices have all the information in additional id bytes */
2524 if (!type->pagesize) {
2525 int extid;
2526 /* The 3rd id byte holds MLC / multichip data */
2527 chip->cellinfo = chip->read_byte(mtd);
2528 /* The 4th id byte is the important one */
2529 extid = chip->read_byte(mtd);
2530 /* Calc pagesize */
2531 mtd->writesize = 1024 << (extid & 0x3);
2532 extid >>= 2;
2533 /* Calc oobsize */
2534 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2535 extid >>= 2;
2536 /* Calc blocksize. Blocksize is multiples of 64KiB */
2537 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2538 extid >>= 2;
2539 /* Get buswidth information */
2540 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002541
William Juul52c07962007-10-31 13:53:06 +01002542 } else {
2543 /*
2544 * Old devices have chip data hardcoded in the device id table
2545 */
2546 mtd->erasesize = type->erasesize;
2547 mtd->writesize = type->pagesize;
2548 mtd->oobsize = mtd->writesize / 32;
2549 busw = type->options & NAND_BUSWIDTH_16;
2550 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002551
William Juul52c07962007-10-31 13:53:06 +01002552 /* Try to identify manufacturer */
2553 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2554 if (nand_manuf_ids[maf_idx].id == *maf_id)
2555 break;
2556 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002557
William Juul52c07962007-10-31 13:53:06 +01002558 /*
2559 * Check, if buswidth is correct. Hardware drivers should set
2560 * chip correct !
2561 */
2562 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2563 printk(KERN_INFO "NAND device: Manufacturer ID:"
2564 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2565 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2566 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2567 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2568 busw ? 16 : 8);
2569 return ERR_PTR(-EINVAL);
2570 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002571
William Juul52c07962007-10-31 13:53:06 +01002572 /* Calculate the address shift from the page size */
2573 chip->page_shift = ffs(mtd->writesize) - 1;
2574 /* Convert chipsize to number of pages per chip -1. */
2575 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002576
William Juul52c07962007-10-31 13:53:06 +01002577 chip->bbt_erase_shift = chip->phys_erase_shift =
2578 ffs(mtd->erasesize) - 1;
2579 chip->chip_shift = ffs(chip->chipsize) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002580
William Juul52c07962007-10-31 13:53:06 +01002581 /* Set the bad block position */
2582 chip->badblockpos = mtd->writesize > 512 ?
2583 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002584
William Juul52c07962007-10-31 13:53:06 +01002585 /* Get chip options, preserve non chip based options */
2586 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2587 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002588
William Juul52c07962007-10-31 13:53:06 +01002589 /*
2590 * Set chip as a default. Board drivers can override it, if necessary
2591 */
2592 chip->options |= NAND_NO_AUTOINCR;
2593
2594 /* Check if chip is a not a samsung device. Do not clear the
2595 * options for chips which are not having an extended id.
2596 */
2597 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2598 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2599
2600 /* Check for AND chips with 4 page planes */
2601 if (chip->options & NAND_4PAGE_ARRAY)
2602 chip->erase_cmd = multi_erase_cmd;
2603 else
2604 chip->erase_cmd = single_erase_cmd;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002605
William Juul52c07962007-10-31 13:53:06 +01002606 /* Do not replace user supplied command function ! */
2607 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2608 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002609
Stefan Roese4fe71432008-01-10 18:47:33 +01002610 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2611 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2612 nand_manuf_ids[maf_idx].name, type->name);
William Juul52c07962007-10-31 13:53:06 +01002613
2614 return type;
2615}
2616
2617/**
2618 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2619 * @mtd: MTD device structure
2620 * @maxchips: Number of chips to scan for
2621 *
2622 * This is the first phase of the normal nand_scan() function. It
2623 * reads the flash ID and sets up MTD fields accordingly.
2624 *
2625 * The mtd->owner field must be set to the module of the caller.
2626 */
2627int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2628{
2629 int i, busw, nand_maf_id;
2630 struct nand_chip *chip = mtd->priv;
2631 struct nand_flash_dev *type;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002632
William Juul52c07962007-10-31 13:53:06 +01002633 /* Get buswidth to select the correct functions */
2634 busw = chip->options & NAND_BUSWIDTH_16;
2635 /* Set the default functions */
2636 nand_set_defaults(chip, busw);
2637
2638 /* Read the flash type */
2639 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2640
2641 if (IS_ERR(type)) {
Peter Tyserf96047a2009-02-04 13:39:40 -06002642#ifndef CONFIG_SYS_NAND_QUIET_TEST
William Juul52c07962007-10-31 13:53:06 +01002643 printk(KERN_WARNING "No NAND device found!!!\n");
Peter Tyserf96047a2009-02-04 13:39:40 -06002644#endif
William Juul52c07962007-10-31 13:53:06 +01002645 chip->select_chip(mtd, -1);
2646 return PTR_ERR(type);
2647 }
2648
2649 /* Check for a chip array */
2650 for (i = 1; i < maxchips; i++) {
2651 chip->select_chip(mtd, i);
Karl Beldanb6322fc2008-09-15 16:08:03 +02002652 /* See comment in nand_get_flash_type for reset */
2653 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002654 /* Send the command for reading device ID */
2655 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002656 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002657 if (nand_maf_id != chip->read_byte(mtd) ||
2658 type->id != chip->read_byte(mtd))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002659 break;
2660 }
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01002661#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002662 if (i > 1)
2663 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01002664#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002665
William Juul52c07962007-10-31 13:53:06 +01002666 /* Store the number of chips and calc total size for mtd */
2667 chip->numchips = i;
2668 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002669
William Juul52c07962007-10-31 13:53:06 +01002670 return 0;
2671}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002672
William Juul52c07962007-10-31 13:53:06 +01002673
2674/**
2675 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2676 * @mtd: MTD device structure
2677 * @maxchips: Number of chips to scan for
2678 *
2679 * This is the second phase of the normal nand_scan() function. It
2680 * fills out all the uninitialized function pointers with the defaults
2681 * and scans for a bad block table if appropriate.
2682 */
2683int nand_scan_tail(struct mtd_info *mtd)
2684{
2685 int i;
2686 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002687
William Juul52c07962007-10-31 13:53:06 +01002688 if (!(chip->options & NAND_OWN_BUFFERS))
2689 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2690 if (!chip->buffers)
2691 return -ENOMEM;
2692
2693 /* Set the internal oob buffer location, just after the page data */
2694 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2695
2696 /*
2697 * If no default placement scheme is given, select an appropriate one
2698 */
2699 if (!chip->ecc.layout) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002700 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002701 case 8:
William Juul52c07962007-10-31 13:53:06 +01002702 chip->ecc.layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002703 break;
2704 case 16:
William Juul52c07962007-10-31 13:53:06 +01002705 chip->ecc.layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002706 break;
2707 case 64:
William Juul52c07962007-10-31 13:53:06 +01002708 chip->ecc.layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002709 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002710 case 128:
William Juul52c07962007-10-31 13:53:06 +01002711 chip->ecc.layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002712 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002713 default:
William Juul52c07962007-10-31 13:53:06 +01002714 printk(KERN_WARNING "No oob scheme defined for "
2715 "oobsize %d\n", mtd->oobsize);
William Juul9e9c2c12007-11-09 13:32:30 +01002716/* BUG(); */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002717 }
2718 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002719
William Juul52c07962007-10-31 13:53:06 +01002720 if (!chip->write_page)
2721 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002722
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002723 /*
William Juul52c07962007-10-31 13:53:06 +01002724 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2725 * selected and we have 256 byte pagesize fallback to software ECC
2726 */
2727 if (!chip->ecc.read_page_raw)
2728 chip->ecc.read_page_raw = nand_read_page_raw;
2729 if (!chip->ecc.write_page_raw)
2730 chip->ecc.write_page_raw = nand_write_page_raw;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002731
William Juul52c07962007-10-31 13:53:06 +01002732 switch (chip->ecc.mode) {
2733 case NAND_ECC_HW:
2734 /* Use standard hwecc read page function ? */
2735 if (!chip->ecc.read_page)
2736 chip->ecc.read_page = nand_read_page_hwecc;
2737 if (!chip->ecc.write_page)
2738 chip->ecc.write_page = nand_write_page_hwecc;
2739 if (!chip->ecc.read_oob)
2740 chip->ecc.read_oob = nand_read_oob_std;
2741 if (!chip->ecc.write_oob)
2742 chip->ecc.write_oob = nand_write_oob_std;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002743
William Juul52c07962007-10-31 13:53:06 +01002744 case NAND_ECC_HW_SYNDROME:
Scott Wood5fd2c302008-03-18 15:29:14 -05002745 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2746 !chip->ecc.hwctl) &&
2747 (!chip->ecc.read_page ||
2748 chip->ecc.read_page == nand_read_page_hwecc ||
2749 !chip->ecc.write_page ||
2750 chip->ecc.write_page == nand_write_page_hwecc)) {
William Juul52c07962007-10-31 13:53:06 +01002751 printk(KERN_WARNING "No ECC functions supplied, "
2752 "Hardware ECC not possible\n");
2753 BUG();
2754 }
2755 /* Use standard syndrome read/write page function ? */
2756 if (!chip->ecc.read_page)
2757 chip->ecc.read_page = nand_read_page_syndrome;
2758 if (!chip->ecc.write_page)
2759 chip->ecc.write_page = nand_write_page_syndrome;
2760 if (!chip->ecc.read_oob)
2761 chip->ecc.read_oob = nand_read_oob_syndrome;
2762 if (!chip->ecc.write_oob)
2763 chip->ecc.write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002764
William Juul52c07962007-10-31 13:53:06 +01002765 if (mtd->writesize >= chip->ecc.size)
2766 break;
2767 printk(KERN_WARNING "%d byte HW ECC not possible on "
2768 "%d byte page size, fallback to SW ECC\n",
2769 chip->ecc.size, mtd->writesize);
2770 chip->ecc.mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002771
William Juul52c07962007-10-31 13:53:06 +01002772 case NAND_ECC_SOFT:
2773 chip->ecc.calculate = nand_calculate_ecc;
2774 chip->ecc.correct = nand_correct_data;
2775 chip->ecc.read_page = nand_read_page_swecc;
Scott Wood3628f002008-10-24 16:20:43 -05002776 chip->ecc.read_subpage = nand_read_subpage;
William Juul52c07962007-10-31 13:53:06 +01002777 chip->ecc.write_page = nand_write_page_swecc;
2778 chip->ecc.read_oob = nand_read_oob_std;
2779 chip->ecc.write_oob = nand_write_oob_std;
2780 chip->ecc.size = 256;
2781 chip->ecc.bytes = 3;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002782 break;
2783
William Juul52c07962007-10-31 13:53:06 +01002784 case NAND_ECC_NONE:
2785 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2786 "This is not recommended !!\n");
2787 chip->ecc.read_page = nand_read_page_raw;
2788 chip->ecc.write_page = nand_write_page_raw;
2789 chip->ecc.read_oob = nand_read_oob_std;
2790 chip->ecc.write_oob = nand_write_oob_std;
2791 chip->ecc.size = mtd->writesize;
2792 chip->ecc.bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002793 break;
2794
2795 default:
William Juul52c07962007-10-31 13:53:06 +01002796 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2797 chip->ecc.mode);
2798 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002799 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002800
William Juul52c07962007-10-31 13:53:06 +01002801 /*
2802 * The number of bytes available for a client to place data into
2803 * the out of band area
2804 */
2805 chip->ecc.layout->oobavail = 0;
2806 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2807 chip->ecc.layout->oobavail +=
2808 chip->ecc.layout->oobfree[i].length;
2809 mtd->oobavail = chip->ecc.layout->oobavail;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002810
William Juul52c07962007-10-31 13:53:06 +01002811 /*
2812 * Set the number of read / write steps for one page depending on ECC
2813 * mode
2814 */
2815 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2816 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2817 printk(KERN_WARNING "Invalid ecc parameters\n");
2818 BUG();
2819 }
2820 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002821
William Juul52c07962007-10-31 13:53:06 +01002822 /*
2823 * Allow subpage writes up to ecc.steps. Not possible for MLC
2824 * FLASH.
2825 */
2826 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2827 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2828 switch(chip->ecc.steps) {
2829 case 2:
2830 mtd->subpage_sft = 1;
2831 break;
2832 case 4:
2833 case 8:
2834 mtd->subpage_sft = 2;
2835 break;
2836 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002837 }
William Juul52c07962007-10-31 13:53:06 +01002838 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002839
William Juul52c07962007-10-31 13:53:06 +01002840 /* Initialize state */
2841 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002842
2843 /* De-select the device */
William Juul52c07962007-10-31 13:53:06 +01002844 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002845
2846 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01002847 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002848
2849 /* Fill in remaining MTD driver data */
2850 mtd->type = MTD_NANDFLASH;
William Juul52c07962007-10-31 13:53:06 +01002851 mtd->flags = MTD_CAP_NANDFLASH;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002852 mtd->erase = nand_erase;
2853 mtd->point = NULL;
2854 mtd->unpoint = NULL;
2855 mtd->read = nand_read;
2856 mtd->write = nand_write;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002857 mtd->read_oob = nand_read_oob;
2858 mtd->write_oob = nand_write_oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002859 mtd->sync = nand_sync;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002860 mtd->lock = NULL;
2861 mtd->unlock = NULL;
William Juul52c07962007-10-31 13:53:06 +01002862 mtd->suspend = nand_suspend;
2863 mtd->resume = nand_resume;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002864 mtd->block_isbad = nand_block_isbad;
2865 mtd->block_markbad = nand_block_markbad;
2866
William Juul52c07962007-10-31 13:53:06 +01002867 /* propagate ecc.layout to mtd_info */
2868 mtd->ecclayout = chip->ecc.layout;
2869
2870 /* Check, if we should skip the bad block table scan */
2871 if (chip->options & NAND_SKIP_BBTSCAN)
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02002872 chip->options |= NAND_BBT_SCANNED;
William Juul52c07962007-10-31 13:53:06 +01002873
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02002874 return 0;
William Juul52c07962007-10-31 13:53:06 +01002875}
2876
2877/* module_text_address() isn't exported, and it's mostly a pointless
2878 test if this is a module _anyway_ -- they'd have to try _really_ hard
2879 to call us from in-kernel code if the core NAND support is modular. */
2880#ifdef MODULE
2881#define caller_is_module() (1)
2882#else
2883#define caller_is_module() \
2884 module_text_address((unsigned long)__builtin_return_address(0))
2885#endif
2886
2887/**
2888 * nand_scan - [NAND Interface] Scan for the NAND device
2889 * @mtd: MTD device structure
2890 * @maxchips: Number of chips to scan for
2891 *
2892 * This fills out all the uninitialized function pointers
2893 * with the defaults.
2894 * The flash ID is read and the mtd/chip structures are
2895 * filled with the appropriate values.
2896 * The mtd->owner field must be set to the module of the caller
2897 *
2898 */
2899int nand_scan(struct mtd_info *mtd, int maxchips)
2900{
2901 int ret;
2902
2903 /* Many callers got this wrong, so check for it for a while... */
2904 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002905#if 0
William Juul52c07962007-10-31 13:53:06 +01002906 if (!mtd->owner && caller_is_module()) {
2907 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2908 BUG();
2909 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002910#endif
William Juulb76ec382007-11-08 10:39:53 +01002911
William Juul52c07962007-10-31 13:53:06 +01002912 ret = nand_scan_ident(mtd, maxchips);
2913 if (!ret)
2914 ret = nand_scan_tail(mtd);
2915 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002916}
2917
2918/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002919 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002920 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002921*/
2922void nand_release(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002923{
William Juul52c07962007-10-31 13:53:06 +01002924 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002925
2926#ifdef CONFIG_MTD_PARTITIONS
2927 /* Deregister partitions */
William Juul52c07962007-10-31 13:53:06 +01002928 del_mtd_partitions(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002929#endif
2930 /* Deregister the device */
William Juul52c07962007-10-31 13:53:06 +01002931 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002932#if 0
William Juul52c07962007-10-31 13:53:06 +01002933 del_mtd_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002934#endif
William Juul52c07962007-10-31 13:53:06 +01002935
2936 /* Free bad block table memory */
2937 kfree(chip->bbt);
2938 if (!(chip->options & NAND_OWN_BUFFERS))
2939 kfree(chip->buffers);
2940}
2941
2942/* XXX U-BOOT XXX */
2943#if 0
2944EXPORT_SYMBOL_GPL(nand_scan);
2945EXPORT_SYMBOL_GPL(nand_scan_ident);
2946EXPORT_SYMBOL_GPL(nand_scan_tail);
2947EXPORT_SYMBOL_GPL(nand_release);
2948
2949static int __init nand_base_init(void)
2950{
2951 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2952 return 0;
2953}
2954
2955static void __exit nand_base_exit(void)
2956{
2957 led_trigger_unregister_simple(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002958}
2959
William Juul52c07962007-10-31 13:53:06 +01002960module_init(nand_base_init);
2961module_exit(nand_base_exit);
2962
2963MODULE_LICENSE("GPL");
2964MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2965MODULE_DESCRIPTION("Generic NAND flash driver code");
2966#endif