]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/mtd/nand/nand_base.c
bootm: Added CONFIG_BOOTM_{LINUX, NETBSD, RTEMS}
[people/ms/u-boot.git] / drivers / mtd / nand / nand_base.c
CommitLineData
932394ac
WD
1/*
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.
ac7eb8a3 8 *
932394ac
WD
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
ac7eb8a3 11 *
932394ac 12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
cfa460ad 13 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
932394ac 14 *
cfa460ad 15 * Credits:
ac7eb8a3
WD
16 * David Woodhouse for adding multichip support
17 *
932394ac
WD
18 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
cfa460ad 21 * TODO:
932394ac
WD
22 * 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.
27 *
932394ac
WD
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License version 2 as
30 * published by the Free Software Foundation.
31 *
32 */
33
34/* XXX U-BOOT XXX */
35#if 0
cfa460ad 36#include <linux/module.h>
932394ac
WD
37#include <linux/delay.h>
38#include <linux/errno.h>
cfa460ad 39#include <linux/err.h>
932394ac
WD
40#include <linux/sched.h>
41#include <linux/slab.h>
42#include <linux/types.h>
43#include <linux/mtd/mtd.h>
44#include <linux/mtd/nand.h>
45#include <linux/mtd/nand_ecc.h>
46#include <linux/mtd/compatmac.h>
47#include <linux/interrupt.h>
48#include <linux/bitops.h>
cfa460ad 49#include <linux/leds.h>
932394ac
WD
50#include <asm/io.h>
51
52#ifdef CONFIG_MTD_PARTITIONS
53#include <linux/mtd/partitions.h>
54#endif
55
ac7eb8a3 56#endif
932394ac
WD
57
58#include <common.h>
addb2e16 59
cfa460ad
WJ
60#define ENOTSUPP 524 /* Operation is not supported */
61
932394ac
WD
62#include <malloc.h>
63#include <watchdog.h>
cfa460ad 64#include <linux/err.h>
932394ac
WD
65#include <linux/mtd/compat.h>
66#include <linux/mtd/mtd.h>
67#include <linux/mtd/nand.h>
68#include <linux/mtd/nand_ecc.h>
69
70#include <asm/io.h>
71#include <asm/errno.h>
72
73#ifdef CONFIG_JFFS2_NAND
74#include <jffs2/jffs2.h>
75#endif
76
932394ac 77/* Define default oob placement schemes for large and small page devices */
cfa460ad 78static struct nand_ecclayout nand_oob_8 = {
932394ac
WD
79 .eccbytes = 3,
80 .eccpos = {0, 1, 2},
cfa460ad
WJ
81 .oobfree = {
82 {.offset = 3,
83 .length = 2},
84 {.offset = 6,
85 .length = 2}}
932394ac
WD
86};
87
cfa460ad 88static struct nand_ecclayout nand_oob_16 = {
932394ac
WD
89 .eccbytes = 6,
90 .eccpos = {0, 1, 2, 3, 6, 7},
cfa460ad
WJ
91 .oobfree = {
92 {.offset = 8,
93 . length = 8}}
932394ac
WD
94};
95
cfa460ad 96static struct nand_ecclayout nand_oob_64 = {
932394ac
WD
97 .eccbytes = 24,
98 .eccpos = {
cfa460ad
WJ
99 40, 41, 42, 43, 44, 45, 46, 47,
100 48, 49, 50, 51, 52, 53, 54, 55,
101 56, 57, 58, 59, 60, 61, 62, 63},
102 .oobfree = {
103 {.offset = 2,
104 .length = 38}}
932394ac
WD
105};
106
cfa460ad 107static struct nand_ecclayout nand_oob_128 = {
248ae5cf
SP
108 .eccbytes = 48,
109 .eccpos = {
cfa460ad
WJ
110 80, 81, 82, 83, 84, 85, 86, 87,
111 88, 89, 90, 91, 92, 93, 94, 95,
112 96, 97, 98, 99, 100, 101, 102, 103,
113 104, 105, 106, 107, 108, 109, 110, 111,
114 112, 113, 114, 115, 116, 117, 118, 119,
115 120, 121, 122, 123, 124, 125, 126, 127},
116 .oobfree = {
117 {.offset = 2,
118 .length = 78}}
932394ac
WD
119};
120
cfa460ad
WJ
121
122static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
123 int new_state);
124
125static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
126 struct mtd_oob_ops *ops);
127
128static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
248ae5cf 129
932394ac 130/*
cfa460ad
WJ
131 * For devices which display every fart in the system on a seperate LED. Is
132 * compiled away when LED support is disabled.
932394ac 133 */
932394ac
WD
134/* XXX U-BOOT XXX */
135#if 0
cfa460ad 136DEFINE_LED_TRIGGER(nand_led_trigger);
932394ac 137#endif
ac7eb8a3 138
932394ac
WD
139/**
140 * nand_release_device - [GENERIC] release chip
141 * @mtd: MTD device structure
ac7eb8a3
WD
142 *
143 * Deselect, release chip lock and wake up anyone waiting on the device
932394ac
WD
144 */
145/* XXX U-BOOT XXX */
146#if 0
cfa460ad 147static void nand_release_device(struct mtd_info *mtd)
932394ac 148{
cfa460ad 149 struct nand_chip *chip = mtd->priv;
932394ac
WD
150
151 /* De-select the NAND device */
cfa460ad
WJ
152 chip->select_chip(mtd, -1);
153
154 /* Release the controller and the chip */
155 spin_lock(&chip->controller->lock);
156 chip->controller->active = NULL;
157 chip->state = FL_READY;
158 wake_up(&chip->controller->wq);
159 spin_unlock(&chip->controller->lock);
932394ac
WD
160}
161#else
8e9655f8
WD
162static void nand_release_device (struct mtd_info *mtd)
163{
164 struct nand_chip *this = mtd->priv;
165 this->select_chip(mtd, -1); /* De-select the NAND device */
166}
932394ac
WD
167#endif
168
169/**
170 * nand_read_byte - [DEFAULT] read one byte from the chip
171 * @mtd: MTD device structure
172 *
173 * Default read function for 8bit buswith
174 */
cfa460ad 175static uint8_t nand_read_byte(struct mtd_info *mtd)
932394ac 176{
cfa460ad
WJ
177 struct nand_chip *chip = mtd->priv;
178 return readb(chip->IO_ADDR_R);
932394ac
WD
179}
180
181/**
182 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
183 * @mtd: MTD device structure
184 *
ac7eb8a3 185 * Default read function for 16bit buswith with
932394ac
WD
186 * endianess conversion
187 */
cfa460ad 188static uint8_t nand_read_byte16(struct mtd_info *mtd)
932394ac 189{
cfa460ad
WJ
190 struct nand_chip *chip = mtd->priv;
191 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
932394ac
WD
192}
193
194/**
195 * nand_read_word - [DEFAULT] read one word from the chip
196 * @mtd: MTD device structure
197 *
ac7eb8a3 198 * Default read function for 16bit buswith without
932394ac
WD
199 * endianess conversion
200 */
201static u16 nand_read_word(struct mtd_info *mtd)
202{
cfa460ad
WJ
203 struct nand_chip *chip = mtd->priv;
204 return readw(chip->IO_ADDR_R);
932394ac
WD
205}
206
207/**
208 * nand_select_chip - [DEFAULT] control CE line
209 * @mtd: MTD device structure
cfa460ad 210 * @chipnr: chipnumber to select, -1 for deselect
932394ac
WD
211 *
212 * Default select function for 1 chip devices.
213 */
cfa460ad 214static void nand_select_chip(struct mtd_info *mtd, int chipnr)
932394ac 215{
cfa460ad
WJ
216 struct nand_chip *chip = mtd->priv;
217
218 switch (chipnr) {
932394ac 219 case -1:
cfa460ad 220 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
932394ac
WD
221 break;
222 case 0:
932394ac
WD
223 break;
224
225 default:
226 BUG();
227 }
228}
229
230/**
231 * nand_write_buf - [DEFAULT] write buffer to chip
232 * @mtd: MTD device structure
233 * @buf: data buffer
234 * @len: number of bytes to write
235 *
236 * Default write function for 8bit buswith
237 */
cfa460ad 238static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
932394ac
WD
239{
240 int i;
cfa460ad 241 struct nand_chip *chip = mtd->priv;
932394ac 242
cfa460ad
WJ
243 for (i = 0; i < len; i++)
244 writeb(buf[i], chip->IO_ADDR_W);
932394ac
WD
245}
246
247/**
ac7eb8a3 248 * nand_read_buf - [DEFAULT] read chip data into buffer
932394ac
WD
249 * @mtd: MTD device structure
250 * @buf: buffer to store date
251 * @len: number of bytes to read
252 *
253 * Default read function for 8bit buswith
254 */
cfa460ad 255static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
932394ac
WD
256{
257 int i;
cfa460ad 258 struct nand_chip *chip = mtd->priv;
932394ac 259
cfa460ad
WJ
260 for (i = 0; i < len; i++)
261 buf[i] = readb(chip->IO_ADDR_R);
932394ac
WD
262}
263
264/**
ac7eb8a3 265 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
932394ac
WD
266 * @mtd: MTD device structure
267 * @buf: buffer containing the data to compare
268 * @len: number of bytes to compare
269 *
270 * Default verify function for 8bit buswith
271 */
cfa460ad 272static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
932394ac
WD
273{
274 int i;
cfa460ad 275 struct nand_chip *chip = mtd->priv;
932394ac 276
cfa460ad
WJ
277 for (i = 0; i < len; i++)
278 if (buf[i] != readb(chip->IO_ADDR_R))
932394ac 279 return -EFAULT;
932394ac
WD
280 return 0;
281}
282
283/**
284 * nand_write_buf16 - [DEFAULT] write buffer to chip
285 * @mtd: MTD device structure
286 * @buf: data buffer
287 * @len: number of bytes to write
288 *
289 * Default write function for 16bit buswith
290 */
cfa460ad 291static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
932394ac
WD
292{
293 int i;
cfa460ad 294 struct nand_chip *chip = mtd->priv;
932394ac
WD
295 u16 *p = (u16 *) buf;
296 len >>= 1;
ac7eb8a3 297
cfa460ad
WJ
298 for (i = 0; i < len; i++)
299 writew(p[i], chip->IO_ADDR_W);
ac7eb8a3 300
932394ac
WD
301}
302
303/**
ac7eb8a3 304 * nand_read_buf16 - [DEFAULT] read chip data into buffer
932394ac
WD
305 * @mtd: MTD device structure
306 * @buf: buffer to store date
307 * @len: number of bytes to read
308 *
309 * Default read function for 16bit buswith
310 */
cfa460ad 311static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
932394ac
WD
312{
313 int i;
cfa460ad 314 struct nand_chip *chip = mtd->priv;
932394ac
WD
315 u16 *p = (u16 *) buf;
316 len >>= 1;
317
cfa460ad
WJ
318 for (i = 0; i < len; i++)
319 p[i] = readw(chip->IO_ADDR_R);
932394ac
WD
320}
321
322/**
ac7eb8a3 323 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
932394ac
WD
324 * @mtd: MTD device structure
325 * @buf: buffer containing the data to compare
326 * @len: number of bytes to compare
327 *
328 * Default verify function for 16bit buswith
329 */
cfa460ad 330static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
932394ac
WD
331{
332 int i;
cfa460ad 333 struct nand_chip *chip = mtd->priv;
932394ac
WD
334 u16 *p = (u16 *) buf;
335 len >>= 1;
336
cfa460ad
WJ
337 for (i = 0; i < len; i++)
338 if (p[i] != readw(chip->IO_ADDR_R))
932394ac
WD
339 return -EFAULT;
340
341 return 0;
342}
343
344/**
345 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
346 * @mtd: MTD device structure
347 * @ofs: offset from device start
348 * @getchip: 0, if the chip is already selected
349 *
ac7eb8a3 350 * Check, if the block is bad.
932394ac
WD
351 */
352static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
353{
354 int page, chipnr, res = 0;
cfa460ad 355 struct nand_chip *chip = mtd->priv;
932394ac
WD
356 u16 bad;
357
cfa460ad 358 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
a7988659 359
932394ac 360 if (getchip) {
cfa460ad 361 chipnr = (int)(ofs >> chip->chip_shift);
932394ac 362
cfa460ad 363 nand_get_device(chip, mtd, FL_READING);
932394ac
WD
364
365 /* Select the NAND device */
cfa460ad 366 chip->select_chip(mtd, chipnr);
a7988659 367 }
932394ac 368
cfa460ad
WJ
369 if (chip->options & NAND_BUSWIDTH_16) {
370 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
371 page);
372 bad = cpu_to_le16(chip->read_word(mtd));
373 if (chip->badblockpos & 0x1)
374 bad >>= 8;
932394ac
WD
375 if ((bad & 0xFF) != 0xff)
376 res = 1;
377 } else {
cfa460ad
WJ
378 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
379 if (chip->read_byte(mtd) != 0xff)
932394ac
WD
380 res = 1;
381 }
ac7eb8a3 382
cfa460ad 383 if (getchip)
932394ac 384 nand_release_device(mtd);
ac7eb8a3 385
932394ac
WD
386 return res;
387}
388
389/**
390 * nand_default_block_markbad - [DEFAULT] mark a block bad
391 * @mtd: MTD device structure
392 * @ofs: offset from device start
393 *
394 * This is the default implementation, which can be overridden by
395 * a hardware specific driver.
396*/
397static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
398{
cfa460ad
WJ
399 struct nand_chip *chip = mtd->priv;
400 uint8_t buf[2] = { 0, 0 };
401 int block, ret;
ac7eb8a3 402
932394ac 403 /* Get block number */
cfa460ad
WJ
404 block = (int)(ofs >> chip->bbt_erase_shift);
405 if (chip->bbt)
406 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
932394ac
WD
407
408 /* Do we have a flash based bad block table ? */
cfa460ad
WJ
409 if (chip->options & NAND_USE_FLASH_BBT)
410 ret = nand_update_bbt(mtd, ofs);
411 else {
412 /* We write two bytes, so we dont have to mess with 16 bit
413 * access
414 */
415 ofs += mtd->oobsize;
416 chip->ops.len = chip->ops.ooblen = 2;
417 chip->ops.datbuf = NULL;
418 chip->ops.oobbuf = buf;
419 chip->ops.ooboffs = chip->badblockpos & ~0x01;
ac7eb8a3 420
cfa460ad
WJ
421 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
422 }
423 if (!ret)
424 mtd->ecc_stats.badblocks++;
425 return ret;
932394ac
WD
426}
427
ac7eb8a3 428/**
932394ac
WD
429 * nand_check_wp - [GENERIC] check if the chip is write protected
430 * @mtd: MTD device structure
ac7eb8a3 431 * Check, if the device is write protected
932394ac 432 *
ac7eb8a3 433 * The function expects, that the device is already selected
932394ac 434 */
cfa460ad 435static int nand_check_wp(struct mtd_info *mtd)
932394ac 436{
cfa460ad 437 struct nand_chip *chip = mtd->priv;
932394ac 438 /* Check the WP bit */
cfa460ad
WJ
439 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
440 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
932394ac 441}