blob: d33fee242f0ad68e15840401a56dc4d864a3c6b4 [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
71#include <asm/io.h>
72#include <asm/errno.h>
73
74#ifdef CONFIG_JFFS2_NAND
75#include <jffs2/jffs2.h>
76#endif
77
Peter Tyserf9f36222009-02-04 13:47:22 -060078/*
79 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
80 * a flash. NAND flash is initialized prior to interrupts so standard timers
81 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
82 * which is greater than (max NAND reset time / NAND status read time).
83 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
84 */
85#ifndef CONFIG_SYS_NAND_RESET_CNT
86#define CONFIG_SYS_NAND_RESET_CNT 200000
87#endif
88
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020089/* Define default oob placement schemes for large and small page devices */
William Juul52c07962007-10-31 13:53:06 +010090static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020091 .eccbytes = 3,
92 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010093 .oobfree = {
94 {.offset = 3,
95 .length = 2},
96 {.offset = 6,
97 .length = 2}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020098};
99
William Juul52c07962007-10-31 13:53:06 +0100100static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200101 .eccbytes = 6,
102 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +0100103 .oobfree = {
104 {.offset = 8,
105 . length = 8}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200106};
107
William Juul52c07962007-10-31 13:53:06 +0100108static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200109 .eccbytes = 24,
110 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +0100111 40, 41, 42, 43, 44, 45, 46, 47,
112 48, 49, 50, 51, 52, 53, 54, 55,
113 56, 57, 58, 59, 60, 61, 62, 63},
114 .oobfree = {
115 {.offset = 2,
116 .length = 38}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200117};
118
William Juul52c07962007-10-31 13:53:06 +0100119static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200120 .eccbytes = 48,
121 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +0100122 80, 81, 82, 83, 84, 85, 86, 87,
123 88, 89, 90, 91, 92, 93, 94, 95,
124 96, 97, 98, 99, 100, 101, 102, 103,
125 104, 105, 106, 107, 108, 109, 110, 111,
126 112, 113, 114, 115, 116, 117, 118, 119,
127 120, 121, 122, 123, 124, 125, 126, 127},
128 .oobfree = {
129 {.offset = 2,
130 .length = 78}}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200131};
132
William Juul52c07962007-10-31 13:53:06 +0100133
134static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
135 int new_state);
136
137static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
138 struct mtd_oob_ops *ops);
139
140static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200141
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200142/*
Scott Wood3628f002008-10-24 16:20:43 -0500143 * For devices which display every fart in the system on a separate LED. Is
William Juul52c07962007-10-31 13:53:06 +0100144 * compiled away when LED support is disabled.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200145 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146/* XXX U-BOOT XXX */
147#if 0
William Juul52c07962007-10-31 13:53:06 +0100148DEFINE_LED_TRIGGER(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200149#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200150
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200151/**
152 * nand_release_device - [GENERIC] release chip
153 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200154 *
155 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200156 */
157/* XXX U-BOOT XXX */
158#if 0
William Juul52c07962007-10-31 13:53:06 +0100159static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160{
William Juul52c07962007-10-31 13:53:06 +0100161 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200162
163 /* De-select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100164 chip->select_chip(mtd, -1);
165
166 /* Release the controller and the chip */
167 spin_lock(&chip->controller->lock);
168 chip->controller->active = NULL;
169 chip->state = FL_READY;
170 wake_up(&chip->controller->wq);
171 spin_unlock(&chip->controller->lock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200172}
173#else
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100174static void nand_release_device (struct mtd_info *mtd)
175{
176 struct nand_chip *this = mtd->priv;
177 this->select_chip(mtd, -1); /* De-select the NAND device */
178}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200179#endif
180
181/**
182 * nand_read_byte - [DEFAULT] read one byte from the chip
183 * @mtd: MTD device structure
184 *
185 * Default read function for 8bit buswith
186 */
William Juul52c07962007-10-31 13:53:06 +0100187static uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200188{
William Juul52c07962007-10-31 13:53:06 +0100189 struct nand_chip *chip = mtd->priv;
190 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200191}
192
193/**
194 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
195 * @mtd: MTD device structure
196 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200197 * Default read function for 16bit buswith with
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200198 * endianess conversion
199 */
William Juul52c07962007-10-31 13:53:06 +0100200static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200201{
William Juul52c07962007-10-31 13:53:06 +0100202 struct nand_chip *chip = mtd->priv;
203 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200204}
205
206/**
207 * nand_read_word - [DEFAULT] read one word from the chip
208 * @mtd: MTD device structure
209 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200210 * Default read function for 16bit buswith without
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200211 * endianess conversion
212 */
213static u16 nand_read_word(struct mtd_info *mtd)
214{
William Juul52c07962007-10-31 13:53:06 +0100215 struct nand_chip *chip = mtd->priv;
216 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200217}
218
219/**
220 * nand_select_chip - [DEFAULT] control CE line
221 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100222 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200223 *
224 * Default select function for 1 chip devices.
225 */
William Juul52c07962007-10-31 13:53:06 +0100226static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200227{
William Juul52c07962007-10-31 13:53:06 +0100228 struct nand_chip *chip = mtd->priv;
229
230 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200231 case -1:
William Juul52c07962007-10-31 13:53:06 +0100232 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200233 break;
234 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200235 break;
236
237 default:
238 BUG();
239 }
240}
241
242/**
243 * nand_write_buf - [DEFAULT] write buffer to chip
244 * @mtd: MTD device structure
245 * @buf: data buffer
246 * @len: number of bytes to write
247 *
248 * Default write function for 8bit buswith
249 */
William Juul52c07962007-10-31 13:53:06 +0100250static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200251{
252 int i;
William Juul52c07962007-10-31 13:53:06 +0100253 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200254
William Juul52c07962007-10-31 13:53:06 +0100255 for (i = 0; i < len; i++)
256 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200257}
258
259/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200260 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200261 * @mtd: MTD device structure
262 * @buf: buffer to store date
263 * @len: number of bytes to read
264 *
265 * Default read function for 8bit buswith
266 */
William Juul52c07962007-10-31 13:53:06 +0100267static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200268{
269 int i;
William Juul52c07962007-10-31 13:53:06 +0100270 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200271
William Juul52c07962007-10-31 13:53:06 +0100272 for (i = 0; i < len; i++)
273 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200274}
275
276/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200277 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200278 * @mtd: MTD device structure
279 * @buf: buffer containing the data to compare
280 * @len: number of bytes to compare
281 *
282 * Default verify function for 8bit buswith
283 */
William Juul52c07962007-10-31 13:53:06 +0100284static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200285{
286 int i;
William Juul52c07962007-10-31 13:53:06 +0100287 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200288
William Juul52c07962007-10-31 13:53:06 +0100289 for (i = 0; i < len; i++)
290 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200291 return -EFAULT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200292 return 0;
293}
294
295/**
296 * nand_write_buf16 - [DEFAULT] write buffer to chip
297 * @mtd: MTD device structure
298 * @buf: data buffer
299 * @len: number of bytes to write
300 *
301 * Default write function for 16bit buswith
302 */
William Juul52c07962007-10-31 13:53:06 +0100303static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200304{
305 int i;
William Juul52c07962007-10-31 13:53:06 +0100306 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307 u16 *p = (u16 *) buf;
308 len >>= 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200309
William Juul52c07962007-10-31 13:53:06 +0100310 for (i = 0; i < len; i++)
311 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200312
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200313}
314
315/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200316 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200317 * @mtd: MTD device structure
318 * @buf: buffer to store date
319 * @len: number of bytes to read
320 *
321 * Default read function for 16bit buswith
322 */
William Juul52c07962007-10-31 13:53:06 +0100323static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200324{
325 int i;
William Juul52c07962007-10-31 13:53:06 +0100326 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200327 u16 *p = (u16 *) buf;
328 len >>= 1;
329
William Juul52c07962007-10-31 13:53:06 +0100330 for (i = 0; i < len; i++)
331 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200332}
333
334/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200335 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200336 * @mtd: MTD device structure
337 * @buf: buffer containing the data to compare
338 * @len: number of bytes to compare
339 *
340 * Default verify function for 16bit buswith
341 */
William Juul52c07962007-10-31 13:53:06 +0100342static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200343{
344 int i;
William Juul52c07962007-10-31 13:53:06 +0100345 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200346 u16 *p = (u16 *) buf;
347 len >>= 1;
348
William Juul52c07962007-10-31 13:53:06 +0100349 for (i = 0; i < len; i++)
350 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200351 return -EFAULT;
352
353 return 0;
354}
355
356/**
357 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
358 * @mtd: MTD device structure
359 * @ofs: offset from device start
360 * @getchip: 0, if the chip is already selected
361 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200362 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200363 */
364static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
365{
366 int page, chipnr, res = 0;
William Juul52c07962007-10-31 13:53:06 +0100367 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200368 u16 bad;
369
William Juul52c07962007-10-31 13:53:06 +0100370 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200371
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200372 if (getchip) {
William Juul52c07962007-10-31 13:53:06 +0100373 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200374
William Juul52c07962007-10-31 13:53:06 +0100375 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200376
377 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100378 chip->select_chip(mtd, chipnr);
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200379 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200380
William Juul52c07962007-10-31 13:53:06 +0100381 if (chip->options & NAND_BUSWIDTH_16) {
382 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
383 page);
384 bad = cpu_to_le16(chip->read_word(mtd));
385 if (chip->badblockpos & 0x1)
386 bad >>= 8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200387 if ((bad & 0xFF) != 0xff)
388 res = 1;
389 } else {
William Juul52c07962007-10-31 13:53:06 +0100390 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
391 if (chip->read_byte(mtd) != 0xff)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200392 res = 1;
393 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200394
William Juul52c07962007-10-31 13:53:06 +0100395 if (getchip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200396 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200397
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200398 return res;
399}
400
401/**
402 * nand_default_block_markbad - [DEFAULT] mark a block bad
403 * @mtd: MTD device structure
404 * @ofs: offset from device start
405 *
406 * This is the default implementation, which can be overridden by
407 * a hardware specific driver.
408*/
409static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
410{
William Juul52c07962007-10-31 13:53:06 +0100411 struct nand_chip *chip = mtd->priv;
412 uint8_t buf[2] = { 0, 0 };
413 int block, ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200414
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200415 /* Get block number */
William Juul52c07962007-10-31 13:53:06 +0100416 block = (int)(ofs >> chip->bbt_erase_shift);
417 if (chip->bbt)
418 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200419
420 /* Do we have a flash based bad block table ? */
William Juul52c07962007-10-31 13:53:06 +0100421 if (chip->options & NAND_USE_FLASH_BBT)
422 ret = nand_update_bbt(mtd, ofs);
423 else {
424 /* We write two bytes, so we dont have to mess with 16 bit
425 * access
426 */
Scott Wood3628f002008-10-24 16:20:43 -0500427 nand_get_device(chip, mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +0100428 ofs += mtd->oobsize;
429 chip->ops.len = chip->ops.ooblen = 2;
430 chip->ops.datbuf = NULL;
431 chip->ops.oobbuf = buf;
432 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200433
William Juul52c07962007-10-31 13:53:06 +0100434 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Scott Wood3628f002008-10-24 16:20:43 -0500435 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100436 }
437 if (!ret)
438 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500439
William Juul52c07962007-10-31 13:53:06 +0100440 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200441}
442
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200443/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200444 * nand_check_wp - [GENERIC] check if the chip is write protected
445 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200446 * Check, if the device is write protected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200447 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200448 * The function expects, that the device is already selected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200449 */
William Juul52c07962007-10-31 13:53:06 +0100450static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200451{
William Juul52c07962007-10-31 13:53:06 +0100452 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200453 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100454 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
455 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200456}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100457
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200458/**
459 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
460 * @mtd: MTD device structure
461 * @ofs: offset from device start
462 * @getchip: 0, if the chip is already selected
463 * @allowbbt: 1, if its allowed to access the bbt area
464 *
465 * Check, if the block is bad. Either by reading the bad block table or
466 * calling of the scan function.
467 */
William Juul52c07962007-10-31 13:53:06 +0100468static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
469 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200470{
William Juul52c07962007-10-31 13:53:06 +0100471 struct nand_chip *chip = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200472
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200473 if (!(chip->options & NAND_BBT_SCANNED)) {
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200474 chip->options |= NAND_BBT_SCANNED;
Scott Wood11b12a32008-12-16 14:24:16 -0600475 chip->scan_bbt(mtd);
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +0200476 }
477
William Juul52c07962007-10-31 13:53:06 +0100478 if (!chip->bbt)
479 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200480
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200481 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100482 return nand_isbad_bbt(mtd, ofs, allowbbt);
483}
484
485/*
486 * Wait for the ready pin, after a command
487 * The timeout is catched later.
488 */
489/* XXX U-BOOT XXX */
490#if 0
491void nand_wait_ready(struct mtd_info *mtd)
492{
493 struct nand_chip *chip = mtd->priv;
494 unsigned long timeo = jiffies + 2;
495
496 led_trigger_event(nand_led_trigger, LED_FULL);
497 /* wait until command is processed or timeout occures */
498 do {
499 if (chip->dev_ready(mtd))
500 break;
501 touch_softlockup_watchdog();
502 } while (time_before(jiffies, timeo));
503 led_trigger_event(nand_led_trigger, LED_OFF);
504}
505EXPORT_SYMBOL_GPL(nand_wait_ready);
506#else
507void nand_wait_ready(struct mtd_info *mtd)
508{
509 struct nand_chip *chip = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200510 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100511
512 reset_timer();
513
514 /* wait until command is processed or timeout occures */
515 while (get_timer(0) < timeo) {
516 if (chip->dev_ready)
517 if (chip->dev_ready(mtd))
518 break;
519 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200520}
William Juul52c07962007-10-31 13:53:06 +0100521#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200522
523/**
524 * nand_command - [DEFAULT] Send command to NAND device
525 * @mtd: MTD device structure
526 * @command: the command to be sent
527 * @column: the column address for this command, -1 if none
528 * @page_addr: the page address for this command, -1 if none
529 *
530 * Send command to NAND device. This function is used for small page
531 * devices (256/512 Bytes per page)
532 */
William Juul52c07962007-10-31 13:53:06 +0100533static void nand_command(struct mtd_info *mtd, unsigned int command,
534 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200535{
William Juul52c07962007-10-31 13:53:06 +0100536 register struct nand_chip *chip = mtd->priv;
537 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyserf9f36222009-02-04 13:47:22 -0600538 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200539
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200540 /*
541 * Write out the command to the device.
542 */
543 if (command == NAND_CMD_SEQIN) {
544 int readcmd;
545
William Juul52c07962007-10-31 13:53:06 +0100546 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200547 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100548 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200549 readcmd = NAND_CMD_READOOB;
550 } else if (column < 256) {
551 /* First 256 bytes --> READ0 */
552 readcmd = NAND_CMD_READ0;
553 } else {
554 column -= 256;
555 readcmd = NAND_CMD_READ1;
556 }
William Juul52c07962007-10-31 13:53:06 +0100557 chip->cmd_ctrl(mtd, readcmd, ctrl);
558 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200559 }
William Juul52c07962007-10-31 13:53:06 +0100560 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200561
William Juul52c07962007-10-31 13:53:06 +0100562 /*
563 * Address cycle, when necessary
564 */
565 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
566 /* Serially input address */
567 if (column != -1) {
568 /* Adjust columns for 16 bit buswidth */
569 if (chip->options & NAND_BUSWIDTH_16)
570 column >>= 1;
571 chip->cmd_ctrl(mtd, column, ctrl);
572 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200573 }
William Juul52c07962007-10-31 13:53:06 +0100574 if (page_addr != -1) {
575 chip->cmd_ctrl(mtd, page_addr, ctrl);
576 ctrl &= ~NAND_CTRL_CHANGE;
577 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
578 /* One more address cycle for devices > 32MiB */
579 if (chip->chipsize > (32 << 20))
580 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
581 }
582 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200583
584 /*
585 * program and erase have their own busy handlers
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200586 * status and sequential in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100587 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200588 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200589
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200590 case NAND_CMD_PAGEPROG:
591 case NAND_CMD_ERASE1:
592 case NAND_CMD_ERASE2:
593 case NAND_CMD_SEQIN:
594 case NAND_CMD_STATUS:
595 return;
596
597 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100598 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200599 break;
William Juul52c07962007-10-31 13:53:06 +0100600 udelay(chip->chip_delay);
601 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
602 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
603 chip->cmd_ctrl(mtd,
604 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyserf9f36222009-02-04 13:47:22 -0600605 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
606 (rst_sts_cnt--));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200607 return;
608
William Juul52c07962007-10-31 13:53:06 +0100609 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200610 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200611 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200612 * If we don't have access to the busy pin, we apply the given
613 * command delay
William Juul52c07962007-10-31 13:53:06 +0100614 */
615 if (!chip->dev_ready) {
616 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200617 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200618 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200619 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200620 /* Apply this short delay always to ensure that we do wait tWB in
621 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100622 ndelay(100);
623
624 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200625}
626
627/**
628 * nand_command_lp - [DEFAULT] Send command to NAND large page device
629 * @mtd: MTD device structure
630 * @command: the command to be sent
631 * @column: the column address for this command, -1 if none
632 * @page_addr: the page address for this command, -1 if none
633 *
William Juul52c07962007-10-31 13:53:06 +0100634 * Send command to NAND device. This is the version for the new large page
635 * devices We dont have the separate regions as we have in the small page
636 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200637 */
William Juul52c07962007-10-31 13:53:06 +0100638static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
639 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200640{
William Juul52c07962007-10-31 13:53:06 +0100641 register struct nand_chip *chip = mtd->priv;
Peter Tyserf9f36222009-02-04 13:47:22 -0600642 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200643
644 /* Emulate NAND_CMD_READOOB */
645 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100646 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200647 command = NAND_CMD_READ0;
648 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200649
William Juul52c07962007-10-31 13:53:06 +0100650 /* Command latch cycle */
651 chip->cmd_ctrl(mtd, command & 0xff,
652 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200653
654 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100655 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200656
657 /* Serially input address */
658 if (column != -1) {
659 /* Adjust columns for 16 bit buswidth */
William Juul52c07962007-10-31 13:53:06 +0100660 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200661 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100662 chip->cmd_ctrl(mtd, column, ctrl);
663 ctrl &= ~NAND_CTRL_CHANGE;
664 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200665 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200666 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100667 chip->cmd_ctrl(mtd, page_addr, ctrl);
668 chip->cmd_ctrl(mtd, page_addr >> 8,
669 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200670 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-10-31 13:53:06 +0100671 if (chip->chipsize > (128 << 20))
672 chip->cmd_ctrl(mtd, page_addr >> 16,
673 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200674 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200675 }
William Juul52c07962007-10-31 13:53:06 +0100676 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200677
678 /*
679 * program and erase have their own busy handlers
William Juul52c07962007-10-31 13:53:06 +0100680 * status, sequential in, and deplete1 need no delay
681 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200682 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200683
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200684 case NAND_CMD_CACHEDPROG:
685 case NAND_CMD_PAGEPROG:
686 case NAND_CMD_ERASE1:
687 case NAND_CMD_ERASE2:
688 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100689 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200690 case NAND_CMD_STATUS:
William Juul52c07962007-10-31 13:53:06 +0100691 case NAND_CMD_DEPLETE1:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200692 return;
693
William Juul52c07962007-10-31 13:53:06 +0100694 /*
695 * read error status commands require only a short delay
696 */
697 case NAND_CMD_STATUS_ERROR:
698 case NAND_CMD_STATUS_ERROR0:
699 case NAND_CMD_STATUS_ERROR1:
700 case NAND_CMD_STATUS_ERROR2:
701 case NAND_CMD_STATUS_ERROR3:
702 udelay(chip->chip_delay);
703 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200704
705 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100706 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200707 break;
William Juul52c07962007-10-31 13:53:06 +0100708 udelay(chip->chip_delay);
709 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
710 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
711 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
712 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyserf9f36222009-02-04 13:47:22 -0600713 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
714 (rst_sts_cnt--));
William Juul52c07962007-10-31 13:53:06 +0100715 return;
716
717 case NAND_CMD_RNDOUT:
718 /* No ready / busy check necessary */
719 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
720 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
721 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
722 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200723 return;
724
725 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100726 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
727 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
728 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
729 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200730
William Juul52c07962007-10-31 13:53:06 +0100731 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200732 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200733 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200734 * If we don't have access to the busy pin, we apply the given
735 * command delay
William Juul52c07962007-10-31 13:53:06 +0100736 */
737 if (!chip->dev_ready) {
738 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200739 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200740 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200741 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200742
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200743 /* Apply this short delay always to ensure that we do wait tWB in
744 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100745 ndelay(100);
746
747 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200748}
749
750/**
751 * nand_get_device - [GENERIC] Get chip for selected access
William Juul52c07962007-10-31 13:53:06 +0100752 * @chip: the nand chip descriptor
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200753 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200754 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200755 *
756 * Get the device and lock it for exclusive access
757 */
758/* XXX U-BOOT XXX */
759#if 0
William Juul52c07962007-10-31 13:53:06 +0100760static int
761nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200762{
William Juul52c07962007-10-31 13:53:06 +0100763 spinlock_t *lock = &chip->controller->lock;
764 wait_queue_head_t *wq = &chip->controller->wq;
765 DECLARE_WAITQUEUE(wait, current);
766 retry:
767 spin_lock(lock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200768
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200769 /* Hardware controller shared among independend devices */
William Juul52c07962007-10-31 13:53:06 +0100770 /* Hardware controller shared among independend devices */
771 if (!chip->controller->active)
772 chip->controller->active = chip;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200773
William Juul52c07962007-10-31 13:53:06 +0100774 if (chip->controller->active == chip && chip->state == FL_READY) {
775 chip->state = new_state;
776 spin_unlock(lock);
777 return 0;
778 }
779 if (new_state == FL_PM_SUSPENDED) {
780 spin_unlock(lock);
781 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200782 }
William Juul52c07962007-10-31 13:53:06 +0100783 set_current_state(TASK_UNINTERRUPTIBLE);
784 add_wait_queue(wq, &wait);
785 spin_unlock(lock);
786 schedule();
787 remove_wait_queue(wq, &wait);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200788 goto retry;
789}
790#else
William Juul52c07962007-10-31 13:53:06 +0100791static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
792{
Marcel Ziswilerf30a2aa2008-06-22 16:30:06 +0200793 this->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100794 return 0;
795}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200796#endif
797
798/**
799 * nand_wait - [DEFAULT] wait until the command is done
800 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100801 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200802 *
803 * Wait for command done. This applies to erase and program only
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200804 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200805 * general NAND and SmartMedia specs
William Juul52c07962007-10-31 13:53:06 +0100806 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200807/* XXX U-BOOT XXX */
808#if 0
William Juul52c07962007-10-31 13:53:06 +0100809static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200810{
William Juul52c07962007-10-31 13:53:06 +0100811
812 unsigned long timeo = jiffies;
813 int status, state = chip->state;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200814
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200815 if (state == FL_ERASING)
William Juul52c07962007-10-31 13:53:06 +0100816 timeo += (HZ * 400) / 1000;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200817 else
William Juul52c07962007-10-31 13:53:06 +0100818 timeo += (HZ * 20) / 1000;
819
820 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200821
822 /* Apply this short delay always to ensure that we do wait tWB in
823 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100824 ndelay(100);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200825
William Juul52c07962007-10-31 13:53:06 +0100826 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
827 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200828 else
William Juul52c07962007-10-31 13:53:06 +0100829 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200830
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200831 while (time_before(jiffies, timeo)) {
William Juul52c07962007-10-31 13:53:06 +0100832 if (chip->dev_ready) {
833 if (chip->dev_ready(mtd))
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200834 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200835 } else {
William Juul52c07962007-10-31 13:53:06 +0100836 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200837 break;
838 }
William Juul52c07962007-10-31 13:53:06 +0100839 cond_resched();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200840 }
William Juul52c07962007-10-31 13:53:06 +0100841 led_trigger_event(nand_led_trigger, LED_OFF);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200842
William Juul52c07962007-10-31 13:53:06 +0100843 status = (int)chip->read_byte(mtd);
844 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200845}
846#else
William Juul52c07962007-10-31 13:53:06 +0100847static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200848{
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100849 unsigned long timeo;
William Juul52c07962007-10-31 13:53:06 +0100850 int state = this->state;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100851
852 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200853 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100854 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200855 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100856
857 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
858 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
859 else
860 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
861
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100862 reset_timer();
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100863
864 while (1) {
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100865 if (get_timer(0) > timeo) {
866 printf("Timeout!");
Stefan Roeseef26d242006-11-27 17:22:19 +0100867 return 0x01;
868 }
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100869
870 if (this->dev_ready) {
871 if (this->dev_ready(mtd))
872 break;
873 } else {
874 if (this->read_byte(mtd) & NAND_STATUS_READY)
875 break;
876 }
877 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100878#ifdef PPCHAMELON_NAND_TIMER_HACK
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100879 reset_timer();
880 while (get_timer(0) < 10);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100881#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100882
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100883 return this->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200884}
885#endif
886
887/**
William Juul52c07962007-10-31 13:53:06 +0100888 * nand_read_page_raw - [Intern] read raw page data without ecc
889 * @mtd: mtd info structure
890 * @chip: nand chip info structure
891 * @buf: buffer to store read data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200892 */
William Juul52c07962007-10-31 13:53:06 +0100893static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
894 uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200895{
William Juul52c07962007-10-31 13:53:06 +0100896 chip->read_buf(mtd, buf, mtd->writesize);
897 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
898 return 0;
899}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200900
William Juul52c07962007-10-31 13:53:06 +0100901/**
902 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
903 * @mtd: mtd info structure
904 * @chip: nand chip info structure
905 * @buf: buffer to store read data
906 */
907static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
908 uint8_t *buf)
909{
910 int i, eccsize = chip->ecc.size;
911 int eccbytes = chip->ecc.bytes;
912 int eccsteps = chip->ecc.steps;
913 uint8_t *p = buf;
914 uint8_t *ecc_calc = chip->buffers->ecccalc;
915 uint8_t *ecc_code = chip->buffers->ecccode;
916 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200917
William Juul52c07962007-10-31 13:53:06 +0100918 chip->ecc.read_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200919
William Juul52c07962007-10-31 13:53:06 +0100920 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
921 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200922
William Juul52c07962007-10-31 13:53:06 +0100923 for (i = 0; i < chip->ecc.total; i++)
924 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200925
William Juul52c07962007-10-31 13:53:06 +0100926 eccsteps = chip->ecc.steps;
927 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200928
William Juul52c07962007-10-31 13:53:06 +0100929 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
930 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200931
William Juul52c07962007-10-31 13:53:06 +0100932 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood3628f002008-10-24 16:20:43 -0500933 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +0100934 mtd->ecc_stats.failed++;
935 else
936 mtd->ecc_stats.corrected += stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200937 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200938 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200939}
940
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200941/**
Scott Wood3628f002008-10-24 16:20:43 -0500942 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
943 * @mtd: mtd info structure
944 * @chip: nand chip info structure
945 * @dataofs offset of requested data within the page
946 * @readlen data length
947 * @buf: buffer to store read data
948 */
949static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
950{
951 int start_step, end_step, num_steps;
952 uint32_t *eccpos = chip->ecc.layout->eccpos;
953 uint8_t *p;
954 int data_col_addr, i, gaps = 0;
955 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
956 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
957
958 /* Column address wihin the page aligned to ECC size (256bytes). */
959 start_step = data_offs / chip->ecc.size;
960 end_step = (data_offs + readlen - 1) / chip->ecc.size;
961 num_steps = end_step - start_step + 1;
962
963 /* Data size aligned to ECC ecc.size*/
964 datafrag_len = num_steps * chip->ecc.size;
965 eccfrag_len = num_steps * chip->ecc.bytes;
966
967 data_col_addr = start_step * chip->ecc.size;
968 /* If we read not a page aligned data */
969 if (data_col_addr != 0)
970 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
971
972 p = bufpoi + data_col_addr;
973 chip->read_buf(mtd, p, datafrag_len);
974
975 /* Calculate ECC */
976 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
977 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
978
979 /* The performance is faster if to position offsets
980 according to ecc.pos. Let make sure here that
981 there are no gaps in ecc positions */
982 for (i = 0; i < eccfrag_len - 1; i++) {
983 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
984 eccpos[i + start_step * chip->ecc.bytes + 1]) {
985 gaps = 1;
986 break;
987 }
988 }
989 if (gaps) {
990 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
991 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
992 } else {
993 /* send the command to read the particular ecc bytes */
994 /* take care about buswidth alignment in read_buf */
995 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
996 aligned_len = eccfrag_len;
997 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
998 aligned_len++;
999 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1000 aligned_len++;
1001
1002 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1003 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1004 }
1005
1006 for (i = 0; i < eccfrag_len; i++)
1007 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1008
1009 p = bufpoi + data_col_addr;
1010 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1011 int stat;
1012
1013 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1014 if (stat < 0)
1015 mtd->ecc_stats.failed++;
1016 else
1017 mtd->ecc_stats.corrected += stat;
1018 }
1019 return 0;
1020}
1021
1022/**
William Juul52c07962007-10-31 13:53:06 +01001023 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
1024 * @mtd: mtd info structure
1025 * @chip: nand chip info structure
1026 * @buf: buffer to store read data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001027 *
William Juul52c07962007-10-31 13:53:06 +01001028 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001029 */
William Juul52c07962007-10-31 13:53:06 +01001030static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1031 uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001032{
William Juul52c07962007-10-31 13:53:06 +01001033 int i, eccsize = chip->ecc.size;
1034 int eccbytes = chip->ecc.bytes;
1035 int eccsteps = chip->ecc.steps;
1036 uint8_t *p = buf;
1037 uint8_t *ecc_calc = chip->buffers->ecccalc;
1038 uint8_t *ecc_code = chip->buffers->ecccode;
1039 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001040
William Juul52c07962007-10-31 13:53:06 +01001041 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1042 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1043 chip->read_buf(mtd, p, eccsize);
1044 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1045 }
1046 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001047
William Juul52c07962007-10-31 13:53:06 +01001048 for (i = 0; i < chip->ecc.total; i++)
1049 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001050
William Juul52c07962007-10-31 13:53:06 +01001051 eccsteps = chip->ecc.steps;
1052 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001053
William Juul52c07962007-10-31 13:53:06 +01001054 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1055 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001056
William Juul52c07962007-10-31 13:53:06 +01001057 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1058 if (stat == -1)
1059 mtd->ecc_stats.failed++;
1060 else
1061 mtd->ecc_stats.corrected += stat;
1062 }
1063 return 0;
1064}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001065
William Juul52c07962007-10-31 13:53:06 +01001066/**
1067 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1068 * @mtd: mtd info structure
1069 * @chip: nand chip info structure
1070 * @buf: buffer to store read data
1071 *
1072 * The hw generator calculates the error syndrome automatically. Therefor
1073 * we need a special oob layout and handling.
1074 */
1075static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1076 uint8_t *buf)
1077{
1078 int i, eccsize = chip->ecc.size;
1079 int eccbytes = chip->ecc.bytes;
1080 int eccsteps = chip->ecc.steps;
1081 uint8_t *p = buf;
1082 uint8_t *oob = chip->oob_poi;
1083
1084 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1085 int stat;
1086
1087 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1088 chip->read_buf(mtd, p, eccsize);
1089
1090 if (chip->ecc.prepad) {
1091 chip->read_buf(mtd, oob, chip->ecc.prepad);
1092 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001093 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001094
William Juul52c07962007-10-31 13:53:06 +01001095 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1096 chip->read_buf(mtd, oob, eccbytes);
1097 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001098
Scott Wood3628f002008-10-24 16:20:43 -05001099 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001100 mtd->ecc_stats.failed++;
1101 else
1102 mtd->ecc_stats.corrected += stat;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001103
William Juul52c07962007-10-31 13:53:06 +01001104 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001105
William Juul52c07962007-10-31 13:53:06 +01001106 if (chip->ecc.postpad) {
1107 chip->read_buf(mtd, oob, chip->ecc.postpad);
1108 oob += chip->ecc.postpad;
1109 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001110 }
William Juul52c07962007-10-31 13:53:06 +01001111
1112 /* Calculate remaining oob bytes */
1113 i = mtd->oobsize - (oob - chip->oob_poi);
1114 if (i)
1115 chip->read_buf(mtd, oob, i);
1116
1117 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001118}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001119
1120/**
William Juul52c07962007-10-31 13:53:06 +01001121 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1122 * @chip: nand chip structure
1123 * @oob: oob destination address
1124 * @ops: oob ops structure
1125 * @len: size of oob to transfer
1126 */
1127static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1128 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001129{
William Juul52c07962007-10-31 13:53:06 +01001130 switch(ops->mode) {
1131
1132 case MTD_OOB_PLACE:
1133 case MTD_OOB_RAW:
1134 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1135 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001136
William Juul52c07962007-10-31 13:53:06 +01001137 case MTD_OOB_AUTO: {
1138 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1139 uint32_t boffs = 0, roffs = ops->ooboffs;
1140 size_t bytes = 0;
1141
1142 for(; free->length && len; free++, len -= bytes) {
1143 /* Read request not from offset 0 ? */
1144 if (unlikely(roffs)) {
1145 if (roffs >= free->length) {
1146 roffs -= free->length;
1147 continue;
1148 }
1149 boffs = free->offset + roffs;
1150 bytes = min_t(size_t, len,
1151 (free->length - roffs));
1152 roffs = 0;
1153 } else {
1154 bytes = min_t(size_t, len, free->length);
1155 boffs = free->offset;
1156 }
1157 memcpy(oob, chip->oob_poi + boffs, bytes);
1158 oob += bytes;
1159 }
1160 return oob;
1161 }
1162 default:
1163 BUG();
1164 }
1165 return NULL;
1166}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001167
1168/**
William Juul52c07962007-10-31 13:53:06 +01001169 * nand_do_read_ops - [Internal] Read data with ECC
1170 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001171 * @mtd: MTD device structure
1172 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001173 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001174 *
William Juul52c07962007-10-31 13:53:06 +01001175 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001176 */
William Juul52c07962007-10-31 13:53:06 +01001177static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1178 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001179{
William Juul52c07962007-10-31 13:53:06 +01001180 int chipnr, page, realpage, col, bytes, aligned;
1181 struct nand_chip *chip = mtd->priv;
1182 struct mtd_ecc_stats stats;
1183 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1184 int sndcmd = 1;
1185 int ret = 0;
1186 uint32_t readlen = ops->len;
1187 uint32_t oobreadlen = ops->ooblen;
1188 uint8_t *bufpoi, *oob, *buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001189
William Juul52c07962007-10-31 13:53:06 +01001190 stats = mtd->ecc_stats;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001191
William Juul52c07962007-10-31 13:53:06 +01001192 chipnr = (int)(from >> chip->chip_shift);
1193 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001194
William Juul52c07962007-10-31 13:53:06 +01001195 realpage = (int)(from >> chip->page_shift);
1196 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001197
William Juul52c07962007-10-31 13:53:06 +01001198 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001199
William Juul52c07962007-10-31 13:53:06 +01001200 buf = ops->datbuf;
1201 oob = ops->oobbuf;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001202
William Juul52c07962007-10-31 13:53:06 +01001203 while(1) {
1204 bytes = min(mtd->writesize - col, readlen);
1205 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001206
William Juul52c07962007-10-31 13:53:06 +01001207 /* Is the current page in the buffer ? */
1208 if (realpage != chip->pagebuf || oob) {
1209 bufpoi = aligned ? buf : chip->buffers->databuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001210
William Juul52c07962007-10-31 13:53:06 +01001211 if (likely(sndcmd)) {
1212 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1213 sndcmd = 0;
1214 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001215
William Juul52c07962007-10-31 13:53:06 +01001216 /* Now read the page into the buffer */
1217 if (unlikely(ops->mode == MTD_OOB_RAW))
1218 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
Scott Wood3628f002008-10-24 16:20:43 -05001219 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1220 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
William Juul52c07962007-10-31 13:53:06 +01001221 else
1222 ret = chip->ecc.read_page(mtd, chip, bufpoi);
1223 if (ret < 0)
1224 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001225
William Juul52c07962007-10-31 13:53:06 +01001226 /* Transfer not aligned data */
1227 if (!aligned) {
Scott Wood3628f002008-10-24 16:20:43 -05001228 if (!NAND_SUBPAGE_READ(chip) && !oob)
1229 chip->pagebuf = realpage;
William Juul52c07962007-10-31 13:53:06 +01001230 memcpy(buf, chip->buffers->databuf + col, bytes);
1231 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001232
William Juul52c07962007-10-31 13:53:06 +01001233 buf += bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001234
William Juul52c07962007-10-31 13:53:06 +01001235 if (unlikely(oob)) {
1236 /* Raw mode does data:oob:data:oob */
1237 if (ops->mode != MTD_OOB_RAW) {
1238 int toread = min(oobreadlen,
1239 chip->ecc.layout->oobavail);
1240 if (toread) {
1241 oob = nand_transfer_oob(chip,
1242 oob, ops, toread);
1243 oobreadlen -= toread;
1244 }
1245 } else
1246 buf = nand_transfer_oob(chip,
1247 buf, ops, mtd->oobsize);
1248 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001249
William Juul52c07962007-10-31 13:53:06 +01001250 if (!(chip->options & NAND_NO_READRDY)) {
1251 /*
1252 * Apply delay or wait for ready/busy pin. Do
1253 * this before the AUTOINCR check, so no
1254 * problems arise if a chip which does auto
1255 * increment is marked as NOAUTOINCR by the
1256 * board driver.
1257 */
1258 if (!chip->dev_ready)
1259 udelay(chip->chip_delay);
1260 else
1261 nand_wait_ready(mtd);
1262 }
1263 } else {
1264 memcpy(buf, chip->buffers->databuf + col, bytes);
1265 buf += bytes;
1266 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001267
William Juul52c07962007-10-31 13:53:06 +01001268 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001269
William Juul52c07962007-10-31 13:53:06 +01001270 if (!readlen)
1271 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001272
William Juul52c07962007-10-31 13:53:06 +01001273 /* For subsequent reads align to page boundary. */
1274 col = 0;
1275 /* Increment page address */
1276 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001277
William Juul52c07962007-10-31 13:53:06 +01001278 page = realpage & chip->pagemask;
1279 /* Check, if we cross a chip boundary */
1280 if (!page) {
1281 chipnr++;
1282 chip->select_chip(mtd, -1);
1283 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001284 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001285
William Juul52c07962007-10-31 13:53:06 +01001286 /* Check, if the chip supports auto page increment
1287 * or if we have hit a block boundary.
1288 */
1289 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1290 sndcmd = 1;
1291 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001292
William Juul52c07962007-10-31 13:53:06 +01001293 ops->retlen = ops->len - (size_t) readlen;
1294 if (oob)
1295 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001296
William Juul52c07962007-10-31 13:53:06 +01001297 if (ret)
1298 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001299
William Juul52c07962007-10-31 13:53:06 +01001300 if (mtd->ecc_stats.failed - stats.failed)
1301 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001302
William Juul52c07962007-10-31 13:53:06 +01001303 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1304}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001305
William Juul52c07962007-10-31 13:53:06 +01001306/**
1307 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1308 * @mtd: MTD device structure
1309 * @from: offset to read from
1310 * @len: number of bytes to read
1311 * @retlen: pointer to variable to store the number of read bytes
1312 * @buf: the databuffer to put data
1313 *
1314 * Get hold of the chip and call nand_do_read
1315 */
1316static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1317 size_t *retlen, uint8_t *buf)
1318{
1319 struct nand_chip *chip = mtd->priv;
1320 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001321
William Juul52c07962007-10-31 13:53:06 +01001322 /* Do not allow reads past end of device */
1323 if ((from + len) > mtd->size)
1324 return -EINVAL;
1325 if (!len)
1326 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001327
William Juul52c07962007-10-31 13:53:06 +01001328 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001329
William Juul52c07962007-10-31 13:53:06 +01001330 chip->ops.len = len;
1331 chip->ops.datbuf = buf;
1332 chip->ops.oobbuf = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001333
William Juul52c07962007-10-31 13:53:06 +01001334 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001335
William Juul52c07962007-10-31 13:53:06 +01001336 *retlen = chip->ops.retlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001337
William Juul52c07962007-10-31 13:53:06 +01001338 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001339
William Juul52c07962007-10-31 13:53:06 +01001340 return ret;
1341}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001342
William Juul52c07962007-10-31 13:53:06 +01001343/**
1344 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1345 * @mtd: mtd info structure
1346 * @chip: nand chip info structure
1347 * @page: page number to read
1348 * @sndcmd: flag whether to issue read command or not
1349 */
1350static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1351 int page, int sndcmd)
1352{
1353 if (sndcmd) {
1354 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1355 sndcmd = 0;
1356 }
1357 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1358 return sndcmd;
1359}
1360
1361/**
1362 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1363 * with syndromes
1364 * @mtd: mtd info structure
1365 * @chip: nand chip info structure
1366 * @page: page number to read
1367 * @sndcmd: flag whether to issue read command or not
1368 */
1369static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1370 int page, int sndcmd)
1371{
1372 uint8_t *buf = chip->oob_poi;
1373 int length = mtd->oobsize;
1374 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1375 int eccsize = chip->ecc.size;
1376 uint8_t *bufpoi = buf;
1377 int i, toread, sndrnd = 0, pos;
1378
1379 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1380 for (i = 0; i < chip->ecc.steps; i++) {
1381 if (sndrnd) {
1382 pos = eccsize + i * (eccsize + chunk);
1383 if (mtd->writesize > 512)
1384 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1385 else
1386 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001387 } else
William Juul52c07962007-10-31 13:53:06 +01001388 sndrnd = 1;
1389 toread = min_t(int, length, chunk);
1390 chip->read_buf(mtd, bufpoi, toread);
1391 bufpoi += toread;
1392 length -= toread;
1393 }
1394 if (length > 0)
1395 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001396
William Juul52c07962007-10-31 13:53:06 +01001397 return 1;
1398}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001399
William Juul52c07962007-10-31 13:53:06 +01001400/**
1401 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1402 * @mtd: mtd info structure
1403 * @chip: nand chip info structure
1404 * @page: page number to write
1405 */
1406static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1407 int page)
1408{
1409 int status = 0;
1410 const uint8_t *buf = chip->oob_poi;
1411 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001412
William Juul52c07962007-10-31 13:53:06 +01001413 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1414 chip->write_buf(mtd, buf, length);
1415 /* Send command to program the OOB data */
1416 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001417
William Juul52c07962007-10-31 13:53:06 +01001418 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001419
William Juul52c07962007-10-31 13:53:06 +01001420 return status & NAND_STATUS_FAIL ? -EIO : 0;
1421}
1422
1423/**
1424 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1425 * with syndrome - only for large page flash !
1426 * @mtd: mtd info structure
1427 * @chip: nand chip info structure
1428 * @page: page number to write
1429 */
1430static int nand_write_oob_syndrome(struct mtd_info *mtd,
1431 struct nand_chip *chip, int page)
1432{
1433 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1434 int eccsize = chip->ecc.size, length = mtd->oobsize;
1435 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1436 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001437
1438 /*
William Juul52c07962007-10-31 13:53:06 +01001439 * data-ecc-data-ecc ... ecc-oob
1440 * or
1441 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001442 */
William Juul52c07962007-10-31 13:53:06 +01001443 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1444 pos = steps * (eccsize + chunk);
1445 steps = 0;
1446 } else
1447 pos = eccsize;
1448
1449 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1450 for (i = 0; i < steps; i++) {
1451 if (sndcmd) {
1452 if (mtd->writesize <= 512) {
1453 uint32_t fill = 0xFFFFFFFF;
1454
1455 len = eccsize;
1456 while (len > 0) {
1457 int num = min_t(int, len, 4);
1458 chip->write_buf(mtd, (uint8_t *)&fill,
1459 num);
1460 len -= num;
1461 }
1462 } else {
1463 pos = eccsize + i * (eccsize + chunk);
1464 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1465 }
1466 } else
1467 sndcmd = 1;
1468 len = min_t(int, length, chunk);
1469 chip->write_buf(mtd, bufpoi, len);
1470 bufpoi += len;
1471 length -= len;
1472 }
1473 if (length > 0)
1474 chip->write_buf(mtd, bufpoi, length);
1475
1476 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1477 status = chip->waitfunc(mtd, chip);
1478
1479 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001480}
1481
1482/**
William Juul52c07962007-10-31 13:53:06 +01001483 * nand_do_read_oob - [Intern] NAND read out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001484 * @mtd: MTD device structure
1485 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001486 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001487 *
1488 * NAND read out-of-band data from the spare area
1489 */
William Juul52c07962007-10-31 13:53:06 +01001490static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1491 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001492{
William Juul52c07962007-10-31 13:53:06 +01001493 int page, realpage, chipnr, sndcmd = 1;
1494 struct nand_chip *chip = mtd->priv;
1495 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1496 int readlen = ops->ooblen;
1497 int len;
1498 uint8_t *buf = ops->oobbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001499
William Juul52c07962007-10-31 13:53:06 +01001500 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1501 (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001502
William Juul52c07962007-10-31 13:53:06 +01001503 if (ops->mode == MTD_OOB_AUTO)
1504 len = chip->ecc.layout->oobavail;
1505 else
1506 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001507
William Juul52c07962007-10-31 13:53:06 +01001508 if (unlikely(ops->ooboffs >= len)) {
1509 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1510 "Attempt to start read outside oob\n");
1511 return -EINVAL;
1512 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001513
1514 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001515 if (unlikely(from >= mtd->size ||
1516 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1517 (from >> chip->page_shift)) * len)) {
1518 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1519 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001520 return -EINVAL;
1521 }
1522
William Juul52c07962007-10-31 13:53:06 +01001523 chipnr = (int)(from >> chip->chip_shift);
1524 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001525
William Juul52c07962007-10-31 13:53:06 +01001526 /* Shift to get page */
1527 realpage = (int)(from >> chip->page_shift);
1528 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001529
William Juul52c07962007-10-31 13:53:06 +01001530 while(1) {
1531 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001532
William Juul52c07962007-10-31 13:53:06 +01001533 len = min(len, readlen);
1534 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001535
William Juul52c07962007-10-31 13:53:06 +01001536 if (!(chip->options & NAND_NO_READRDY)) {
1537 /*
1538 * Apply delay or wait for ready/busy pin. Do this
1539 * before the AUTOINCR check, so no problems arise if a
1540 * chip which does auto increment is marked as
1541 * NOAUTOINCR by the board driver.
1542 */
1543 if (!chip->dev_ready)
1544 udelay(chip->chip_delay);
1545 else
1546 nand_wait_ready(mtd);
1547 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001548
William Juul52c07962007-10-31 13:53:06 +01001549 readlen -= len;
1550 if (!readlen)
1551 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001552
William Juul52c07962007-10-31 13:53:06 +01001553 /* Increment page address */
1554 realpage++;
1555
1556 page = realpage & chip->pagemask;
1557 /* Check, if we cross a chip boundary */
1558 if (!page) {
1559 chipnr++;
1560 chip->select_chip(mtd, -1);
1561 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001562 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001563
William Juul52c07962007-10-31 13:53:06 +01001564 /* Check, if the chip supports auto page increment
1565 * or if we have hit a block boundary.
1566 */
1567 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1568 sndcmd = 1;
1569 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001570
William Juul52c07962007-10-31 13:53:06 +01001571 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001572 return 0;
1573}
1574
1575/**
William Juul52c07962007-10-31 13:53:06 +01001576 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001577 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001578 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001579 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001580 *
William Juul52c07962007-10-31 13:53:06 +01001581 * NAND read data and/or out-of-band data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001582 */
William Juul52c07962007-10-31 13:53:06 +01001583static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1584 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001585{
William Juul52c07962007-10-31 13:53:06 +01001586 struct nand_chip *chip = mtd->priv;
1587 int ret = -ENOTSUPP;
1588
1589 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001590
1591 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001592 if (ops->datbuf && (from + ops->len) > mtd->size) {
1593 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1594 "Attempt read beyond end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001595 return -EINVAL;
1596 }
1597
William Juul52c07962007-10-31 13:53:06 +01001598 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001599
William Juul52c07962007-10-31 13:53:06 +01001600 switch(ops->mode) {
1601 case MTD_OOB_PLACE:
1602 case MTD_OOB_AUTO:
1603 case MTD_OOB_RAW:
1604 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001605
William Juul52c07962007-10-31 13:53:06 +01001606 default:
1607 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001608 }
1609
William Juul52c07962007-10-31 13:53:06 +01001610 if (!ops->datbuf)
1611 ret = nand_do_read_oob(mtd, from, ops);
1612 else
1613 ret = nand_do_read_ops(mtd, from, ops);
1614
1615 out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001616 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001617 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001618}
1619
1620
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001621/**
William Juul52c07962007-10-31 13:53:06 +01001622 * nand_write_page_raw - [Intern] raw page write function
1623 * @mtd: mtd info structure
1624 * @chip: nand chip info structure
1625 * @buf: data buffer
1626 */
1627static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1628 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001629{
William Juul52c07962007-10-31 13:53:06 +01001630 chip->write_buf(mtd, buf, mtd->writesize);
1631 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1632}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001633
William Juul52c07962007-10-31 13:53:06 +01001634/**
1635 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1636 * @mtd: mtd info structure
1637 * @chip: nand chip info structure
1638 * @buf: data buffer
1639 */
1640static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1641 const uint8_t *buf)
1642{
1643 int i, eccsize = chip->ecc.size;
1644 int eccbytes = chip->ecc.bytes;
1645 int eccsteps = chip->ecc.steps;
1646 uint8_t *ecc_calc = chip->buffers->ecccalc;
1647 const uint8_t *p = buf;
1648 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001649
William Juul52c07962007-10-31 13:53:06 +01001650 /* Software ecc calculation */
1651 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1652 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001653
William Juul52c07962007-10-31 13:53:06 +01001654 for (i = 0; i < chip->ecc.total; i++)
1655 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001656
William Juul52c07962007-10-31 13:53:06 +01001657 chip->ecc.write_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001658}
1659
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001660/**
William Juul52c07962007-10-31 13:53:06 +01001661 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1662 * @mtd: mtd info structure
1663 * @chip: nand chip info structure
1664 * @buf: data buffer
1665 */
1666static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1667 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001668{
William Juul52c07962007-10-31 13:53:06 +01001669 int i, eccsize = chip->ecc.size;
1670 int eccbytes = chip->ecc.bytes;
1671 int eccsteps = chip->ecc.steps;
1672 uint8_t *ecc_calc = chip->buffers->ecccalc;
1673 const uint8_t *p = buf;
1674 uint32_t *eccpos = chip->ecc.layout->eccpos;
1675
1676 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1677 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1678 chip->write_buf(mtd, p, eccsize);
1679 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1680 }
1681
1682 for (i = 0; i < chip->ecc.total; i++)
1683 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1684
1685 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001686}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001687
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001688/**
William Juul52c07962007-10-31 13:53:06 +01001689 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1690 * @mtd: mtd info structure
1691 * @chip: nand chip info structure
1692 * @buf: data buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001693 *
William Juul52c07962007-10-31 13:53:06 +01001694 * The hw generator calculates the error syndrome automatically. Therefor
1695 * we need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001696 */
William Juul52c07962007-10-31 13:53:06 +01001697static void nand_write_page_syndrome(struct mtd_info *mtd,
1698 struct nand_chip *chip, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001699{
William Juul52c07962007-10-31 13:53:06 +01001700 int i, eccsize = chip->ecc.size;
1701 int eccbytes = chip->ecc.bytes;
1702 int eccsteps = chip->ecc.steps;
1703 const uint8_t *p = buf;
1704 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001705
William Juul52c07962007-10-31 13:53:06 +01001706 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001707
William Juul52c07962007-10-31 13:53:06 +01001708 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1709 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001710
William Juul52c07962007-10-31 13:53:06 +01001711 if (chip->ecc.prepad) {
1712 chip->write_buf(mtd, oob, chip->ecc.prepad);
1713 oob += chip->ecc.prepad;
1714 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001715
William Juul52c07962007-10-31 13:53:06 +01001716 chip->ecc.calculate(mtd, p, oob);
1717 chip->write_buf(mtd, oob, eccbytes);
1718 oob += eccbytes;
1719
1720 if (chip->ecc.postpad) {
1721 chip->write_buf(mtd, oob, chip->ecc.postpad);
1722 oob += chip->ecc.postpad;
1723 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001724 }
1725
William Juul52c07962007-10-31 13:53:06 +01001726 /* Calculate remaining oob bytes */
1727 i = mtd->oobsize - (oob - chip->oob_poi);
1728 if (i)
1729 chip->write_buf(mtd, oob, i);
1730}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001731
William Juul52c07962007-10-31 13:53:06 +01001732/**
1733 * nand_write_page - [REPLACEABLE] write one page
1734 * @mtd: MTD device structure
1735 * @chip: NAND chip descriptor
1736 * @buf: the data to write
1737 * @page: page number to write
1738 * @cached: cached programming
1739 * @raw: use _raw version of write_page
1740 */
1741static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1742 const uint8_t *buf, int page, int cached, int raw)
1743{
1744 int status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001745
William Juul52c07962007-10-31 13:53:06 +01001746 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1747
1748 if (unlikely(raw))
1749 chip->ecc.write_page_raw(mtd, chip, buf);
1750 else
1751 chip->ecc.write_page(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001752
William Juul52c07962007-10-31 13:53:06 +01001753 /*
1754 * Cached progamming disabled for now, Not sure if its worth the
1755 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1756 */
1757 cached = 0;
1758
1759 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1760
1761 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1762 status = chip->waitfunc(mtd, chip);
1763 /*
1764 * See if operation failed and additional status checks are
1765 * available
1766 */
1767 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1768 status = chip->errstat(mtd, chip, FL_WRITING, status,
1769 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001770
William Juul52c07962007-10-31 13:53:06 +01001771 if (status & NAND_STATUS_FAIL)
1772 return -EIO;
1773 } else {
1774 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1775 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001776 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001777
William Juul52c07962007-10-31 13:53:06 +01001778#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1779 /* Send command to read back the data */
1780 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001781
William Juul52c07962007-10-31 13:53:06 +01001782 if (chip->verify_buf(mtd, buf, mtd->writesize))
1783 return -EIO;
1784#endif
1785 return 0;
1786}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001787
William Juul52c07962007-10-31 13:53:06 +01001788/**
1789 * nand_fill_oob - [Internal] Transfer client buffer to oob
1790 * @chip: nand chip structure
1791 * @oob: oob data buffer
1792 * @ops: oob ops structure
1793 */
1794static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1795 struct mtd_oob_ops *ops)
1796{
1797 size_t len = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001798
William Juul52c07962007-10-31 13:53:06 +01001799 switch(ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001800
William Juul52c07962007-10-31 13:53:06 +01001801 case MTD_OOB_PLACE:
1802 case MTD_OOB_RAW:
1803 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1804 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001805
William Juul52c07962007-10-31 13:53:06 +01001806 case MTD_OOB_AUTO: {
1807 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1808 uint32_t boffs = 0, woffs = ops->ooboffs;
1809 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001810
William Juul52c07962007-10-31 13:53:06 +01001811 for(; free->length && len; free++, len -= bytes) {
1812 /* Write request not from offset 0 ? */
1813 if (unlikely(woffs)) {
1814 if (woffs >= free->length) {
1815 woffs -= free->length;
1816 continue;
1817 }
1818 boffs = free->offset + woffs;
1819 bytes = min_t(size_t, len,
1820 (free->length - woffs));
1821 woffs = 0;
1822 } else {
1823 bytes = min_t(size_t, len, free->length);
1824 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001825 }
William Juul52c07962007-10-31 13:53:06 +01001826 memcpy(chip->oob_poi + boffs, oob, bytes);
1827 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001828 }
William Juul52c07962007-10-31 13:53:06 +01001829 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001830 }
William Juul52c07962007-10-31 13:53:06 +01001831 default:
1832 BUG();
1833 }
1834 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001835}
1836
William Juul52c07962007-10-31 13:53:06 +01001837#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001838
1839/**
William Juul52c07962007-10-31 13:53:06 +01001840 * nand_do_write_ops - [Internal] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001841 * @mtd: MTD device structure
1842 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001843 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001844 *
William Juul52c07962007-10-31 13:53:06 +01001845 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001846 */
William Juul52c07962007-10-31 13:53:06 +01001847static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1848 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001849{
William Juul52c07962007-10-31 13:53:06 +01001850 int chipnr, realpage, page, blockmask, column;
1851 struct nand_chip *chip = mtd->priv;
1852 uint32_t writelen = ops->len;
1853 uint8_t *oob = ops->oobbuf;
1854 uint8_t *buf = ops->datbuf;
1855 int ret, subpage;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001856
William Juul52c07962007-10-31 13:53:06 +01001857 ops->retlen = 0;
1858 if (!writelen)
1859 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001860
William Juul52c07962007-10-31 13:53:06 +01001861 /* reject writes, which are not page aligned */
1862 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
1863 printk(KERN_NOTICE "nand_write: "
1864 "Attempt to write not page aligned data\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001865 return -EINVAL;
1866 }
1867
William Juul52c07962007-10-31 13:53:06 +01001868 column = to & (mtd->writesize - 1);
1869 subpage = column || (writelen & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001870
William Juul52c07962007-10-31 13:53:06 +01001871 if (subpage && oob)
1872 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001873
William Juul52c07962007-10-31 13:53:06 +01001874 chipnr = (int)(to >> chip->chip_shift);
1875 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001876
1877 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01001878 if (nand_check_wp(mtd)) {
1879 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1880 return -EIO;
1881 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001882
William Juul52c07962007-10-31 13:53:06 +01001883 realpage = (int)(to >> chip->page_shift);
1884 page = realpage & chip->pagemask;
1885 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001886
William Juul52c07962007-10-31 13:53:06 +01001887 /* Invalidate the page cache, when we write to the cached page */
1888 if (to <= (chip->pagebuf << chip->page_shift) &&
1889 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1890 chip->pagebuf = -1;
1891
1892 /* If we're not given explicit OOB data, let it be 0xFF */
1893 if (likely(!oob))
1894 memset(chip->oob_poi, 0xff, mtd->oobsize);
1895
1896 while(1) {
1897 int bytes = mtd->writesize;
1898 int cached = writelen > bytes && page != blockmask;
1899 uint8_t *wbuf = buf;
1900
1901 /* Partial page write ? */
1902 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1903 cached = 0;
1904 bytes = min_t(int, bytes - column, (int) writelen);
1905 chip->pagebuf = -1;
1906 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1907 memcpy(&chip->buffers->databuf[column], buf, bytes);
1908 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02001909 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001910
William Juul52c07962007-10-31 13:53:06 +01001911 if (unlikely(oob))
1912 oob = nand_fill_oob(chip, oob, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001913
William Juul52c07962007-10-31 13:53:06 +01001914 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1915 (ops->mode == MTD_OOB_RAW));
1916 if (ret)
1917 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001918
William Juul52c07962007-10-31 13:53:06 +01001919 writelen -= bytes;
1920 if (!writelen)
1921 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001922
William Juul52c07962007-10-31 13:53:06 +01001923 column = 0;
1924 buf += bytes;
1925 realpage++;
1926
1927 page = realpage & chip->pagemask;
1928 /* Check, if we cross a chip boundary */
1929 if (!page) {
1930 chipnr++;
1931 chip->select_chip(mtd, -1);
1932 chip->select_chip(mtd, chipnr);
1933 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001934 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001935
William Juul52c07962007-10-31 13:53:06 +01001936 ops->retlen = ops->len - writelen;
1937 if (unlikely(oob))
1938 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001939 return ret;
1940}
1941
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001942/**
William Juul52c07962007-10-31 13:53:06 +01001943 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001944 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001945 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001946 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001947 * @retlen: pointer to variable to store the number of written bytes
William Juul52c07962007-10-31 13:53:06 +01001948 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001949 *
William Juul52c07962007-10-31 13:53:06 +01001950 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001951 */
William Juul52c07962007-10-31 13:53:06 +01001952static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1953 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001954{
William Juul52c07962007-10-31 13:53:06 +01001955 struct nand_chip *chip = mtd->priv;
1956 int ret;
1957
1958 /* Do not allow reads past end of device */
1959 if ((to + len) > mtd->size)
1960 return -EINVAL;
1961 if (!len)
1962 return 0;
1963
1964 nand_get_device(chip, mtd, FL_WRITING);
1965
1966 chip->ops.len = len;
1967 chip->ops.datbuf = (uint8_t *)buf;
1968 chip->ops.oobbuf = NULL;
1969
1970 ret = nand_do_write_ops(mtd, to, &chip->ops);
1971
1972 *retlen = chip->ops.retlen;
1973
1974 nand_release_device(mtd);
1975
1976 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001977}
1978
1979/**
William Juul52c07962007-10-31 13:53:06 +01001980 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001981 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001982 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001983 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001984 *
William Juul52c07962007-10-31 13:53:06 +01001985 * NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001986 */
William Juul52c07962007-10-31 13:53:06 +01001987static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1988 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001989{
William Juul52c07962007-10-31 13:53:06 +01001990 int chipnr, page, status, len;
1991 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001992
William Juul52c07962007-10-31 13:53:06 +01001993 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1994 (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001995
William Juul52c07962007-10-31 13:53:06 +01001996 if (ops->mode == MTD_OOB_AUTO)
1997 len = chip->ecc.layout->oobavail;
1998 else
1999 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002000
2001 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002002 if ((ops->ooboffs + ops->ooblen) > len) {
2003 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
2004 "Attempt to write past end of page\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002005 return -EINVAL;
2006 }
2007
William Juul52c07962007-10-31 13:53:06 +01002008 if (unlikely(ops->ooboffs >= len)) {
2009 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2010 "Attempt to start write outside oob\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002011 return -EINVAL;
2012 }
2013
William Juul52c07962007-10-31 13:53:06 +01002014 /* Do not allow reads past end of device */
2015 if (unlikely(to >= mtd->size ||
2016 ops->ooboffs + ops->ooblen >
2017 ((mtd->size >> chip->page_shift) -
2018 (to >> chip->page_shift)) * len)) {
2019 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2020 "Attempt write beyond end of device\n");
2021 return -EINVAL;
2022 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002023
William Juul52c07962007-10-31 13:53:06 +01002024 chipnr = (int)(to >> chip->chip_shift);
2025 chip->select_chip(mtd, chipnr);
2026
2027 /* Shift to get page */
2028 page = (int)(to >> chip->page_shift);
2029
2030 /*
2031 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2032 * of my DiskOnChip 2000 test units) will clear the whole data page too
2033 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2034 * it in the doc2000 driver in August 1999. dwmw2.
2035 */
2036 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002037
2038 /* Check, if it is write protected */
2039 if (nand_check_wp(mtd))
William Juul52c07962007-10-31 13:53:06 +01002040 return -EROFS;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002041
William Juul52c07962007-10-31 13:53:06 +01002042 /* Invalidate the page cache, if we write to the cached page */
2043 if (page == chip->pagebuf)
2044 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002045
William Juul52c07962007-10-31 13:53:06 +01002046 memset(chip->oob_poi, 0xff, mtd->oobsize);
2047 nand_fill_oob(chip, ops->oobbuf, ops);
2048 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2049 memset(chip->oob_poi, 0xff, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002050
William Juul52c07962007-10-31 13:53:06 +01002051 if (status)
2052 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002053
William Juul52c07962007-10-31 13:53:06 +01002054 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002055
William Juul52c07962007-10-31 13:53:06 +01002056 return 0;
2057}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002058
William Juul52c07962007-10-31 13:53:06 +01002059/**
2060 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2061 * @mtd: MTD device structure
2062 * @to: offset to write to
2063 * @ops: oob operation description structure
2064 */
2065static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2066 struct mtd_oob_ops *ops)
2067{
2068 struct nand_chip *chip = mtd->priv;
2069 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002070
William Juul52c07962007-10-31 13:53:06 +01002071 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002072
William Juul52c07962007-10-31 13:53:06 +01002073 /* Do not allow writes past end of device */
2074 if (ops->datbuf && (to + ops->len) > mtd->size) {
2075 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2076 "Attempt read beyond end of device\n");
2077 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002078 }
William Juul52c07962007-10-31 13:53:06 +01002079
2080 nand_get_device(chip, mtd, FL_WRITING);
2081
2082 switch(ops->mode) {
2083 case MTD_OOB_PLACE:
2084 case MTD_OOB_AUTO:
2085 case MTD_OOB_RAW:
2086 break;
2087
2088 default:
2089 goto out;
2090 }
2091
2092 if (!ops->datbuf)
2093 ret = nand_do_write_oob(mtd, to, ops);
2094 else
2095 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002096
William Juul52c07962007-10-31 13:53:06 +01002097 out:
2098 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002099 return ret;
2100}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002101
2102/**
2103 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2104 * @mtd: MTD device structure
2105 * @page: the page address of the block which will be erased
2106 *
2107 * Standard erase command for NAND chips
2108 */
William Juul52c07962007-10-31 13:53:06 +01002109static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002110{
William Juul52c07962007-10-31 13:53:06 +01002111 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002112 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002113 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2114 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002115}
2116
2117/**
2118 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2119 * @mtd: MTD device structure
2120 * @page: the page address of the block which will be erased
2121 *
2122 * AND multi block erase command function
2123 * Erase 4 consecutive blocks
2124 */
William Juul52c07962007-10-31 13:53:06 +01002125static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002126{
William Juul52c07962007-10-31 13:53:06 +01002127 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002128 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002129 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2130 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2131 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2132 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2133 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002134}
2135
2136/**
2137 * nand_erase - [MTD Interface] erase block(s)
2138 * @mtd: MTD device structure
2139 * @instr: erase instruction
2140 *
2141 * Erase one ore more blocks
2142 */
William Juul52c07962007-10-31 13:53:06 +01002143static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002144{
William Juul52c07962007-10-31 13:53:06 +01002145 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002146}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002147
William Juul52c07962007-10-31 13:53:06 +01002148#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002149/**
William Juul52c07962007-10-31 13:53:06 +01002150 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002151 * @mtd: MTD device structure
2152 * @instr: erase instruction
2153 * @allowbbt: allow erasing the bbt area
2154 *
2155 * Erase one ore more blocks
2156 */
William Juul52c07962007-10-31 13:53:06 +01002157int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2158 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002159{
2160 int page, len, status, pages_per_block, ret, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002161 struct nand_chip *chip = mtd->priv;
Wolfgang Grandegger03025502009-01-16 18:55:54 +01002162 int rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS]={0};
William Juul52c07962007-10-31 13:53:06 +01002163 unsigned int bbt_masked_page = 0xffffffff;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002164
Scott Wooddf83c472008-06-20 12:38:57 -05002165 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2166 (unsigned int) instr->addr, (unsigned int) instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002167
2168 /* Start address must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002169 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002170 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002171 return -EINVAL;
2172 }
2173
2174 /* Length must align on block boundary */
William Juul52c07962007-10-31 13:53:06 +01002175 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002176 MTDDEBUG (MTD_DEBUG_LEVEL0,
2177 "nand_erase: Length not block aligned\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002178 return -EINVAL;
2179 }
2180
2181 /* Do not allow erase past end of device */
2182 if ((instr->len + instr->addr) > mtd->size) {
Scott Wooddf83c472008-06-20 12:38:57 -05002183 MTDDEBUG (MTD_DEBUG_LEVEL0,
2184 "nand_erase: Erase past end of device\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002185 return -EINVAL;
2186 }
2187
2188 instr->fail_addr = 0xffffffff;
2189
2190 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002191 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002192
2193 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002194 page = (int)(instr->addr >> chip->page_shift);
2195 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002196
2197 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002198 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002199
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002200 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002201 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002202
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002203 /* Check, if it is write protected */
2204 if (nand_check_wp(mtd)) {
Scott Wooddf83c472008-06-20 12:38:57 -05002205 MTDDEBUG (MTD_DEBUG_LEVEL0,
2206 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002207 instr->state = MTD_ERASE_FAILED;
2208 goto erase_exit;
2209 }
2210
William Juul52c07962007-10-31 13:53:06 +01002211 /*
2212 * If BBT requires refresh, set the BBT page mask to see if the BBT
2213 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2214 * can not be matched. This is also done when the bbt is actually
2215 * erased to avoid recusrsive updates
2216 */
2217 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2218 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2219
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002220 /* Loop through the pages */
2221 len = instr->len;
2222
2223 instr->state = MTD_ERASING;
2224
2225 while (len) {
William Juul52c07962007-10-31 13:53:06 +01002226 /*
2227 * heck if we have a bad block, we do not erase bad blocks !
2228 */
2229 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2230 chip->page_shift, 0, allowbbt)) {
2231 printk(KERN_WARNING "nand_erase: attempt to erase a "
2232 "bad block at page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002233 instr->state = MTD_ERASE_FAILED;
2234 goto erase_exit;
2235 }
William Juul52c07962007-10-31 13:53:06 +01002236
2237 /*
2238 * Invalidate the page cache, if we erase the block which
2239 * contains the current cached page
2240 */
2241 if (page <= chip->pagebuf && chip->pagebuf <
2242 (page + pages_per_block))
2243 chip->pagebuf = -1;
2244
2245 chip->erase_cmd(mtd, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002246
William Juul52c07962007-10-31 13:53:06 +01002247 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002248
William Juul52c07962007-10-31 13:53:06 +01002249 /*
2250 * See if operation failed and additional status checks are
2251 * available
2252 */
2253 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2254 status = chip->errstat(mtd, chip, FL_ERASING,
2255 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002256
2257 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002258 if (status & NAND_STATUS_FAIL) {
Scott Wooddf83c472008-06-20 12:38:57 -05002259 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2260 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002261 instr->state = MTD_ERASE_FAILED;
William Juul52c07962007-10-31 13:53:06 +01002262 instr->fail_addr = (page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002263 goto erase_exit;
2264 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002265
William Juul52c07962007-10-31 13:53:06 +01002266 /*
2267 * If BBT requires refresh, set the BBT rewrite flag to the
2268 * page being erased
2269 */
2270 if (bbt_masked_page != 0xffffffff &&
2271 (page & BBT_PAGE_MASK) == bbt_masked_page)
2272 rewrite_bbt[chipnr] = (page << chip->page_shift);
2273
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002274 /* Increment page address and decrement length */
William Juul52c07962007-10-31 13:53:06 +01002275 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002276 page += pages_per_block;
2277
2278 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002279 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002280 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002281 chip->select_chip(mtd, -1);
2282 chip->select_chip(mtd, chipnr);
2283
2284 /*
2285 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2286 * page mask to see if this BBT should be rewritten
2287 */
2288 if (bbt_masked_page != 0xffffffff &&
2289 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2290 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2291 BBT_PAGE_MASK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002292 }
2293 }
2294 instr->state = MTD_ERASE_DONE;
2295
William Juul52c07962007-10-31 13:53:06 +01002296 erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002297
2298 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002299
2300 /* Deselect and wake up anyone waiting on the device */
2301 nand_release_device(mtd);
2302
Scott Wood3628f002008-10-24 16:20:43 -05002303 /* Do call back function */
2304 if (!ret)
2305 mtd_erase_callback(instr);
2306
William Juul52c07962007-10-31 13:53:06 +01002307 /*
2308 * If BBT requires refresh and erase was successful, rewrite any
2309 * selected bad block tables
2310 */
2311 if (bbt_masked_page == 0xffffffff || ret)
2312 return ret;
2313
2314 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2315 if (!rewrite_bbt[chipnr])
2316 continue;
2317 /* update the BBT for chip */
2318 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2319 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2320 chip->bbt_td->pages[chipnr]);
2321 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2322 }
2323
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002324 /* Return more or less happy */
2325 return ret;
2326}
2327
2328/**
2329 * nand_sync - [MTD Interface] sync
2330 * @mtd: MTD device structure
2331 *
2332 * Sync is actually a wait for chip ready function
2333 */
William Juul52c07962007-10-31 13:53:06 +01002334static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002335{
William Juul52c07962007-10-31 13:53:06 +01002336 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002337
Scott Wooddf83c472008-06-20 12:38:57 -05002338 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002339
2340 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002341 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002342 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002343 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002344}
2345
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002346/**
William Juul52c07962007-10-31 13:53:06 +01002347 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002348 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002349 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002350 */
William Juul52c07962007-10-31 13:53:06 +01002351static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002352{
2353 /* Check for invalid offset */
William Juul52c07962007-10-31 13:53:06 +01002354 if (offs > mtd->size)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002355 return -EINVAL;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002356
William Juul52c07962007-10-31 13:53:06 +01002357 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002358}
2359
2360/**
William Juul52c07962007-10-31 13:53:06 +01002361 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002362 * @mtd: MTD device structure
2363 * @ofs: offset relative to mtd start
2364 */
William Juul52c07962007-10-31 13:53:06 +01002365static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002366{
William Juul52c07962007-10-31 13:53:06 +01002367 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002368 int ret;
2369
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002370 if ((ret = nand_block_isbad(mtd, ofs))) {
2371 /* If it was bad already, return success and do nothing. */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002372 if (ret > 0)
2373 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002374 return ret;
2375 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002376
William Juul52c07962007-10-31 13:53:06 +01002377 return chip->block_markbad(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002378}
2379
2380/**
William Juul52c07962007-10-31 13:53:06 +01002381 * nand_suspend - [MTD Interface] Suspend the NAND flash
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002382 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002383 */
William Juul52c07962007-10-31 13:53:06 +01002384static int nand_suspend(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002385{
William Juul52c07962007-10-31 13:53:06 +01002386 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002387
William Juul52c07962007-10-31 13:53:06 +01002388 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2389}
2390
2391/**
2392 * nand_resume - [MTD Interface] Resume the NAND flash
2393 * @mtd: MTD device structure
2394 */
2395static void nand_resume(struct mtd_info *mtd)
2396{
2397 struct nand_chip *chip = mtd->priv;
2398
2399 if (chip->state == FL_PM_SUSPENDED)
2400 nand_release_device(mtd);
2401 else
2402 printk(KERN_ERR "nand_resume() called for a chip which is not "
2403 "in suspended state\n");
2404}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002405
William Juul52c07962007-10-31 13:53:06 +01002406/*
2407 * Set default functions
2408 */
2409static void nand_set_defaults(struct nand_chip *chip, int busw)
2410{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002411 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01002412 if (!chip->chip_delay)
2413 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002414
2415 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01002416 if (chip->cmdfunc == NULL)
2417 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002418
2419 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01002420 if (chip->waitfunc == NULL)
2421 chip->waitfunc = nand_wait;
2422
2423 if (!chip->select_chip)
2424 chip->select_chip = nand_select_chip;
2425 if (!chip->read_byte)
2426 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2427 if (!chip->read_word)
2428 chip->read_word = nand_read_word;
2429 if (!chip->block_bad)
2430 chip->block_bad = nand_block_bad;
2431 if (!chip->block_markbad)
2432 chip->block_markbad = nand_default_block_markbad;
2433 if (!chip->write_buf)
2434 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2435 if (!chip->read_buf)
2436 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2437 if (!chip->verify_buf)
2438 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2439 if (!chip->scan_bbt)
2440 chip->scan_bbt = nand_default_bbt;
2441
2442 if (!chip->controller) {
2443 chip->controller = &chip->hwcontrol;
2444
2445 /* XXX U-BOOT XXX */
2446#if 0
2447 spin_lock_init(&chip->controller->lock);
2448 init_waitqueue_head(&chip->controller->wq);
2449#endif
2450 }
2451
2452}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002453
William Juul52c07962007-10-31 13:53:06 +01002454/*
2455 * Get the flash and manufacturer id and lookup if the type is supported
2456 */
2457static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2458 struct nand_chip *chip,
2459 int busw, int *maf_id)
2460{
2461 struct nand_flash_dev *type = NULL;
2462 int i, dev_id, maf_idx;
Scott Wood3628f002008-10-24 16:20:43 -05002463 int tmp_id, tmp_manf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002464
2465 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01002466 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002467
Karl Beldanb6322fc2008-09-15 16:08:03 +02002468 /*
2469 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2470 * after power-up
2471 */
2472 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2473
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002474 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01002475 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002476
2477 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002478 *maf_id = chip->read_byte(mtd);
2479 dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002480
Scott Wood3628f002008-10-24 16:20:43 -05002481 /* Try again to make sure, as some systems the bus-hold or other
2482 * interface concerns can cause random data which looks like a
2483 * possibly credible NAND flash to appear. If the two results do
2484 * not match, ignore the device completely.
2485 */
2486
2487 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2488
2489 /* Read manufacturer and device IDs */
2490
2491 tmp_manf = chip->read_byte(mtd);
2492 tmp_id = chip->read_byte(mtd);
2493
2494 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2495 printk(KERN_INFO "%s: second ID read did not match "
2496 "%02x,%02x against %02x,%02x\n", __func__,
2497 *maf_id, dev_id, tmp_manf, tmp_id);
2498 return ERR_PTR(-ENODEV);
2499 }
2500
William Juul52c07962007-10-31 13:53:06 +01002501 /* Lookup the flash id */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002502 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
William Juul52c07962007-10-31 13:53:06 +01002503 if (dev_id == nand_flash_ids[i].id) {
2504 type = &nand_flash_ids[i];
2505 break;
2506 }
2507 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002508
William Juul52c07962007-10-31 13:53:06 +01002509 if (!type)
2510 return ERR_PTR(-ENODEV);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002511
William Juul52c07962007-10-31 13:53:06 +01002512 if (!mtd->name)
2513 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002514
William Juul52c07962007-10-31 13:53:06 +01002515 chip->chipsize = type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002516
William Juul52c07962007-10-31 13:53:06 +01002517 /* Newer devices have all the information in additional id bytes */
2518 if (!type->pagesize) {
2519 int extid;
2520 /* The 3rd id byte holds MLC / multichip data */
2521 chip->cellinfo = chip->read_byte(mtd);
2522 /* The 4th id byte is the important one */
2523 extid = chip->read_byte(mtd);
2524 /* Calc pagesize */
2525 mtd->writesize = 1024 << (extid & 0x3);
2526 extid >>= 2;
2527 /* Calc oobsize */
2528 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2529 extid >>= 2;
2530 /* Calc blocksize. Blocksize is multiples of 64KiB */
2531 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2532 extid >>= 2;
2533 /* Get buswidth information */
2534 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002535
William Juul52c07962007-10-31 13:53:06 +01002536 } else {
2537 /*
2538 * Old devices have chip data hardcoded in the device id table
2539 */
2540 mtd->erasesize = type->erasesize;
2541 mtd->writesize = type->pagesize;
2542 mtd->oobsize = mtd->writesize / 32;
2543 busw = type->options & NAND_BUSWIDTH_16;
2544 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002545
William Juul52c07962007-10-31 13:53:06 +01002546 /* Try to identify manufacturer */
2547 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2548 if (nand_manuf_ids[maf_idx].id == *maf_id)
2549 break;
2550 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002551
William Juul52c07962007-10-31 13:53:06 +01002552 /*
2553 * Check, if buswidth is correct. Hardware drivers should set
2554 * chip correct !
2555 */
2556 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2557 printk(KERN_INFO "NAND device: Manufacturer ID:"
2558 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2559 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2560 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2561 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2562 busw ? 16 : 8);
2563 return ERR_PTR(-EINVAL);
2564 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002565
William Juul52c07962007-10-31 13:53:06 +01002566 /* Calculate the address shift from the page size */
2567 chip->page_shift = ffs(mtd->writesize) - 1;
2568 /* Convert chipsize to number of pages per chip -1. */
2569 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002570
William Juul52c07962007-10-31 13:53:06 +01002571 chip->bbt_erase_shift = chip->phys_erase_shift =
2572 ffs(mtd->erasesize) - 1;
2573 chip->chip_shift = ffs(chip->chipsize) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002574
William Juul52c07962007-10-31 13:53:06 +01002575 /* Set the bad block position */
2576 chip->badblockpos = mtd->writesize > 512 ?
2577 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002578
William Juul52c07962007-10-31 13:53:06 +01002579 /* Get chip options, preserve non chip based options */
2580 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2581 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002582
William Juul52c07962007-10-31 13:53:06 +01002583 /*
2584 * Set chip as a default. Board drivers can override it, if necessary
2585 */
2586 chip->options |= NAND_NO_AUTOINCR;
2587
2588 /* Check if chip is a not a samsung device. Do not clear the
2589 * options for chips which are not having an extended id.
2590 */
2591 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2592 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2593
2594 /* Check for AND chips with 4 page planes */
2595 if (chip->options & NAND_4PAGE_ARRAY)
2596 chip->erase_cmd = multi_erase_cmd;
2597 else
2598 chip->erase_cmd = single_erase_cmd;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002599
William Juul52c07962007-10-31 13:53:06 +01002600 /* Do not replace user supplied command function ! */
2601 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2602 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002603
Stefan Roese4fe71432008-01-10 18:47:33 +01002604 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2605 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2606 nand_manuf_ids[maf_idx].name, type->name);
William Juul52c07962007-10-31 13:53:06 +01002607
2608 return type;
2609}
2610
2611/**
2612 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2613 * @mtd: MTD device structure
2614 * @maxchips: Number of chips to scan for
2615 *
2616 * This is the first phase of the normal nand_scan() function. It
2617 * reads the flash ID and sets up MTD fields accordingly.
2618 *
2619 * The mtd->owner field must be set to the module of the caller.
2620 */
2621int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2622{
2623 int i, busw, nand_maf_id;
2624 struct nand_chip *chip = mtd->priv;
2625 struct nand_flash_dev *type;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002626
William Juul52c07962007-10-31 13:53:06 +01002627 /* Get buswidth to select the correct functions */
2628 busw = chip->options & NAND_BUSWIDTH_16;
2629 /* Set the default functions */
2630 nand_set_defaults(chip, busw);
2631
2632 /* Read the flash type */
2633 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2634
2635 if (IS_ERR(type)) {
Peter Tyserf96047a2009-02-04 13:39:40 -06002636#ifndef CONFIG_SYS_NAND_QUIET_TEST
William Juul52c07962007-10-31 13:53:06 +01002637 printk(KERN_WARNING "No NAND device found!!!\n");
Peter Tyserf96047a2009-02-04 13:39:40 -06002638#endif
William Juul52c07962007-10-31 13:53:06 +01002639 chip->select_chip(mtd, -1);
2640 return PTR_ERR(type);
2641 }
2642
2643 /* Check for a chip array */
2644 for (i = 1; i < maxchips; i++) {
2645 chip->select_chip(mtd, i);
Karl Beldanb6322fc2008-09-15 16:08:03 +02002646 /* See comment in nand_get_flash_type for reset */
2647 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002648 /* Send the command for reading device ID */
2649 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002650 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002651 if (nand_maf_id != chip->read_byte(mtd) ||
2652 type->id != chip->read_byte(mtd))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002653 break;
2654 }
2655 if (i > 1)
2656 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002657
William Juul52c07962007-10-31 13:53:06 +01002658 /* Store the number of chips and calc total size for mtd */
2659 chip->numchips = i;
2660 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002661
William Juul52c07962007-10-31 13:53:06 +01002662 return 0;
2663}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002664
William Juul52c07962007-10-31 13:53:06 +01002665
2666/**
2667 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2668 * @mtd: MTD device structure
2669 * @maxchips: Number of chips to scan for
2670 *
2671 * This is the second phase of the normal nand_scan() function. It
2672 * fills out all the uninitialized function pointers with the defaults
2673 * and scans for a bad block table if appropriate.
2674 */
2675int nand_scan_tail(struct mtd_info *mtd)
2676{
2677 int i;
2678 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002679
William Juul52c07962007-10-31 13:53:06 +01002680 if (!(chip->options & NAND_OWN_BUFFERS))
2681 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2682 if (!chip->buffers)
2683 return -ENOMEM;
2684
2685 /* Set the internal oob buffer location, just after the page data */
2686 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2687
2688 /*
2689 * If no default placement scheme is given, select an appropriate one
2690 */
2691 if (!chip->ecc.layout) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002692 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002693 case 8:
William Juul52c07962007-10-31 13:53:06 +01002694 chip->ecc.layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002695 break;
2696 case 16:
William Juul52c07962007-10-31 13:53:06 +01002697 chip->ecc.layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002698 break;
2699 case 64:
William Juul52c07962007-10-31 13:53:06 +01002700 chip->ecc.layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002701 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002702 case 128:
William Juul52c07962007-10-31 13:53:06 +01002703 chip->ecc.layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002704 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002705 default:
William Juul52c07962007-10-31 13:53:06 +01002706 printk(KERN_WARNING "No oob scheme defined for "
2707 "oobsize %d\n", mtd->oobsize);
William Juul9e9c2c12007-11-09 13:32:30 +01002708/* BUG(); */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002709 }
2710 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002711
William Juul52c07962007-10-31 13:53:06 +01002712 if (!chip->write_page)
2713 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002714
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002715 /*
William Juul52c07962007-10-31 13:53:06 +01002716 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2717 * selected and we have 256 byte pagesize fallback to software ECC
2718 */
2719 if (!chip->ecc.read_page_raw)
2720 chip->ecc.read_page_raw = nand_read_page_raw;
2721 if (!chip->ecc.write_page_raw)
2722 chip->ecc.write_page_raw = nand_write_page_raw;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002723
William Juul52c07962007-10-31 13:53:06 +01002724 switch (chip->ecc.mode) {
2725 case NAND_ECC_HW:
2726 /* Use standard hwecc read page function ? */
2727 if (!chip->ecc.read_page)
2728 chip->ecc.read_page = nand_read_page_hwecc;
2729 if (!chip->ecc.write_page)
2730 chip->ecc.write_page = nand_write_page_hwecc;
2731 if (!chip->ecc.read_oob)
2732 chip->ecc.read_oob = nand_read_oob_std;
2733 if (!chip->ecc.write_oob)
2734 chip->ecc.write_oob = nand_write_oob_std;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002735
William Juul52c07962007-10-31 13:53:06 +01002736 case NAND_ECC_HW_SYNDROME:
Scott Wood5fd2c302008-03-18 15:29:14 -05002737 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2738 !chip->ecc.hwctl) &&
2739 (!chip->ecc.read_page ||
2740 chip->ecc.read_page == nand_read_page_hwecc ||
2741 !chip->ecc.write_page ||
2742 chip->ecc.write_page == nand_write_page_hwecc)) {
William Juul52c07962007-10-31 13:53:06 +01002743 printk(KERN_WARNING "No ECC functions supplied, "
2744 "Hardware ECC not possible\n");
2745 BUG();
2746 }
2747 /* Use standard syndrome read/write page function ? */
2748 if (!chip->ecc.read_page)
2749 chip->ecc.read_page = nand_read_page_syndrome;
2750 if (!chip->ecc.write_page)
2751 chip->ecc.write_page = nand_write_page_syndrome;
2752 if (!chip->ecc.read_oob)
2753 chip->ecc.read_oob = nand_read_oob_syndrome;
2754 if (!chip->ecc.write_oob)
2755 chip->ecc.write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002756
William Juul52c07962007-10-31 13:53:06 +01002757 if (mtd->writesize >= chip->ecc.size)
2758 break;
2759 printk(KERN_WARNING "%d byte HW ECC not possible on "
2760 "%d byte page size, fallback to SW ECC\n",
2761 chip->ecc.size, mtd->writesize);
2762 chip->ecc.mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002763
William Juul52c07962007-10-31 13:53:06 +01002764 case NAND_ECC_SOFT:
2765 chip->ecc.calculate = nand_calculate_ecc;
2766 chip->ecc.correct = nand_correct_data;
2767 chip->ecc.read_page = nand_read_page_swecc;
Scott Wood3628f002008-10-24 16:20:43 -05002768 chip->ecc.read_subpage = nand_read_subpage;
William Juul52c07962007-10-31 13:53:06 +01002769 chip->ecc.write_page = nand_write_page_swecc;
2770 chip->ecc.read_oob = nand_read_oob_std;
2771 chip->ecc.write_oob = nand_write_oob_std;
2772 chip->ecc.size = 256;
2773 chip->ecc.bytes = 3;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002774 break;
2775
William Juul52c07962007-10-31 13:53:06 +01002776 case NAND_ECC_NONE:
2777 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2778 "This is not recommended !!\n");
2779 chip->ecc.read_page = nand_read_page_raw;
2780 chip->ecc.write_page = nand_write_page_raw;
2781 chip->ecc.read_oob = nand_read_oob_std;
2782 chip->ecc.write_oob = nand_write_oob_std;
2783 chip->ecc.size = mtd->writesize;
2784 chip->ecc.bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002785 break;
2786
2787 default:
William Juul52c07962007-10-31 13:53:06 +01002788 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2789 chip->ecc.mode);
2790 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002791 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002792
William Juul52c07962007-10-31 13:53:06 +01002793 /*
2794 * The number of bytes available for a client to place data into
2795 * the out of band area
2796 */
2797 chip->ecc.layout->oobavail = 0;
2798 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2799 chip->ecc.layout->oobavail +=
2800 chip->ecc.layout->oobfree[i].length;
2801 mtd->oobavail = chip->ecc.layout->oobavail;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002802
William Juul52c07962007-10-31 13:53:06 +01002803 /*
2804 * Set the number of read / write steps for one page depending on ECC
2805 * mode
2806 */
2807 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2808 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2809 printk(KERN_WARNING "Invalid ecc parameters\n");
2810 BUG();
2811 }
2812 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002813
William Juul52c07962007-10-31 13:53:06 +01002814 /*
2815 * Allow subpage writes up to ecc.steps. Not possible for MLC
2816 * FLASH.
2817 */
2818 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2819 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2820 switch(chip->ecc.steps) {
2821 case 2:
2822 mtd->subpage_sft = 1;
2823 break;
2824 case 4:
2825 case 8:
2826 mtd->subpage_sft = 2;
2827 break;
2828 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002829 }
William Juul52c07962007-10-31 13:53:06 +01002830 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002831
William Juul52c07962007-10-31 13:53:06 +01002832 /* Initialize state */
2833 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002834
2835 /* De-select the device */
William Juul52c07962007-10-31 13:53:06 +01002836 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002837
2838 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01002839 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002840
2841 /* Fill in remaining MTD driver data */
2842 mtd->type = MTD_NANDFLASH;
William Juul52c07962007-10-31 13:53:06 +01002843 mtd->flags = MTD_CAP_NANDFLASH;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002844 mtd->erase = nand_erase;
2845 mtd->point = NULL;
2846 mtd->unpoint = NULL;
2847 mtd->read = nand_read;
2848 mtd->write = nand_write;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002849 mtd->read_oob = nand_read_oob;
2850 mtd->write_oob = nand_write_oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002851 mtd->sync = nand_sync;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002852 mtd->lock = NULL;
2853 mtd->unlock = NULL;
William Juul52c07962007-10-31 13:53:06 +01002854 mtd->suspend = nand_suspend;
2855 mtd->resume = nand_resume;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002856 mtd->block_isbad = nand_block_isbad;
2857 mtd->block_markbad = nand_block_markbad;
2858
William Juul52c07962007-10-31 13:53:06 +01002859 /* propagate ecc.layout to mtd_info */
2860 mtd->ecclayout = chip->ecc.layout;
2861
2862 /* Check, if we should skip the bad block table scan */
2863 if (chip->options & NAND_SKIP_BBTSCAN)
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02002864 chip->options |= NAND_BBT_SCANNED;
William Juul52c07962007-10-31 13:53:06 +01002865
Ilya Yanoka2d4dfe2008-06-30 15:34:40 +02002866 return 0;
William Juul52c07962007-10-31 13:53:06 +01002867}
2868
2869/* module_text_address() isn't exported, and it's mostly a pointless
2870 test if this is a module _anyway_ -- they'd have to try _really_ hard
2871 to call us from in-kernel code if the core NAND support is modular. */
2872#ifdef MODULE
2873#define caller_is_module() (1)
2874#else
2875#define caller_is_module() \
2876 module_text_address((unsigned long)__builtin_return_address(0))
2877#endif
2878
2879/**
2880 * nand_scan - [NAND Interface] Scan for the NAND device
2881 * @mtd: MTD device structure
2882 * @maxchips: Number of chips to scan for
2883 *
2884 * This fills out all the uninitialized function pointers
2885 * with the defaults.
2886 * The flash ID is read and the mtd/chip structures are
2887 * filled with the appropriate values.
2888 * The mtd->owner field must be set to the module of the caller
2889 *
2890 */
2891int nand_scan(struct mtd_info *mtd, int maxchips)
2892{
2893 int ret;
2894
2895 /* Many callers got this wrong, so check for it for a while... */
2896 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002897#if 0
William Juul52c07962007-10-31 13:53:06 +01002898 if (!mtd->owner && caller_is_module()) {
2899 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2900 BUG();
2901 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002902#endif
William Juulb76ec382007-11-08 10:39:53 +01002903
William Juul52c07962007-10-31 13:53:06 +01002904 ret = nand_scan_ident(mtd, maxchips);
2905 if (!ret)
2906 ret = nand_scan_tail(mtd);
2907 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002908}
2909
2910/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002911 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002912 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002913*/
2914void nand_release(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002915{
William Juul52c07962007-10-31 13:53:06 +01002916 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002917
2918#ifdef CONFIG_MTD_PARTITIONS
2919 /* Deregister partitions */
William Juul52c07962007-10-31 13:53:06 +01002920 del_mtd_partitions(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002921#endif
2922 /* Deregister the device */
William Juul52c07962007-10-31 13:53:06 +01002923 /* XXX U-BOOT XXX */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002924#if 0
William Juul52c07962007-10-31 13:53:06 +01002925 del_mtd_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002926#endif
William Juul52c07962007-10-31 13:53:06 +01002927
2928 /* Free bad block table memory */
2929 kfree(chip->bbt);
2930 if (!(chip->options & NAND_OWN_BUFFERS))
2931 kfree(chip->buffers);
2932}
2933
2934/* XXX U-BOOT XXX */
2935#if 0
2936EXPORT_SYMBOL_GPL(nand_scan);
2937EXPORT_SYMBOL_GPL(nand_scan_ident);
2938EXPORT_SYMBOL_GPL(nand_scan_tail);
2939EXPORT_SYMBOL_GPL(nand_release);
2940
2941static int __init nand_base_init(void)
2942{
2943 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2944 return 0;
2945}
2946
2947static void __exit nand_base_exit(void)
2948{
2949 led_trigger_unregister_simple(nand_led_trigger);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002950}
2951
William Juul52c07962007-10-31 13:53:06 +01002952module_init(nand_base_init);
2953module_exit(nand_base_exit);
2954
2955MODULE_LICENSE("GPL");
2956MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2957MODULE_DESCRIPTION("Generic NAND flash driver code");
2958#endif