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