]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/mtd/nand/nand_base.c
[MTD NAND] Export nand_wait_ready() for use by board drivers
[mirror_ubuntu-zesty-kernel.git] / drivers / mtd / nand / nand_base.c
CommitLineData
1da177e4
LT
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.
61b03bd7 8 *
1da177e4
LT
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
61b03bd7 11 *
1da177e4 12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
ace4dfee 13 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
1da177e4 14 *
ace4dfee 15 * Credits:
61b03bd7
TG
16 * David Woodhouse for adding multichip support
17 *
1da177e4
LT
18 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
ace4dfee 21 * TODO:
1da177e4
LT
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 *
1da177e4
LT
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
552d9205 34#include <linux/module.h>
1da177e4
LT
35#include <linux/delay.h>
36#include <linux/errno.h>
7aa65bfd 37#include <linux/err.h>
1da177e4
LT
38#include <linux/sched.h>
39#include <linux/slab.h>
40#include <linux/types.h>
41#include <linux/mtd/mtd.h>
42#include <linux/mtd/nand.h>
43#include <linux/mtd/nand_ecc.h>
44#include <linux/mtd/compatmac.h>
45#include <linux/interrupt.h>
46#include <linux/bitops.h>
8fe833c1 47#include <linux/leds.h>
1da177e4
LT
48#include <asm/io.h>
49
50#ifdef CONFIG_MTD_PARTITIONS
51#include <linux/mtd/partitions.h>
52#endif
53
54/* Define default oob placement schemes for large and small page devices */
5bd34c09 55static struct nand_ecclayout nand_oob_8 = {
1da177e4
LT
56 .eccbytes = 3,
57 .eccpos = {0, 1, 2},
5bd34c09
TG
58 .oobfree = {
59 {.offset = 3,
60 .length = 2},
61 {.offset = 6,
62 .length = 2}}
1da177e4
LT
63};
64
5bd34c09 65static struct nand_ecclayout nand_oob_16 = {
1da177e4
LT
66 .eccbytes = 6,
67 .eccpos = {0, 1, 2, 3, 6, 7},
5bd34c09
TG
68 .oobfree = {
69 {.offset = 8,
70 . length = 8}}
1da177e4
LT
71};
72
5bd34c09 73static struct nand_ecclayout nand_oob_64 = {
1da177e4
LT
74 .eccbytes = 24,
75 .eccpos = {
e0c7d767
DW
76 40, 41, 42, 43, 44, 45, 46, 47,
77 48, 49, 50, 51, 52, 53, 54, 55,
78 56, 57, 58, 59, 60, 61, 62, 63},
5bd34c09
TG
79 .oobfree = {
80 {.offset = 2,
81 .length = 38}}
1da177e4
LT
82};
83
ace4dfee 84static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
2c0a2bed 85 int new_state);
1da177e4 86
8593fbc6
TG
87static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
88 struct mtd_oob_ops *ops);
89
d470a97c
TG
90/*
91 * For devices which display every fart in the system on a seperate LED. Is
92 * compiled away when LED support is disabled.
93 */
94DEFINE_LED_TRIGGER(nand_led_trigger);
95
1da177e4
LT
96/**
97 * nand_release_device - [GENERIC] release chip
98 * @mtd: MTD device structure
61b03bd7
TG
99 *
100 * Deselect, release chip lock and wake up anyone waiting on the device
1da177e4 101 */
e0c7d767 102static void nand_release_device(struct mtd_info *mtd)
1da177e4 103{
ace4dfee 104 struct nand_chip *chip = mtd->priv;
1da177e4
LT
105
106 /* De-select the NAND device */
ace4dfee 107 chip->select_chip(mtd, -1);
0dfc6246 108
a36ed299 109 /* Release the controller and the chip */
ace4dfee
TG
110 spin_lock(&chip->controller->lock);
111 chip->controller->active = NULL;
112 chip->state = FL_READY;
113 wake_up(&chip->controller->wq);
114 spin_unlock(&chip->controller->lock);
1da177e4
LT
115}
116
117/**
118 * nand_read_byte - [DEFAULT] read one byte from the chip
119 * @mtd: MTD device structure
120 *
121 * Default read function for 8bit buswith
122 */
58dd8f2b 123static uint8_t nand_read_byte(struct mtd_info *mtd)
1da177e4 124{
ace4dfee
TG
125 struct nand_chip *chip = mtd->priv;
126 return readb(chip->IO_ADDR_R);
1da177e4
LT
127}
128
1da177e4
LT
129/**
130 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
131 * @mtd: MTD device structure
132 *
61b03bd7 133 * Default read function for 16bit buswith with
1da177e4
LT
134 * endianess conversion
135 */
58dd8f2b 136static uint8_t nand_read_byte16(struct mtd_info *mtd)
1da177e4 137{
ace4dfee
TG
138 struct nand_chip *chip = mtd->priv;
139 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
1da177e4
LT
140}
141
1da177e4
LT
142/**
143 * nand_read_word - [DEFAULT] read one word from the chip
144 * @mtd: MTD device structure
145 *
61b03bd7 146 * Default read function for 16bit buswith without
1da177e4
LT
147 * endianess conversion
148 */
149static u16 nand_read_word(struct mtd_info *mtd)
150{
ace4dfee
TG
151 struct nand_chip *chip = mtd->priv;
152 return readw(chip->IO_ADDR_R);
1da177e4
LT
153}
154
1da177e4
LT
155/**
156 * nand_select_chip - [DEFAULT] control CE line
157 * @mtd: MTD device structure
844d3b42 158 * @chipnr: chipnumber to select, -1 for deselect
1da177e4
LT
159 *
160 * Default select function for 1 chip devices.
161 */
ace4dfee 162static void nand_select_chip(struct mtd_info *mtd, int chipnr)
1da177e4 163{
ace4dfee
TG
164 struct nand_chip *chip = mtd->priv;
165
166 switch (chipnr) {
1da177e4 167 case -1:
ace4dfee 168 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
1da177e4
LT
169 break;
170 case 0:
1da177e4
LT
171 break;
172
173 default:
174 BUG();
175 }
176}
177
178/**
179 * nand_write_buf - [DEFAULT] write buffer to chip
180 * @mtd: MTD device structure
181 * @buf: data buffer
182 * @len: number of bytes to write
183 *
184 * Default write function for 8bit buswith
185 */
58dd8f2b 186static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
1da177e4
LT
187{
188 int i;
ace4dfee 189 struct nand_chip *chip = mtd->priv;
1da177e4 190
e0c7d767 191 for (i = 0; i < len; i++)
ace4dfee 192 writeb(buf[i], chip->IO_ADDR_W);
1da177e4
LT
193}
194
195/**
61b03bd7 196 * nand_read_buf - [DEFAULT] read chip data into buffer
1da177e4
LT
197 * @mtd: MTD device structure
198 * @buf: buffer to store date
199 * @len: number of bytes to read
200 *
201 * Default read function for 8bit buswith
202 */
58dd8f2b 203static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1da177e4
LT
204{
205 int i;
ace4dfee 206 struct nand_chip *chip = mtd->priv;
1da177e4 207
e0c7d767 208 for (i = 0; i < len; i++)
ace4dfee 209 buf[i] = readb(chip->IO_ADDR_R);
1da177e4
LT
210}
211
212/**
61b03bd7 213 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
1da177e4
LT
214 * @mtd: MTD device structure
215 * @buf: buffer containing the data to compare
216 * @len: number of bytes to compare
217 *
218 * Default verify function for 8bit buswith
219 */
58dd8f2b 220static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
1da177e4
LT
221{
222 int i;
ace4dfee 223 struct nand_chip *chip = mtd->priv;
1da177e4 224
e0c7d767 225 for (i = 0; i < len; i++)
ace4dfee 226 if (buf[i] != readb(chip->IO_ADDR_R))
1da177e4 227 return -EFAULT;
1da177e4
LT
228 return 0;
229}
230
231/**
232 * nand_write_buf16 - [DEFAULT] write buffer to chip
233 * @mtd: MTD device structure
234 * @buf: data buffer
235 * @len: number of bytes to write
236 *
237 * Default write function for 16bit buswith
238 */
58dd8f2b 239static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
1da177e4
LT
240{
241 int i;
ace4dfee 242 struct nand_chip *chip = mtd->priv;
1da177e4
LT
243 u16 *p = (u16 *) buf;
244 len >>= 1;
61b03bd7 245
e0c7d767 246 for (i = 0; i < len; i++)
ace4dfee 247 writew(p[i], chip->IO_ADDR_W);
61b03bd7 248
1da177e4
LT
249}
250
251/**
61b03bd7 252 * nand_read_buf16 - [DEFAULT] read chip data into buffer
1da177e4
LT
253 * @mtd: MTD device structure
254 * @buf: buffer to store date
255 * @len: number of bytes to read
256 *
257 * Default read function for 16bit buswith
258 */
58dd8f2b 259static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
1da177e4
LT
260{
261 int i;
ace4dfee 262 struct nand_chip *chip = mtd->priv;
1da177e4
LT
263 u16 *p = (u16 *) buf;
264 len >>= 1;
265
e0c7d767 266 for (i = 0; i < len; i++)
ace4dfee 267 p[i] = readw(chip->IO_ADDR_R);
1da177e4
LT
268}
269
270/**
61b03bd7 271 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
1da177e4
LT
272 * @mtd: MTD device structure
273 * @buf: buffer containing the data to compare
274 * @len: number of bytes to compare
275 *
276 * Default verify function for 16bit buswith
277 */
58dd8f2b 278static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
1da177e4
LT
279{
280 int i;
ace4dfee 281 struct nand_chip *chip = mtd->priv;
1da177e4
LT
282 u16 *p = (u16 *) buf;
283 len >>= 1;
284
e0c7d767 285 for (i = 0; i < len; i++)
ace4dfee 286 if (p[i] != readw(chip->IO_ADDR_R))
1da177e4
LT
287 return -EFAULT;
288
289 return 0;
290}
291
292/**
293 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
294 * @mtd: MTD device structure
295 * @ofs: offset from device start
296 * @getchip: 0, if the chip is already selected
297 *
61b03bd7 298 * Check, if the block is bad.
1da177e4
LT
299 */
300static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
301{
302 int page, chipnr, res = 0;
ace4dfee 303 struct nand_chip *chip = mtd->priv;
1da177e4
LT
304 u16 bad;
305
306 if (getchip) {
ace4dfee
TG
307 page = (int)(ofs >> chip->page_shift);
308 chipnr = (int)(ofs >> chip->chip_shift);
1da177e4 309
ace4dfee 310 nand_get_device(chip, mtd, FL_READING);
1da177e4
LT
311
312 /* Select the NAND device */
ace4dfee 313 chip->select_chip(mtd, chipnr);
61b03bd7 314 } else
e0c7d767 315 page = (int)ofs;
1da177e4 316
ace4dfee
TG
317 if (chip->options & NAND_BUSWIDTH_16) {
318 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
319 page & chip->pagemask);
320 bad = cpu_to_le16(chip->read_word(mtd));
321 if (chip->badblockpos & 0x1)
49196f33 322 bad >>= 8;
1da177e4
LT
323 if ((bad & 0xFF) != 0xff)
324 res = 1;
325 } else {
ace4dfee
TG
326 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
327 page & chip->pagemask);
328 if (chip->read_byte(mtd) != 0xff)
1da177e4
LT
329 res = 1;
330 }
61b03bd7 331
ace4dfee 332 if (getchip)
1da177e4 333 nand_release_device(mtd);
61b03bd7 334
1da177e4
LT
335 return res;
336}
337
338/**
339 * nand_default_block_markbad - [DEFAULT] mark a block bad
340 * @mtd: MTD device structure
341 * @ofs: offset from device start
342 *
343 * This is the default implementation, which can be overridden by
344 * a hardware specific driver.
345*/
346static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
347{
ace4dfee 348 struct nand_chip *chip = mtd->priv;
58dd8f2b 349 uint8_t buf[2] = { 0, 0 };
f1a28c02 350 int block, ret;
61b03bd7 351
1da177e4 352 /* Get block number */
ace4dfee
TG
353 block = ((int)ofs) >> chip->bbt_erase_shift;
354 if (chip->bbt)
355 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1da177e4
LT
356
357 /* Do we have a flash based bad block table ? */
ace4dfee 358 if (chip->options & NAND_USE_FLASH_BBT)
f1a28c02
TG
359 ret = nand_update_bbt(mtd, ofs);
360 else {
361 /* We write two bytes, so we dont have to mess with 16 bit
362 * access
363 */
364 ofs += mtd->oobsize;
365 chip->ops.len = 2;
366 chip->ops.datbuf = NULL;
367 chip->ops.oobbuf = buf;
368 chip->ops.ooboffs = chip->badblockpos & ~0x01;
369
370 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
371 }
372 if (!ret)
373 mtd->ecc_stats.badblocks++;
374 return ret;
1da177e4
LT
375}
376
61b03bd7 377/**
1da177e4
LT
378 * nand_check_wp - [GENERIC] check if the chip is write protected
379 * @mtd: MTD device structure
61b03bd7 380 * Check, if the device is write protected
1da177e4 381 *
61b03bd7 382 * The function expects, that the device is already selected
1da177e4 383 */
e0c7d767 384static int nand_check_wp(struct mtd_info *mtd)
1da177e4 385{
ace4dfee 386 struct nand_chip *chip = mtd->priv;
1da177e4 387 /* Check the WP bit */
ace4dfee
TG
388 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
389 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
1da177e4
LT
390}
391
392/**
393 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
394 * @mtd: MTD device structure
395 * @ofs: offset from device start
396 * @getchip: 0, if the chip is already selected
397 * @allowbbt: 1, if its allowed to access the bbt area
398 *
399 * Check, if the block is bad. Either by reading the bad block table or
400 * calling of the scan function.
401 */
2c0a2bed
TG
402static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
403 int allowbbt)
1da177e4 404{
ace4dfee 405 struct nand_chip *chip = mtd->priv;
61b03bd7 406
ace4dfee
TG
407 if (!chip->bbt)
408 return chip->block_bad(mtd, ofs, getchip);
61b03bd7 409
1da177e4 410 /* Return info from the table */
e0c7d767 411 return nand_isbad_bbt(mtd, ofs, allowbbt);
1da177e4
LT
412}
413
61b03bd7 414/*
3b88775c
TG
415 * Wait for the ready pin, after a command
416 * The timeout is catched later.
417 */
4b648b02 418void nand_wait_ready(struct mtd_info *mtd)
3b88775c 419{
ace4dfee 420 struct nand_chip *chip = mtd->priv;
e0c7d767 421 unsigned long timeo = jiffies + 2;
3b88775c 422
8fe833c1 423 led_trigger_event(nand_led_trigger, LED_FULL);
3b88775c
TG
424 /* wait until command is processed or timeout occures */
425 do {
ace4dfee 426 if (chip->dev_ready(mtd))
8fe833c1 427 break;
8446f1d3 428 touch_softlockup_watchdog();
61b03bd7 429 } while (time_before(jiffies, timeo));
8fe833c1 430 led_trigger_event(nand_led_trigger, LED_OFF);
3b88775c 431}
4b648b02 432EXPORT_SYMBOL_GPL(nand_wait_ready);
3b88775c 433
1da177e4
LT
434/**
435 * nand_command - [DEFAULT] Send command to NAND device
436 * @mtd: MTD device structure
437 * @command: the command to be sent
438 * @column: the column address for this command, -1 if none
439 * @page_addr: the page address for this command, -1 if none
440 *
441 * Send command to NAND device. This function is used for small page
442 * devices (256/512 Bytes per page)
443 */
7abd3ef9
TG
444static void nand_command(struct mtd_info *mtd, unsigned int command,
445 int column, int page_addr)
1da177e4 446{
ace4dfee 447 register struct nand_chip *chip = mtd->priv;
7abd3ef9 448 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
1da177e4 449
1da177e4
LT
450 /*
451 * Write out the command to the device.
452 */
453 if (command == NAND_CMD_SEQIN) {
454 int readcmd;
455
28318776 456 if (column >= mtd->writesize) {
1da177e4 457 /* OOB area */
28318776 458 column -= mtd->writesize;
1da177e4
LT
459 readcmd = NAND_CMD_READOOB;
460 } else if (column < 256) {
461 /* First 256 bytes --> READ0 */
462 readcmd = NAND_CMD_READ0;
463 } else {
464 column -= 256;
465 readcmd = NAND_CMD_READ1;
466 }
ace4dfee 467 chip->cmd_ctrl(mtd, readcmd, ctrl);
7abd3ef9 468 ctrl &= ~NAND_CTRL_CHANGE;
1da177e4 469 }
ace4dfee 470 chip->cmd_ctrl(mtd, command, ctrl);
1da177e4 471
7abd3ef9
TG
472 /*
473 * Address cycle, when necessary
474 */
475 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
476 /* Serially input address */
477 if (column != -1) {
478 /* Adjust columns for 16 bit buswidth */
ace4dfee 479 if (chip->options & NAND_BUSWIDTH_16)
7abd3ef9 480 column >>= 1;
ace4dfee 481 chip->cmd_ctrl(mtd, column, ctrl);
7abd3ef9
TG
482 ctrl &= ~NAND_CTRL_CHANGE;
483 }
484 if (page_addr != -1) {
ace4dfee 485 chip->cmd_ctrl(mtd, page_addr, ctrl);
7abd3ef9 486 ctrl &= ~NAND_CTRL_CHANGE;
ace4dfee 487 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
7abd3ef9 488 /* One more address cycle for devices > 32MiB */
ace4dfee
TG
489 if (chip->chipsize > (32 << 20))
490 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
1da177e4 491 }
ace4dfee 492 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
61b03bd7
TG
493
494 /*
495 * program and erase have their own busy handlers
1da177e4 496 * status and sequential in needs no delay
e0c7d767 497 */
1da177e4 498 switch (command) {
61b03bd7 499
1da177e4
LT
500 case NAND_CMD_PAGEPROG:
501 case NAND_CMD_ERASE1:
502 case NAND_CMD_ERASE2:
503 case NAND_CMD_SEQIN:
504 case NAND_CMD_STATUS:
505 return;
506
507 case NAND_CMD_RESET:
ace4dfee 508 if (chip->dev_ready)
1da177e4 509 break;
ace4dfee
TG
510 udelay(chip->chip_delay);
511 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
7abd3ef9 512 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
12efdde3
TG
513 chip->cmd_ctrl(mtd,
514 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
ace4dfee 515 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
1da177e4
LT
516 return;
517
e0c7d767 518 /* This applies to read commands */
1da177e4 519 default:
61b03bd7 520 /*
1da177e4
LT
521 * If we don't have access to the busy pin, we apply the given
522 * command delay
e0c7d767 523 */
ace4dfee
TG
524 if (!chip->dev_ready) {
525 udelay(chip->chip_delay);
1da177e4 526 return;
61b03bd7 527 }
1da177e4 528 }
1da177e4
LT
529 /* Apply this short delay always to ensure that we do wait tWB in
530 * any case on any machine. */
e0c7d767 531 ndelay(100);
3b88775c
TG
532
533 nand_wait_ready(mtd);
1da177e4
LT
534}
535
536/**
537 * nand_command_lp - [DEFAULT] Send command to NAND large page device
538 * @mtd: MTD device structure
539 * @command: the command to be sent
540 * @column: the column address for this command, -1 if none
541 * @page_addr: the page address for this command, -1 if none
542 *
7abd3ef9
TG
543 * Send command to NAND device. This is the version for the new large page
544 * devices We dont have the separate regions as we have in the small page
545 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
1da177e4 546 */
7abd3ef9
TG
547static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
548 int column, int page_addr)
1da177e4 549{
ace4dfee 550 register struct nand_chip *chip = mtd->priv;
1da177e4
LT
551
552 /* Emulate NAND_CMD_READOOB */
553 if (command == NAND_CMD_READOOB) {
28318776 554 column += mtd->writesize;
1da177e4
LT
555 command = NAND_CMD_READ0;
556 }
61b03bd7 557
7abd3ef9 558 /* Command latch cycle */
ace4dfee 559 chip->cmd_ctrl(mtd, command & 0xff,
7abd3ef9 560 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
1da177e4
LT
561
562 if (column != -1 || page_addr != -1) {
7abd3ef9 563 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
1da177e4
LT
564
565 /* Serially input address */
566 if (column != -1) {
567 /* Adjust columns for 16 bit buswidth */
ace4dfee 568 if (chip->options & NAND_BUSWIDTH_16)
1da177e4 569 column >>= 1;
ace4dfee 570 chip->cmd_ctrl(mtd, column, ctrl);
7abd3ef9 571 ctrl &= ~NAND_CTRL_CHANGE;
ace4dfee 572 chip->cmd_ctrl(mtd, column >> 8, ctrl);
61b03bd7 573 }
1da177e4 574 if (page_addr != -1) {
ace4dfee
TG
575 chip->cmd_ctrl(mtd, page_addr, ctrl);
576 chip->cmd_ctrl(mtd, page_addr >> 8,
7abd3ef9 577 NAND_NCE | NAND_ALE);
1da177e4 578 /* One more address cycle for devices > 128MiB */
ace4dfee
TG
579 if (chip->chipsize > (128 << 20))
580 chip->cmd_ctrl(mtd, page_addr >> 16,
7abd3ef9 581 NAND_NCE | NAND_ALE);
1da177e4 582 }
1da177e4 583 }
ace4dfee 584 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
61b03bd7
TG
585
586 /*
587 * program and erase have their own busy handlers
30f464b7
DM
588 * status, sequential in, and deplete1 need no delay
589 */
1da177e4 590 switch (command) {
61b03bd7 591
1da177e4
LT
592 case NAND_CMD_CACHEDPROG:
593 case NAND_CMD_PAGEPROG:
594 case NAND_CMD_ERASE1:
595 case NAND_CMD_ERASE2:
596 case NAND_CMD_SEQIN:
7bc3312b 597 case NAND_CMD_RNDIN:
1da177e4 598 case NAND_CMD_STATUS:
30f464b7 599 case NAND_CMD_DEPLETE1:
1da177e4
LT
600 return;
601
e0c7d767
DW
602 /*
603 * read error status commands require only a short delay
604 */
30f464b7
DM
605 case NAND_CMD_STATUS_ERROR:
606 case NAND_CMD_STATUS_ERROR0:
607 case NAND_CMD_STATUS_ERROR1:
608 case NAND_CMD_STATUS_ERROR2:
609 case NAND_CMD_STATUS_ERROR3:
ace4dfee 610 udelay(chip->chip_delay);
30f464b7 611 return;
1da177e4
LT
612
613 case NAND_CMD_RESET:
ace4dfee 614 if (chip->dev_ready)
1da177e4 615 break;
ace4dfee 616 udelay(chip->chip_delay);
12efdde3
TG
617 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
618 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
619 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
620 NAND_NCE | NAND_CTRL_CHANGE);
ace4dfee 621 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
1da177e4
LT
622 return;
623
7bc3312b
TG
624 case NAND_CMD_RNDOUT:
625 /* No ready / busy check necessary */
626 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
627 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
628 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
629 NAND_NCE | NAND_CTRL_CHANGE);
630 return;
631
1da177e4 632 case NAND_CMD_READ0:
12efdde3
TG
633 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
634 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
635 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
636 NAND_NCE | NAND_CTRL_CHANGE);
61b03bd7 637
e0c7d767 638 /* This applies to read commands */
1da177e4 639 default:
61b03bd7 640 /*
1da177e4
LT
641 * If we don't have access to the busy pin, we apply the given
642 * command delay
e0c7d767 643 */
ace4dfee
TG
644 if (!chip->dev_ready) {
645 udelay(chip->chip_delay);
1da177e4 646 return;
61b03bd7 647 }
1da177e4 648 }
3b88775c 649
1da177e4
LT
650 /* Apply this short delay always to ensure that we do wait tWB in
651 * any case on any machine. */
e0c7d767 652 ndelay(100);
3b88775c
TG
653
654 nand_wait_ready(mtd);
1da177e4
LT
655}
656
657/**
658 * nand_get_device - [GENERIC] Get chip for selected access
844d3b42 659 * @chip: the nand chip descriptor
1da177e4 660 * @mtd: MTD device structure
61b03bd7 661 * @new_state: the state which is requested
1da177e4
LT
662 *
663 * Get the device and lock it for exclusive access
664 */
2c0a2bed 665static int
ace4dfee 666nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
1da177e4 667{
ace4dfee
TG
668 spinlock_t *lock = &chip->controller->lock;
669 wait_queue_head_t *wq = &chip->controller->wq;
e0c7d767 670 DECLARE_WAITQUEUE(wait, current);
e0c7d767 671 retry:
0dfc6246
TG
672 spin_lock(lock);
673
1da177e4 674 /* Hardware controller shared among independend devices */
a36ed299 675 /* Hardware controller shared among independend devices */
ace4dfee
TG
676 if (!chip->controller->active)
677 chip->controller->active = chip;
a36ed299 678
ace4dfee
TG
679 if (chip->controller->active == chip && chip->state == FL_READY) {
680 chip->state = new_state;
0dfc6246 681 spin_unlock(lock);
962034f4
VW
682 return 0;
683 }
684 if (new_state == FL_PM_SUSPENDED) {
685 spin_unlock(lock);
ace4dfee 686 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
0dfc6246
TG
687 }
688 set_current_state(TASK_UNINTERRUPTIBLE);
689 add_wait_queue(wq, &wait);
690 spin_unlock(lock);
691 schedule();
692 remove_wait_queue(wq, &wait);
1da177e4
LT
693 goto retry;
694}
695
696/**
697 * nand_wait - [DEFAULT] wait until the command is done
698 * @mtd: MTD device structure
844d3b42 699 * @chip: NAND chip structure
1da177e4
LT
700 *
701 * Wait for command done. This applies to erase and program only
61b03bd7 702 * Erase can take up to 400ms and program up to 20ms according to
1da177e4 703 * general NAND and SmartMedia specs
844d3b42 704 */
7bc3312b 705static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
1da177e4
LT
706{
707
e0c7d767 708 unsigned long timeo = jiffies;
7bc3312b 709 int status, state = chip->state;
61b03bd7 710
1da177e4 711 if (state == FL_ERASING)
e0c7d767 712 timeo += (HZ * 400) / 1000;
1da177e4 713 else
e0c7d767 714 timeo += (HZ * 20) / 1000;
1da177e4 715
8fe833c1
RP
716 led_trigger_event(nand_led_trigger, LED_FULL);
717
1da177e4
LT
718 /* Apply this short delay always to ensure that we do wait tWB in
719 * any case on any machine. */
e0c7d767 720 ndelay(100);
1da177e4 721
ace4dfee
TG
722 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
723 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
61b03bd7 724 else
ace4dfee 725 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1da177e4 726
61b03bd7 727 while (time_before(jiffies, timeo)) {
ace4dfee
TG
728 if (chip->dev_ready) {
729 if (chip->dev_ready(mtd))
61b03bd7 730 break;
1da177e4 731 } else {
ace4dfee 732 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1da177e4
LT
733 break;
734 }
20a6c211 735 cond_resched();
1da177e4 736 }
8fe833c1
RP
737 led_trigger_event(nand_led_trigger, LED_OFF);
738
ace4dfee 739 status = (int)chip->read_byte(mtd);
1da177e4
LT
740 return status;
741}
742
8593fbc6
TG
743/**
744 * nand_read_page_raw - [Intern] read raw page data without ecc
745 * @mtd: mtd info structure
746 * @chip: nand chip info structure
747 * @buf: buffer to store read data
748 */
749static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
750 uint8_t *buf)
751{
752 chip->read_buf(mtd, buf, mtd->writesize);
753 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
754 return 0;
755}
756
1da177e4 757/**
f5bbdacc
TG
758 * nand_read_page_swecc - {REPLACABLE] software ecc based page read function
759 * @mtd: mtd info structure
760 * @chip: nand chip info structure
761 * @buf: buffer to store read data
068e3c0a 762 */
f5bbdacc
TG
763static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
764 uint8_t *buf)
1da177e4 765{
f5bbdacc
TG
766 int i, eccsize = chip->ecc.size;
767 int eccbytes = chip->ecc.bytes;
768 int eccsteps = chip->ecc.steps;
769 uint8_t *p = buf;
f75e5097
TG
770 uint8_t *ecc_calc = chip->buffers.ecccalc;
771 uint8_t *ecc_code = chip->buffers.ecccode;
5bd34c09 772 int *eccpos = chip->ecc.layout->eccpos;
f5bbdacc 773
8593fbc6 774 nand_read_page_raw(mtd, chip, buf);
f5bbdacc
TG
775
776 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
777 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
778
779 for (i = 0; i < chip->ecc.total; i++)
f75e5097 780 ecc_code[i] = chip->oob_poi[eccpos[i]];
f5bbdacc
TG
781
782 eccsteps = chip->ecc.steps;
783 p = buf;
784
785 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
786 int stat;
787
788 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
789 if (stat == -1)
790 mtd->ecc_stats.failed++;
791 else
792 mtd->ecc_stats.corrected += stat;
793 }
794 return 0;
22c60f5f 795}
1da177e4 796
068e3c0a 797/**
f5bbdacc
TG
798 * nand_read_page_hwecc - {REPLACABLE] hardware ecc based page read function
799 * @mtd: mtd info structure
800 * @chip: nand chip info structure
801 * @buf: buffer to store read data
068e3c0a 802 *
f5bbdacc 803 * Not for syndrome calculating ecc controllers which need a special oob layout
068e3c0a 804 */
f5bbdacc
TG
805static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
806 uint8_t *buf)
1da177e4 807{
f5bbdacc
TG
808 int i, eccsize = chip->ecc.size;
809 int eccbytes = chip->ecc.bytes;
810 int eccsteps = chip->ecc.steps;
811 uint8_t *p = buf;
f75e5097
TG
812 uint8_t *ecc_calc = chip->buffers.ecccalc;
813 uint8_t *ecc_code = chip->buffers.ecccode;
5bd34c09 814 int *eccpos = chip->ecc.layout->eccpos;
f5bbdacc
TG
815
816 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
817 chip->ecc.hwctl(mtd, NAND_ECC_READ);
818 chip->read_buf(mtd, p, eccsize);
819 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1da177e4 820 }
f75e5097 821 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1da177e4 822
f5bbdacc 823 for (i = 0; i < chip->ecc.total; i++)
f75e5097 824 ecc_code[i] = chip->oob_poi[eccpos[i]];
1da177e4 825
f5bbdacc
TG
826 eccsteps = chip->ecc.steps;
827 p = buf;
61b03bd7 828
f5bbdacc
TG
829 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
830 int stat;
1da177e4 831
f5bbdacc
TG
832 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
833 if (stat == -1)
834 mtd->ecc_stats.failed++;
835 else
836 mtd->ecc_stats.corrected += stat;
837 }
838 return 0;
839}
1da177e4 840
f5bbdacc
TG
841/**
842 * nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
843 * @mtd: mtd info structure
844 * @chip: nand chip info structure
845 * @buf: buffer to store read data
846 *
847 * The hw generator calculates the error syndrome automatically. Therefor
f75e5097 848 * we need a special oob layout and handling.
f5bbdacc
TG
849 */
850static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
851 uint8_t *buf)
852{
853 int i, eccsize = chip->ecc.size;
854 int eccbytes = chip->ecc.bytes;
855 int eccsteps = chip->ecc.steps;
856 uint8_t *p = buf;
f75e5097 857 uint8_t *oob = chip->oob_poi;
1da177e4 858
f5bbdacc
TG
859 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
860 int stat;
61b03bd7 861
f5bbdacc
TG
862 chip->ecc.hwctl(mtd, NAND_ECC_READ);
863 chip->read_buf(mtd, p, eccsize);
1da177e4 864
f5bbdacc
TG
865 if (chip->ecc.prepad) {
866 chip->read_buf(mtd, oob, chip->ecc.prepad);
867 oob += chip->ecc.prepad;
868 }
1da177e4 869
f5bbdacc
TG
870 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
871 chip->read_buf(mtd, oob, eccbytes);
872 stat = chip->ecc.correct(mtd, p, oob, NULL);
61b03bd7 873
f5bbdacc
TG
874 if (stat == -1)
875 mtd->ecc_stats.failed++;
61b03bd7 876 else
f5bbdacc 877 mtd->ecc_stats.corrected += stat;
61b03bd7 878
f5bbdacc 879 oob += eccbytes;
1da177e4 880
f5bbdacc
TG
881 if (chip->ecc.postpad) {
882 chip->read_buf(mtd, oob, chip->ecc.postpad);
883 oob += chip->ecc.postpad;
61b03bd7 884 }
f5bbdacc 885 }
1da177e4 886
f5bbdacc 887 /* Calculate remaining oob bytes */
7e4178f9 888 i = mtd->oobsize - (oob - chip->oob_poi);
f5bbdacc
TG
889 if (i)
890 chip->read_buf(mtd, oob, i);
61b03bd7 891
f5bbdacc
TG
892 return 0;
893}
1da177e4 894
f5bbdacc 895/**
8593fbc6
TG
896 * nand_transfer_oob - [Internal] Transfer oob to client buffer
897 * @chip: nand chip structure
844d3b42 898 * @oob: oob destination address
8593fbc6
TG
899 * @ops: oob ops structure
900 */
901static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
902 struct mtd_oob_ops *ops)
903{
904 size_t len = ops->ooblen;
905
906 switch(ops->mode) {
907
908 case MTD_OOB_PLACE:
909 case MTD_OOB_RAW:
910 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
911 return oob + len;
912
913 case MTD_OOB_AUTO: {
914 struct nand_oobfree *free = chip->ecc.layout->oobfree;
7bc3312b
TG
915 uint32_t boffs = 0, roffs = ops->ooboffs;
916 size_t bytes = 0;
8593fbc6
TG
917
918 for(; free->length && len; free++, len -= bytes) {
7bc3312b
TG
919 /* Read request not from offset 0 ? */
920 if (unlikely(roffs)) {
921 if (roffs >= free->length) {
922 roffs -= free->length;
923 continue;
924 }
925 boffs = free->offset + roffs;
926 bytes = min_t(size_t, len,
927 (free->length - roffs));
928 roffs = 0;
929 } else {
930 bytes = min_t(size_t, len, free->length);
931 boffs = free->offset;
932 }
933 memcpy(oob, chip->oob_poi + boffs, bytes);
8593fbc6
TG
934 oob += bytes;
935 }
936 return oob;
937 }
938 default:
939 BUG();
940 }
941 return NULL;
942}
943
944/**
945 * nand_do_read_ops - [Internal] Read data with ECC
f5bbdacc
TG
946 *
947 * @mtd: MTD device structure
948 * @from: offset to read from
844d3b42 949 * @ops: oob ops structure
f5bbdacc
TG
950 *
951 * Internal function. Called with chip held.
952 */
8593fbc6
TG
953static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
954 struct mtd_oob_ops *ops)
f5bbdacc
TG
955{
956 int chipnr, page, realpage, col, bytes, aligned;
957 struct nand_chip *chip = mtd->priv;
958 struct mtd_ecc_stats stats;
959 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
960 int sndcmd = 1;
961 int ret = 0;
8593fbc6
TG
962 uint32_t readlen = ops->len;
963 uint8_t *bufpoi, *oob, *buf;
1da177e4 964
f5bbdacc 965 stats = mtd->ecc_stats;
1da177e4 966
f5bbdacc
TG
967 chipnr = (int)(from >> chip->chip_shift);
968 chip->select_chip(mtd, chipnr);
61b03bd7 969
f5bbdacc
TG
970 realpage = (int)(from >> chip->page_shift);
971 page = realpage & chip->pagemask;
1da177e4 972
f5bbdacc 973 col = (int)(from & (mtd->writesize - 1));
f75e5097 974 chip->oob_poi = chip->buffers.oobrbuf;
61b03bd7 975
8593fbc6
TG
976 buf = ops->datbuf;
977 oob = ops->oobbuf;
978
f5bbdacc
TG
979 while(1) {
980 bytes = min(mtd->writesize - col, readlen);
981 aligned = (bytes == mtd->writesize);
61b03bd7 982
f5bbdacc 983 /* Is the current page in the buffer ? */
8593fbc6 984 if (realpage != chip->pagebuf || oob) {
f75e5097 985 bufpoi = aligned ? buf : chip->buffers.databuf;
61b03bd7 986
f5bbdacc
TG
987 if (likely(sndcmd)) {
988 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
989 sndcmd = 0;
1da177e4 990 }
1da177e4 991
f5bbdacc
TG
992 /* Now read the page into the buffer */
993 ret = chip->ecc.read_page(mtd, chip, bufpoi);
994 if (ret < 0)
1da177e4 995 break;
f5bbdacc
TG
996
997 /* Transfer not aligned data */
998 if (!aligned) {
999 chip->pagebuf = realpage;
f75e5097 1000 memcpy(buf, chip->buffers.databuf + col, bytes);
f5bbdacc
TG
1001 }
1002
8593fbc6
TG
1003 buf += bytes;
1004
1005 if (unlikely(oob)) {
1006 /* Raw mode does data:oob:data:oob */
1007 if (ops->mode != MTD_OOB_RAW)
1008 oob = nand_transfer_oob(chip, oob, ops);
1009 else
1010 buf = nand_transfer_oob(chip, buf, ops);
1011 }
1012
f5bbdacc
TG
1013 if (!(chip->options & NAND_NO_READRDY)) {
1014 /*
1015 * Apply delay or wait for ready/busy pin. Do
1016 * this before the AUTOINCR check, so no
1017 * problems arise if a chip which does auto
1018 * increment is marked as NOAUTOINCR by the
1019 * board driver.
1020 */
1021 if (!chip->dev_ready)
1022 udelay(chip->chip_delay);
1023 else
1024 nand_wait_ready(mtd);
1da177e4 1025 }
8593fbc6 1026 } else {
f75e5097 1027 memcpy(buf, chip->buffers.databuf + col, bytes);
8593fbc6
TG
1028 buf += bytes;
1029 }
1da177e4 1030
f5bbdacc 1031 readlen -= bytes;
61b03bd7 1032
f5bbdacc 1033 if (!readlen)
61b03bd7 1034 break;
1da177e4
LT
1035
1036 /* For subsequent reads align to page boundary. */
1037 col = 0;
1038 /* Increment page address */
1039 realpage++;
1040
ace4dfee 1041 page = realpage & chip->pagemask;
1da177e4
LT
1042 /* Check, if we cross a chip boundary */
1043 if (!page) {
1044 chipnr++;
ace4dfee
TG
1045 chip->select_chip(mtd, -1);
1046 chip->select_chip(mtd, chipnr);
1da177e4 1047 }
f5bbdacc 1048
61b03bd7
TG
1049 /* Check, if the chip supports auto page increment
1050 * or if we have hit a block boundary.
e0c7d767 1051 */
f5bbdacc 1052 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
61b03bd7 1053 sndcmd = 1;
1da177e4
LT
1054 }
1055
8593fbc6 1056 ops->retlen = ops->len - (size_t) readlen;
1da177e4 1057
f5bbdacc
TG
1058 if (ret)
1059 return ret;
1060
9a1fcdfd
TG
1061 if (mtd->ecc_stats.failed - stats.failed)
1062 return -EBADMSG;
1063
1064 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
f5bbdacc
TG
1065}
1066
1067/**
1068 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1069 * @mtd: MTD device structure
1070 * @from: offset to read from
1071 * @len: number of bytes to read
1072 * @retlen: pointer to variable to store the number of read bytes
1073 * @buf: the databuffer to put data
1074 *
1075 * Get hold of the chip and call nand_do_read
1076 */
1077static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1078 size_t *retlen, uint8_t *buf)
1079{
8593fbc6 1080 struct nand_chip *chip = mtd->priv;
f5bbdacc
TG
1081 int ret;
1082
f5bbdacc
TG
1083 /* Do not allow reads past end of device */
1084 if ((from + len) > mtd->size)
1085 return -EINVAL;
1086 if (!len)
1087 return 0;
1088
8593fbc6 1089 nand_get_device(chip, mtd, FL_READING);
f5bbdacc 1090
8593fbc6
TG
1091 chip->ops.len = len;
1092 chip->ops.datbuf = buf;
1093 chip->ops.oobbuf = NULL;
1094
1095 ret = nand_do_read_ops(mtd, from, &chip->ops);
f5bbdacc 1096
7fd5aecc
RP
1097 *retlen = chip->ops.retlen;
1098
f5bbdacc
TG
1099 nand_release_device(mtd);
1100
1101 return ret;
1da177e4
LT
1102}
1103
7bc3312b
TG
1104/**
1105 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1106 * @mtd: mtd info structure
1107 * @chip: nand chip info structure
1108 * @page: page number to read
1109 * @sndcmd: flag whether to issue read command or not
1110 */
1111static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1112 int page, int sndcmd)
1113{
1114 if (sndcmd) {
1115 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1116 sndcmd = 0;
1117 }
1118 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1119 return sndcmd;
1120}
1121
1122/**
1123 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1124 * with syndromes
1125 * @mtd: mtd info structure
1126 * @chip: nand chip info structure
1127 * @page: page number to read
1128 * @sndcmd: flag whether to issue read command or not
1129 */
1130static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1131 int page, int sndcmd)
1132{
1133 uint8_t *buf = chip->oob_poi;
1134 int length = mtd->oobsize;
1135 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1136 int eccsize = chip->ecc.size;
1137 uint8_t *bufpoi = buf;
1138 int i, toread, sndrnd = 0, pos;
1139
1140 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1141 for (i = 0; i < chip->ecc.steps; i++) {
1142 if (sndrnd) {
1143 pos = eccsize + i * (eccsize + chunk);
1144 if (mtd->writesize > 512)
1145 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1146 else
1147 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1148 } else
1149 sndrnd = 1;
1150 toread = min_t(int, length, chunk);
1151 chip->read_buf(mtd, bufpoi, toread);
1152 bufpoi += toread;
1153 length -= toread;
1154 }
1155 if (length > 0)
1156 chip->read_buf(mtd, bufpoi, length);
1157
1158 return 1;
1159}
1160
1161/**
1162 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1163 * @mtd: mtd info structure
1164 * @chip: nand chip info structure
1165 * @page: page number to write
1166 */
1167static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1168 int page)
1169{
1170 int status = 0;
1171 const uint8_t *buf = chip->oob_poi;
1172 int length = mtd->oobsize;
1173
1174 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1175 chip->write_buf(mtd, buf, length);
1176 /* Send command to program the OOB data */
1177 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1178
1179 status = chip->waitfunc(mtd, chip);
1180
0d420f9d 1181 return status & NAND_STATUS_FAIL ? -EIO : 0;
7bc3312b
TG
1182}
1183
1184/**
1185 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1186 * with syndrome - only for large page flash !
1187 * @mtd: mtd info structure
1188 * @chip: nand chip info structure
1189 * @page: page number to write
1190 */
1191static int nand_write_oob_syndrome(struct mtd_info *mtd,
1192 struct nand_chip *chip, int page)
1193{
1194 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1195 int eccsize = chip->ecc.size, length = mtd->oobsize;
1196 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1197 const uint8_t *bufpoi = chip->oob_poi;
1198
1199 /*
1200 * data-ecc-data-ecc ... ecc-oob
1201 * or
1202 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1203 */
1204 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1205 pos = steps * (eccsize + chunk);
1206 steps = 0;
1207 } else
8b0036ee 1208 pos = eccsize;
7bc3312b
TG
1209
1210 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1211 for (i = 0; i < steps; i++) {
1212 if (sndcmd) {
1213 if (mtd->writesize <= 512) {
1214 uint32_t fill = 0xFFFFFFFF;
1215
1216 len = eccsize;
1217 while (len > 0) {
1218 int num = min_t(int, len, 4);
1219 chip->write_buf(mtd, (uint8_t *)&fill,
1220 num);
1221 len -= num;
1222 }
1223 } else {
1224 pos = eccsize + i * (eccsize + chunk);
1225 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1226 }
1227 } else
1228 sndcmd = 1;
1229 len = min_t(int, length, chunk);
1230 chip->write_buf(mtd, bufpoi, len);
1231 bufpoi += len;
1232 length -= len;
1233 }
1234 if (length > 0)
1235 chip->write_buf(mtd, bufpoi, length);
1236
1237 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1238 status = chip->waitfunc(mtd, chip);
1239
1240 return status & NAND_STATUS_FAIL ? -EIO : 0;
1241}
1242
1da177e4 1243/**
8593fbc6 1244 * nand_do_read_oob - [Intern] NAND read out-of-band
1da177e4
LT
1245 * @mtd: MTD device structure
1246 * @from: offset to read from
8593fbc6 1247 * @ops: oob operations description structure
1da177e4
LT
1248 *
1249 * NAND read out-of-band data from the spare area
1250 */
8593fbc6
TG
1251static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1252 struct mtd_oob_ops *ops)
1da177e4 1253{
7bc3312b 1254 int page, realpage, chipnr, sndcmd = 1;
ace4dfee 1255 struct nand_chip *chip = mtd->priv;
7314e9e7 1256 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
7bc3312b
TG
1257 int readlen = ops->len;
1258 uint8_t *buf = ops->oobbuf;
61b03bd7 1259
7e9a0bb0
AM
1260 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1261 (unsigned long long)from, readlen);
1da177e4 1262
7314e9e7 1263 chipnr = (int)(from >> chip->chip_shift);
ace4dfee 1264 chip->select_chip(mtd, chipnr);
1da177e4 1265
7314e9e7
TG
1266 /* Shift to get page */
1267 realpage = (int)(from >> chip->page_shift);
1268 page = realpage & chip->pagemask;
1da177e4 1269
7bc3312b 1270 chip->oob_poi = chip->buffers.oobrbuf;
7314e9e7
TG
1271
1272 while(1) {
7bc3312b
TG
1273 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1274 buf = nand_transfer_oob(chip, buf, ops);
8593fbc6 1275
7314e9e7
TG
1276 if (!(chip->options & NAND_NO_READRDY)) {
1277 /*
1278 * Apply delay or wait for ready/busy pin. Do this
1279 * before the AUTOINCR check, so no problems arise if a
1280 * chip which does auto increment is marked as
1281 * NOAUTOINCR by the board driver.
19870da7 1282 */
ace4dfee
TG
1283 if (!chip->dev_ready)
1284 udelay(chip->chip_delay);
19870da7
TG
1285 else
1286 nand_wait_ready(mtd);
7314e9e7 1287 }
19870da7 1288
0d420f9d
SZ
1289 readlen -= ops->ooblen;
1290 if (!readlen)
1291 break;
1292
7314e9e7
TG
1293 /* Increment page address */
1294 realpage++;
1295
1296 page = realpage & chip->pagemask;
1297 /* Check, if we cross a chip boundary */
1298 if (!page) {
1299 chipnr++;
1300 chip->select_chip(mtd, -1);
1301 chip->select_chip(mtd, chipnr);
1da177e4 1302 }
7314e9e7
TG
1303
1304 /* Check, if the chip supports auto page increment
1305 * or if we have hit a block boundary.
1306 */
1307 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1308 sndcmd = 1;
1da177e4
LT
1309 }
1310
8593fbc6 1311 ops->retlen = ops->len;
1da177e4
LT
1312 return 0;
1313}
1314
1315/**
8593fbc6 1316 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1da177e4 1317 * @mtd: MTD device structure
1da177e4 1318 * @from: offset to read from
8593fbc6 1319 * @ops: oob operation description structure
1da177e4 1320 *
8593fbc6 1321 * NAND read data and/or out-of-band data
1da177e4 1322 */
8593fbc6
TG
1323static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1324 struct mtd_oob_ops *ops)
1da177e4 1325{
8593fbc6
TG
1326 int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
1327 uint8_t *buf) = NULL;
ace4dfee 1328 struct nand_chip *chip = mtd->priv;
8593fbc6
TG
1329 int ret = -ENOTSUPP;
1330
1331 ops->retlen = 0;
1da177e4
LT
1332
1333 /* Do not allow reads past end of device */
8593fbc6
TG
1334 if ((from + ops->len) > mtd->size) {
1335 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
ace4dfee 1336 "Attempt read beyond end of device\n");
1da177e4
LT
1337 return -EINVAL;
1338 }
1339
ace4dfee 1340 nand_get_device(chip, mtd, FL_READING);
1da177e4 1341
8593fbc6
TG
1342 switch(ops->mode) {
1343 case MTD_OOB_PLACE:
1344 case MTD_OOB_AUTO:
1345 break;
61b03bd7 1346
8593fbc6
TG
1347 case MTD_OOB_RAW:
1348 /* Replace the read_page algorithm temporary */
1349 read_page = chip->ecc.read_page;
1350 chip->ecc.read_page = nand_read_page_raw;
1351 break;
1da177e4 1352
8593fbc6
TG
1353 default:
1354 goto out;
1355 }
1da177e4 1356
8593fbc6
TG
1357 if (!ops->datbuf)
1358 ret = nand_do_read_oob(mtd, from, ops);
1359 else
1360 ret = nand_do_read_ops(mtd, from, ops);
61b03bd7 1361
8593fbc6
TG
1362 if (unlikely(ops->mode == MTD_OOB_RAW))
1363 chip->ecc.read_page = read_page;
1364 out:
1365 nand_release_device(mtd);
1366 return ret;
1367}
61b03bd7 1368
1da177e4 1369
8593fbc6
TG
1370/**
1371 * nand_write_page_raw - [Intern] raw page write function
1372 * @mtd: mtd info structure
1373 * @chip: nand chip info structure
1374 * @buf: data buffer
1375 */
1376static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1377 const uint8_t *buf)
1378{
1379 chip->write_buf(mtd, buf, mtd->writesize);
1380 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1da177e4
LT
1381}
1382
9223a456 1383/**
f75e5097
TG
1384 * nand_write_page_swecc - {REPLACABLE] software ecc based page write function
1385 * @mtd: mtd info structure
1386 * @chip: nand chip info structure
1387 * @buf: data buffer
9223a456 1388 */
f75e5097
TG
1389static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1390 const uint8_t *buf)
9223a456 1391{
f75e5097
TG
1392 int i, eccsize = chip->ecc.size;
1393 int eccbytes = chip->ecc.bytes;
1394 int eccsteps = chip->ecc.steps;
1395 uint8_t *ecc_calc = chip->buffers.ecccalc;
1396 const uint8_t *p = buf;
5bd34c09 1397 int *eccpos = chip->ecc.layout->eccpos;
9223a456 1398
8593fbc6
TG
1399 /* Software ecc calculation */
1400 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1401 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
9223a456 1402
8593fbc6
TG
1403 for (i = 0; i < chip->ecc.total; i++)
1404 chip->oob_poi[eccpos[i]] = ecc_calc[i];
9223a456 1405
8593fbc6 1406 nand_write_page_raw(mtd, chip, buf);
f75e5097 1407}
9223a456 1408
f75e5097
TG
1409/**
1410 * nand_write_page_hwecc - {REPLACABLE] hardware ecc based page write function
1411 * @mtd: mtd info structure
1412 * @chip: nand chip info structure
1413 * @buf: data buffer
1414 */
1415static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1416 const uint8_t *buf)
1417{
1418 int i, eccsize = chip->ecc.size;
1419 int eccbytes = chip->ecc.bytes;
1420 int eccsteps = chip->ecc.steps;
1421 uint8_t *ecc_calc = chip->buffers.ecccalc;
1422 const uint8_t *p = buf;
5bd34c09 1423 int *eccpos = chip->ecc.layout->eccpos;
9223a456 1424
f75e5097
TG
1425 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1426 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
29da9cea 1427 chip->write_buf(mtd, p, eccsize);
f75e5097 1428 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
9223a456
TG
1429 }
1430
f75e5097
TG
1431 for (i = 0; i < chip->ecc.total; i++)
1432 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1433
1434 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
9223a456
TG
1435}
1436
61b03bd7 1437/**
f75e5097
TG
1438 * nand_write_page_syndrome - {REPLACABLE] hardware ecc syndrom based page write
1439 * @mtd: mtd info structure
1440 * @chip: nand chip info structure
1441 * @buf: data buffer
1da177e4 1442 *
f75e5097
TG
1443 * The hw generator calculates the error syndrome automatically. Therefor
1444 * we need a special oob layout and handling.
1445 */
1446static void nand_write_page_syndrome(struct mtd_info *mtd,
1447 struct nand_chip *chip, const uint8_t *buf)
1da177e4 1448{
f75e5097
TG
1449 int i, eccsize = chip->ecc.size;
1450 int eccbytes = chip->ecc.bytes;
1451 int eccsteps = chip->ecc.steps;
1452 const uint8_t *p = buf;
1453 uint8_t *oob = chip->oob_poi;
1da177e4 1454
f75e5097 1455 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1da177e4 1456
f75e5097
TG
1457 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1458 chip->write_buf(mtd, p, eccsize);
61b03bd7 1459
f75e5097
TG
1460 if (chip->ecc.prepad) {
1461 chip->write_buf(mtd, oob, chip->ecc.prepad);
1462 oob += chip->ecc.prepad;
1463 }
1464
1465 chip->ecc.calculate(mtd, p, oob);
1466 chip->write_buf(mtd, oob, eccbytes);
1467 oob += eccbytes;
1468
1469 if (chip->ecc.postpad) {
1470 chip->write_buf(mtd, oob, chip->ecc.postpad);
1471 oob += chip->ecc.postpad;
1da177e4 1472 }
1da177e4 1473 }
f75e5097
TG
1474
1475 /* Calculate remaining oob bytes */
7e4178f9 1476 i = mtd->oobsize - (oob - chip->oob_poi);
f75e5097
TG
1477 if (i)
1478 chip->write_buf(mtd, oob, i);
1479}
1480
1481/**
1482 * nand_write_page - [INTERNAL] write one page
1483 * @mtd: MTD device structure
1484 * @chip: NAND chip descriptor
1485 * @buf: the data to write
1486 * @page: page number to write
1487 * @cached: cached programming
1488 */
1489static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1490 const uint8_t *buf, int page, int cached)
1491{
1492 int status;
1493
1494 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1495
1496 chip->ecc.write_page(mtd, chip, buf);
1497
1498 /*
1499 * Cached progamming disabled for now, Not sure if its worth the
1500 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1501 */
1502 cached = 0;
1503
1504 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1505
1506 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
7bc3312b 1507 status = chip->waitfunc(mtd, chip);
f75e5097
TG
1508 /*
1509 * See if operation failed and additional status checks are
1510 * available
1511 */
1512 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1513 status = chip->errstat(mtd, chip, FL_WRITING, status,
1514 page);
1515
1516 if (status & NAND_STATUS_FAIL)
1517 return -EIO;
1518 } else {
1519 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
7bc3312b 1520 status = chip->waitfunc(mtd, chip);
f75e5097
TG
1521 }
1522
1523#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1524 /* Send command to read back the data */
1525 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1526
1527 if (chip->verify_buf(mtd, buf, mtd->writesize))
1528 return -EIO;
1529#endif
1530 return 0;
1da177e4
LT
1531}
1532
8593fbc6
TG
1533/**
1534 * nand_fill_oob - [Internal] Transfer client buffer to oob
1535 * @chip: nand chip structure
1536 * @oob: oob data buffer
1537 * @ops: oob ops structure
1538 */
1539static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1540 struct mtd_oob_ops *ops)
1541{
1542 size_t len = ops->ooblen;
1543
1544 switch(ops->mode) {
1545
1546 case MTD_OOB_PLACE:
1547 case MTD_OOB_RAW:
1548 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1549 return oob + len;
1550
1551 case MTD_OOB_AUTO: {
1552 struct nand_oobfree *free = chip->ecc.layout->oobfree;
7bc3312b
TG
1553 uint32_t boffs = 0, woffs = ops->ooboffs;
1554 size_t bytes = 0;
8593fbc6
TG
1555
1556 for(; free->length && len; free++, len -= bytes) {
7bc3312b
TG
1557 /* Write request not from offset 0 ? */
1558 if (unlikely(woffs)) {
1559 if (woffs >= free->length) {
1560 woffs -= free->length;
1561 continue;
1562 }
1563 boffs = free->offset + woffs;
1564 bytes = min_t(size_t, len,
1565 (free->length - woffs));
1566 woffs = 0;
1567 } else {
1568 bytes = min_t(size_t, len, free->length);
1569 boffs = free->offset;
1570 }
8b0036ee 1571 memcpy(chip->oob_poi + boffs, oob, bytes);
8593fbc6
TG
1572 oob += bytes;
1573 }
1574 return oob;
1575 }
1576 default:
1577 BUG();
1578 }
1579 return NULL;
1580}
1581
28318776 1582#define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0
1da177e4
LT
1583
1584/**
8593fbc6 1585 * nand_do_write_ops - [Internal] NAND write with ECC
1da177e4
LT
1586 * @mtd: MTD device structure
1587 * @to: offset to write to
8593fbc6 1588 * @ops: oob operations description structure
1da177e4
LT
1589 *
1590 * NAND write with ECC
1591 */
8593fbc6
TG
1592static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1593 struct mtd_oob_ops *ops)
1da177e4 1594{
f75e5097 1595 int chipnr, realpage, page, blockmask;
ace4dfee 1596 struct nand_chip *chip = mtd->priv;
8593fbc6
TG
1597 uint32_t writelen = ops->len;
1598 uint8_t *oob = ops->oobbuf;
1599 uint8_t *buf = ops->datbuf;
f75e5097 1600 int bytes = mtd->writesize;
8593fbc6 1601 int ret;
1da177e4 1602
8593fbc6 1603 ops->retlen = 0;
1da177e4 1604
61b03bd7 1605 /* reject writes, which are not page aligned */
8593fbc6 1606 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
f75e5097
TG
1607 printk(KERN_NOTICE "nand_write: "
1608 "Attempt to write not page aligned data\n");
1da177e4
LT
1609 return -EINVAL;
1610 }
1611
8593fbc6 1612 if (!writelen)
f75e5097 1613 return 0;
1da177e4 1614
6a930961
TG
1615 chipnr = (int)(to >> chip->chip_shift);
1616 chip->select_chip(mtd, chipnr);
1617
1da177e4
LT
1618 /* Check, if it is write protected */
1619 if (nand_check_wp(mtd))
8593fbc6 1620 return -EIO;
1da177e4 1621
f75e5097
TG
1622 realpage = (int)(to >> chip->page_shift);
1623 page = realpage & chip->pagemask;
1624 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1625
1626 /* Invalidate the page cache, when we write to the cached page */
1627 if (to <= (chip->pagebuf << chip->page_shift) &&
8593fbc6 1628 (chip->pagebuf << chip->page_shift) < (to + ops->len))
ace4dfee 1629 chip->pagebuf = -1;
61b03bd7 1630
f75e5097 1631 chip->oob_poi = chip->buffers.oobwbuf;
61b03bd7 1632
f75e5097
TG
1633 while(1) {
1634 int cached = writelen > bytes && page != blockmask;
1da177e4 1635
8593fbc6
TG
1636 if (unlikely(oob))
1637 oob = nand_fill_oob(chip, oob, ops);
1638
f75e5097
TG
1639 ret = nand_write_page(mtd, chip, buf, page, cached);
1640 if (ret)
1641 break;
1642
1643 writelen -= bytes;
1644 if (!writelen)
1645 break;
1646
1647 buf += bytes;
1648 realpage++;
1649
1650 page = realpage & chip->pagemask;
1651 /* Check, if we cross a chip boundary */
1652 if (!page) {
1653 chipnr++;
1654 chip->select_chip(mtd, -1);
1655 chip->select_chip(mtd, chipnr);
1da177e4
LT
1656 }
1657 }
8593fbc6
TG
1658
1659 if (unlikely(oob))
1660 memset(chip->oob_poi, 0xff, mtd->oobsize);
1661
1662 ops->retlen = ops->len - writelen;
1da177e4
LT
1663 return ret;
1664}
1665
f75e5097 1666/**
8593fbc6 1667 * nand_write - [MTD Interface] NAND write with ECC
f75e5097 1668 * @mtd: MTD device structure
f75e5097
TG
1669 * @to: offset to write to
1670 * @len: number of bytes to write
8593fbc6
TG
1671 * @retlen: pointer to variable to store the number of written bytes
1672 * @buf: the data to write
f75e5097 1673 *
8593fbc6 1674 * NAND write with ECC
f75e5097 1675 */
8593fbc6
TG
1676static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1677 size_t *retlen, const uint8_t *buf)
f75e5097
TG
1678{
1679 struct nand_chip *chip = mtd->priv;
f75e5097
TG
1680 int ret;
1681
8593fbc6
TG
1682 /* Do not allow reads past end of device */
1683 if ((to + len) > mtd->size)
f75e5097 1684 return -EINVAL;
8593fbc6
TG
1685 if (!len)
1686 return 0;
f75e5097 1687
7bc3312b 1688 nand_get_device(chip, mtd, FL_WRITING);
f75e5097 1689
8593fbc6
TG
1690 chip->ops.len = len;
1691 chip->ops.datbuf = (uint8_t *)buf;
1692 chip->ops.oobbuf = NULL;
f75e5097 1693
8593fbc6 1694 ret = nand_do_write_ops(mtd, to, &chip->ops);
f75e5097 1695
7fd5aecc
RP
1696 *retlen = chip->ops.retlen;
1697
f75e5097 1698 nand_release_device(mtd);
8593fbc6 1699
8593fbc6 1700 return ret;
f75e5097 1701}
7314e9e7 1702
1da177e4 1703/**
8593fbc6 1704 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1da177e4
LT
1705 * @mtd: MTD device structure
1706 * @to: offset to write to
8593fbc6 1707 * @ops: oob operation description structure
1da177e4
LT
1708 *
1709 * NAND write out-of-band
1710 */
8593fbc6
TG
1711static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1712 struct mtd_oob_ops *ops)
1da177e4 1713{
8593fbc6 1714 int chipnr, page, status;
ace4dfee 1715 struct nand_chip *chip = mtd->priv;
1da177e4 1716
7314e9e7 1717 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
8593fbc6 1718 (unsigned int)to, (int)ops->len);
1da177e4
LT
1719
1720 /* Do not allow write past end of page */
8593fbc6 1721 if ((ops->ooboffs + ops->len) > mtd->oobsize) {
7314e9e7
TG
1722 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1723 "Attempt to write past end of page\n");
1da177e4
LT
1724 return -EINVAL;
1725 }
1726
7314e9e7 1727 chipnr = (int)(to >> chip->chip_shift);
ace4dfee 1728 chip->select_chip(mtd, chipnr);
1da177e4 1729
7314e9e7
TG
1730 /* Shift to get page */
1731 page = (int)(to >> chip->page_shift);
1732
1733 /*
1734 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1735 * of my DiskOnChip 2000 test units) will clear the whole data page too
1736 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1737 * it in the doc2000 driver in August 1999. dwmw2.
1738 */
ace4dfee 1739 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1da177e4
LT
1740
1741 /* Check, if it is write protected */
1742 if (nand_check_wp(mtd))
8593fbc6 1743 return -EROFS;
61b03bd7 1744
1da177e4 1745 /* Invalidate the page cache, if we write to the cached page */
ace4dfee
TG
1746 if (page == chip->pagebuf)
1747 chip->pagebuf = -1;
1da177e4 1748
7bc3312b
TG
1749 chip->oob_poi = chip->buffers.oobwbuf;
1750 memset(chip->oob_poi, 0xff, mtd->oobsize);
1751 nand_fill_oob(chip, ops->oobbuf, ops);
1752 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1753 memset(chip->oob_poi, 0xff, mtd->oobsize);
1da177e4 1754
7bc3312b
TG
1755 if (status)
1756 return status;
1da177e4 1757
8593fbc6 1758 ops->retlen = ops->len;
1da177e4 1759
7bc3312b 1760 return 0;
8593fbc6
TG
1761}
1762
1763/**
1764 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1765 * @mtd: MTD device structure
844d3b42 1766 * @to: offset to write to
8593fbc6
TG
1767 * @ops: oob operation description structure
1768 */
1769static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1770 struct mtd_oob_ops *ops)
1771{
1772 void (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
1773 const uint8_t *buf) = NULL;
1774 struct nand_chip *chip = mtd->priv;
1775 int ret = -ENOTSUPP;
1776
1777 ops->retlen = 0;
1778
1779 /* Do not allow writes past end of device */
1780 if ((to + ops->len) > mtd->size) {
1781 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1782 "Attempt read beyond end of device\n");
1783 return -EINVAL;
1784 }
1785
7bc3312b 1786 nand_get_device(chip, mtd, FL_WRITING);
8593fbc6
TG
1787
1788 switch(ops->mode) {
1789 case MTD_OOB_PLACE:
1790 case MTD_OOB_AUTO:
1791 break;
1792
1793 case MTD_OOB_RAW:
1794 /* Replace the write_page algorithm temporary */
1795 write_page = chip->ecc.write_page;
1796 chip->ecc.write_page = nand_write_page_raw;
1797 break;
1798
1799 default:
1800 goto out;
1801 }
1802
1803 if (!ops->datbuf)
1804 ret = nand_do_write_oob(mtd, to, ops);
1805 else
1806 ret = nand_do_write_ops(mtd, to, ops);
1807
1808 if (unlikely(ops->mode == MTD_OOB_RAW))
1809 chip->ecc.write_page = write_page;
e0c7d767 1810 out:
1da177e4 1811 nand_release_device(mtd);
1da177e4
LT
1812 return ret;
1813}
1814
1da177e4
LT
1815/**
1816 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1817 * @mtd: MTD device structure
1818 * @page: the page address of the block which will be erased
1819 *
1820 * Standard erase command for NAND chips
1821 */
e0c7d767 1822static void single_erase_cmd(struct mtd_info *mtd, int page)
1da177e4 1823{
ace4dfee 1824 struct nand_chip *chip = mtd->priv;
1da177e4 1825 /* Send commands to erase a block */
ace4dfee
TG
1826 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1827 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1da177e4
LT
1828}
1829
1830/**
1831 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1832 * @mtd: MTD device structure
1833 * @page: the page address of the block which will be erased
1834 *
1835 * AND multi block erase command function
1836 * Erase 4 consecutive blocks
1837 */
e0c7d767 1838static void multi_erase_cmd(struct mtd_info *mtd, int page)
1da177e4 1839{
ace4dfee 1840 struct nand_chip *chip = mtd->priv;
1da177e4 1841 /* Send commands to erase a block */
ace4dfee
TG
1842 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1843 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1844 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1845 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1846 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1da177e4
LT
1847}
1848
1849/**
1850 * nand_erase - [MTD Interface] erase block(s)
1851 * @mtd: MTD device structure
1852 * @instr: erase instruction
1853 *
1854 * Erase one ore more blocks
1855 */
e0c7d767 1856static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
1da177e4 1857{
e0c7d767 1858 return nand_erase_nand(mtd, instr, 0);
1da177e4 1859}
61b03bd7 1860
30f464b7 1861#define BBT_PAGE_MASK 0xffffff3f
1da177e4 1862/**
ace4dfee 1863 * nand_erase_nand - [Internal] erase block(s)
1da177e4
LT
1864 * @mtd: MTD device structure
1865 * @instr: erase instruction
1866 * @allowbbt: allow erasing the bbt area
1867 *
1868 * Erase one ore more blocks
1869 */
ace4dfee
TG
1870int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
1871 int allowbbt)
1da177e4
LT
1872{
1873 int page, len, status, pages_per_block, ret, chipnr;
ace4dfee
TG
1874 struct nand_chip *chip = mtd->priv;
1875 int rewrite_bbt[NAND_MAX_CHIPS]={0};
1876 unsigned int bbt_masked_page = 0xffffffff;
1da177e4 1877
ace4dfee
TG
1878 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
1879 (unsigned int)instr->addr, (unsigned int)instr->len);
1da177e4
LT
1880
1881 /* Start address must align on block boundary */
ace4dfee 1882 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
e0c7d767 1883 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
1da177e4
LT
1884 return -EINVAL;
1885 }
1886
1887 /* Length must align on block boundary */
ace4dfee
TG
1888 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
1889 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1890 "Length not block aligned\n");
1da177e4
LT
1891 return -EINVAL;
1892 }
1893
1894 /* Do not allow erase past end of device */
1895 if ((instr->len + instr->addr) > mtd->size) {
ace4dfee
TG
1896 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1897 "Erase past end of device\n");
1da177e4
LT
1898 return -EINVAL;
1899 }
1900
1901 instr->fail_addr = 0xffffffff;
1902
1903 /* Grab the lock and see if the device is available */
ace4dfee 1904 nand_get_device(chip, mtd, FL_ERASING);
1da177e4
LT
1905
1906 /* Shift to get first page */
ace4dfee
TG
1907 page = (int)(instr->addr >> chip->page_shift);
1908 chipnr = (int)(instr->addr >> chip->chip_shift);
1da177e4
LT
1909
1910 /* Calculate pages in each block */
ace4dfee 1911 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
1da177e4
LT
1912
1913 /* Select the NAND device */
ace4dfee 1914 chip->select_chip(mtd, chipnr);
1da177e4 1915
1da177e4
LT
1916 /* Check, if it is write protected */
1917 if (nand_check_wp(mtd)) {
ace4dfee
TG
1918 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1919 "Device is write protected!!!\n");
1da177e4
LT
1920 instr->state = MTD_ERASE_FAILED;
1921 goto erase_exit;
1922 }
1923
ace4dfee
TG
1924 /*
1925 * If BBT requires refresh, set the BBT page mask to see if the BBT
1926 * should be rewritten. Otherwise the mask is set to 0xffffffff which
1927 * can not be matched. This is also done when the bbt is actually
1928 * erased to avoid recusrsive updates
1929 */
1930 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
1931 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
30f464b7 1932
1da177e4
LT
1933 /* Loop through the pages */
1934 len = instr->len;
1935
1936 instr->state = MTD_ERASING;
1937
1938 while (len) {
ace4dfee
TG
1939 /*
1940 * heck if we have a bad block, we do not erase bad blocks !
1941 */
1942 if (nand_block_checkbad(mtd, ((loff_t) page) <<
1943 chip->page_shift, 0, allowbbt)) {
1944 printk(KERN_WARNING "nand_erase: attempt to erase a "
1945 "bad block at page 0x%08x\n", page);
1da177e4
LT
1946 instr->state = MTD_ERASE_FAILED;
1947 goto erase_exit;
1948 }
61b03bd7 1949
ace4dfee
TG
1950 /*
1951 * Invalidate the page cache, if we erase the block which
1952 * contains the current cached page
1953 */
1954 if (page <= chip->pagebuf && chip->pagebuf <
1955 (page + pages_per_block))
1956 chip->pagebuf = -1;
1da177e4 1957
ace4dfee 1958 chip->erase_cmd(mtd, page & chip->pagemask);
61b03bd7 1959
7bc3312b 1960 status = chip->waitfunc(mtd, chip);
1da177e4 1961
ace4dfee
TG
1962 /*
1963 * See if operation failed and additional status checks are
1964 * available
1965 */
1966 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1967 status = chip->errstat(mtd, chip, FL_ERASING,
1968 status, page);
068e3c0a 1969
1da177e4 1970 /* See if block erase succeeded */
a4ab4c5d 1971 if (status & NAND_STATUS_FAIL) {
ace4dfee
TG
1972 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1973 "Failed erase, page 0x%08x\n", page);
1da177e4 1974 instr->state = MTD_ERASE_FAILED;
ace4dfee 1975 instr->fail_addr = (page << chip->page_shift);
1da177e4
LT
1976 goto erase_exit;
1977 }
30f464b7 1978
ace4dfee
TG
1979 /*
1980 * If BBT requires refresh, set the BBT rewrite flag to the
1981 * page being erased
1982 */
1983 if (bbt_masked_page != 0xffffffff &&
1984 (page & BBT_PAGE_MASK) == bbt_masked_page)
1985 rewrite_bbt[chipnr] = (page << chip->page_shift);
61b03bd7 1986
1da177e4 1987 /* Increment page address and decrement length */
ace4dfee 1988 len -= (1 << chip->phys_erase_shift);
1da177e4
LT
1989 page += pages_per_block;
1990
1991 /* Check, if we cross a chip boundary */
ace4dfee 1992 if (len && !(page & chip->pagemask)) {
1da177e4 1993 chipnr++;
ace4dfee
TG
1994 chip->select_chip(mtd, -1);
1995 chip->select_chip(mtd, chipnr);
30f464b7 1996
ace4dfee
TG
1997 /*
1998 * If BBT requires refresh and BBT-PERCHIP, set the BBT
1999 * page mask to see if this BBT should be rewritten
2000 */
2001 if (bbt_masked_page != 0xffffffff &&
2002 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2003 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2004 BBT_PAGE_MASK;
1da177e4
LT
2005 }
2006 }
2007 instr->state = MTD_ERASE_DONE;
2008
e0c7d767 2009 erase_exit:
1da177e4
LT
2010
2011 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2012 /* Do call back function */
2013 if (!ret)
2014 mtd_erase_callback(instr);
2015
2016 /* Deselect and wake up anyone waiting on the device */
2017 nand_release_device(mtd);
2018
ace4dfee
TG
2019 /*
2020 * If BBT requires refresh and erase was successful, rewrite any
2021 * selected bad block tables
2022 */
2023 if (bbt_masked_page == 0xffffffff || ret)
2024 return ret;
2025
2026 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2027 if (!rewrite_bbt[chipnr])
2028 continue;
2029 /* update the BBT for chip */
2030 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2031 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2032 chip->bbt_td->pages[chipnr]);
2033 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
30f464b7
DM
2034 }
2035
1da177e4
LT
2036 /* Return more or less happy */
2037 return ret;
2038}
2039
2040/**
2041 * nand_sync - [MTD Interface] sync
2042 * @mtd: MTD device structure
2043 *
2044 * Sync is actually a wait for chip ready function
2045 */
e0c7d767 2046static void nand_sync(struct mtd_info *mtd)
1da177e4 2047{
ace4dfee 2048 struct nand_chip *chip = mtd->priv;
1da177e4 2049
e0c7d767 2050 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
1da177e4
LT
2051
2052 /* Grab the lock and see if the device is available */
ace4dfee 2053 nand_get_device(chip, mtd, FL_SYNCING);
1da177e4 2054 /* Release it and go back */
e0c7d767 2055 nand_release_device(mtd);
1da177e4
LT
2056}
2057
1da177e4 2058/**
ace4dfee 2059 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
1da177e4 2060 * @mtd: MTD device structure
844d3b42 2061 * @offs: offset relative to mtd start
1da177e4 2062 */
ace4dfee 2063static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
1da177e4
LT
2064{
2065 /* Check for invalid offset */
ace4dfee 2066 if (offs > mtd->size)
1da177e4 2067 return -EINVAL;
61b03bd7 2068
ace4dfee 2069 return nand_block_checkbad(mtd, offs, 1, 0);
1da177e4
LT
2070}
2071
2072/**
ace4dfee 2073 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
1da177e4
LT
2074 * @mtd: MTD device structure
2075 * @ofs: offset relative to mtd start
2076 */
e0c7d767 2077static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1da177e4 2078{
ace4dfee 2079 struct nand_chip *chip = mtd->priv;
1da177e4
LT
2080 int ret;
2081
e0c7d767
DW
2082 if ((ret = nand_block_isbad(mtd, ofs))) {
2083 /* If it was bad already, return success and do nothing. */
1da177e4
LT
2084 if (ret > 0)
2085 return 0;
e0c7d767
DW
2086 return ret;
2087 }
1da177e4 2088
ace4dfee 2089 return chip->block_markbad(mtd, ofs);
1da177e4
LT
2090}
2091
962034f4
VW
2092/**
2093 * nand_suspend - [MTD Interface] Suspend the NAND flash
2094 * @mtd: MTD device structure
2095 */
2096static int nand_suspend(struct mtd_info *mtd)
2097{
ace4dfee 2098 struct nand_chip *chip = mtd->priv;
962034f4 2099
ace4dfee 2100 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
962034f4
VW
2101}
2102
2103/**
2104 * nand_resume - [MTD Interface] Resume the NAND flash
2105 * @mtd: MTD device structure
2106 */
2107static void nand_resume(struct mtd_info *mtd)
2108{
ace4dfee 2109 struct nand_chip *chip = mtd->priv;
962034f4 2110
ace4dfee 2111 if (chip->state == FL_PM_SUSPENDED)
962034f4
VW
2112 nand_release_device(mtd);
2113 else
2c0a2bed
TG
2114 printk(KERN_ERR "nand_resume() called for a chip which is not "
2115 "in suspended state\n");
962034f4
VW
2116}
2117
7aa65bfd
TG
2118/*
2119 * Set default functions
2120 */
ace4dfee 2121static void nand_set_defaults(struct nand_chip *chip, int busw)
7aa65bfd 2122{
1da177e4 2123 /* check for proper chip_delay setup, set 20us if not */
ace4dfee
TG
2124 if (!chip->chip_delay)
2125 chip->chip_delay = 20;
1da177e4
LT
2126
2127 /* check, if a user supplied command function given */
ace4dfee
TG
2128 if (chip->cmdfunc == NULL)
2129 chip->cmdfunc = nand_command;
1da177e4
LT
2130
2131 /* check, if a user supplied wait function given */
ace4dfee
TG
2132 if (chip->waitfunc == NULL)
2133 chip->waitfunc = nand_wait;
2134
2135 if (!chip->select_chip)
2136 chip->select_chip = nand_select_chip;
2137 if (!chip->read_byte)
2138 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2139 if (!chip->read_word)
2140 chip->read_word = nand_read_word;
2141 if (!chip->block_bad)
2142 chip->block_bad = nand_block_bad;
2143 if (!chip->block_markbad)
2144 chip->block_markbad = nand_default_block_markbad;
2145 if (!chip->write_buf)
2146 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2147 if (!chip->read_buf)
2148 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2149 if (!chip->verify_buf)
2150 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2151 if (!chip->scan_bbt)
2152 chip->scan_bbt = nand_default_bbt;
f75e5097
TG
2153
2154 if (!chip->controller) {
2155 chip->controller = &chip->hwcontrol;
2156 spin_lock_init(&chip->controller->lock);
2157 init_waitqueue_head(&chip->controller->wq);
2158 }
2159
7aa65bfd
TG
2160}
2161
2162/*
ace4dfee 2163 * Get the flash and manufacturer id and lookup if the type is supported
7aa65bfd
TG
2164 */
2165static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
ace4dfee 2166 struct nand_chip *chip,
7aa65bfd
TG
2167 int busw, int *maf_id)
2168{
2169 struct nand_flash_dev *type = NULL;
2170 int i, dev_id, maf_idx;
1da177e4
LT
2171
2172 /* Select the device */
ace4dfee 2173 chip->select_chip(mtd, 0);
1da177e4
LT
2174
2175 /* Send the command for reading device ID */
ace4dfee 2176 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
1da177e4
LT
2177
2178 /* Read manufacturer and device IDs */
ace4dfee
TG
2179 *maf_id = chip->read_byte(mtd);
2180 dev_id = chip->read_byte(mtd);
1da177e4 2181
7aa65bfd 2182 /* Lookup the flash id */
1da177e4 2183 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
7aa65bfd
TG
2184 if (dev_id == nand_flash_ids[i].id) {
2185 type = &nand_flash_ids[i];
2186 break;
2187 }
2188 }
61b03bd7 2189
7aa65bfd
TG
2190 if (!type)
2191 return ERR_PTR(-ENODEV);
2192
ba0251fe
TG
2193 if (!mtd->name)
2194 mtd->name = type->name;
2195
2196 chip->chipsize = type->chipsize << 20;
7aa65bfd
TG
2197
2198 /* Newer devices have all the information in additional id bytes */
ba0251fe 2199 if (!type->pagesize) {
7aa65bfd
TG
2200 int extid;
2201 /* The 3rd id byte contains non relevant data ATM */
ace4dfee 2202 extid = chip->read_byte(mtd);
7aa65bfd 2203 /* The 4th id byte is the important one */
ace4dfee 2204 extid = chip->read_byte(mtd);
7aa65bfd 2205 /* Calc pagesize */
4cbb9b80 2206 mtd->writesize = 1024 << (extid & 0x3);
7aa65bfd
TG
2207 extid >>= 2;
2208 /* Calc oobsize */
4cbb9b80 2209 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
7aa65bfd
TG
2210 extid >>= 2;
2211 /* Calc blocksize. Blocksize is multiples of 64KiB */
2212 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2213 extid >>= 2;
2214 /* Get buswidth information */
2215 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
61b03bd7 2216
7aa65bfd
TG
2217 } else {
2218 /*
ace4dfee 2219 * Old devices have chip data hardcoded in the device id table
7aa65bfd 2220 */
ba0251fe
TG
2221 mtd->erasesize = type->erasesize;
2222 mtd->writesize = type->pagesize;
4cbb9b80 2223 mtd->oobsize = mtd->writesize / 32;
ba0251fe 2224 busw = type->options & NAND_BUSWIDTH_16;
7aa65bfd 2225 }
1da177e4 2226
7aa65bfd 2227 /* Try to identify manufacturer */
9a909867 2228 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
7aa65bfd
TG
2229 if (nand_manuf_ids[maf_idx].id == *maf_id)
2230 break;
2231 }
0ea4a755 2232
7aa65bfd
TG
2233 /*
2234 * Check, if buswidth is correct. Hardware drivers should set
ace4dfee 2235 * chip correct !
7aa65bfd 2236 */
ace4dfee 2237 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
7aa65bfd
TG
2238 printk(KERN_INFO "NAND device: Manufacturer ID:"
2239 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2240 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2241 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
ace4dfee 2242 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
7aa65bfd
TG
2243 busw ? 16 : 8);
2244 return ERR_PTR(-EINVAL);
2245 }
61b03bd7 2246
7aa65bfd 2247 /* Calculate the address shift from the page size */
ace4dfee 2248 chip->page_shift = ffs(mtd->writesize) - 1;
7aa65bfd 2249 /* Convert chipsize to number of pages per chip -1. */
ace4dfee 2250 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
61b03bd7 2251
ace4dfee 2252 chip->bbt_erase_shift = chip->phys_erase_shift =
7aa65bfd 2253 ffs(mtd->erasesize) - 1;
ace4dfee 2254 chip->chip_shift = ffs(chip->chipsize) - 1;
1da177e4 2255
7aa65bfd 2256 /* Set the bad block position */
ace4dfee 2257 chip->badblockpos = mtd->writesize > 512 ?
7aa65bfd 2258 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
61b03bd7 2259
7aa65bfd 2260 /* Get chip options, preserve non chip based options */
ace4dfee 2261 chip->options &= ~NAND_CHIPOPTIONS_MSK;
ba0251fe 2262 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
7aa65bfd
TG
2263
2264 /*
ace4dfee 2265 * Set chip as a default. Board drivers can override it, if necessary
7aa65bfd 2266 */
ace4dfee 2267 chip->options |= NAND_NO_AUTOINCR;
7aa65bfd 2268
ace4dfee 2269 /* Check if chip is a not a samsung device. Do not clear the
7aa65bfd
TG
2270 * options for chips which are not having an extended id.
2271 */
ba0251fe 2272 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
ace4dfee 2273 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
7aa65bfd
TG
2274
2275 /* Check for AND chips with 4 page planes */
ace4dfee
TG
2276 if (chip->options & NAND_4PAGE_ARRAY)
2277 chip->erase_cmd = multi_erase_cmd;
7aa65bfd 2278 else
ace4dfee 2279 chip->erase_cmd = single_erase_cmd;
7aa65bfd
TG
2280
2281 /* Do not replace user supplied command function ! */
ace4dfee
TG
2282 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2283 chip->cmdfunc = nand_command_lp;
7aa65bfd
TG
2284
2285 printk(KERN_INFO "NAND device: Manufacturer ID:"
2286 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2287 nand_manuf_ids[maf_idx].name, type->name);
2288
2289 return type;
2290}
2291
2292/* module_text_address() isn't exported, and it's mostly a pointless
2293 test if this is a module _anyway_ -- they'd have to try _really_ hard
2294 to call us from in-kernel code if the core NAND support is modular. */
2295#ifdef MODULE
2296#define caller_is_module() (1)
2297#else
2298#define caller_is_module() \
2299 module_text_address((unsigned long)__builtin_return_address(0))
2300#endif
2301
2302/**
2303 * nand_scan - [NAND Interface] Scan for the NAND device
2304 * @mtd: MTD device structure
2305 * @maxchips: Number of chips to scan for
2306 *
2307 * This fills out all the uninitialized function pointers
2308 * with the defaults.
2309 * The flash ID is read and the mtd/chip structures are
f75e5097 2310 * filled with the appropriate values.
7aa65bfd
TG
2311 * The mtd->owner field must be set to the module of the caller
2312 *
2313 */
2314int nand_scan(struct mtd_info *mtd, int maxchips)
2315{
2316 int i, busw, nand_maf_id;
ace4dfee 2317 struct nand_chip *chip = mtd->priv;
7aa65bfd
TG
2318 struct nand_flash_dev *type;
2319
2320 /* Many callers got this wrong, so check for it for a while... */
2321 if (!mtd->owner && caller_is_module()) {
2322 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2323 BUG();
1da177e4
LT
2324 }
2325
7aa65bfd 2326 /* Get buswidth to select the correct functions */
ace4dfee 2327 busw = chip->options & NAND_BUSWIDTH_16;
7aa65bfd 2328 /* Set the default functions */
ace4dfee 2329 nand_set_defaults(chip, busw);
7aa65bfd
TG
2330
2331 /* Read the flash type */
ace4dfee 2332 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
7aa65bfd
TG
2333
2334 if (IS_ERR(type)) {
e0c7d767 2335 printk(KERN_WARNING "No NAND device found!!!\n");
ace4dfee 2336 chip->select_chip(mtd, -1);
7aa65bfd 2337 return PTR_ERR(type);
1da177e4
LT
2338 }
2339
7aa65bfd 2340 /* Check for a chip array */
e0c7d767 2341 for (i = 1; i < maxchips; i++) {
ace4dfee 2342 chip->select_chip(mtd, i);
1da177e4 2343 /* Send the command for reading device ID */
ace4dfee 2344 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
1da177e4 2345 /* Read manufacturer and device IDs */
ace4dfee
TG
2346 if (nand_maf_id != chip->read_byte(mtd) ||
2347 type->id != chip->read_byte(mtd))
1da177e4
LT
2348 break;
2349 }
2350 if (i > 1)
2351 printk(KERN_INFO "%d NAND chips detected\n", i);
61b03bd7 2352
1da177e4 2353 /* Store the number of chips and calc total size for mtd */
ace4dfee
TG
2354 chip->numchips = i;
2355 mtd->size = i * chip->chipsize;
7aa65bfd 2356
f75e5097
TG
2357 /* Preset the internal oob write buffer */
2358 memset(chip->buffers.oobwbuf, 0xff, mtd->oobsize);
1da177e4 2359
7aa65bfd
TG
2360 /*
2361 * If no default placement scheme is given, select an appropriate one
2362 */
5bd34c09 2363 if (!chip->ecc.layout) {
61b03bd7 2364 switch (mtd->oobsize) {
1da177e4 2365 case 8:
5bd34c09 2366 chip->ecc.layout = &nand_oob_8;
1da177e4
LT
2367 break;
2368 case 16:
5bd34c09 2369 chip->ecc.layout = &nand_oob_16;
1da177e4
LT
2370 break;
2371 case 64:
5bd34c09 2372 chip->ecc.layout = &nand_oob_64;
1da177e4
LT
2373 break;
2374 default:
7aa65bfd
TG
2375 printk(KERN_WARNING "No oob scheme defined for "
2376 "oobsize %d\n", mtd->oobsize);
1da177e4
LT
2377 BUG();
2378 }
2379 }
61b03bd7 2380
61b03bd7 2381 /*
7aa65bfd
TG
2382 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2383 * selected and we have 256 byte pagesize fallback to software ECC
e0c7d767 2384 */
ace4dfee 2385 switch (chip->ecc.mode) {
6dfc6d25 2386 case NAND_ECC_HW:
f5bbdacc
TG
2387 /* Use standard hwecc read page function ? */
2388 if (!chip->ecc.read_page)
2389 chip->ecc.read_page = nand_read_page_hwecc;
f75e5097
TG
2390 if (!chip->ecc.write_page)
2391 chip->ecc.write_page = nand_write_page_hwecc;
7bc3312b
TG
2392 if (!chip->ecc.read_oob)
2393 chip->ecc.read_oob = nand_read_oob_std;
2394 if (!chip->ecc.write_oob)
2395 chip->ecc.write_oob = nand_write_oob_std;
f5bbdacc 2396
6dfc6d25 2397 case NAND_ECC_HW_SYNDROME:
ace4dfee
TG
2398 if (!chip->ecc.calculate || !chip->ecc.correct ||
2399 !chip->ecc.hwctl) {
6dfc6d25
TG
2400 printk(KERN_WARNING "No ECC functions supplied, "
2401 "Hardware ECC not possible\n");
2402 BUG();
2403 }
f75e5097 2404 /* Use standard syndrome read/write page function ? */
f5bbdacc
TG
2405 if (!chip->ecc.read_page)
2406 chip->ecc.read_page = nand_read_page_syndrome;
f75e5097
TG
2407 if (!chip->ecc.write_page)
2408 chip->ecc.write_page = nand_write_page_syndrome;
7bc3312b
TG
2409 if (!chip->ecc.read_oob)
2410 chip->ecc.read_oob = nand_read_oob_syndrome;
2411 if (!chip->ecc.write_oob)
2412 chip->ecc.write_oob = nand_write_oob_syndrome;
f5bbdacc 2413
ace4dfee 2414 if (mtd->writesize >= chip->ecc.size)
6dfc6d25
TG
2415 break;
2416 printk(KERN_WARNING "%d byte HW ECC not possible on "
2417 "%d byte page size, fallback to SW ECC\n",
ace4dfee
TG
2418 chip->ecc.size, mtd->writesize);
2419 chip->ecc.mode = NAND_ECC_SOFT;
61b03bd7 2420
6dfc6d25 2421 case NAND_ECC_SOFT:
ace4dfee
TG
2422 chip->ecc.calculate = nand_calculate_ecc;
2423 chip->ecc.correct = nand_correct_data;
f5bbdacc 2424 chip->ecc.read_page = nand_read_page_swecc;
f75e5097 2425 chip->ecc.write_page = nand_write_page_swecc;
7bc3312b
TG
2426 chip->ecc.read_oob = nand_read_oob_std;
2427 chip->ecc.write_oob = nand_write_oob_std;
ace4dfee
TG
2428 chip->ecc.size = 256;
2429 chip->ecc.bytes = 3;
1da177e4 2430 break;
61b03bd7
TG
2431
2432 case NAND_ECC_NONE:
7aa65bfd
TG
2433 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2434 "This is not recommended !!\n");
8593fbc6
TG
2435 chip->ecc.read_page = nand_read_page_raw;
2436 chip->ecc.write_page = nand_write_page_raw;
7bc3312b
TG
2437 chip->ecc.read_oob = nand_read_oob_std;
2438 chip->ecc.write_oob = nand_write_oob_std;
ace4dfee
TG
2439 chip->ecc.size = mtd->writesize;
2440 chip->ecc.bytes = 0;
1da177e4 2441 break;
1da177e4 2442 default:
7aa65bfd 2443 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
ace4dfee 2444 chip->ecc.mode);
61b03bd7 2445 BUG();
1da177e4 2446 }
61b03bd7 2447
5bd34c09
TG
2448 /*
2449 * The number of bytes available for a client to place data into
2450 * the out of band area
2451 */
2452 chip->ecc.layout->oobavail = 0;
2453 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2454 chip->ecc.layout->oobavail +=
2455 chip->ecc.layout->oobfree[i].length;
2456
7aa65bfd
TG
2457 /*
2458 * Set the number of read / write steps for one page depending on ECC
2459 * mode
2460 */
ace4dfee
TG
2461 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2462 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
6dfc6d25
TG
2463 printk(KERN_WARNING "Invalid ecc parameters\n");
2464 BUG();
1da177e4 2465 }
f5bbdacc 2466 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
61b03bd7 2467
04bbd0ea 2468 /* Initialize state */
ace4dfee 2469 chip->state = FL_READY;
1da177e4
LT
2470
2471 /* De-select the device */
ace4dfee 2472 chip->select_chip(mtd, -1);
1da177e4
LT
2473
2474 /* Invalidate the pagebuffer reference */
ace4dfee 2475 chip->pagebuf = -1;
1da177e4
LT
2476
2477 /* Fill in remaining MTD driver data */
2478 mtd->type = MTD_NANDFLASH;
5fa43394 2479 mtd->flags = MTD_CAP_NANDFLASH;
1da177e4
LT
2480 mtd->ecctype = MTD_ECC_SW;
2481 mtd->erase = nand_erase;
2482 mtd->point = NULL;
2483 mtd->unpoint = NULL;
2484 mtd->read = nand_read;
2485 mtd->write = nand_write;
1da177e4
LT
2486 mtd->read_oob = nand_read_oob;
2487 mtd->write_oob = nand_write_oob;
1da177e4
LT
2488 mtd->sync = nand_sync;
2489 mtd->lock = NULL;
2490 mtd->unlock = NULL;
962034f4
VW
2491 mtd->suspend = nand_suspend;
2492 mtd->resume = nand_resume;
1da177e4
LT
2493 mtd->block_isbad = nand_block_isbad;
2494 mtd->block_markbad = nand_block_markbad;
2495
5bd34c09
TG
2496 /* propagate ecc.layout to mtd_info */
2497 mtd->ecclayout = chip->ecc.layout;
1da177e4 2498
0040bf38 2499 /* Check, if we should skip the bad block table scan */
ace4dfee 2500 if (chip->options & NAND_SKIP_BBTSCAN)
0040bf38 2501 return 0;
1da177e4
LT
2502
2503 /* Build bad block table */
ace4dfee 2504 return chip->scan_bbt(mtd);
1da177e4
LT
2505}
2506
2507/**
61b03bd7 2508 * nand_release - [NAND Interface] Free resources held by the NAND device
1da177e4
LT
2509 * @mtd: MTD device structure
2510*/
e0c7d767 2511void nand_release(struct mtd_info *mtd)
1da177e4 2512{
ace4dfee 2513 struct nand_chip *chip = mtd->priv;
1da177e4
LT
2514
2515#ifdef CONFIG_MTD_PARTITIONS
2516 /* Deregister partitions */
e0c7d767 2517 del_mtd_partitions(mtd);
1da177e4
LT
2518#endif
2519 /* Deregister the device */
e0c7d767 2520 del_mtd_device(mtd);
1da177e4 2521
fa671646 2522 /* Free bad block table memory */
ace4dfee 2523 kfree(chip->bbt);
1da177e4
LT
2524}
2525
e0c7d767
DW
2526EXPORT_SYMBOL_GPL(nand_scan);
2527EXPORT_SYMBOL_GPL(nand_release);
8fe833c1
RP
2528
2529static int __init nand_base_init(void)
2530{
2531 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2532 return 0;
2533}
2534
2535static void __exit nand_base_exit(void)
2536{
2537 led_trigger_unregister_simple(nand_led_trigger);
2538}
2539
2540module_init(nand_base_init);
2541module_exit(nand_base_exit);
2542
e0c7d767
DW
2543MODULE_LICENSE("GPL");
2544MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2545MODULE_DESCRIPTION("Generic NAND flash driver code");