]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/mtd/nand/nand_base.c
mtd: physmap_of: Add non-obsolete map_rom probe
[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.
61b03bd7 7 *
1da177e4 8 * Additional technical information is available on
8b2b403c 9 * http://www.linux-mtd.infradead.org/doc/nand.html
61b03bd7 10 *
1da177e4 11 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
ace4dfee 12 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
1da177e4 13 *
ace4dfee 14 * Credits:
61b03bd7
TG
15 * David Woodhouse for adding multichip support
16 *
1da177e4
LT
17 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
18 * rework for 2K page size chips
19 *
ace4dfee 20 * TODO:
1da177e4
LT
21 * Enable cached programming for 2k page size chips
22 * Check, if mtd->ecctype should be set to MTD_ECC_HW
7854d3f7 23 * if we have HW ECC support.
c0b8ba7b 24 * BBT table is not serialized, has to be fixed
1da177e4 25 *
1da177e4
LT
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License version 2 as
28 * published by the Free Software Foundation.
29 *
30 */
31
20171642
EG
32#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
66507c7b 40#include <linux/mm.h>
1da177e4
LT
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
193bd400 45#include <linux/mtd/nand_bch.h>
1da177e4
LT
46#include <linux/interrupt.h>
47#include <linux/bitops.h>
8fe833c1 48#include <linux/leds.h>
7351d3a5 49#include <linux/io.h>
1da177e4 50#include <linux/mtd/partitions.h>
1da177e4
LT
51
52/* Define default oob placement schemes for large and small page devices */
5bd34c09 53static struct nand_ecclayout nand_oob_8 = {
1da177e4
LT
54 .eccbytes = 3,
55 .eccpos = {0, 1, 2},
5bd34c09
TG
56 .oobfree = {
57 {.offset = 3,
58 .length = 2},
59 {.offset = 6,
f8ac0414 60 .length = 2} }
1da177e4
LT
61};
62
5bd34c09 63static struct nand_ecclayout nand_oob_16 = {
1da177e4
LT
64 .eccbytes = 6,
65 .eccpos = {0, 1, 2, 3, 6, 7},
5bd34c09
TG
66 .oobfree = {
67 {.offset = 8,
f8ac0414 68 . length = 8} }
1da177e4
LT
69};
70
5bd34c09 71static struct nand_ecclayout nand_oob_64 = {
1da177e4
LT
72 .eccbytes = 24,
73 .eccpos = {
e0c7d767
DW
74 40, 41, 42, 43, 44, 45, 46, 47,
75 48, 49, 50, 51, 52, 53, 54, 55,
76 56, 57, 58, 59, 60, 61, 62, 63},
5bd34c09
TG
77 .oobfree = {
78 {.offset = 2,
f8ac0414 79 .length = 38} }
1da177e4
LT
80};
81
81ec5364
TG
82static struct nand_ecclayout nand_oob_128 = {
83 .eccbytes = 48,
84 .eccpos = {
85 80, 81, 82, 83, 84, 85, 86, 87,
86 88, 89, 90, 91, 92, 93, 94, 95,
87 96, 97, 98, 99, 100, 101, 102, 103,
88 104, 105, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 116, 117, 118, 119,
90 120, 121, 122, 123, 124, 125, 126, 127},
91 .oobfree = {
92 {.offset = 2,
f8ac0414 93 .length = 78} }
81ec5364
TG
94};
95
6a8214aa 96static int nand_get_device(struct mtd_info *mtd, int new_state);
1da177e4 97
8593fbc6
TG
98static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
99 struct mtd_oob_ops *ops);
100
d470a97c 101/*
8e87d782 102 * For devices which display every fart in the system on a separate LED. Is
d470a97c
TG
103 * compiled away when LED support is disabled.
104 */
105DEFINE_LED_TRIGGER(nand_led_trigger);
106
6fe5a6ac
VS
107static int check_offs_len(struct mtd_info *mtd,
108 loff_t ofs, uint64_t len)
109{
110 struct nand_chip *chip = mtd->priv;
111 int ret = 0;
112
113 /* Start address must align on block boundary */
daae74ca 114 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
289c0522 115 pr_debug("%s: unaligned address\n", __func__);
6fe5a6ac
VS
116 ret = -EINVAL;
117 }
118
119 /* Length must align on block boundary */
daae74ca 120 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
289c0522 121 pr_debug("%s: length not block aligned\n", __func__);
6fe5a6ac
VS
122 ret = -EINVAL;
123 }
124
6fe5a6ac
VS
125 return ret;
126}
127
1da177e4
LT
128/**
129 * nand_release_device - [GENERIC] release chip
8b6e50c9 130 * @mtd: MTD device structure
61b03bd7 131 *
b0bb6903 132 * Release chip lock and wake up anyone waiting on the device.
1da177e4 133 */
e0c7d767 134static void nand_release_device(struct mtd_info *mtd)
1da177e4 135{
ace4dfee 136 struct nand_chip *chip = mtd->priv;
1da177e4 137
a36ed299 138 /* Release the controller and the chip */
ace4dfee
TG
139 spin_lock(&chip->controller->lock);
140 chip->controller->active = NULL;
141 chip->state = FL_READY;
142 wake_up(&chip->controller->wq);
143 spin_unlock(&chip->controller->lock);
1da177e4
LT
144}
145
146/**
147 * nand_read_byte - [DEFAULT] read one byte from the chip
8b6e50c9 148 * @mtd: MTD device structure
1da177e4 149 *
7854d3f7 150 * Default read function for 8bit buswidth
1da177e4 151 */
58dd8f2b 152static uint8_t nand_read_byte(struct mtd_info *mtd)
1da177e4 153{
ace4dfee
TG
154 struct nand_chip *chip = mtd->priv;
155 return readb(chip->IO_ADDR_R);
1da177e4
LT
156}
157
1da177e4 158/**
064a7694 159 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
7854d3f7 160 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
8b6e50c9 161 * @mtd: MTD device structure
1da177e4 162 *
7854d3f7
BN
163 * Default read function for 16bit buswidth with endianness conversion.
164 *
1da177e4 165 */
58dd8f2b 166static uint8_t nand_read_byte16(struct mtd_info *mtd)
1da177e4 167{
ace4dfee
TG
168 struct nand_chip *chip = mtd->priv;
169 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
1da177e4
LT
170}
171
1da177e4
LT
172/**
173 * nand_read_word - [DEFAULT] read one word from the chip
8b6e50c9 174 * @mtd: MTD device structure
1da177e4 175 *
7854d3f7 176 * Default read function for 16bit buswidth without endianness conversion.
1da177e4
LT
177 */
178static u16 nand_read_word(struct mtd_info *mtd)
179{
ace4dfee
TG
180 struct nand_chip *chip = mtd->priv;
181 return readw(chip->IO_ADDR_R);
1da177e4
LT
182}
183
1da177e4
LT
184/**
185 * nand_select_chip - [DEFAULT] control CE line
8b6e50c9
BN
186 * @mtd: MTD device structure
187 * @chipnr: chipnumber to select, -1 for deselect
1da177e4
LT
188 *
189 * Default select function for 1 chip devices.
190 */
ace4dfee 191static void nand_select_chip(struct mtd_info *mtd, int chipnr)
1da177e4 192{
ace4dfee
TG
193 struct nand_chip *chip = mtd->priv;
194
195 switch (chipnr) {
1da177e4 196 case -1:
ace4dfee 197 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
1da177e4
LT
198 break;
199 case 0:
1da177e4
LT
200 break;
201
202 default:
203 BUG();
204 }
205}
206
05f78359
UKK
207/**
208 * nand_write_byte - [DEFAULT] write single byte to chip
209 * @mtd: MTD device structure
210 * @byte: value to write
211 *
212 * Default function to write a byte to I/O[7:0]
213 */
214static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
215{
216 struct nand_chip *chip = mtd->priv;
217
218 chip->write_buf(mtd, &byte, 1);
219}
220
221/**
222 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
223 * @mtd: MTD device structure
224 * @byte: value to write
225 *
226 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
227 */
228static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
229{
230 struct nand_chip *chip = mtd->priv;
231 uint16_t word = byte;
232
233 /*
234 * It's not entirely clear what should happen to I/O[15:8] when writing
235 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
236 *
237 * When the host supports a 16-bit bus width, only data is
238 * transferred at the 16-bit width. All address and command line
239 * transfers shall use only the lower 8-bits of the data bus. During
240 * command transfers, the host may place any value on the upper
241 * 8-bits of the data bus. During address transfers, the host shall
242 * set the upper 8-bits of the data bus to 00h.
243 *
244 * One user of the write_byte callback is nand_onfi_set_features. The
245 * four parameters are specified to be written to I/O[7:0], but this is
246 * neither an address nor a command transfer. Let's assume a 0 on the
247 * upper I/O lines is OK.
248 */
249 chip->write_buf(mtd, (uint8_t *)&word, 2);
250}
251
1da177e4
LT
252/**
253 * nand_write_buf - [DEFAULT] write buffer to chip
8b6e50c9
BN
254 * @mtd: MTD device structure
255 * @buf: data buffer
256 * @len: number of bytes to write
1da177e4 257 *
7854d3f7 258 * Default write function for 8bit buswidth.
1da177e4 259 */
58dd8f2b 260static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
1da177e4 261{
ace4dfee 262 struct nand_chip *chip = mtd->priv;
1da177e4 263
76413839 264 iowrite8_rep(chip->IO_ADDR_W, buf, len);
1da177e4
LT
265}
266
267/**
61b03bd7 268 * nand_read_buf - [DEFAULT] read chip data into buffer
8b6e50c9
BN
269 * @mtd: MTD device structure
270 * @buf: buffer to store date
271 * @len: number of bytes to read
1da177e4 272 *
7854d3f7 273 * Default read function for 8bit buswidth.
1da177e4 274 */
58dd8f2b 275static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1da177e4 276{
ace4dfee 277 struct nand_chip *chip = mtd->priv;
1da177e4 278
76413839 279 ioread8_rep(chip->IO_ADDR_R, buf, len);
1da177e4
LT
280}
281
1da177e4
LT
282/**
283 * nand_write_buf16 - [DEFAULT] write buffer to chip
8b6e50c9
BN
284 * @mtd: MTD device structure
285 * @buf: data buffer
286 * @len: number of bytes to write
1da177e4 287 *
7854d3f7 288 * Default write function for 16bit buswidth.
1da177e4 289 */
58dd8f2b 290static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
1da177e4 291{
ace4dfee 292 struct nand_chip *chip = mtd->priv;
1da177e4 293 u16 *p = (u16 *) buf;
61b03bd7 294
76413839 295 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
1da177e4
LT
296}
297
298/**
61b03bd7 299 * nand_read_buf16 - [DEFAULT] read chip data into buffer
8b6e50c9
BN
300 * @mtd: MTD device structure
301 * @buf: buffer to store date
302 * @len: number of bytes to read
1da177e4 303 *
7854d3f7 304 * Default read function for 16bit buswidth.
1da177e4 305 */
58dd8f2b 306static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
1da177e4 307{
ace4dfee 308 struct nand_chip *chip = mtd->priv;
1da177e4 309 u16 *p = (u16 *) buf;
1da177e4 310
76413839 311 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
1da177e4
LT
312}
313
1da177e4
LT
314/**
315 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
8b6e50c9
BN
316 * @mtd: MTD device structure
317 * @ofs: offset from device start
318 * @getchip: 0, if the chip is already selected
1da177e4 319 *
61b03bd7 320 * Check, if the block is bad.
1da177e4
LT
321 */
322static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
323{
cdbec050 324 int page, chipnr, res = 0, i = 0;
ace4dfee 325 struct nand_chip *chip = mtd->priv;
1da177e4
LT
326 u16 bad;
327
5fb1549d 328 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
b60b08b0
KC
329 ofs += mtd->erasesize - mtd->writesize;
330
1a12f46a
TK
331 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
332
1da177e4 333 if (getchip) {
ace4dfee 334 chipnr = (int)(ofs >> chip->chip_shift);
1da177e4 335
6a8214aa 336 nand_get_device(mtd, FL_READING);
1da177e4
LT
337
338 /* Select the NAND device */
ace4dfee 339 chip->select_chip(mtd, chipnr);
1a12f46a 340 }
1da177e4 341
cdbec050
BN
342 do {
343 if (chip->options & NAND_BUSWIDTH_16) {
344 chip->cmdfunc(mtd, NAND_CMD_READOOB,
345 chip->badblockpos & 0xFE, page);
346 bad = cpu_to_le16(chip->read_word(mtd));
347 if (chip->badblockpos & 0x1)
348 bad >>= 8;
349 else
350 bad &= 0xFF;
351 } else {
352 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
353 page);
354 bad = chip->read_byte(mtd);
355 }
356
357 if (likely(chip->badblockbits == 8))
358 res = bad != 0xFF;
e0b58d0a 359 else
cdbec050
BN
360 res = hweight8(bad) < chip->badblockbits;
361 ofs += mtd->writesize;
362 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
363 i++;
364 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
e0b58d0a 365
b0bb6903
HS
366 if (getchip) {
367 chip->select_chip(mtd, -1);
1da177e4 368 nand_release_device(mtd);
b0bb6903 369 }
61b03bd7 370
1da177e4
LT
371 return res;
372}
373
374/**
5a0edb25 375 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
8b6e50c9
BN
376 * @mtd: MTD device structure
377 * @ofs: offset from device start
1da177e4 378 *
8b6e50c9 379 * This is the default implementation, which can be overridden by a hardware
5a0edb25
BN
380 * specific driver. It provides the details for writing a bad block marker to a
381 * block.
382 */
383static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
384{
385 struct nand_chip *chip = mtd->priv;
386 struct mtd_oob_ops ops;
387 uint8_t buf[2] = { 0, 0 };
388 int ret = 0, res, i = 0;
389
390 ops.datbuf = NULL;
391 ops.oobbuf = buf;
392 ops.ooboffs = chip->badblockpos;
393 if (chip->options & NAND_BUSWIDTH_16) {
394 ops.ooboffs &= ~0x01;
395 ops.len = ops.ooblen = 2;
396 } else {
397 ops.len = ops.ooblen = 1;
398 }
399 ops.mode = MTD_OPS_PLACE_OOB;
400
401 /* Write to first/last page(s) if necessary */
402 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
403 ofs += mtd->erasesize - mtd->writesize;
404 do {
405 res = nand_do_write_oob(mtd, ofs, &ops);
406 if (!ret)
407 ret = res;
408
409 i++;
410 ofs += mtd->writesize;
411 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
412
413 return ret;
414}
415
416/**
417 * nand_block_markbad_lowlevel - mark a block bad
418 * @mtd: MTD device structure
419 * @ofs: offset from device start
420 *
421 * This function performs the generic NAND bad block marking steps (i.e., bad
422 * block table(s) and/or marker(s)). We only allow the hardware driver to
423 * specify how to write bad block markers to OOB (chip->block_markbad).
424 *
b32843b7 425 * We try operations in the following order:
e2414f4c 426 * (1) erase the affected block, to allow OOB marker to be written cleanly
b32843b7
BN
427 * (2) write bad block marker to OOB area of affected block (unless flag
428 * NAND_BBT_NO_OOB_BBM is present)
429 * (3) update the BBT
430 * Note that we retain the first error encountered in (2) or (3), finish the
e2414f4c 431 * procedures, and dump the error in the end.
1da177e4 432*/
5a0edb25 433static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
1da177e4 434{
ace4dfee 435 struct nand_chip *chip = mtd->priv;
b32843b7 436 int res, ret = 0;
61b03bd7 437
b32843b7 438 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
00918429
BN
439 struct erase_info einfo;
440
441 /* Attempt erase before marking OOB */
442 memset(&einfo, 0, sizeof(einfo));
443 einfo.mtd = mtd;
444 einfo.addr = ofs;
daae74ca 445 einfo.len = 1ULL << chip->phys_erase_shift;
00918429 446 nand_erase_nand(mtd, &einfo, 0);
1da177e4 447
b32843b7 448 /* Write bad block marker to OOB */
6a8214aa 449 nand_get_device(mtd, FL_WRITING);
5a0edb25 450 ret = chip->block_markbad(mtd, ofs);
c0b8ba7b 451 nand_release_device(mtd);
f1a28c02 452 }
e2414f4c 453
b32843b7
BN
454 /* Mark block bad in BBT */
455 if (chip->bbt) {
456 res = nand_markbad_bbt(mtd, ofs);
e2414f4c
BN
457 if (!ret)
458 ret = res;
459 }
460
f1a28c02
TG
461 if (!ret)
462 mtd->ecc_stats.badblocks++;
c0b8ba7b 463
f1a28c02 464 return ret;
1da177e4
LT
465}
466
61b03bd7 467/**
1da177e4 468 * nand_check_wp - [GENERIC] check if the chip is write protected
8b6e50c9 469 * @mtd: MTD device structure
1da177e4 470 *
8b6e50c9
BN
471 * Check, if the device is write protected. The function expects, that the
472 * device is already selected.
1da177e4 473 */
e0c7d767 474static int nand_check_wp(struct mtd_info *mtd)
1da177e4 475{
ace4dfee 476 struct nand_chip *chip = mtd->priv;
93edbad6 477
8b6e50c9 478 /* Broken xD cards report WP despite being writable */
93edbad6
ML
479 if (chip->options & NAND_BROKEN_XD)
480 return 0;
481
1da177e4 482 /* Check the WP bit */
ace4dfee
TG
483 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
484 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
1da177e4
LT
485}
486
8471bb73
EG
487/**
488 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
489 * @mtd: MTD device structure
490 * @ofs: offset from device start
491 *
492 * Check if the block is mark as reserved.
493 */
494static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
495{
496 struct nand_chip *chip = mtd->priv;
497
498 if (!chip->bbt)
499 return 0;
500 /* Return info from the table */
501 return nand_isreserved_bbt(mtd, ofs);
502}
503
1da177e4
LT
504/**
505 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
8b6e50c9
BN
506 * @mtd: MTD device structure
507 * @ofs: offset from device start
508 * @getchip: 0, if the chip is already selected
509 * @allowbbt: 1, if its allowed to access the bbt area
1da177e4
LT
510 *
511 * Check, if the block is bad. Either by reading the bad block table or
512 * calling of the scan function.
513 */
2c0a2bed
TG
514static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
515 int allowbbt)
1da177e4 516{
ace4dfee 517 struct nand_chip *chip = mtd->priv;
61b03bd7 518
ace4dfee
TG
519 if (!chip->bbt)
520 return chip->block_bad(mtd, ofs, getchip);
61b03bd7 521
1da177e4 522 /* Return info from the table */
e0c7d767 523 return nand_isbad_bbt(mtd, ofs, allowbbt);
1da177e4
LT
524}
525
2af7c653
SK
526/**
527 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
8b6e50c9
BN
528 * @mtd: MTD device structure
529 * @timeo: Timeout
2af7c653
SK
530 *
531 * Helper function for nand_wait_ready used when needing to wait in interrupt
532 * context.
533 */
534static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
535{
536 struct nand_chip *chip = mtd->priv;
537 int i;
538
539 /* Wait for the device to get ready */
540 for (i = 0; i < timeo; i++) {
541 if (chip->dev_ready(mtd))
542 break;
543 touch_softlockup_watchdog();
544 mdelay(1);
545 }
546}
547
7854d3f7 548/* Wait for the ready pin, after a command. The timeout is caught later. */
4b648b02 549void nand_wait_ready(struct mtd_info *mtd)
3b88775c 550{
ace4dfee 551 struct nand_chip *chip = mtd->priv;
ca6a2489 552 unsigned long timeo = jiffies + msecs_to_jiffies(20);
3b88775c 553
2af7c653
SK
554 /* 400ms timeout */
555 if (in_interrupt() || oops_in_progress)
556 return panic_nand_wait_ready(mtd, 400);
557
8fe833c1 558 led_trigger_event(nand_led_trigger, LED_FULL);
7854d3f7 559 /* Wait until command is processed or timeout occurs */
3b88775c 560 do {
ace4dfee 561 if (chip->dev_ready(mtd))
8fe833c1 562 break;
8446f1d3 563 touch_softlockup_watchdog();
61b03bd7 564 } while (time_before(jiffies, timeo));
8fe833c1 565 led_trigger_event(nand_led_trigger, LED_OFF);
3b88775c 566}
4b648b02 567EXPORT_SYMBOL_GPL(nand_wait_ready);
3b88775c 568
1da177e4
LT
569/**
570 * nand_command - [DEFAULT] Send command to NAND device
8b6e50c9
BN
571 * @mtd: MTD device structure
572 * @command: the command to be sent
573 * @column: the column address for this command, -1 if none
574 * @page_addr: the page address for this command, -1 if none
1da177e4 575 *
8b6e50c9 576 * Send command to NAND device. This function is used for small page devices
51148f1f 577 * (512 Bytes per page).
1da177e4 578 */
7abd3ef9
TG
579static void nand_command(struct mtd_info *mtd, unsigned int command,
580 int column, int page_addr)
1da177e4 581{
ace4dfee 582 register struct nand_chip *chip = mtd->priv;
7abd3ef9 583 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
1da177e4 584
8b6e50c9 585 /* Write out the command to the device */
1da177e4
LT
586 if (command == NAND_CMD_SEQIN) {
587 int readcmd;
588
28318776 589 if (column >= mtd->writesize) {
1da177e4 590 /* OOB area */
28318776 591 column -= mtd->writesize;
1da177e4
LT
592 readcmd = NAND_CMD_READOOB;
593 } else if (column < 256) {
594 /* First 256 bytes --> READ0 */
595 readcmd = NAND_CMD_READ0;
596 } else {
597 column -= 256;
598 readcmd = NAND_CMD_READ1;
599 }
ace4dfee 600 chip->cmd_ctrl(mtd, readcmd, ctrl);
7abd3ef9 601 ctrl &= ~NAND_CTRL_CHANGE;
1da177e4 602 }
ace4dfee 603 chip->cmd_ctrl(mtd, command, ctrl);
1da177e4 604
8b6e50c9 605 /* Address cycle, when necessary */
7abd3ef9
TG
606 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
607 /* Serially input address */
608 if (column != -1) {
609 /* Adjust columns for 16 bit buswidth */
3dad2344
BN
610 if (chip->options & NAND_BUSWIDTH_16 &&
611 !nand_opcode_8bits(command))
7abd3ef9 612 column >>= 1;
ace4dfee 613 chip->cmd_ctrl(mtd, column, ctrl);
7abd3ef9
TG
614 ctrl &= ~NAND_CTRL_CHANGE;
615 }
616 if (page_addr != -1) {
ace4dfee 617 chip->cmd_ctrl(mtd, page_addr, ctrl);
7abd3ef9 618 ctrl &= ~NAND_CTRL_CHANGE;
ace4dfee 619 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
7abd3ef9 620 /* One more address cycle for devices > 32MiB */
ace4dfee
TG
621 if (chip->chipsize > (32 << 20))
622 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
1da177e4 623 }
ace4dfee 624 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
61b03bd7
TG
625
626 /*
8b6e50c9
BN
627 * Program and erase have their own busy handlers status and sequential
628 * in needs no delay
e0c7d767 629 */
1da177e4 630 switch (command) {
61b03bd7 631
1da177e4
LT
632 case NAND_CMD_PAGEPROG:
633 case NAND_CMD_ERASE1:
634 case NAND_CMD_ERASE2:
635 case NAND_CMD_SEQIN:
636 case NAND_CMD_STATUS:
637 return;
638
639 case NAND_CMD_RESET:
ace4dfee 640 if (chip->dev_ready)
1da177e4 641 break;
ace4dfee
TG
642 udelay(chip->chip_delay);
643 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
7abd3ef9 644 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
12efdde3
TG
645 chip->cmd_ctrl(mtd,
646 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
f8ac0414
FF
647 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
648 ;
1da177e4
LT
649 return;
650
e0c7d767 651 /* This applies to read commands */
1da177e4 652 default:
61b03bd7 653 /*
1da177e4
LT
654 * If we don't have access to the busy pin, we apply the given
655 * command delay
e0c7d767 656 */
ace4dfee
TG
657 if (!chip->dev_ready) {
658 udelay(chip->chip_delay);
1da177e4 659 return;
61b03bd7 660 }
1da177e4 661 }
8b6e50c9
BN
662 /*
663 * Apply this short delay always to ensure that we do wait tWB in
664 * any case on any machine.
665 */
e0c7d767 666 ndelay(100);
3b88775c
TG
667
668 nand_wait_ready(mtd);
1da177e4
LT
669}
670
671/**
672 * nand_command_lp - [DEFAULT] Send command to NAND large page device
8b6e50c9
BN
673 * @mtd: MTD device structure
674 * @command: the command to be sent
675 * @column: the column address for this command, -1 if none
676 * @page_addr: the page address for this command, -1 if none
1da177e4 677 *
7abd3ef9 678 * Send command to NAND device. This is the version for the new large page
7854d3f7
BN
679 * devices. We don't have the separate regions as we have in the small page
680 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
1da177e4 681 */
7abd3ef9
TG
682static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
683 int column, int page_addr)
1da177e4 684{
ace4dfee 685 register struct nand_chip *chip = mtd->priv;
1da177e4
LT
686
687 /* Emulate NAND_CMD_READOOB */
688 if (command == NAND_CMD_READOOB) {
28318776 689 column += mtd->writesize;
1da177e4
LT
690 command = NAND_CMD_READ0;
691 }
61b03bd7 692
7abd3ef9 693 /* Command latch cycle */
fb066ada 694 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
1da177e4
LT
695
696 if (column != -1 || page_addr != -1) {
7abd3ef9 697 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
1da177e4
LT
698
699 /* Serially input address */
700 if (column != -1) {
701 /* Adjust columns for 16 bit buswidth */
3dad2344
BN
702 if (chip->options & NAND_BUSWIDTH_16 &&
703 !nand_opcode_8bits(command))
1da177e4 704 column >>= 1;
ace4dfee 705 chip->cmd_ctrl(mtd, column, ctrl);
7abd3ef9 706 ctrl &= ~NAND_CTRL_CHANGE;
ace4dfee 707 chip->cmd_ctrl(mtd, column >> 8, ctrl);
61b03bd7 708 }
1da177e4 709 if (page_addr != -1) {
ace4dfee
TG
710 chip->cmd_ctrl(mtd, page_addr, ctrl);
711 chip->cmd_ctrl(mtd, page_addr >> 8,
7abd3ef9 712 NAND_NCE | NAND_ALE);
1da177e4 713 /* One more address cycle for devices > 128MiB */
ace4dfee
TG
714 if (chip->chipsize > (128 << 20))
715 chip->cmd_ctrl(mtd, page_addr >> 16,
7abd3ef9 716 NAND_NCE | NAND_ALE);
1da177e4 717 }
1da177e4 718 }
ace4dfee 719 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
61b03bd7
TG
720
721 /*
8b6e50c9
BN
722 * Program and erase have their own busy handlers status, sequential
723 * in, and deplete1 need no delay.
30f464b7 724 */
1da177e4 725 switch (command) {
61b03bd7 726
1da177e4
LT
727 case NAND_CMD_CACHEDPROG:
728 case NAND_CMD_PAGEPROG:
729 case NAND_CMD_ERASE1:
730 case NAND_CMD_ERASE2:
731 case NAND_CMD_SEQIN:
7bc3312b 732 case NAND_CMD_RNDIN:
1da177e4 733 case NAND_CMD_STATUS:
30f464b7 734 return;
1da177e4
LT
735
736 case NAND_CMD_RESET:
ace4dfee 737 if (chip->dev_ready)
1da177e4 738 break;
ace4dfee 739 udelay(chip->chip_delay);
12efdde3
TG
740 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
741 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
742 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
743 NAND_NCE | NAND_CTRL_CHANGE);
f8ac0414
FF
744 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
745 ;
1da177e4
LT
746 return;
747
7bc3312b
TG
748 case NAND_CMD_RNDOUT:
749 /* No ready / busy check necessary */
750 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
751 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
752 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
753 NAND_NCE | NAND_CTRL_CHANGE);
754 return;
755
1da177e4 756 case NAND_CMD_READ0:
12efdde3
TG
757 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
758 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
759 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
760 NAND_NCE | NAND_CTRL_CHANGE);
61b03bd7 761
e0c7d767 762 /* This applies to read commands */
1da177e4 763 default:
61b03bd7 764 /*
1da177e4 765 * If we don't have access to the busy pin, we apply the given
8b6e50c9 766 * command delay.
e0c7d767 767 */
ace4dfee
TG
768 if (!chip->dev_ready) {
769 udelay(chip->chip_delay);
1da177e4 770 return;
61b03bd7 771 }
1da177e4 772 }
3b88775c 773
8b6e50c9
BN
774 /*
775 * Apply this short delay always to ensure that we do wait tWB in
776 * any case on any machine.
777 */
e0c7d767 778 ndelay(100);
3b88775c
TG
779
780 nand_wait_ready(mtd);
1da177e4
LT
781}
782
2af7c653
SK
783/**
784 * panic_nand_get_device - [GENERIC] Get chip for selected access
8b6e50c9
BN
785 * @chip: the nand chip descriptor
786 * @mtd: MTD device structure
787 * @new_state: the state which is requested
2af7c653
SK
788 *
789 * Used when in panic, no locks are taken.
790 */
791static void panic_nand_get_device(struct nand_chip *chip,
792 struct mtd_info *mtd, int new_state)
793{
7854d3f7 794 /* Hardware controller shared among independent devices */
2af7c653
SK
795 chip->controller->active = chip;
796 chip->state = new_state;
797}
798
1da177e4
LT
799/**
800 * nand_get_device - [GENERIC] Get chip for selected access
8b6e50c9
BN
801 * @mtd: MTD device structure
802 * @new_state: the state which is requested
1da177e4
LT
803 *
804 * Get the device and lock it for exclusive access
805 */
2c0a2bed 806static int
6a8214aa 807nand_get_device(struct mtd_info *mtd, int new_state)
1da177e4 808{
6a8214aa 809 struct nand_chip *chip = mtd->priv;
ace4dfee
TG
810 spinlock_t *lock = &chip->controller->lock;
811 wait_queue_head_t *wq = &chip->controller->wq;
e0c7d767 812 DECLARE_WAITQUEUE(wait, current);
7351d3a5 813retry:
0dfc6246
TG
814 spin_lock(lock);
815
b8b3ee9a 816 /* Hardware controller shared among independent devices */
ace4dfee
TG
817 if (!chip->controller->active)
818 chip->controller->active = chip;
a36ed299 819
ace4dfee
TG
820 if (chip->controller->active == chip && chip->state == FL_READY) {
821 chip->state = new_state;
0dfc6246 822 spin_unlock(lock);
962034f4
VW
823 return 0;
824 }
825 if (new_state == FL_PM_SUSPENDED) {
6b0d9a84
LY
826 if (chip->controller->active->state == FL_PM_SUSPENDED) {
827 chip->state = FL_PM_SUSPENDED;
828 spin_unlock(lock);
829 return 0;
6b0d9a84 830 }
0dfc6246
TG
831 }
832 set_current_state(TASK_UNINTERRUPTIBLE);
833 add_wait_queue(wq, &wait);
834 spin_unlock(lock);
835 schedule();
836 remove_wait_queue(wq, &wait);
1da177e4
LT
837 goto retry;
838}
839
2af7c653 840/**
8b6e50c9
BN
841 * panic_nand_wait - [GENERIC] wait until the command is done
842 * @mtd: MTD device structure
843 * @chip: NAND chip structure
844 * @timeo: timeout
2af7c653
SK
845 *
846 * Wait for command done. This is a helper function for nand_wait used when
847 * we are in interrupt context. May happen when in panic and trying to write
b595076a 848 * an oops through mtdoops.
2af7c653
SK
849 */
850static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
851 unsigned long timeo)
852{
853 int i;
854 for (i = 0; i < timeo; i++) {
855 if (chip->dev_ready) {
856 if (chip->dev_ready(mtd))
857 break;
858 } else {
859 if (chip->read_byte(mtd) & NAND_STATUS_READY)
860 break;
861 }
862 mdelay(1);
f8ac0414 863 }
2af7c653
SK
864}
865
1da177e4 866/**
8b6e50c9
BN
867 * nand_wait - [DEFAULT] wait until the command is done
868 * @mtd: MTD device structure
869 * @chip: NAND chip structure
1da177e4 870 *
8b6e50c9
BN
871 * Wait for command done. This applies to erase and program only. Erase can
872 * take up to 400ms and program up to 20ms according to general NAND and
873 * SmartMedia specs.
844d3b42 874 */
7bc3312b 875static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
1da177e4
LT
876{
877
7bc3312b 878 int status, state = chip->state;
6d2559f8 879 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
1da177e4 880
8fe833c1
RP
881 led_trigger_event(nand_led_trigger, LED_FULL);
882
8b6e50c9
BN
883 /*
884 * Apply this short delay always to ensure that we do wait tWB in any
885 * case on any machine.
886 */
e0c7d767 887 ndelay(100);
1da177e4 888
14c65786 889 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1da177e4 890
2af7c653
SK
891 if (in_interrupt() || oops_in_progress)
892 panic_nand_wait(mtd, chip, timeo);
893 else {
6d2559f8 894 timeo = jiffies + msecs_to_jiffies(timeo);
2af7c653
SK
895 while (time_before(jiffies, timeo)) {
896 if (chip->dev_ready) {
897 if (chip->dev_ready(mtd))
898 break;
899 } else {
900 if (chip->read_byte(mtd) & NAND_STATUS_READY)
901 break;
902 }
903 cond_resched();
1da177e4 904 }
1da177e4 905 }
8fe833c1
RP
906 led_trigger_event(nand_led_trigger, LED_OFF);
907
ace4dfee 908 status = (int)chip->read_byte(mtd);
f251b8df
MC
909 /* This can happen if in case of timeout or buggy dev_ready */
910 WARN_ON(!(status & NAND_STATUS_READY));
1da177e4
LT
911 return status;
912}
913
7d70f334 914/**
b6d676db 915 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
b6d676db
RD
916 * @mtd: mtd info
917 * @ofs: offset to start unlock from
918 * @len: length to unlock
8b6e50c9
BN
919 * @invert: when = 0, unlock the range of blocks within the lower and
920 * upper boundary address
921 * when = 1, unlock the range of blocks outside the boundaries
922 * of the lower and upper boundary address
7d70f334 923 *
8b6e50c9 924 * Returs unlock status.
7d70f334
VS
925 */
926static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
927 uint64_t len, int invert)
928{
929 int ret = 0;
930 int status, page;
931 struct nand_chip *chip = mtd->priv;
932
933 /* Submit address of first page to unlock */
934 page = ofs >> chip->page_shift;
935 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
936
937 /* Submit address of last page to unlock */
938 page = (ofs + len) >> chip->page_shift;
939 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
940 (page | invert) & chip->pagemask);
941
942 /* Call wait ready function */
943 status = chip->waitfunc(mtd, chip);
7d70f334 944 /* See if device thinks it succeeded */
74830966 945 if (status & NAND_STATUS_FAIL) {
289c0522 946 pr_debug("%s: error status = 0x%08x\n",
7d70f334
VS
947 __func__, status);
948 ret = -EIO;
949 }
950
951 return ret;
952}
953
954/**
b6d676db 955 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
b6d676db
RD
956 * @mtd: mtd info
957 * @ofs: offset to start unlock from
958 * @len: length to unlock
7d70f334 959 *
8b6e50c9 960 * Returns unlock status.
7d70f334
VS
961 */
962int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
963{
964 int ret = 0;
965 int chipnr;
966 struct nand_chip *chip = mtd->priv;
967
289c0522 968 pr_debug("%s: start = 0x%012llx, len = %llu\n",
7d70f334
VS
969 __func__, (unsigned long long)ofs, len);
970
971 if (check_offs_len(mtd, ofs, len))
972 ret = -EINVAL;
973
974 /* Align to last block address if size addresses end of the device */
975 if (ofs + len == mtd->size)
976 len -= mtd->erasesize;
977
6a8214aa 978 nand_get_device(mtd, FL_UNLOCKING);
7d70f334
VS
979
980 /* Shift to get chip number */
981 chipnr = ofs >> chip->chip_shift;
982
983 chip->select_chip(mtd, chipnr);
984
57d3a9a8
WD
985 /*
986 * Reset the chip.
987 * If we want to check the WP through READ STATUS and check the bit 7
988 * we must reset the chip
989 * some operation can also clear the bit 7 of status register
990 * eg. erase/program a locked block
991 */
992 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
993
7d70f334
VS
994 /* Check, if it is write protected */
995 if (nand_check_wp(mtd)) {
289c0522 996 pr_debug("%s: device is write protected!\n",
7d70f334
VS
997 __func__);
998 ret = -EIO;
999 goto out;
1000 }
1001
1002 ret = __nand_unlock(mtd, ofs, len, 0);
1003
1004out:
b0bb6903 1005 chip->select_chip(mtd, -1);
7d70f334
VS
1006 nand_release_device(mtd);
1007
1008 return ret;
1009}
7351d3a5 1010EXPORT_SYMBOL(nand_unlock);
7d70f334
VS
1011
1012/**
b6d676db 1013 * nand_lock - [REPLACEABLE] locks all blocks present in the device
b6d676db
RD
1014 * @mtd: mtd info
1015 * @ofs: offset to start unlock from
1016 * @len: length to unlock
7d70f334 1017 *
8b6e50c9
BN
1018 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1019 * have this feature, but it allows only to lock all blocks, not for specified
1020 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1021 * now.
7d70f334 1022 *
8b6e50c9 1023 * Returns lock status.
7d70f334
VS
1024 */
1025int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1026{
1027 int ret = 0;
1028 int chipnr, status, page;
1029 struct nand_chip *chip = mtd->priv;
1030
289c0522 1031 pr_debug("%s: start = 0x%012llx, len = %llu\n",
7d70f334
VS
1032 __func__, (unsigned long long)ofs, len);
1033
1034 if (check_offs_len(mtd, ofs, len))
1035 ret = -EINVAL;
1036
6a8214aa 1037 nand_get_device(mtd, FL_LOCKING);
7d70f334
VS
1038
1039 /* Shift to get chip number */
1040 chipnr = ofs >> chip->chip_shift;
1041
1042 chip->select_chip(mtd, chipnr);
1043
57d3a9a8
WD
1044 /*
1045 * Reset the chip.
1046 * If we want to check the WP through READ STATUS and check the bit 7
1047 * we must reset the chip
1048 * some operation can also clear the bit 7 of status register
1049 * eg. erase/program a locked block
1050 */
1051 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1052
7d70f334
VS
1053 /* Check, if it is write protected */
1054 if (nand_check_wp(mtd)) {
289c0522 1055 pr_debug("%s: device is write protected!\n",
7d70f334
VS
1056 __func__);
1057 status = MTD_ERASE_FAILED;
1058 ret = -EIO;
1059 goto out;
1060 }
1061
1062 /* Submit address of first page to lock */
1063 page = ofs >> chip->page_shift;
1064 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1065
1066 /* Call wait ready function */
1067 status = chip->waitfunc(mtd, chip);
7d70f334 1068 /* See if device thinks it succeeded */
74830966 1069 if (status & NAND_STATUS_FAIL) {
289c0522 1070 pr_debug("%s: error status = 0x%08x\n",
7d70f334
VS
1071 __func__, status);
1072 ret = -EIO;
1073 goto out;
1074 }
1075
1076 ret = __nand_unlock(mtd, ofs, len, 0x1);
1077
1078out:
b0bb6903 1079 chip->select_chip(mtd, -1);
7d70f334
VS
1080 nand_release_device(mtd);
1081
1082 return ret;
1083}
7351d3a5 1084EXPORT_SYMBOL(nand_lock);
7d70f334 1085
8593fbc6 1086/**
7854d3f7 1087 * nand_read_page_raw - [INTERN] read raw page data without ecc
8b6e50c9
BN
1088 * @mtd: mtd info structure
1089 * @chip: nand chip info structure
1090 * @buf: buffer to store read data
1fbb938d 1091 * @oob_required: caller requires OOB data read to chip->oob_poi
8b6e50c9 1092 * @page: page number to read
52ff49df 1093 *
7854d3f7 1094 * Not for syndrome calculating ECC controllers, which use a special oob layout.
8593fbc6
TG
1095 */
1096static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1fbb938d 1097 uint8_t *buf, int oob_required, int page)
8593fbc6
TG
1098{
1099 chip->read_buf(mtd, buf, mtd->writesize);
279f08d4
BN
1100 if (oob_required)
1101 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
8593fbc6
TG
1102 return 0;
1103}
1104
52ff49df 1105/**
7854d3f7 1106 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
8b6e50c9
BN
1107 * @mtd: mtd info structure
1108 * @chip: nand chip info structure
1109 * @buf: buffer to store read data
1fbb938d 1110 * @oob_required: caller requires OOB data read to chip->oob_poi
8b6e50c9 1111 * @page: page number to read
52ff49df
DB
1112 *
1113 * We need a special oob layout and handling even when OOB isn't used.
1114 */
7351d3a5 1115static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1fbb938d
BN
1116 struct nand_chip *chip, uint8_t *buf,
1117 int oob_required, int page)
52ff49df
DB
1118{
1119 int eccsize = chip->ecc.size;
1120 int eccbytes = chip->ecc.bytes;
1121 uint8_t *oob = chip->oob_poi;
1122 int steps, size;
1123
1124 for (steps = chip->ecc.steps; steps > 0; steps--) {
1125 chip->read_buf(mtd, buf, eccsize);
1126 buf += eccsize;
1127
1128 if (chip->ecc.prepad) {
1129 chip->read_buf(mtd, oob, chip->ecc.prepad);
1130 oob += chip->ecc.prepad;
1131 }
1132
1133 chip->read_buf(mtd, oob, eccbytes);
1134 oob += eccbytes;
1135
1136 if (chip->ecc.postpad) {
1137 chip->read_buf(mtd, oob, chip->ecc.postpad);
1138 oob += chip->ecc.postpad;
1139 }
1140 }
1141
1142 size = mtd->oobsize - (oob - chip->oob_poi);
1143 if (size)
1144 chip->read_buf(mtd, oob, size);
1145
1146 return 0;
1147}
1148
1da177e4 1149/**
7854d3f7 1150 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
8b6e50c9
BN
1151 * @mtd: mtd info structure
1152 * @chip: nand chip info structure
1153 * @buf: buffer to store read data
1fbb938d 1154 * @oob_required: caller requires OOB data read to chip->oob_poi
8b6e50c9 1155 * @page: page number to read
068e3c0a 1156 */
f5bbdacc 1157static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1fbb938d 1158 uint8_t *buf, int oob_required, int page)
1da177e4 1159{
f5bbdacc
TG
1160 int i, eccsize = chip->ecc.size;
1161 int eccbytes = chip->ecc.bytes;
1162 int eccsteps = chip->ecc.steps;
1163 uint8_t *p = buf;
4bf63fcb
DW
1164 uint8_t *ecc_calc = chip->buffers->ecccalc;
1165 uint8_t *ecc_code = chip->buffers->ecccode;
8b099a39 1166 uint32_t *eccpos = chip->ecc.layout->eccpos;
3f91e94f 1167 unsigned int max_bitflips = 0;
f5bbdacc 1168
1fbb938d 1169 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
f5bbdacc
TG
1170
1171 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1172 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1173
1174 for (i = 0; i < chip->ecc.total; i++)
f75e5097 1175 ecc_code[i] = chip->oob_poi[eccpos[i]];
f5bbdacc
TG
1176
1177 eccsteps = chip->ecc.steps;
1178 p = buf;
1179
1180 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1181 int stat;
1182
1183 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3f91e94f 1184 if (stat < 0) {
f5bbdacc 1185 mtd->ecc_stats.failed++;
3f91e94f 1186 } else {
f5bbdacc 1187 mtd->ecc_stats.corrected += stat;
3f91e94f
MD
1188 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1189 }
f5bbdacc 1190 }
3f91e94f 1191 return max_bitflips;
22c60f5f 1192}
1da177e4 1193
3d459559 1194/**
837a6ba4 1195 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
8b6e50c9
BN
1196 * @mtd: mtd info structure
1197 * @chip: nand chip info structure
1198 * @data_offs: offset of requested data within the page
1199 * @readlen: data length
1200 * @bufpoi: buffer to store read data
e004debd 1201 * @page: page number to read
3d459559 1202 */
7351d3a5 1203static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
e004debd
HS
1204 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1205 int page)
3d459559
AK
1206{
1207 int start_step, end_step, num_steps;
1208 uint32_t *eccpos = chip->ecc.layout->eccpos;
1209 uint8_t *p;
1210 int data_col_addr, i, gaps = 0;
1211 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1212 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
4a4163ca 1213 int index;
3f91e94f 1214 unsigned int max_bitflips = 0;
3d459559 1215
7854d3f7 1216 /* Column address within the page aligned to ECC size (256bytes) */
3d459559
AK
1217 start_step = data_offs / chip->ecc.size;
1218 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1219 num_steps = end_step - start_step + 1;
4a4163ca 1220 index = start_step * chip->ecc.bytes;
3d459559 1221
8b6e50c9 1222 /* Data size aligned to ECC ecc.size */
3d459559
AK
1223 datafrag_len = num_steps * chip->ecc.size;
1224 eccfrag_len = num_steps * chip->ecc.bytes;
1225
1226 data_col_addr = start_step * chip->ecc.size;
1227 /* If we read not a page aligned data */
1228 if (data_col_addr != 0)
1229 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1230
1231 p = bufpoi + data_col_addr;
1232 chip->read_buf(mtd, p, datafrag_len);
1233
8b6e50c9 1234 /* Calculate ECC */
3d459559
AK
1235 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1236 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1237
8b6e50c9
BN
1238 /*
1239 * The performance is faster if we position offsets according to
7854d3f7 1240 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
8b6e50c9 1241 */
3d459559 1242 for (i = 0; i < eccfrag_len - 1; i++) {
47570bb1 1243 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
3d459559
AK
1244 gaps = 1;
1245 break;
1246 }
1247 }
1248 if (gaps) {
1249 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1250 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1251 } else {
8b6e50c9 1252 /*
7854d3f7 1253 * Send the command to read the particular ECC bytes take care
8b6e50c9
BN
1254 * about buswidth alignment in read_buf.
1255 */
7351d3a5 1256 aligned_pos = eccpos[index] & ~(busw - 1);
3d459559 1257 aligned_len = eccfrag_len;
7351d3a5 1258 if (eccpos[index] & (busw - 1))
3d459559 1259 aligned_len++;
7351d3a5 1260 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
3d459559
AK
1261 aligned_len++;
1262
7351d3a5
FF
1263 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1264 mtd->writesize + aligned_pos, -1);
3d459559
AK
1265 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1266 }
1267
1268 for (i = 0; i < eccfrag_len; i++)
7351d3a5 1269 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
3d459559
AK
1270
1271 p = bufpoi + data_col_addr;
1272 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1273 int stat;
1274
7351d3a5
FF
1275 stat = chip->ecc.correct(mtd, p,
1276 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
3f91e94f 1277 if (stat < 0) {
3d459559 1278 mtd->ecc_stats.failed++;
3f91e94f 1279 } else {
3d459559 1280 mtd->ecc_stats.corrected += stat;
3f91e94f
MD
1281 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1282 }
3d459559 1283 }
3f91e94f 1284 return max_bitflips;
3d459559
AK
1285}
1286
068e3c0a 1287/**
7854d3f7 1288 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
8b6e50c9
BN
1289 * @mtd: mtd info structure
1290 * @chip: nand chip info structure
1291 * @buf: buffer to store read data
1fbb938d 1292 * @oob_required: caller requires OOB data read to chip->oob_poi
8b6e50c9 1293 * @page: page number to read
068e3c0a 1294 *
7854d3f7 1295 * Not for syndrome calculating ECC controllers which need a special oob layout.
068e3c0a 1296 */
f5bbdacc 1297static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1fbb938d 1298 uint8_t *buf, int oob_required, int page)
1da177e4 1299{
f5bbdacc
TG
1300 int i, eccsize = chip->ecc.size;
1301 int eccbytes = chip->ecc.bytes;
1302 int eccsteps = chip->ecc.steps;
1303 uint8_t *p = buf;
4bf63fcb
DW
1304 uint8_t *ecc_calc = chip->buffers->ecccalc;
1305 uint8_t *ecc_code = chip->buffers->ecccode;
8b099a39 1306 uint32_t *eccpos = chip->ecc.layout->eccpos;
3f91e94f 1307 unsigned int max_bitflips = 0;
f5bbdacc
TG
1308
1309 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1310 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1311 chip->read_buf(mtd, p, eccsize);
1312 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1da177e4 1313 }
f75e5097 1314 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1da177e4 1315
f5bbdacc 1316 for (i = 0; i < chip->ecc.total; i++)
f75e5097 1317 ecc_code[i] = chip->oob_poi[eccpos[i]];
1da177e4 1318
f5bbdacc
TG
1319 eccsteps = chip->ecc.steps;
1320 p = buf;
61b03bd7 1321
f5bbdacc
TG
1322 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1323 int stat;
1da177e4 1324
f5bbdacc 1325 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3f91e94f 1326 if (stat < 0) {
f5bbdacc 1327 mtd->ecc_stats.failed++;
3f91e94f 1328 } else {
f5bbdacc 1329 mtd->ecc_stats.corrected += stat;
3f91e94f
MD
1330 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1331 }
f5bbdacc 1332 }
3f91e94f 1333 return max_bitflips;
f5bbdacc 1334}
1da177e4 1335
6e0cb135 1336/**
7854d3f7 1337 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
8b6e50c9
BN
1338 * @mtd: mtd info structure
1339 * @chip: nand chip info structure
1340 * @buf: buffer to store read data
1fbb938d 1341 * @oob_required: caller requires OOB data read to chip->oob_poi
8b6e50c9 1342 * @page: page number to read
6e0cb135 1343 *
8b6e50c9
BN
1344 * Hardware ECC for large page chips, require OOB to be read first. For this
1345 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1346 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1347 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1348 * the data area, by overwriting the NAND manufacturer bad block markings.
6e0cb135
SN
1349 */
1350static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1fbb938d 1351 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
6e0cb135
SN
1352{
1353 int i, eccsize = chip->ecc.size;
1354 int eccbytes = chip->ecc.bytes;
1355 int eccsteps = chip->ecc.steps;
1356 uint8_t *p = buf;
1357 uint8_t *ecc_code = chip->buffers->ecccode;
1358 uint32_t *eccpos = chip->ecc.layout->eccpos;
1359 uint8_t *ecc_calc = chip->buffers->ecccalc;
3f91e94f 1360 unsigned int max_bitflips = 0;
6e0cb135
SN
1361
1362 /* Read the OOB area first */
1363 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1364 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1365 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1366
1367 for (i = 0; i < chip->ecc.total; i++)
1368 ecc_code[i] = chip->oob_poi[eccpos[i]];
1369
1370 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1371 int stat;
1372
1373 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1374 chip->read_buf(mtd, p, eccsize);
1375 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1376
1377 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
3f91e94f 1378 if (stat < 0) {
6e0cb135 1379 mtd->ecc_stats.failed++;
3f91e94f 1380 } else {
6e0cb135 1381 mtd->ecc_stats.corrected += stat;
3f91e94f
MD
1382 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1383 }
6e0cb135 1384 }
3f91e94f 1385 return max_bitflips;
6e0cb135
SN
1386}
1387
f5bbdacc 1388/**
7854d3f7 1389 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
8b6e50c9
BN
1390 * @mtd: mtd info structure
1391 * @chip: nand chip info structure
1392 * @buf: buffer to store read data
1fbb938d 1393 * @oob_required: caller requires OOB data read to chip->oob_poi
8b6e50c9 1394 * @page: page number to read
f5bbdacc 1395 *
8b6e50c9
BN
1396 * The hw generator calculates the error syndrome automatically. Therefore we
1397 * need a special oob layout and handling.
f5bbdacc
TG
1398 */
1399static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1fbb938d 1400 uint8_t *buf, int oob_required, int page)
f5bbdacc
TG
1401{
1402 int i, eccsize = chip->ecc.size;
1403 int eccbytes = chip->ecc.bytes;
1404 int eccsteps = chip->ecc.steps;
1405 uint8_t *p = buf;
f75e5097 1406 uint8_t *oob = chip->oob_poi;
3f91e94f 1407 unsigned int max_bitflips = 0;
1da177e4 1408
f5bbdacc
TG
1409 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1410 int stat;
61b03bd7 1411
f5bbdacc
TG
1412 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1413 chip->read_buf(mtd, p, eccsize);
1da177e4 1414
f5bbdacc
TG
1415 if (chip->ecc.prepad) {
1416 chip->read_buf(mtd, oob, chip->ecc.prepad);
1417 oob += chip->ecc.prepad;
1418 }
1da177e4 1419
f5bbdacc
TG
1420 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1421 chip->read_buf(mtd, oob, eccbytes);
1422 stat = chip->ecc.correct(mtd, p, oob, NULL);
61b03bd7 1423
3f91e94f 1424 if (stat < 0) {
f5bbdacc 1425 mtd->ecc_stats.failed++;
3f91e94f 1426 } else {
f5bbdacc 1427 mtd->ecc_stats.corrected += stat;
3f91e94f
MD
1428 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1429 }
61b03bd7 1430
f5bbdacc 1431 oob += eccbytes;
1da177e4 1432
f5bbdacc
TG
1433 if (chip->ecc.postpad) {
1434 chip->read_buf(mtd, oob, chip->ecc.postpad);
1435 oob += chip->ecc.postpad;
61b03bd7 1436 }
f5bbdacc 1437 }
1da177e4 1438
f5bbdacc 1439 /* Calculate remaining oob bytes */
7e4178f9 1440 i = mtd->oobsize - (oob - chip->oob_poi);
f5bbdacc
TG
1441 if (i)
1442 chip->read_buf(mtd, oob, i);
61b03bd7 1443
3f91e94f 1444 return max_bitflips;
f5bbdacc 1445}
1da177e4 1446
f5bbdacc 1447/**
7854d3f7 1448 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
8b6e50c9
BN
1449 * @chip: nand chip structure
1450 * @oob: oob destination address
1451 * @ops: oob ops structure
1452 * @len: size of oob to transfer
8593fbc6
TG
1453 */
1454static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
7014568b 1455 struct mtd_oob_ops *ops, size_t len)
8593fbc6 1456{
f8ac0414 1457 switch (ops->mode) {
8593fbc6 1458
0612b9dd
BN
1459 case MTD_OPS_PLACE_OOB:
1460 case MTD_OPS_RAW:
8593fbc6
TG
1461 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1462 return oob + len;
1463
0612b9dd 1464 case MTD_OPS_AUTO_OOB: {
8593fbc6 1465 struct nand_oobfree *free = chip->ecc.layout->oobfree;
7bc3312b
TG
1466 uint32_t boffs = 0, roffs = ops->ooboffs;
1467 size_t bytes = 0;
8593fbc6 1468
f8ac0414 1469 for (; free->length && len; free++, len -= bytes) {
8b6e50c9 1470 /* Read request not from offset 0? */
7bc3312b
TG
1471 if (unlikely(roffs)) {
1472 if (roffs >= free->length) {
1473 roffs -= free->length;
1474 continue;
1475 }
1476 boffs = free->offset + roffs;
1477 bytes = min_t(size_t, len,
1478 (free->length - roffs));
1479 roffs = 0;
1480 } else {
1481 bytes = min_t(size_t, len, free->length);
1482 boffs = free->offset;
1483 }
1484 memcpy(oob, chip->oob_poi + boffs, bytes);
8593fbc6
TG
1485 oob += bytes;
1486 }
1487 return oob;
1488 }
1489 default:
1490 BUG();
1491 }
1492 return NULL;
1493}
1494
ba84fb59
BN
1495/**
1496 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1497 * @mtd: MTD device structure
1498 * @retry_mode: the retry mode to use
1499 *
1500 * Some vendors supply a special command to shift the Vt threshold, to be used
1501 * when there are too many bitflips in a page (i.e., ECC error). After setting
1502 * a new threshold, the host should retry reading the page.
1503 */
1504static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1505{
1506 struct nand_chip *chip = mtd->priv;
1507
1508 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1509
1510 if (retry_mode >= chip->read_retries)
1511 return -EINVAL;
1512
1513 if (!chip->setup_read_retry)
1514 return -EOPNOTSUPP;
1515
1516 return chip->setup_read_retry(mtd, retry_mode);
1517}
1518
8593fbc6 1519/**
7854d3f7 1520 * nand_do_read_ops - [INTERN] Read data with ECC
8b6e50c9
BN
1521 * @mtd: MTD device structure
1522 * @from: offset to read from
1523 * @ops: oob ops structure
f5bbdacc
TG
1524 *
1525 * Internal function. Called with chip held.
1526 */
8593fbc6
TG
1527static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1528 struct mtd_oob_ops *ops)
f5bbdacc 1529{
e47f3db4 1530 int chipnr, page, realpage, col, bytes, aligned, oob_required;
f5bbdacc 1531 struct nand_chip *chip = mtd->priv;
f5bbdacc 1532 int ret = 0;
8593fbc6 1533 uint32_t readlen = ops->len;
7014568b 1534 uint32_t oobreadlen = ops->ooblen;
0612b9dd 1535 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
9aca334e
ML
1536 mtd->oobavail : mtd->oobsize;
1537
8593fbc6 1538 uint8_t *bufpoi, *oob, *buf;
66507c7b 1539 int use_bufpoi;
edbc4540 1540 unsigned int max_bitflips = 0;
ba84fb59 1541 int retry_mode = 0;
b72f3dfb 1542 bool ecc_fail = false;
1da177e4 1543
f5bbdacc
TG
1544 chipnr = (int)(from >> chip->chip_shift);
1545 chip->select_chip(mtd, chipnr);
61b03bd7 1546
f5bbdacc
TG
1547 realpage = (int)(from >> chip->page_shift);
1548 page = realpage & chip->pagemask;
1da177e4 1549
f5bbdacc 1550 col = (int)(from & (mtd->writesize - 1));
61b03bd7 1551
8593fbc6
TG
1552 buf = ops->datbuf;
1553 oob = ops->oobbuf;
e47f3db4 1554 oob_required = oob ? 1 : 0;
8593fbc6 1555
f8ac0414 1556 while (1) {
b72f3dfb
BN
1557 unsigned int ecc_failures = mtd->ecc_stats.failed;
1558
f5bbdacc
TG
1559 bytes = min(mtd->writesize - col, readlen);
1560 aligned = (bytes == mtd->writesize);
61b03bd7 1561
66507c7b
KD
1562 if (!aligned)
1563 use_bufpoi = 1;
1564 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1565 use_bufpoi = !virt_addr_valid(buf);
1566 else
1567 use_bufpoi = 0;
1568
8b6e50c9 1569 /* Is the current page in the buffer? */
8593fbc6 1570 if (realpage != chip->pagebuf || oob) {
66507c7b
KD
1571 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1572
1573 if (use_bufpoi && aligned)
1574 pr_debug("%s: using read bounce buffer for buf@%p\n",
1575 __func__, buf);
61b03bd7 1576
ba84fb59 1577read_retry:
c00a0991 1578 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1da177e4 1579
edbc4540
MD
1580 /*
1581 * Now read the page into the buffer. Absent an error,
1582 * the read methods return max bitflips per ecc step.
1583 */
0612b9dd 1584 if (unlikely(ops->mode == MTD_OPS_RAW))
1fbb938d 1585 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
e47f3db4
BN
1586 oob_required,
1587 page);
a5ff4f10
JW
1588 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1589 !oob)
7351d3a5 1590 ret = chip->ecc.read_subpage(mtd, chip,
e004debd
HS
1591 col, bytes, bufpoi,
1592 page);
956e944c 1593 else
46a8cf2d 1594 ret = chip->ecc.read_page(mtd, chip, bufpoi,
e47f3db4 1595 oob_required, page);
6d77b9d0 1596 if (ret < 0) {
66507c7b 1597 if (use_bufpoi)
6d77b9d0
BN
1598 /* Invalidate page cache */
1599 chip->pagebuf = -1;
1da177e4 1600 break;
6d77b9d0 1601 }
f5bbdacc 1602
edbc4540
MD
1603 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1604
f5bbdacc 1605 /* Transfer not aligned data */
66507c7b 1606 if (use_bufpoi) {
a5ff4f10 1607 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
b72f3dfb 1608 !(mtd->ecc_stats.failed - ecc_failures) &&
edbc4540 1609 (ops->mode != MTD_OPS_RAW)) {
3d459559 1610 chip->pagebuf = realpage;
edbc4540
MD
1611 chip->pagebuf_bitflips = ret;
1612 } else {
6d77b9d0
BN
1613 /* Invalidate page cache */
1614 chip->pagebuf = -1;
edbc4540 1615 }
4bf63fcb 1616 memcpy(buf, chip->buffers->databuf + col, bytes);
f5bbdacc
TG
1617 }
1618
8593fbc6 1619 if (unlikely(oob)) {
b64d39d8
ML
1620 int toread = min(oobreadlen, max_oobsize);
1621
1622 if (toread) {
1623 oob = nand_transfer_oob(chip,
1624 oob, ops, toread);
1625 oobreadlen -= toread;
1626 }
8593fbc6 1627 }
5bc7c33c
BN
1628
1629 if (chip->options & NAND_NEED_READRDY) {
1630 /* Apply delay or wait for ready/busy pin */
1631 if (!chip->dev_ready)
1632 udelay(chip->chip_delay);
1633 else
1634 nand_wait_ready(mtd);
1635 }
b72f3dfb 1636
ba84fb59 1637 if (mtd->ecc_stats.failed - ecc_failures) {
28fa65e6 1638 if (retry_mode + 1 < chip->read_retries) {
ba84fb59
BN
1639 retry_mode++;
1640 ret = nand_setup_read_retry(mtd,
1641 retry_mode);
1642 if (ret < 0)
1643 break;
1644
1645 /* Reset failures; retry */
1646 mtd->ecc_stats.failed = ecc_failures;
1647 goto read_retry;
1648 } else {
1649 /* No more retry modes; real failure */
1650 ecc_fail = true;
1651 }
1652 }
1653
1654 buf += bytes;
8593fbc6 1655 } else {
4bf63fcb 1656 memcpy(buf, chip->buffers->databuf + col, bytes);
8593fbc6 1657 buf += bytes;
edbc4540
MD
1658 max_bitflips = max_t(unsigned int, max_bitflips,
1659 chip->pagebuf_bitflips);
8593fbc6 1660 }
1da177e4 1661
f5bbdacc 1662 readlen -= bytes;
61b03bd7 1663
ba84fb59
BN
1664 /* Reset to retry mode 0 */
1665 if (retry_mode) {
1666 ret = nand_setup_read_retry(mtd, 0);
1667 if (ret < 0)
1668 break;
1669 retry_mode = 0;
1670 }
1671
f5bbdacc 1672 if (!readlen)
61b03bd7 1673 break;
1da177e4 1674
8b6e50c9 1675 /* For subsequent reads align to page boundary */
1da177e4
LT
1676 col = 0;
1677 /* Increment page address */
1678 realpage++;
1679
ace4dfee 1680 page = realpage & chip->pagemask;
1da177e4
LT
1681 /* Check, if we cross a chip boundary */
1682 if (!page) {
1683 chipnr++;
ace4dfee
TG
1684 chip->select_chip(mtd, -1);
1685 chip->select_chip(mtd, chipnr);
1da177e4 1686 }
1da177e4 1687 }
b0bb6903 1688 chip->select_chip(mtd, -1);
1da177e4 1689
8593fbc6 1690 ops->retlen = ops->len - (size_t) readlen;
7014568b
VW
1691 if (oob)
1692 ops->oobretlen = ops->ooblen - oobreadlen;
1da177e4 1693
3f91e94f 1694 if (ret < 0)
f5bbdacc
TG
1695 return ret;
1696
b72f3dfb 1697 if (ecc_fail)
9a1fcdfd
TG
1698 return -EBADMSG;
1699
edbc4540 1700 return max_bitflips;
f5bbdacc
TG
1701}
1702
1703/**
25985edc 1704 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
8b6e50c9
BN
1705 * @mtd: MTD device structure
1706 * @from: offset to read from
1707 * @len: number of bytes to read
1708 * @retlen: pointer to variable to store the number of read bytes
1709 * @buf: the databuffer to put data
f5bbdacc 1710 *
8b6e50c9 1711 * Get hold of the chip and call nand_do_read.
f5bbdacc
TG
1712 */
1713static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1714 size_t *retlen, uint8_t *buf)
1715{
4a89ff88 1716 struct mtd_oob_ops ops;
f5bbdacc
TG
1717 int ret;
1718
6a8214aa 1719 nand_get_device(mtd, FL_READING);
4a89ff88
BN
1720 ops.len = len;
1721 ops.datbuf = buf;
1722 ops.oobbuf = NULL;
11041ae6 1723 ops.mode = MTD_OPS_PLACE_OOB;
4a89ff88 1724 ret = nand_do_read_ops(mtd, from, &ops);
4a89ff88 1725 *retlen = ops.retlen;
f5bbdacc 1726 nand_release_device(mtd);
f5bbdacc 1727 return ret;
1da177e4
LT
1728}
1729
7bc3312b 1730/**
7854d3f7 1731 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
8b6e50c9
BN
1732 * @mtd: mtd info structure
1733 * @chip: nand chip info structure
1734 * @page: page number to read
7bc3312b
TG
1735 */
1736static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
5c2ffb11 1737 int page)
7bc3312b 1738{
5c2ffb11 1739 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
7bc3312b 1740 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
5c2ffb11 1741 return 0;
7bc3312b
TG
1742}
1743
1744/**
7854d3f7 1745 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
7bc3312b 1746 * with syndromes
8b6e50c9
BN
1747 * @mtd: mtd info structure
1748 * @chip: nand chip info structure
1749 * @page: page number to read
7bc3312b
TG
1750 */
1751static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
5c2ffb11 1752 int page)
7bc3312b
TG
1753{
1754 uint8_t *buf = chip->oob_poi;
1755 int length = mtd->oobsize;
1756 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1757 int eccsize = chip->ecc.size;
1758 uint8_t *bufpoi = buf;
1759 int i, toread, sndrnd = 0, pos;
1760
1761 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1762 for (i = 0; i < chip->ecc.steps; i++) {
1763 if (sndrnd) {
1764 pos = eccsize + i * (eccsize + chunk);
1765 if (mtd->writesize > 512)
1766 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1767 else
1768 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1769 } else
1770 sndrnd = 1;
1771 toread = min_t(int, length, chunk);
1772 chip->read_buf(mtd, bufpoi, toread);
1773 bufpoi += toread;
1774 length -= toread;
1775 }
1776 if (length > 0)
1777 chip->read_buf(mtd, bufpoi, length);
1778
5c2ffb11 1779 return 0;
7bc3312b
TG
1780}
1781
1782/**
7854d3f7 1783 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
8b6e50c9
BN
1784 * @mtd: mtd info structure
1785 * @chip: nand chip info structure
1786 * @page: page number to write
7bc3312b
TG
1787 */
1788static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1789 int page)
1790{
1791 int status = 0;
1792 const uint8_t *buf = chip->oob_poi;
1793 int length = mtd->oobsize;
1794
1795 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1796 chip->write_buf(mtd, buf, length);
1797 /* Send command to program the OOB data */
1798 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1799
1800 status = chip->waitfunc(mtd, chip);
1801
0d420f9d 1802 return status & NAND_STATUS_FAIL ? -EIO : 0;
7bc3312b
TG
1803}
1804
1805/**
7854d3f7 1806 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
8b6e50c9
BN
1807 * with syndrome - only for large page flash
1808 * @mtd: mtd info structure
1809 * @chip: nand chip info structure
1810 * @page: page number to write
7bc3312b
TG
1811 */
1812static int nand_write_oob_syndrome(struct mtd_info *mtd,
1813 struct nand_chip *chip, int page)
1814{
1815 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1816 int eccsize = chip->ecc.size, length = mtd->oobsize;
1817 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1818 const uint8_t *bufpoi = chip->oob_poi;
1819
1820 /*
1821 * data-ecc-data-ecc ... ecc-oob
1822 * or
1823 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1824 */
1825 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1826 pos = steps * (eccsize + chunk);
1827 steps = 0;
1828 } else
8b0036ee 1829 pos = eccsize;
7bc3312b
TG
1830
1831 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1832 for (i = 0; i < steps; i++) {
1833 if (sndcmd) {
1834 if (mtd->writesize <= 512) {
1835 uint32_t fill = 0xFFFFFFFF;
1836
1837 len = eccsize;
1838 while (len > 0) {
1839 int num = min_t(int, len, 4);
1840 chip->write_buf(mtd, (uint8_t *)&fill,
1841 num);
1842 len -= num;
1843 }
1844 } else {
1845 pos = eccsize + i * (eccsize + chunk);
1846 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1847 }
1848 } else
1849 sndcmd = 1;
1850 len = min_t(int, length, chunk);
1851 chip->write_buf(mtd, bufpoi, len);
1852 bufpoi += len;
1853 length -= len;
1854 }
1855 if (length > 0)
1856 chip->write_buf(mtd, bufpoi, length);
1857
1858 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1859 status = chip->waitfunc(mtd, chip);
1860
1861 return status & NAND_STATUS_FAIL ? -EIO : 0;
1862}
1863
1da177e4 1864/**
7854d3f7 1865 * nand_do_read_oob - [INTERN] NAND read out-of-band
8b6e50c9
BN
1866 * @mtd: MTD device structure
1867 * @from: offset to read from
1868 * @ops: oob operations description structure
1da177e4 1869 *
8b6e50c9 1870 * NAND read out-of-band data from the spare area.
1da177e4 1871 */
8593fbc6
TG
1872static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1873 struct mtd_oob_ops *ops)
1da177e4 1874{
c00a0991 1875 int page, realpage, chipnr;
ace4dfee 1876 struct nand_chip *chip = mtd->priv;
041e4575 1877 struct mtd_ecc_stats stats;
7014568b
VW
1878 int readlen = ops->ooblen;
1879 int len;
7bc3312b 1880 uint8_t *buf = ops->oobbuf;
1951f2f7 1881 int ret = 0;
61b03bd7 1882
289c0522 1883 pr_debug("%s: from = 0x%08Lx, len = %i\n",
20d8e248 1884 __func__, (unsigned long long)from, readlen);
1da177e4 1885
041e4575
BN
1886 stats = mtd->ecc_stats;
1887
0612b9dd 1888 if (ops->mode == MTD_OPS_AUTO_OOB)
7014568b 1889 len = chip->ecc.layout->oobavail;
03736155
AH
1890 else
1891 len = mtd->oobsize;
1892
1893 if (unlikely(ops->ooboffs >= len)) {
289c0522
BN
1894 pr_debug("%s: attempt to start read outside oob\n",
1895 __func__);
03736155
AH
1896 return -EINVAL;
1897 }
1898
1899 /* Do not allow reads past end of device */
1900 if (unlikely(from >= mtd->size ||
1901 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1902 (from >> chip->page_shift)) * len)) {
289c0522
BN
1903 pr_debug("%s: attempt to read beyond end of device\n",
1904 __func__);
03736155
AH
1905 return -EINVAL;
1906 }
7014568b 1907
7314e9e7 1908 chipnr = (int)(from >> chip->chip_shift);
ace4dfee 1909 chip->select_chip(mtd, chipnr);
1da177e4 1910
7314e9e7
TG
1911 /* Shift to get page */
1912 realpage = (int)(from >> chip->page_shift);
1913 page = realpage & chip->pagemask;
1da177e4 1914
f8ac0414 1915 while (1) {
0612b9dd 1916 if (ops->mode == MTD_OPS_RAW)
1951f2f7 1917 ret = chip->ecc.read_oob_raw(mtd, chip, page);
c46f6483 1918 else
1951f2f7
SL
1919 ret = chip->ecc.read_oob(mtd, chip, page);
1920
1921 if (ret < 0)
1922 break;
7014568b
VW
1923
1924 len = min(len, readlen);
1925 buf = nand_transfer_oob(chip, buf, ops, len);
8593fbc6 1926
5bc7c33c
BN
1927 if (chip->options & NAND_NEED_READRDY) {
1928 /* Apply delay or wait for ready/busy pin */
1929 if (!chip->dev_ready)
1930 udelay(chip->chip_delay);
1931 else
1932 nand_wait_ready(mtd);
1933 }
1934
7014568b 1935 readlen -= len;
0d420f9d
SZ
1936 if (!readlen)
1937 break;
1938
7314e9e7
TG
1939 /* Increment page address */
1940 realpage++;
1941
1942 page = realpage & chip->pagemask;
1943 /* Check, if we cross a chip boundary */
1944 if (!page) {
1945 chipnr++;
1946 chip->select_chip(mtd, -1);
1947 chip->select_chip(mtd, chipnr);
1da177e4
LT
1948 }
1949 }
b0bb6903 1950 chip->select_chip(mtd, -1);
1da177e4 1951
1951f2f7
SL
1952 ops->oobretlen = ops->ooblen - readlen;
1953
1954 if (ret < 0)
1955 return ret;
041e4575
BN
1956
1957 if (mtd->ecc_stats.failed - stats.failed)
1958 return -EBADMSG;
1959
1960 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1da177e4
LT
1961}
1962
1963/**
8593fbc6 1964 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
8b6e50c9
BN
1965 * @mtd: MTD device structure
1966 * @from: offset to read from
1967 * @ops: oob operation description structure
1da177e4 1968 *
8b6e50c9 1969 * NAND read data and/or out-of-band data.
1da177e4 1970 */
8593fbc6
TG
1971static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1972 struct mtd_oob_ops *ops)
1da177e4 1973{
8593fbc6
TG
1974 int ret = -ENOTSUPP;
1975
1976 ops->retlen = 0;
1da177e4
LT
1977
1978 /* Do not allow reads past end of device */
7014568b 1979 if (ops->datbuf && (from + ops->len) > mtd->size) {
289c0522
BN
1980 pr_debug("%s: attempt to read beyond end of device\n",
1981 __func__);
1da177e4
LT
1982 return -EINVAL;
1983 }
1984
6a8214aa 1985 nand_get_device(mtd, FL_READING);
1da177e4 1986
f8ac0414 1987 switch (ops->mode) {
0612b9dd
BN
1988 case MTD_OPS_PLACE_OOB:
1989 case MTD_OPS_AUTO_OOB:
1990 case MTD_OPS_RAW:
8593fbc6 1991 break;
1da177e4 1992
8593fbc6
TG
1993 default:
1994 goto out;
1995 }
1da177e4 1996
8593fbc6
TG
1997 if (!ops->datbuf)
1998 ret = nand_do_read_oob(mtd, from, ops);
1999 else
2000 ret = nand_do_read_ops(mtd, from, ops);
61b03bd7 2001
7351d3a5 2002out:
8593fbc6
TG
2003 nand_release_device(mtd);
2004 return ret;
2005}
61b03bd7 2006
1da177e4 2007
8593fbc6 2008/**
7854d3f7 2009 * nand_write_page_raw - [INTERN] raw page write function
8b6e50c9
BN
2010 * @mtd: mtd info structure
2011 * @chip: nand chip info structure
2012 * @buf: data buffer
1fbb938d 2013 * @oob_required: must write chip->oob_poi to OOB
52ff49df 2014 *
7854d3f7 2015 * Not for syndrome calculating ECC controllers, which use a special oob layout.
8593fbc6 2016 */
fdbad98d 2017static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1fbb938d 2018 const uint8_t *buf, int oob_required)
8593fbc6
TG
2019{
2020 chip->write_buf(mtd, buf, mtd->writesize);
279f08d4
BN
2021 if (oob_required)
2022 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
fdbad98d
JW
2023
2024 return 0;
1da177e4
LT
2025}
2026
52ff49df 2027/**
7854d3f7 2028 * nand_write_page_raw_syndrome - [INTERN] raw page write function
8b6e50c9
BN
2029 * @mtd: mtd info structure
2030 * @chip: nand chip info structure
2031 * @buf: data buffer
1fbb938d 2032 * @oob_required: must write chip->oob_poi to OOB
52ff49df
DB
2033 *
2034 * We need a special oob layout and handling even when ECC isn't checked.
2035 */
fdbad98d 2036static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
7351d3a5 2037 struct nand_chip *chip,
1fbb938d 2038 const uint8_t *buf, int oob_required)
52ff49df
DB
2039{
2040 int eccsize = chip->ecc.size;
2041 int eccbytes = chip->ecc.bytes;
2042 uint8_t *oob = chip->oob_poi;
2043 int steps, size;
2044
2045 for (steps = chip->ecc.steps; steps > 0; steps--) {
2046 chip->write_buf(mtd, buf, eccsize);
2047 buf += eccsize;
2048
2049 if (chip->ecc.prepad) {
2050 chip->write_buf(mtd, oob, chip->ecc.prepad);
2051 oob += chip->ecc.prepad;
2052 }
2053
60c3bc1f 2054 chip->write_buf(mtd, oob, eccbytes);
52ff49df
DB
2055 oob += eccbytes;
2056
2057 if (chip->ecc.postpad) {
2058 chip->write_buf(mtd, oob, chip->ecc.postpad);
2059 oob += chip->ecc.postpad;
2060 }
2061 }
2062
2063 size = mtd->oobsize - (oob - chip->oob_poi);
2064 if (size)
2065 chip->write_buf(mtd, oob, size);
fdbad98d
JW
2066
2067 return 0;
52ff49df 2068}
9223a456 2069/**
7854d3f7 2070 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
8b6e50c9
BN
2071 * @mtd: mtd info structure
2072 * @chip: nand chip info structure
2073 * @buf: data buffer
1fbb938d 2074 * @oob_required: must write chip->oob_poi to OOB
9223a456 2075 */
fdbad98d 2076static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1fbb938d 2077 const uint8_t *buf, int oob_required)
9223a456 2078{
f75e5097
TG
2079 int i, eccsize = chip->ecc.size;
2080 int eccbytes = chip->ecc.bytes;
2081 int eccsteps = chip->ecc.steps;
4bf63fcb 2082 uint8_t *ecc_calc = chip->buffers->ecccalc;
f75e5097 2083 const uint8_t *p = buf;
8b099a39 2084 uint32_t *eccpos = chip->ecc.layout->eccpos;
9223a456 2085
7854d3f7 2086 /* Software ECC calculation */
8593fbc6
TG
2087 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2088 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
9223a456 2089
8593fbc6
TG
2090 for (i = 0; i < chip->ecc.total; i++)
2091 chip->oob_poi[eccpos[i]] = ecc_calc[i];
9223a456 2092
fdbad98d 2093 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
f75e5097 2094}
9223a456 2095
f75e5097 2096/**
7854d3f7 2097 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
8b6e50c9
BN
2098 * @mtd: mtd info structure
2099 * @chip: nand chip info structure
2100 * @buf: data buffer
1fbb938d 2101 * @oob_required: must write chip->oob_poi to OOB
f75e5097 2102 */
fdbad98d 2103static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1fbb938d 2104 const uint8_t *buf, int oob_required)
f75e5097
TG
2105{
2106 int i, eccsize = chip->ecc.size;
2107 int eccbytes = chip->ecc.bytes;
2108 int eccsteps = chip->ecc.steps;
4bf63fcb 2109 uint8_t *ecc_calc = chip->buffers->ecccalc;
f75e5097 2110 const uint8_t *p = buf;
8b099a39 2111 uint32_t *eccpos = chip->ecc.layout->eccpos;
9223a456 2112
f75e5097
TG
2113 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2114 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
29da9cea 2115 chip->write_buf(mtd, p, eccsize);
f75e5097 2116 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
9223a456
TG
2117 }
2118
f75e5097
TG
2119 for (i = 0; i < chip->ecc.total; i++)
2120 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2121
2122 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
fdbad98d
JW
2123
2124 return 0;
9223a456
TG
2125}
2126
837a6ba4
GP
2127
2128/**
2129 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2130 * @mtd: mtd info structure
2131 * @chip: nand chip info structure
d6a95080 2132 * @offset: column address of subpage within the page
837a6ba4 2133 * @data_len: data length
d6a95080 2134 * @buf: data buffer
837a6ba4
GP
2135 * @oob_required: must write chip->oob_poi to OOB
2136 */
2137static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2138 struct nand_chip *chip, uint32_t offset,
d6a95080 2139 uint32_t data_len, const uint8_t *buf,
837a6ba4
GP
2140 int oob_required)
2141{
2142 uint8_t *oob_buf = chip->oob_poi;
2143 uint8_t *ecc_calc = chip->buffers->ecccalc;
2144 int ecc_size = chip->ecc.size;
2145 int ecc_bytes = chip->ecc.bytes;
2146 int ecc_steps = chip->ecc.steps;
2147 uint32_t *eccpos = chip->ecc.layout->eccpos;
2148 uint32_t start_step = offset / ecc_size;
2149 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2150 int oob_bytes = mtd->oobsize / ecc_steps;
2151 int step, i;
2152
2153 for (step = 0; step < ecc_steps; step++) {
2154 /* configure controller for WRITE access */
2155 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2156
2157 /* write data (untouched subpages already masked by 0xFF) */
d6a95080 2158 chip->write_buf(mtd, buf, ecc_size);
837a6ba4
GP
2159
2160 /* mask ECC of un-touched subpages by padding 0xFF */
2161 if ((step < start_step) || (step > end_step))
2162 memset(ecc_calc, 0xff, ecc_bytes);
2163 else
d6a95080 2164 chip->ecc.calculate(mtd, buf, ecc_calc);
837a6ba4
GP
2165
2166 /* mask OOB of un-touched subpages by padding 0xFF */
2167 /* if oob_required, preserve OOB metadata of written subpage */
2168 if (!oob_required || (step < start_step) || (step > end_step))
2169 memset(oob_buf, 0xff, oob_bytes);
2170
d6a95080 2171 buf += ecc_size;
837a6ba4
GP
2172 ecc_calc += ecc_bytes;
2173 oob_buf += oob_bytes;
2174 }
2175
2176 /* copy calculated ECC for whole page to chip->buffer->oob */
2177 /* this include masked-value(0xFF) for unwritten subpages */
2178 ecc_calc = chip->buffers->ecccalc;
2179 for (i = 0; i < chip->ecc.total; i++)
2180 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2181
2182 /* write OOB buffer to NAND device */
2183 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2184
2185 return 0;
2186}
2187
2188
61b03bd7 2189/**
7854d3f7 2190 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
8b6e50c9
BN
2191 * @mtd: mtd info structure
2192 * @chip: nand chip info structure
2193 * @buf: data buffer
1fbb938d 2194 * @oob_required: must write chip->oob_poi to OOB
1da177e4 2195 *
8b6e50c9
BN
2196 * The hw generator calculates the error syndrome automatically. Therefore we
2197 * need a special oob layout and handling.
f75e5097 2198 */
fdbad98d 2199static int nand_write_page_syndrome(struct mtd_info *mtd,
1fbb938d
BN
2200 struct nand_chip *chip,
2201 const uint8_t *buf, int oob_required)
1da177e4 2202{
f75e5097
TG
2203 int i, eccsize = chip->ecc.size;
2204 int eccbytes = chip->ecc.bytes;
2205 int eccsteps = chip->ecc.steps;
2206 const uint8_t *p = buf;
2207 uint8_t *oob = chip->oob_poi;
1da177e4 2208
f75e5097 2209 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1da177e4 2210
f75e5097
TG
2211 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2212 chip->write_buf(mtd, p, eccsize);
61b03bd7 2213
f75e5097
TG
2214 if (chip->ecc.prepad) {
2215 chip->write_buf(mtd, oob, chip->ecc.prepad);
2216 oob += chip->ecc.prepad;
2217 }
2218
2219 chip->ecc.calculate(mtd, p, oob);
2220 chip->write_buf(mtd, oob, eccbytes);
2221 oob += eccbytes;
2222
2223 if (chip->ecc.postpad) {
2224 chip->write_buf(mtd, oob, chip->ecc.postpad);
2225 oob += chip->ecc.postpad;
1da177e4 2226 }
1da177e4 2227 }
f75e5097
TG
2228
2229 /* Calculate remaining oob bytes */
7e4178f9 2230 i = mtd->oobsize - (oob - chip->oob_poi);
f75e5097
TG
2231 if (i)
2232 chip->write_buf(mtd, oob, i);
fdbad98d
JW
2233
2234 return 0;
f75e5097
TG
2235}
2236
2237/**
956e944c 2238 * nand_write_page - [REPLACEABLE] write one page
8b6e50c9
BN
2239 * @mtd: MTD device structure
2240 * @chip: NAND chip descriptor
837a6ba4
GP
2241 * @offset: address offset within the page
2242 * @data_len: length of actual data to be written
8b6e50c9 2243 * @buf: the data to write
1fbb938d 2244 * @oob_required: must write chip->oob_poi to OOB
8b6e50c9
BN
2245 * @page: page number to write
2246 * @cached: cached programming
2247 * @raw: use _raw version of write_page
f75e5097
TG
2248 */
2249static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
837a6ba4
GP
2250 uint32_t offset, int data_len, const uint8_t *buf,
2251 int oob_required, int page, int cached, int raw)
f75e5097 2252{
837a6ba4
GP
2253 int status, subpage;
2254
2255 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2256 chip->ecc.write_subpage)
2257 subpage = offset || (data_len < mtd->writesize);
2258 else
2259 subpage = 0;
f75e5097
TG
2260
2261 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2262
956e944c 2263 if (unlikely(raw))
837a6ba4
GP
2264 status = chip->ecc.write_page_raw(mtd, chip, buf,
2265 oob_required);
2266 else if (subpage)
2267 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2268 buf, oob_required);
956e944c 2269 else
fdbad98d
JW
2270 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2271
2272 if (status < 0)
2273 return status;
f75e5097
TG
2274
2275 /*
7854d3f7 2276 * Cached progamming disabled for now. Not sure if it's worth the
8b6e50c9 2277 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
f75e5097
TG
2278 */
2279 cached = 0;
2280
3239a6cd 2281 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
f75e5097
TG
2282
2283 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
7bc3312b 2284 status = chip->waitfunc(mtd, chip);
f75e5097
TG
2285 /*
2286 * See if operation failed and additional status checks are
8b6e50c9 2287 * available.
f75e5097
TG
2288 */
2289 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2290 status = chip->errstat(mtd, chip, FL_WRITING, status,
2291 page);
2292
2293 if (status & NAND_STATUS_FAIL)
2294 return -EIO;
2295 } else {
2296 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
7bc3312b 2297 status = chip->waitfunc(mtd, chip);
f75e5097
TG
2298 }
2299
f75e5097 2300 return 0;
1da177e4
LT
2301}
2302
8593fbc6 2303/**
7854d3f7 2304 * nand_fill_oob - [INTERN] Transfer client buffer to oob
f722013e 2305 * @mtd: MTD device structure
8b6e50c9
BN
2306 * @oob: oob data buffer
2307 * @len: oob data write length
2308 * @ops: oob ops structure
8593fbc6 2309 */
f722013e
TAA
2310static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2311 struct mtd_oob_ops *ops)
8593fbc6 2312{
f722013e
TAA
2313 struct nand_chip *chip = mtd->priv;
2314
2315 /*
2316 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2317 * data from a previous OOB read.
2318 */
2319 memset(chip->oob_poi, 0xff, mtd->oobsize);
2320
f8ac0414 2321 switch (ops->mode) {
8593fbc6 2322
0612b9dd
BN
2323 case MTD_OPS_PLACE_OOB:
2324 case MTD_OPS_RAW:
8593fbc6
TG
2325 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2326 return oob + len;
2327
0612b9dd 2328 case MTD_OPS_AUTO_OOB: {
8593fbc6 2329 struct nand_oobfree *free = chip->ecc.layout->oobfree;
7bc3312b
TG
2330 uint32_t boffs = 0, woffs = ops->ooboffs;
2331 size_t bytes = 0;
8593fbc6 2332
f8ac0414 2333 for (; free->length && len; free++, len -= bytes) {
8b6e50c9 2334 /* Write request not from offset 0? */
7bc3312b
TG
2335 if (unlikely(woffs)) {
2336 if (woffs >= free->length) {
2337 woffs -= free->length;
2338 continue;
2339 }
2340 boffs = free->offset + woffs;
2341 bytes = min_t(size_t, len,
2342 (free->length - woffs));
2343 woffs = 0;
2344 } else {
2345 bytes = min_t(size_t, len, free->length);
2346 boffs = free->offset;
2347 }
8b0036ee 2348 memcpy(chip->oob_poi + boffs, oob, bytes);
8593fbc6
TG
2349 oob += bytes;
2350 }
2351 return oob;
2352 }
2353 default:
2354 BUG();
2355 }
2356 return NULL;
2357}
2358
f8ac0414 2359#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
1da177e4
LT
2360
2361/**
7854d3f7 2362 * nand_do_write_ops - [INTERN] NAND write with ECC
8b6e50c9
BN
2363 * @mtd: MTD device structure
2364 * @to: offset to write to
2365 * @ops: oob operations description structure
1da177e4 2366 *
8b6e50c9 2367 * NAND write with ECC.
1da177e4 2368 */
8593fbc6
TG
2369static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2370 struct mtd_oob_ops *ops)
1da177e4 2371{
29072b96 2372 int chipnr, realpage, page, blockmask, column;
ace4dfee 2373 struct nand_chip *chip = mtd->priv;
8593fbc6 2374 uint32_t writelen = ops->len;
782ce79a
ML
2375
2376 uint32_t oobwritelen = ops->ooblen;
0612b9dd 2377 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
782ce79a
ML
2378 mtd->oobavail : mtd->oobsize;
2379
8593fbc6
TG
2380 uint8_t *oob = ops->oobbuf;
2381 uint8_t *buf = ops->datbuf;
837a6ba4 2382 int ret;
e47f3db4 2383 int oob_required = oob ? 1 : 0;
1da177e4 2384
8593fbc6 2385 ops->retlen = 0;
29072b96
TG
2386 if (!writelen)
2387 return 0;
1da177e4 2388
8b6e50c9 2389 /* Reject writes, which are not page aligned */
8593fbc6 2390 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
d0370219
BN
2391 pr_notice("%s: attempt to write non page aligned data\n",
2392 __func__);
1da177e4
LT
2393 return -EINVAL;
2394 }
2395
29072b96 2396 column = to & (mtd->writesize - 1);
1da177e4 2397
6a930961
TG
2398 chipnr = (int)(to >> chip->chip_shift);
2399 chip->select_chip(mtd, chipnr);
2400
1da177e4 2401 /* Check, if it is write protected */
b0bb6903
HS
2402 if (nand_check_wp(mtd)) {
2403 ret = -EIO;
2404 goto err_out;
2405 }
1da177e4 2406
f75e5097
TG
2407 realpage = (int)(to >> chip->page_shift);
2408 page = realpage & chip->pagemask;
2409 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2410
2411 /* Invalidate the page cache, when we write to the cached page */
537ab1bd
BN
2412 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2413 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
ace4dfee 2414 chip->pagebuf = -1;
61b03bd7 2415
782ce79a 2416 /* Don't allow multipage oob writes with offset */
b0bb6903
HS
2417 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2418 ret = -EINVAL;
2419 goto err_out;
2420 }
782ce79a 2421
f8ac0414 2422 while (1) {
29072b96 2423 int bytes = mtd->writesize;
f75e5097 2424 int cached = writelen > bytes && page != blockmask;
29072b96 2425 uint8_t *wbuf = buf;
66507c7b
KD
2426 int use_bufpoi;
2427 int part_pagewr = (column || writelen < (mtd->writesize - 1));
2428
2429 if (part_pagewr)
2430 use_bufpoi = 1;
2431 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2432 use_bufpoi = !virt_addr_valid(buf);
2433 else
2434 use_bufpoi = 0;
29072b96 2435
66507c7b
KD
2436 /* Partial page write?, or need to use bounce buffer */
2437 if (use_bufpoi) {
2438 pr_debug("%s: using write bounce buffer for buf@%p\n",
2439 __func__, buf);
29072b96 2440 cached = 0;
66507c7b
KD
2441 if (part_pagewr)
2442 bytes = min_t(int, bytes - column, writelen);
29072b96
TG
2443 chip->pagebuf = -1;
2444 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2445 memcpy(&chip->buffers->databuf[column], buf, bytes);
2446 wbuf = chip->buffers->databuf;
2447 }
1da177e4 2448
782ce79a
ML
2449 if (unlikely(oob)) {
2450 size_t len = min(oobwritelen, oobmaxlen);
f722013e 2451 oob = nand_fill_oob(mtd, oob, len, ops);
782ce79a 2452 oobwritelen -= len;
f722013e
TAA
2453 } else {
2454 /* We still need to erase leftover OOB data */
2455 memset(chip->oob_poi, 0xff, mtd->oobsize);
782ce79a 2456 }
837a6ba4
GP
2457 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2458 oob_required, page, cached,
2459 (ops->mode == MTD_OPS_RAW));
f75e5097
TG
2460 if (ret)
2461 break;
2462
2463 writelen -= bytes;
2464 if (!writelen)
2465 break;
2466
29072b96 2467 column = 0;
f75e5097
TG
2468 buf += bytes;
2469 realpage++;
2470
2471 page = realpage & chip->pagemask;
2472 /* Check, if we cross a chip boundary */
2473 if (!page) {
2474 chipnr++;
2475 chip->select_chip(mtd, -1);
2476 chip->select_chip(mtd, chipnr);
1da177e4
LT
2477 }
2478 }
8593fbc6 2479
8593fbc6 2480 ops->retlen = ops->len - writelen;
7014568b
VW
2481 if (unlikely(oob))
2482 ops->oobretlen = ops->ooblen;
b0bb6903
HS
2483
2484err_out:
2485 chip->select_chip(mtd, -1);
1da177e4
LT
2486 return ret;
2487}
2488
2af7c653
SK
2489/**
2490 * panic_nand_write - [MTD Interface] NAND write with ECC
8b6e50c9
BN
2491 * @mtd: MTD device structure
2492 * @to: offset to write to
2493 * @len: number of bytes to write
2494 * @retlen: pointer to variable to store the number of written bytes
2495 * @buf: the data to write
2af7c653
SK
2496 *
2497 * NAND write with ECC. Used when performing writes in interrupt context, this
2498 * may for example be called by mtdoops when writing an oops while in panic.
2499 */
2500static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2501 size_t *retlen, const uint8_t *buf)
2502{
2503 struct nand_chip *chip = mtd->priv;
4a89ff88 2504 struct mtd_oob_ops ops;
2af7c653
SK
2505 int ret;
2506
8b6e50c9 2507 /* Wait for the device to get ready */
2af7c653
SK
2508 panic_nand_wait(mtd, chip, 400);
2509
8b6e50c9 2510 /* Grab the device */
2af7c653
SK
2511 panic_nand_get_device(chip, mtd, FL_WRITING);
2512
4a89ff88
BN
2513 ops.len = len;
2514 ops.datbuf = (uint8_t *)buf;
2515 ops.oobbuf = NULL;
11041ae6 2516 ops.mode = MTD_OPS_PLACE_OOB;
2af7c653 2517
4a89ff88 2518 ret = nand_do_write_ops(mtd, to, &ops);
2af7c653 2519
4a89ff88 2520 *retlen = ops.retlen;
2af7c653
SK
2521 return ret;
2522}
2523
f75e5097 2524/**
8593fbc6 2525 * nand_write - [MTD Interface] NAND write with ECC
8b6e50c9
BN
2526 * @mtd: MTD device structure
2527 * @to: offset to write to
2528 * @len: number of bytes to write
2529 * @retlen: pointer to variable to store the number of written bytes
2530 * @buf: the data to write
f75e5097 2531 *
8b6e50c9 2532 * NAND write with ECC.
f75e5097 2533 */
8593fbc6
TG
2534static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2535 size_t *retlen, const uint8_t *buf)
f75e5097 2536{
4a89ff88 2537 struct mtd_oob_ops ops;
f75e5097
TG
2538 int ret;
2539
6a8214aa 2540 nand_get_device(mtd, FL_WRITING);
4a89ff88
BN
2541 ops.len = len;
2542 ops.datbuf = (uint8_t *)buf;
2543 ops.oobbuf = NULL;
11041ae6 2544 ops.mode = MTD_OPS_PLACE_OOB;
4a89ff88 2545 ret = nand_do_write_ops(mtd, to, &ops);
4a89ff88 2546 *retlen = ops.retlen;
f75e5097 2547 nand_release_device(mtd);
8593fbc6 2548 return ret;
f75e5097 2549}
7314e9e7 2550
1da177e4 2551/**
8593fbc6 2552 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
8b6e50c9
BN
2553 * @mtd: MTD device structure
2554 * @to: offset to write to
2555 * @ops: oob operation description structure
1da177e4 2556 *
8b6e50c9 2557 * NAND write out-of-band.
1da177e4 2558 */
8593fbc6
TG
2559static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2560 struct mtd_oob_ops *ops)
1da177e4 2561{
03736155 2562 int chipnr, page, status, len;
ace4dfee 2563 struct nand_chip *chip = mtd->priv;
1da177e4 2564
289c0522 2565 pr_debug("%s: to = 0x%08x, len = %i\n",
20d8e248 2566 __func__, (unsigned int)to, (int)ops->ooblen);
1da177e4 2567
0612b9dd 2568 if (ops->mode == MTD_OPS_AUTO_OOB)
03736155
AH
2569 len = chip->ecc.layout->oobavail;
2570 else
2571 len = mtd->oobsize;
2572
1da177e4 2573 /* Do not allow write past end of page */
03736155 2574 if ((ops->ooboffs + ops->ooblen) > len) {
289c0522
BN
2575 pr_debug("%s: attempt to write past end of page\n",
2576 __func__);
1da177e4
LT
2577 return -EINVAL;
2578 }
2579
03736155 2580 if (unlikely(ops->ooboffs >= len)) {
289c0522
BN
2581 pr_debug("%s: attempt to start write outside oob\n",
2582 __func__);
03736155
AH
2583 return -EINVAL;
2584 }
2585
775adc3d 2586 /* Do not allow write past end of device */
03736155
AH
2587 if (unlikely(to >= mtd->size ||
2588 ops->ooboffs + ops->ooblen >
2589 ((mtd->size >> chip->page_shift) -
2590 (to >> chip->page_shift)) * len)) {
289c0522
BN
2591 pr_debug("%s: attempt to write beyond end of device\n",
2592 __func__);
03736155
AH
2593 return -EINVAL;
2594 }
2595
7314e9e7 2596 chipnr = (int)(to >> chip->chip_shift);
ace4dfee 2597 chip->select_chip(mtd, chipnr);
1da177e4 2598
7314e9e7
TG
2599 /* Shift to get page */
2600 page = (int)(to >> chip->page_shift);
2601
2602 /*
2603 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2604 * of my DiskOnChip 2000 test units) will clear the whole data page too
2605 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2606 * it in the doc2000 driver in August 1999. dwmw2.
2607 */
ace4dfee 2608 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1da177e4
LT
2609
2610 /* Check, if it is write protected */
b0bb6903
HS
2611 if (nand_check_wp(mtd)) {
2612 chip->select_chip(mtd, -1);
8593fbc6 2613 return -EROFS;
b0bb6903 2614 }
61b03bd7 2615
1da177e4 2616 /* Invalidate the page cache, if we write to the cached page */
ace4dfee
TG
2617 if (page == chip->pagebuf)
2618 chip->pagebuf = -1;
1da177e4 2619
f722013e 2620 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
9ce244b3 2621
0612b9dd 2622 if (ops->mode == MTD_OPS_RAW)
9ce244b3
BN
2623 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2624 else
2625 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1da177e4 2626
b0bb6903
HS
2627 chip->select_chip(mtd, -1);
2628
7bc3312b
TG
2629 if (status)
2630 return status;
1da177e4 2631
7014568b 2632 ops->oobretlen = ops->ooblen;
1da177e4 2633
7bc3312b 2634 return 0;
8593fbc6
TG
2635}
2636
2637/**
2638 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
8b6e50c9
BN
2639 * @mtd: MTD device structure
2640 * @to: offset to write to
2641 * @ops: oob operation description structure
8593fbc6
TG
2642 */
2643static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2644 struct mtd_oob_ops *ops)
2645{
8593fbc6
TG
2646 int ret = -ENOTSUPP;
2647
2648 ops->retlen = 0;
2649
2650 /* Do not allow writes past end of device */
7014568b 2651 if (ops->datbuf && (to + ops->len) > mtd->size) {
289c0522
BN
2652 pr_debug("%s: attempt to write beyond end of device\n",
2653 __func__);
8593fbc6
TG
2654 return -EINVAL;
2655 }
2656
6a8214aa 2657 nand_get_device(mtd, FL_WRITING);
8593fbc6 2658
f8ac0414 2659 switch (ops->mode) {
0612b9dd
BN
2660 case MTD_OPS_PLACE_OOB:
2661 case MTD_OPS_AUTO_OOB:
2662 case MTD_OPS_RAW:
8593fbc6
TG
2663 break;
2664
2665 default:
2666 goto out;
2667 }
2668
2669 if (!ops->datbuf)
2670 ret = nand_do_write_oob(mtd, to, ops);
2671 else
2672 ret = nand_do_write_ops(mtd, to, ops);
2673
7351d3a5 2674out:
1da177e4 2675 nand_release_device(mtd);
1da177e4
LT
2676 return ret;
2677}
2678
1da177e4 2679/**
49c50b97 2680 * single_erase - [GENERIC] NAND standard block erase command function
8b6e50c9
BN
2681 * @mtd: MTD device structure
2682 * @page: the page address of the block which will be erased
1da177e4 2683 *
49c50b97 2684 * Standard erase command for NAND chips. Returns NAND status.
1da177e4 2685 */
49c50b97 2686static int single_erase(struct mtd_info *mtd, int page)
1da177e4 2687{
ace4dfee 2688 struct nand_chip *chip = mtd->priv;
1da177e4 2689 /* Send commands to erase a block */
ace4dfee
TG
2690 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2691 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
49c50b97
BN
2692
2693 return chip->waitfunc(mtd, chip);
1da177e4
LT
2694}
2695
1da177e4
LT
2696/**
2697 * nand_erase - [MTD Interface] erase block(s)
8b6e50c9
BN
2698 * @mtd: MTD device structure
2699 * @instr: erase instruction
1da177e4 2700 *
8b6e50c9 2701 * Erase one ore more blocks.
1da177e4 2702 */
e0c7d767 2703static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
1da177e4 2704{
e0c7d767 2705 return nand_erase_nand(mtd, instr, 0);
1da177e4 2706}
61b03bd7 2707
1da177e4 2708/**
7854d3f7 2709 * nand_erase_nand - [INTERN] erase block(s)
8b6e50c9
BN
2710 * @mtd: MTD device structure
2711 * @instr: erase instruction
2712 * @allowbbt: allow erasing the bbt area
1da177e4 2713 *
8b6e50c9 2714 * Erase one ore more blocks.
1da177e4 2715 */
ace4dfee
TG
2716int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2717 int allowbbt)
1da177e4 2718{
69423d99 2719 int page, status, pages_per_block, ret, chipnr;
ace4dfee 2720 struct nand_chip *chip = mtd->priv;
69423d99 2721 loff_t len;
1da177e4 2722
289c0522
BN
2723 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2724 __func__, (unsigned long long)instr->addr,
2725 (unsigned long long)instr->len);
1da177e4 2726
6fe5a6ac 2727 if (check_offs_len(mtd, instr->addr, instr->len))
1da177e4 2728 return -EINVAL;
1da177e4 2729
1da177e4 2730 /* Grab the lock and see if the device is available */
6a8214aa 2731 nand_get_device(mtd, FL_ERASING);
1da177e4
LT
2732
2733 /* Shift to get first page */
ace4dfee
TG
2734 page = (int)(instr->addr >> chip->page_shift);
2735 chipnr = (int)(instr->addr >> chip->chip_shift);
1da177e4
LT
2736
2737 /* Calculate pages in each block */
ace4dfee 2738 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
1da177e4
LT
2739
2740 /* Select the NAND device */
ace4dfee 2741 chip->select_chip(mtd, chipnr);
1da177e4 2742
1da177e4
LT
2743 /* Check, if it is write protected */
2744 if (nand_check_wp(mtd)) {
289c0522
BN
2745 pr_debug("%s: device is write protected!\n",
2746 __func__);
1da177e4
LT
2747 instr->state = MTD_ERASE_FAILED;
2748 goto erase_exit;
2749 }
2750
2751 /* Loop through the pages */
2752 len = instr->len;
2753
2754 instr->state = MTD_ERASING;
2755
2756 while (len) {
12183a20 2757 /* Check if we have a bad block, we do not erase bad blocks! */
ace4dfee
TG
2758 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2759 chip->page_shift, 0, allowbbt)) {
d0370219
BN
2760 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2761 __func__, page);
1da177e4
LT
2762 instr->state = MTD_ERASE_FAILED;
2763 goto erase_exit;
2764 }
61b03bd7 2765
ace4dfee
TG
2766 /*
2767 * Invalidate the page cache, if we erase the block which
8b6e50c9 2768 * contains the current cached page.
ace4dfee
TG
2769 */
2770 if (page <= chip->pagebuf && chip->pagebuf <
2771 (page + pages_per_block))
2772 chip->pagebuf = -1;
1da177e4 2773
49c50b97 2774 status = chip->erase(mtd, page & chip->pagemask);
1da177e4 2775
ace4dfee
TG
2776 /*
2777 * See if operation failed and additional status checks are
2778 * available
2779 */
2780 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2781 status = chip->errstat(mtd, chip, FL_ERASING,
2782 status, page);
068e3c0a 2783
1da177e4 2784 /* See if block erase succeeded */
a4ab4c5d 2785 if (status & NAND_STATUS_FAIL) {
289c0522
BN
2786 pr_debug("%s: failed erase, page 0x%08x\n",
2787 __func__, page);
1da177e4 2788 instr->state = MTD_ERASE_FAILED;
69423d99
AH
2789 instr->fail_addr =
2790 ((loff_t)page << chip->page_shift);
1da177e4
LT
2791 goto erase_exit;
2792 }
30f464b7 2793
1da177e4 2794 /* Increment page address and decrement length */
daae74ca 2795 len -= (1ULL << chip->phys_erase_shift);
1da177e4
LT
2796 page += pages_per_block;
2797
2798 /* Check, if we cross a chip boundary */
ace4dfee 2799 if (len && !(page & chip->pagemask)) {
1da177e4 2800 chipnr++;
ace4dfee
TG
2801 chip->select_chip(mtd, -1);
2802 chip->select_chip(mtd, chipnr);
1da177e4
LT
2803 }
2804 }
2805 instr->state = MTD_ERASE_DONE;
2806
7351d3a5 2807erase_exit:
1da177e4
LT
2808
2809 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1da177e4
LT
2810
2811 /* Deselect and wake up anyone waiting on the device */
b0bb6903 2812 chip->select_chip(mtd, -1);
1da177e4
LT
2813 nand_release_device(mtd);
2814
49defc01
DW
2815 /* Do call back function */
2816 if (!ret)
2817 mtd_erase_callback(instr);
2818
1da177e4
LT
2819 /* Return more or less happy */
2820 return ret;
2821}
2822
2823/**
2824 * nand_sync - [MTD Interface] sync
8b6e50c9 2825 * @mtd: MTD device structure
1da177e4 2826 *
8b6e50c9 2827 * Sync is actually a wait for chip ready function.
1da177e4 2828 */
e0c7d767 2829static void nand_sync(struct mtd_info *mtd)
1da177e4 2830{
289c0522 2831 pr_debug("%s: called\n", __func__);
1da177e4
LT
2832
2833 /* Grab the lock and see if the device is available */
6a8214aa 2834 nand_get_device(mtd, FL_SYNCING);
1da177e4 2835 /* Release it and go back */
e0c7d767 2836 nand_release_device(mtd);
1da177e4
LT
2837}
2838
1da177e4 2839/**
ace4dfee 2840 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
8b6e50c9
BN
2841 * @mtd: MTD device structure
2842 * @offs: offset relative to mtd start
1da177e4 2843 */
ace4dfee 2844static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
1da177e4 2845{
ace4dfee 2846 return nand_block_checkbad(mtd, offs, 1, 0);
1da177e4
LT
2847}
2848
2849/**
ace4dfee 2850 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
8b6e50c9
BN
2851 * @mtd: MTD device structure
2852 * @ofs: offset relative to mtd start
1da177e4 2853 */
e0c7d767 2854static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1da177e4 2855{
1da177e4
LT
2856 int ret;
2857
f8ac0414
FF
2858 ret = nand_block_isbad(mtd, ofs);
2859 if (ret) {
8b6e50c9 2860 /* If it was bad already, return success and do nothing */
1da177e4
LT
2861 if (ret > 0)
2862 return 0;
e0c7d767
DW
2863 return ret;
2864 }
1da177e4 2865
5a0edb25 2866 return nand_block_markbad_lowlevel(mtd, ofs);
1da177e4
LT
2867}
2868
7db03ecc
HS
2869/**
2870 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2871 * @mtd: MTD device structure
2872 * @chip: nand chip info structure
2873 * @addr: feature address.
2874 * @subfeature_param: the subfeature parameters, a four bytes array.
2875 */
2876static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2877 int addr, uint8_t *subfeature_param)
2878{
2879 int status;
05f78359 2880 int i;
7db03ecc 2881
d914c932
DM
2882 if (!chip->onfi_version ||
2883 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2884 & ONFI_OPT_CMD_SET_GET_FEATURES))
7db03ecc
HS
2885 return -EINVAL;
2886
2887 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
05f78359
UKK
2888 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2889 chip->write_byte(mtd, subfeature_param[i]);
2890
7db03ecc
HS
2891 status = chip->waitfunc(mtd, chip);
2892 if (status & NAND_STATUS_FAIL)
2893 return -EIO;
2894 return 0;
2895}
2896
2897/**
2898 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2899 * @mtd: MTD device structure
2900 * @chip: nand chip info structure
2901 * @addr: feature address.
2902 * @subfeature_param: the subfeature parameters, a four bytes array.
2903 */
2904static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2905 int addr, uint8_t *subfeature_param)
2906{
05f78359
UKK
2907 int i;
2908
d914c932
DM
2909 if (!chip->onfi_version ||
2910 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2911 & ONFI_OPT_CMD_SET_GET_FEATURES))
7db03ecc
HS
2912 return -EINVAL;
2913
2914 /* clear the sub feature parameters */
2915 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2916
2917 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
05f78359
UKK
2918 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2919 *subfeature_param++ = chip->read_byte(mtd);
7db03ecc
HS
2920 return 0;
2921}
2922
962034f4
VW
2923/**
2924 * nand_suspend - [MTD Interface] Suspend the NAND flash
8b6e50c9 2925 * @mtd: MTD device structure
962034f4
VW
2926 */
2927static int nand_suspend(struct mtd_info *mtd)
2928{
6a8214aa 2929 return nand_get_device(mtd, FL_PM_SUSPENDED);
962034f4
VW
2930}
2931
2932/**
2933 * nand_resume - [MTD Interface] Resume the NAND flash
8b6e50c9 2934 * @mtd: MTD device structure
962034f4
VW
2935 */
2936static void nand_resume(struct mtd_info *mtd)
2937{
ace4dfee 2938 struct nand_chip *chip = mtd->priv;
962034f4 2939
ace4dfee 2940 if (chip->state == FL_PM_SUSPENDED)
962034f4
VW
2941 nand_release_device(mtd);
2942 else
d0370219
BN
2943 pr_err("%s called for a chip which is not in suspended state\n",
2944 __func__);
962034f4
VW
2945}
2946
8b6e50c9 2947/* Set default functions */
ace4dfee 2948static void nand_set_defaults(struct nand_chip *chip, int busw)
7aa65bfd 2949{
1da177e4 2950 /* check for proper chip_delay setup, set 20us if not */
ace4dfee
TG
2951 if (!chip->chip_delay)
2952 chip->chip_delay = 20;
1da177e4
LT
2953
2954 /* check, if a user supplied command function given */
ace4dfee
TG
2955 if (chip->cmdfunc == NULL)
2956 chip->cmdfunc = nand_command;
1da177e4
LT
2957
2958 /* check, if a user supplied wait function given */
ace4dfee
TG
2959 if (chip->waitfunc == NULL)
2960 chip->waitfunc = nand_wait;
2961
2962 if (!chip->select_chip)
2963 chip->select_chip = nand_select_chip;
68e80780 2964
4204cccd
HS
2965 /* set for ONFI nand */
2966 if (!chip->onfi_set_features)
2967 chip->onfi_set_features = nand_onfi_set_features;
2968 if (!chip->onfi_get_features)
2969 chip->onfi_get_features = nand_onfi_get_features;
2970
68e80780
BN
2971 /* If called twice, pointers that depend on busw may need to be reset */
2972 if (!chip->read_byte || chip->read_byte == nand_read_byte)
ace4dfee
TG
2973 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2974 if (!chip->read_word)
2975 chip->read_word = nand_read_word;
2976 if (!chip->block_bad)
2977 chip->block_bad = nand_block_bad;
2978 if (!chip->block_markbad)
2979 chip->block_markbad = nand_default_block_markbad;
68e80780 2980 if (!chip->write_buf || chip->write_buf == nand_write_buf)
ace4dfee 2981 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
05f78359
UKK
2982 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2983 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
68e80780 2984 if (!chip->read_buf || chip->read_buf == nand_read_buf)
ace4dfee 2985 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
ace4dfee
TG
2986 if (!chip->scan_bbt)
2987 chip->scan_bbt = nand_default_bbt;
f75e5097
TG
2988
2989 if (!chip->controller) {
2990 chip->controller = &chip->hwcontrol;
2991 spin_lock_init(&chip->controller->lock);
2992 init_waitqueue_head(&chip->controller->wq);
2993 }
2994
7aa65bfd
TG
2995}
2996
8b6e50c9 2997/* Sanitize ONFI strings so we can safely print them */
d1e1f4e4
FF
2998static void sanitize_string(uint8_t *s, size_t len)
2999{
3000 ssize_t i;
3001
8b6e50c9 3002 /* Null terminate */
d1e1f4e4
FF
3003 s[len - 1] = 0;
3004
8b6e50c9 3005 /* Remove non printable chars */
d1e1f4e4
FF
3006 for (i = 0; i < len - 1; i++) {
3007 if (s[i] < ' ' || s[i] > 127)
3008 s[i] = '?';
3009 }
3010
8b6e50c9 3011 /* Remove trailing spaces */
d1e1f4e4
FF
3012 strim(s);
3013}
3014
3015static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3016{
3017 int i;
3018 while (len--) {
3019 crc ^= *p++ << 8;
3020 for (i = 0; i < 8; i++)
3021 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3022 }
3023
3024 return crc;
3025}
3026
6dcbe0cd
HS
3027/* Parse the Extended Parameter Page. */
3028static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3029 struct nand_chip *chip, struct nand_onfi_params *p)
3030{
3031 struct onfi_ext_param_page *ep;
3032 struct onfi_ext_section *s;
3033 struct onfi_ext_ecc_info *ecc;
3034 uint8_t *cursor;
3035 int ret = -EINVAL;
3036 int len;
3037 int i;
3038
3039 len = le16_to_cpu(p->ext_param_page_length) * 16;
3040 ep = kmalloc(len, GFP_KERNEL);
5cb13271
BN
3041 if (!ep)
3042 return -ENOMEM;
6dcbe0cd
HS
3043
3044 /* Send our own NAND_CMD_PARAM. */
3045 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3046
3047 /* Use the Change Read Column command to skip the ONFI param pages. */
3048 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3049 sizeof(*p) * p->num_of_param_pages , -1);
3050
3051 /* Read out the Extended Parameter Page. */
3052 chip->read_buf(mtd, (uint8_t *)ep, len);
3053 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3054 != le16_to_cpu(ep->crc))) {
3055 pr_debug("fail in the CRC.\n");
3056 goto ext_out;
3057 }
3058
3059 /*
3060 * Check the signature.
3061 * Do not strictly follow the ONFI spec, maybe changed in future.
3062 */
3063 if (strncmp(ep->sig, "EPPS", 4)) {
3064 pr_debug("The signature is invalid.\n");
3065 goto ext_out;
3066 }
3067
3068 /* find the ECC section. */
3069 cursor = (uint8_t *)(ep + 1);
3070 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3071 s = ep->sections + i;
3072 if (s->type == ONFI_SECTION_TYPE_2)
3073 break;
3074 cursor += s->length * 16;
3075 }
3076 if (i == ONFI_EXT_SECTION_MAX) {
3077 pr_debug("We can not find the ECC section.\n");
3078 goto ext_out;
3079 }
3080
3081 /* get the info we want. */
3082 ecc = (struct onfi_ext_ecc_info *)cursor;
3083
4ae7d228
BN
3084 if (!ecc->codeword_size) {
3085 pr_debug("Invalid codeword size\n");
3086 goto ext_out;
6dcbe0cd
HS
3087 }
3088
4ae7d228
BN
3089 chip->ecc_strength_ds = ecc->ecc_bits;
3090 chip->ecc_step_ds = 1 << ecc->codeword_size;
5cb13271 3091 ret = 0;
6dcbe0cd
HS
3092
3093ext_out:
3094 kfree(ep);
3095 return ret;
3096}
3097
8429bb39
BN
3098static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3099{
3100 struct nand_chip *chip = mtd->priv;
3101 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3102
3103 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3104 feature);
3105}
3106
3107/*
3108 * Configure chip properties from Micron vendor-specific ONFI table
3109 */
3110static void nand_onfi_detect_micron(struct nand_chip *chip,
3111 struct nand_onfi_params *p)
3112{
3113 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3114
3115 if (le16_to_cpu(p->vendor_revision) < 1)
3116 return;
3117
3118 chip->read_retries = micron->read_retry_options;
3119 chip->setup_read_retry = nand_setup_read_retry_micron;
3120}
3121
6fb277ba 3122/*
8b6e50c9 3123 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
6fb277ba
FF
3124 */
3125static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
08c248fb 3126 int *busw)
6fb277ba
FF
3127{
3128 struct nand_onfi_params *p = &chip->onfi_params;
bd9c6e99 3129 int i, j;
6fb277ba
FF
3130 int val;
3131
7854d3f7 3132 /* Try ONFI for unknown chip or LP */
6fb277ba
FF
3133 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3134 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3135 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3136 return 0;
3137
6fb277ba
FF
3138 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3139 for (i = 0; i < 3; i++) {
bd9c6e99
BN
3140 for (j = 0; j < sizeof(*p); j++)
3141 ((uint8_t *)p)[j] = chip->read_byte(mtd);
6fb277ba
FF
3142 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3143 le16_to_cpu(p->crc)) {
6fb277ba
FF
3144 break;
3145 }
3146 }
3147
c7f23a70
BN
3148 if (i == 3) {
3149 pr_err("Could not find valid ONFI parameter page; aborting\n");
6fb277ba 3150 return 0;
c7f23a70 3151 }
6fb277ba 3152
8b6e50c9 3153 /* Check version */
6fb277ba 3154 val = le16_to_cpu(p->revision);
b7b1a29d
BN
3155 if (val & (1 << 5))
3156 chip->onfi_version = 23;
3157 else if (val & (1 << 4))
6fb277ba
FF
3158 chip->onfi_version = 22;
3159 else if (val & (1 << 3))
3160 chip->onfi_version = 21;
3161 else if (val & (1 << 2))
3162 chip->onfi_version = 20;
b7b1a29d 3163 else if (val & (1 << 1))
6fb277ba 3164 chip->onfi_version = 10;
b7b1a29d
BN
3165
3166 if (!chip->onfi_version) {
20171642 3167 pr_info("unsupported ONFI version: %d\n", val);
b7b1a29d
BN
3168 return 0;
3169 }
6fb277ba
FF
3170
3171 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3172 sanitize_string(p->model, sizeof(p->model));
3173 if (!mtd->name)
3174 mtd->name = p->model;
4355b70c 3175
6fb277ba 3176 mtd->writesize = le32_to_cpu(p->byte_per_page);
4355b70c
BN
3177
3178 /*
3179 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3180 * (don't ask me who thought of this...). MTD assumes that these
3181 * dimensions will be power-of-2, so just truncate the remaining area.
3182 */
3183 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3184 mtd->erasesize *= mtd->writesize;
3185
6fb277ba 3186 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4355b70c
BN
3187
3188 /* See erasesize comment */
3189 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
63795755 3190 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
13fbd179 3191 chip->bits_per_cell = p->bits_per_cell;
e2985fc1
HS
3192
3193 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
08c248fb 3194 *busw = NAND_BUSWIDTH_16;
e2985fc1
HS
3195 else
3196 *busw = 0;
6fb277ba 3197
10c86bab
HS
3198 if (p->ecc_bits != 0xff) {
3199 chip->ecc_strength_ds = p->ecc_bits;
3200 chip->ecc_step_ds = 512;
6dcbe0cd
HS
3201 } else if (chip->onfi_version >= 21 &&
3202 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3203
3204 /*
3205 * The nand_flash_detect_ext_param_page() uses the
3206 * Change Read Column command which maybe not supported
3207 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3208 * now. We do not replace user supplied command function.
3209 */
3210 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3211 chip->cmdfunc = nand_command_lp;
3212
3213 /* The Extended Parameter Page is supported since ONFI 2.1. */
3214 if (nand_flash_detect_ext_param_page(mtd, chip, p))
c7f23a70
BN
3215 pr_warn("Failed to detect ONFI extended param page\n");
3216 } else {
3217 pr_warn("Could not retrieve ONFI ECC requirements\n");
10c86bab
HS
3218 }
3219
8429bb39
BN
3220 if (p->jedec_id == NAND_MFR_MICRON)
3221 nand_onfi_detect_micron(chip, p);
3222
6fb277ba
FF
3223 return 1;
3224}
3225
91361818
HS
3226/*
3227 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3228 */
3229static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3230 int *busw)
3231{
3232 struct nand_jedec_params *p = &chip->jedec_params;
3233 struct jedec_ecc_info *ecc;
3234 int val;
3235 int i, j;
3236
3237 /* Try JEDEC for unknown chip or LP */
3238 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3239 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3240 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3241 chip->read_byte(mtd) != 'C')
3242 return 0;
3243
3244 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3245 for (i = 0; i < 3; i++) {
3246 for (j = 0; j < sizeof(*p); j++)
3247 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3248
3249 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3250 le16_to_cpu(p->crc))
3251 break;
3252 }
3253
3254 if (i == 3) {
3255 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3256 return 0;
3257 }
3258
3259 /* Check version */
3260 val = le16_to_cpu(p->revision);
3261 if (val & (1 << 2))
3262 chip->jedec_version = 10;
3263 else if (val & (1 << 1))
3264 chip->jedec_version = 1; /* vendor specific version */
3265
3266 if (!chip->jedec_version) {
3267 pr_info("unsupported JEDEC version: %d\n", val);
3268 return 0;
3269 }
3270
3271 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3272 sanitize_string(p->model, sizeof(p->model));
3273 if (!mtd->name)
3274 mtd->name = p->model;
3275
3276 mtd->writesize = le32_to_cpu(p->byte_per_page);
3277
3278 /* Please reference to the comment for nand_flash_detect_onfi. */
3279 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3280 mtd->erasesize *= mtd->writesize;
3281
3282 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3283
3284 /* Please reference to the comment for nand_flash_detect_onfi. */
3285 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3286 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3287 chip->bits_per_cell = p->bits_per_cell;
3288
3289 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3290 *busw = NAND_BUSWIDTH_16;
3291 else
3292 *busw = 0;
3293
3294 /* ECC info */
3295 ecc = &p->ecc_info[0];
3296
3297 if (ecc->codeword_size >= 9) {
3298 chip->ecc_strength_ds = ecc->ecc_bits;
3299 chip->ecc_step_ds = 1 << ecc->codeword_size;
3300 } else {
3301 pr_warn("Invalid codeword size\n");
3302 }
3303
3304 return 1;
3305}
3306
e3b88bd6
BN
3307/*
3308 * nand_id_has_period - Check if an ID string has a given wraparound period
3309 * @id_data: the ID string
3310 * @arrlen: the length of the @id_data array
3311 * @period: the period of repitition
3312 *
3313 * Check if an ID string is repeated within a given sequence of bytes at
3314 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
d4d4f1bf 3315 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
e3b88bd6
BN
3316 * if the repetition has a period of @period; otherwise, returns zero.
3317 */
3318static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3319{
3320 int i, j;
3321 for (i = 0; i < period; i++)
3322 for (j = i + period; j < arrlen; j += period)
3323 if (id_data[i] != id_data[j])
3324 return 0;
3325 return 1;
3326}
3327
3328/*
3329 * nand_id_len - Get the length of an ID string returned by CMD_READID
3330 * @id_data: the ID string
3331 * @arrlen: the length of the @id_data array
3332
3333 * Returns the length of the ID string, according to known wraparound/trailing
3334 * zero patterns. If no pattern exists, returns the length of the array.
3335 */
3336static int nand_id_len(u8 *id_data, int arrlen)
3337{
3338 int last_nonzero, period;
3339
3340 /* Find last non-zero byte */
3341 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3342 if (id_data[last_nonzero])
3343 break;
3344
3345 /* All zeros */
3346 if (last_nonzero < 0)
3347 return 0;
3348
3349 /* Calculate wraparound period */
3350 for (period = 1; period < arrlen; period++)
3351 if (nand_id_has_period(id_data, arrlen, period))
3352 break;
3353
3354 /* There's a repeated pattern */
3355 if (period < arrlen)
3356 return period;
3357
3358 /* There are trailing zeros */
3359 if (last_nonzero < arrlen - 1)
3360 return last_nonzero + 1;
3361
3362 /* No pattern detected */
3363 return arrlen;
3364}
3365
7db906b7
HS
3366/* Extract the bits of per cell from the 3rd byte of the extended ID */
3367static int nand_get_bits_per_cell(u8 cellinfo)
3368{
3369 int bits;
3370
3371 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3372 bits >>= NAND_CI_CELLTYPE_SHIFT;
3373 return bits + 1;
3374}
3375
fc09bbc0
BN
3376/*
3377 * Many new NAND share similar device ID codes, which represent the size of the
3378 * chip. The rest of the parameters must be decoded according to generic or
3379 * manufacturer-specific "extended ID" decoding patterns.
3380 */
3381static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3382 u8 id_data[8], int *busw)
3383{
e3b88bd6 3384 int extid, id_len;
fc09bbc0 3385 /* The 3rd id byte holds MLC / multichip data */
7db906b7 3386 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
fc09bbc0
BN
3387 /* The 4th id byte is the important one */
3388 extid = id_data[3];
3389
e3b88bd6
BN
3390 id_len = nand_id_len(id_data, 8);
3391
fc09bbc0
BN
3392 /*
3393 * Field definitions are in the following datasheets:
3394 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
af451af4 3395 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
73ca392f 3396 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
fc09bbc0 3397 *
af451af4
BN
3398 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3399 * ID to decide what to do.
fc09bbc0 3400 */
af451af4 3401 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
1d0ed69d 3402 !nand_is_slc(chip) && id_data[5] != 0x00) {
fc09bbc0
BN
3403 /* Calc pagesize */
3404 mtd->writesize = 2048 << (extid & 0x03);
3405 extid >>= 2;
3406 /* Calc oobsize */
e2d3a35e 3407 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
fc09bbc0
BN
3408 case 1:
3409 mtd->oobsize = 128;
3410 break;
3411 case 2:
3412 mtd->oobsize = 218;
3413 break;
3414 case 3:
3415 mtd->oobsize = 400;
3416 break;
e2d3a35e 3417 case 4:
fc09bbc0
BN
3418 mtd->oobsize = 436;
3419 break;
e2d3a35e
BN
3420 case 5:
3421 mtd->oobsize = 512;
3422 break;
3423 case 6:
e2d3a35e
BN
3424 mtd->oobsize = 640;
3425 break;
94d04e82
HS
3426 case 7:
3427 default: /* Other cases are "reserved" (unknown) */
3428 mtd->oobsize = 1024;
3429 break;
fc09bbc0
BN
3430 }
3431 extid >>= 2;
3432 /* Calc blocksize */
3433 mtd->erasesize = (128 * 1024) <<
3434 (((extid >> 1) & 0x04) | (extid & 0x03));
3435 *busw = 0;
73ca392f 3436 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
1d0ed69d 3437 !nand_is_slc(chip)) {
73ca392f
BN
3438 unsigned int tmp;
3439
3440 /* Calc pagesize */
3441 mtd->writesize = 2048 << (extid & 0x03);
3442 extid >>= 2;
3443 /* Calc oobsize */
3444 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3445 case 0:
3446 mtd->oobsize = 128;
3447 break;
3448 case 1:
3449 mtd->oobsize = 224;
3450 break;
3451 case 2:
3452 mtd->oobsize = 448;
3453 break;
3454 case 3:
3455 mtd->oobsize = 64;
3456 break;
3457 case 4:
3458 mtd->oobsize = 32;
3459 break;
3460 case 5:
3461 mtd->oobsize = 16;
3462 break;
3463 default:
3464 mtd->oobsize = 640;
3465 break;
3466 }
3467 extid >>= 2;
3468 /* Calc blocksize */
3469 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3470 if (tmp < 0x03)
3471 mtd->erasesize = (128 * 1024) << tmp;
3472 else if (tmp == 0x03)
3473 mtd->erasesize = 768 * 1024;
3474 else
3475 mtd->erasesize = (64 * 1024) << tmp;
3476 *busw = 0;
fc09bbc0
BN
3477 } else {
3478 /* Calc pagesize */
3479 mtd->writesize = 1024 << (extid & 0x03);
3480 extid >>= 2;
3481 /* Calc oobsize */
3482 mtd->oobsize = (8 << (extid & 0x01)) *
3483 (mtd->writesize >> 9);
3484 extid >>= 2;
3485 /* Calc blocksize. Blocksize is multiples of 64KiB */
3486 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3487 extid >>= 2;
3488 /* Get buswidth information */
3489 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
60c67382
BN
3490
3491 /*
3492 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3493 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3494 * follows:
3495 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3496 * 110b -> 24nm
3497 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3498 */
3499 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
1d0ed69d 3500 nand_is_slc(chip) &&
60c67382
BN
3501 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3502 !(id_data[4] & 0x80) /* !BENAND */) {
3503 mtd->oobsize = 32 * mtd->writesize >> 9;
3504 }
3505
fc09bbc0
BN
3506 }
3507}
3508
f23a481c
BN
3509/*
3510 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3511 * decodes a matching ID table entry and assigns the MTD size parameters for
3512 * the chip.
3513 */
3514static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3515 struct nand_flash_dev *type, u8 id_data[8],
3516 int *busw)
3517{
3518 int maf_id = id_data[0];
3519
3520 mtd->erasesize = type->erasesize;
3521 mtd->writesize = type->pagesize;
3522 mtd->oobsize = mtd->writesize / 32;
3523 *busw = type->options & NAND_BUSWIDTH_16;
3524
1c195e90
HS
3525 /* All legacy ID NAND are small-page, SLC */
3526 chip->bits_per_cell = 1;
3527
f23a481c
BN
3528 /*
3529 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3530 * some Spansion chips have erasesize that conflicts with size
3531 * listed in nand_ids table.
3532 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3533 */
3534 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3535 && id_data[6] == 0x00 && id_data[7] == 0x00
3536 && mtd->writesize == 512) {
3537 mtd->erasesize = 128 * 1024;
3538 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3539 }
3540}
3541
7e74c2d7
BN
3542/*
3543 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3544 * heuristic patterns using various detected parameters (e.g., manufacturer,
3545 * page size, cell-type information).
3546 */
3547static void nand_decode_bbm_options(struct mtd_info *mtd,
3548 struct nand_chip *chip, u8 id_data[8])
3549{
3550 int maf_id = id_data[0];
3551
3552 /* Set the bad block position */
3553 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3554 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3555 else
3556 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3557
3558 /*
3559 * Bad block marker is stored in the last page of each block on Samsung
3560 * and Hynix MLC devices; stored in first two pages of each block on
3561 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3562 * AMD/Spansion, and Macronix. All others scan only the first page.
3563 */
1d0ed69d 3564 if (!nand_is_slc(chip) &&
7e74c2d7
BN
3565 (maf_id == NAND_MFR_SAMSUNG ||
3566 maf_id == NAND_MFR_HYNIX))
3567 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
1d0ed69d 3568 else if ((nand_is_slc(chip) &&
7e74c2d7
BN
3569 (maf_id == NAND_MFR_SAMSUNG ||
3570 maf_id == NAND_MFR_HYNIX ||
3571 maf_id == NAND_MFR_TOSHIBA ||
3572 maf_id == NAND_MFR_AMD ||
3573 maf_id == NAND_MFR_MACRONIX)) ||
3574 (mtd->writesize == 2048 &&
3575 maf_id == NAND_MFR_MICRON))
3576 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3577}
3578
ec6e87e3
HS
3579static inline bool is_full_id_nand(struct nand_flash_dev *type)
3580{
3581 return type->id_len;
3582}
3583
3584static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3585 struct nand_flash_dev *type, u8 *id_data, int *busw)
3586{
3587 if (!strncmp(type->id, id_data, type->id_len)) {
3588 mtd->writesize = type->pagesize;
3589 mtd->erasesize = type->erasesize;
3590 mtd->oobsize = type->oobsize;
3591
7db906b7 3592 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
ec6e87e3
HS
3593 chip->chipsize = (uint64_t)type->chipsize << 20;
3594 chip->options |= type->options;
57219342
HS
3595 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3596 chip->ecc_step_ds = NAND_ECC_STEP(type);
ec6e87e3
HS
3597
3598 *busw = type->options & NAND_BUSWIDTH_16;
3599
092b6a1d
CZ
3600 if (!mtd->name)
3601 mtd->name = type->name;
3602
ec6e87e3
HS
3603 return true;
3604 }
3605 return false;
3606}
3607
7aa65bfd 3608/*
8b6e50c9 3609 * Get the flash and manufacturer id and lookup if the type is supported.
7aa65bfd
TG
3610 */
3611static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
ace4dfee 3612 struct nand_chip *chip,
7351d3a5 3613 int *maf_id, int *dev_id,
5e81e88a 3614 struct nand_flash_dev *type)
7aa65bfd 3615{
bb77082f 3616 int busw;
d1e1f4e4 3617 int i, maf_idx;
426c457a 3618 u8 id_data[8];
1da177e4
LT
3619
3620 /* Select the device */
ace4dfee 3621 chip->select_chip(mtd, 0);
1da177e4 3622
ef89a880
KB
3623 /*
3624 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
8b6e50c9 3625 * after power-up.
ef89a880
KB
3626 */
3627 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3628
1da177e4 3629 /* Send the command for reading device ID */
ace4dfee 3630 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
1da177e4
LT
3631
3632 /* Read manufacturer and device IDs */
ace4dfee 3633 *maf_id = chip->read_byte(mtd);
d1e1f4e4 3634 *dev_id = chip->read_byte(mtd);
1da177e4 3635
8b6e50c9
BN
3636 /*
3637 * Try again to make sure, as some systems the bus-hold or other
ed8165c7
BD
3638 * interface concerns can cause random data which looks like a
3639 * possibly credible NAND flash to appear. If the two results do
3640 * not match, ignore the device completely.
3641 */
3642
3643 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3644
4aef9b78
BN
3645 /* Read entire ID string */
3646 for (i = 0; i < 8; i++)
426c457a 3647 id_data[i] = chip->read_byte(mtd);
ed8165c7 3648
d1e1f4e4 3649 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
20171642 3650 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
d0370219 3651 *maf_id, *dev_id, id_data[0], id_data[1]);
ed8165c7
BD
3652 return ERR_PTR(-ENODEV);
3653 }
3654
7aa65bfd 3655 if (!type)
5e81e88a
DW
3656 type = nand_flash_ids;
3657
ec6e87e3
HS
3658 for (; type->name != NULL; type++) {
3659 if (is_full_id_nand(type)) {
3660 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3661 goto ident_done;
3662 } else if (*dev_id == type->dev_id) {
3663 break;
3664 }
3665 }
5e81e88a 3666
d1e1f4e4
FF
3667 chip->onfi_version = 0;
3668 if (!type->name || !type->pagesize) {
35fc5195 3669 /* Check if the chip is ONFI compliant */
47450b35 3670 if (nand_flash_detect_onfi(mtd, chip, &busw))
6fb277ba 3671 goto ident_done;
91361818
HS
3672
3673 /* Check if the chip is JEDEC compliant */
3674 if (nand_flash_detect_jedec(mtd, chip, &busw))
3675 goto ident_done;
d1e1f4e4
FF
3676 }
3677
5e81e88a 3678 if (!type->name)
7aa65bfd
TG
3679 return ERR_PTR(-ENODEV);
3680
ba0251fe
TG
3681 if (!mtd->name)
3682 mtd->name = type->name;
3683
69423d99 3684 chip->chipsize = (uint64_t)type->chipsize << 20;
7aa65bfd 3685
12a40a57 3686 if (!type->pagesize && chip->init_size) {
8b6e50c9 3687 /* Set the pagesize, oobsize, erasesize by the driver */
12a40a57
HS
3688 busw = chip->init_size(mtd, chip, id_data);
3689 } else if (!type->pagesize) {
fc09bbc0
BN
3690 /* Decode parameters from extended ID */
3691 nand_decode_ext_id(mtd, chip, id_data, &busw);
7aa65bfd 3692 } else {
f23a481c 3693 nand_decode_id(mtd, chip, type, id_data, &busw);
7aa65bfd 3694 }
bf7a01bf
BN
3695 /* Get chip options */
3696 chip->options |= type->options;
d1e1f4e4 3697
8b6e50c9
BN
3698 /*
3699 * Check if chip is not a Samsung device. Do not clear the
3700 * options for chips which do not have an extended id.
d1e1f4e4
FF
3701 */
3702 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3703 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3704ident_done:
3705
7aa65bfd 3706 /* Try to identify manufacturer */
9a909867 3707 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
7aa65bfd
TG
3708 if (nand_manuf_ids[maf_idx].id == *maf_id)
3709 break;
3710 }
0ea4a755 3711
64b37b2a
MC
3712 if (chip->options & NAND_BUSWIDTH_AUTO) {
3713 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3714 chip->options |= busw;
3715 nand_set_defaults(chip, busw);
3716 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3717 /*
3718 * Check, if buswidth is correct. Hardware drivers should set
3719 * chip correct!
3720 */
20171642
EG
3721 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3722 *maf_id, *dev_id);
3723 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3724 pr_warn("bus width %d instead %d bit\n",
d0370219
BN
3725 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3726 busw ? 16 : 8);
7aa65bfd
TG
3727 return ERR_PTR(-EINVAL);
3728 }
61b03bd7 3729
7e74c2d7
BN
3730 nand_decode_bbm_options(mtd, chip, id_data);
3731
7aa65bfd 3732 /* Calculate the address shift from the page size */
ace4dfee 3733 chip->page_shift = ffs(mtd->writesize) - 1;
8b6e50c9 3734 /* Convert chipsize to number of pages per chip -1 */
ace4dfee 3735 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
61b03bd7 3736
ace4dfee 3737 chip->bbt_erase_shift = chip->phys_erase_shift =
7aa65bfd 3738 ffs(mtd->erasesize) - 1;
69423d99
AH
3739 if (chip->chipsize & 0xffffffff)
3740 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
7351d3a5
FF
3741 else {
3742 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3743 chip->chip_shift += 32 - 1;
3744 }
1da177e4 3745
26d9be11 3746 chip->badblockbits = 8;
49c50b97 3747 chip->erase = single_erase;
7aa65bfd 3748
8b6e50c9 3749 /* Do not replace user supplied command function! */
ace4dfee
TG
3750 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3751 chip->cmdfunc = nand_command_lp;
7aa65bfd 3752
20171642
EG
3753 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3754 *maf_id, *dev_id);
ffdac6cd
HS
3755
3756 if (chip->onfi_version)
3757 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3758 chip->onfi_params.model);
3759 else if (chip->jedec_version)
3760 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3761 chip->jedec_params.model);
3762 else
3763 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3764 type->name);
3765
20171642 3766 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
3723e93c
HS
3767 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3768 mtd->writesize, mtd->oobsize);
7aa65bfd
TG
3769 return type;
3770}
3771
7aa65bfd 3772/**
3b85c321 3773 * nand_scan_ident - [NAND Interface] Scan for the NAND device
8b6e50c9
BN
3774 * @mtd: MTD device structure
3775 * @maxchips: number of chips to scan for
3776 * @table: alternative NAND ID table
7aa65bfd 3777 *
8b6e50c9
BN
3778 * This is the first phase of the normal nand_scan() function. It reads the
3779 * flash ID and sets up MTD fields accordingly.
7aa65bfd 3780 *
3b85c321 3781 * The mtd->owner field must be set to the module of the caller.
7aa65bfd 3782 */
5e81e88a
DW
3783int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3784 struct nand_flash_dev *table)
7aa65bfd 3785{
bb77082f 3786 int i, nand_maf_id, nand_dev_id;
ace4dfee 3787 struct nand_chip *chip = mtd->priv;
7aa65bfd
TG
3788 struct nand_flash_dev *type;
3789
7aa65bfd 3790 /* Set the default functions */
bb77082f 3791 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
7aa65bfd
TG
3792
3793 /* Read the flash type */
bb77082f
CZ
3794 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3795 &nand_dev_id, table);
7aa65bfd
TG
3796
3797 if (IS_ERR(type)) {
b1c6e6db 3798 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
d0370219 3799 pr_warn("No NAND device found\n");
ace4dfee 3800 chip->select_chip(mtd, -1);
7aa65bfd 3801 return PTR_ERR(type);
1da177e4
LT
3802 }
3803
07300164
HS
3804 chip->select_chip(mtd, -1);
3805
7aa65bfd 3806 /* Check for a chip array */
e0c7d767 3807 for (i = 1; i < maxchips; i++) {
ace4dfee 3808 chip->select_chip(mtd, i);
ef89a880
KB
3809 /* See comment in nand_get_flash_type for reset */
3810 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1da177e4 3811 /* Send the command for reading device ID */
ace4dfee 3812 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
1da177e4 3813 /* Read manufacturer and device IDs */
ace4dfee 3814 if (nand_maf_id != chip->read_byte(mtd) ||
07300164
HS
3815 nand_dev_id != chip->read_byte(mtd)) {
3816 chip->select_chip(mtd, -1);
1da177e4 3817 break;
07300164
HS
3818 }
3819 chip->select_chip(mtd, -1);
1da177e4
LT
3820 }
3821 if (i > 1)
20171642 3822 pr_info("%d chips detected\n", i);
61b03bd7 3823
1da177e4 3824 /* Store the number of chips and calc total size for mtd */
ace4dfee
TG
3825 chip->numchips = i;
3826 mtd->size = i * chip->chipsize;
7aa65bfd 3827
3b85c321
DW
3828 return 0;
3829}
7351d3a5 3830EXPORT_SYMBOL(nand_scan_ident);
3b85c321 3831
67a9ad9b
EG
3832/*
3833 * Check if the chip configuration meet the datasheet requirements.
3834
3835 * If our configuration corrects A bits per B bytes and the minimum
3836 * required correction level is X bits per Y bytes, then we must ensure
3837 * both of the following are true:
3838 *
3839 * (1) A / B >= X / Y
3840 * (2) A >= X
3841 *
3842 * Requirement (1) ensures we can correct for the required bitflip density.
3843 * Requirement (2) ensures we can correct even when all bitflips are clumped
3844 * in the same sector.
3845 */
3846static bool nand_ecc_strength_good(struct mtd_info *mtd)
3847{
3848 struct nand_chip *chip = mtd->priv;
3849 struct nand_ecc_ctrl *ecc = &chip->ecc;
3850 int corr, ds_corr;
3851
3852 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3853 /* Not enough information */
3854 return true;
3855
3856 /*
3857 * We get the number of corrected bits per page to compare
3858 * the correction density.
3859 */
3860 corr = (mtd->writesize * ecc->strength) / ecc->size;
3861 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3862
3863 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3864}
3b85c321
DW
3865
3866/**
3867 * nand_scan_tail - [NAND Interface] Scan for the NAND device
8b6e50c9 3868 * @mtd: MTD device structure
3b85c321 3869 *
8b6e50c9
BN
3870 * This is the second phase of the normal nand_scan() function. It fills out
3871 * all the uninitialized function pointers with the defaults and scans for a
3872 * bad block table if appropriate.
3b85c321
DW
3873 */
3874int nand_scan_tail(struct mtd_info *mtd)
3875{
3876 int i;
3877 struct nand_chip *chip = mtd->priv;
97de79e0 3878 struct nand_ecc_ctrl *ecc = &chip->ecc;
f02ea4e6 3879 struct nand_buffers *nbuf;
3b85c321 3880
e2414f4c
BN
3881 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3882 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3883 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3884
f02ea4e6
HS
3885 if (!(chip->options & NAND_OWN_BUFFERS)) {
3886 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3887 + mtd->oobsize * 3, GFP_KERNEL);
3888 if (!nbuf)
3889 return -ENOMEM;
3890 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3891 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3892 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3893
3894 chip->buffers = nbuf;
3895 } else {
3896 if (!chip->buffers)
3897 return -ENOMEM;
3898 }
4bf63fcb 3899
7dcdcbef 3900 /* Set the internal oob buffer location, just after the page data */
784f4d5e 3901 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
1da177e4 3902
7aa65bfd 3903 /*
8b6e50c9 3904 * If no default placement scheme is given, select an appropriate one.
7aa65bfd 3905 */
97de79e0 3906 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
61b03bd7 3907 switch (mtd->oobsize) {
1da177e4 3908 case 8:
97de79e0 3909 ecc->layout = &nand_oob_8;
1da177e4
LT
3910 break;
3911 case 16:
97de79e0 3912 ecc->layout = &nand_oob_16;
1da177e4
LT
3913 break;
3914 case 64:
97de79e0 3915 ecc->layout = &nand_oob_64;
1da177e4 3916 break;
81ec5364 3917 case 128:
97de79e0 3918 ecc->layout = &nand_oob_128;
81ec5364 3919 break;
1da177e4 3920 default:
d0370219
BN
3921 pr_warn("No oob scheme defined for oobsize %d\n",
3922 mtd->oobsize);
1da177e4
LT
3923 BUG();
3924 }
3925 }
61b03bd7 3926
956e944c
DW
3927 if (!chip->write_page)
3928 chip->write_page = nand_write_page;
3929
61b03bd7 3930 /*
8b6e50c9 3931 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
7aa65bfd 3932 * selected and we have 256 byte pagesize fallback to software ECC
e0c7d767 3933 */
956e944c 3934
97de79e0 3935 switch (ecc->mode) {
6e0cb135
SN
3936 case NAND_ECC_HW_OOB_FIRST:
3937 /* Similar to NAND_ECC_HW, but a separate read_page handle */
97de79e0 3938 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
2ac63d90 3939 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
6e0cb135
SN
3940 BUG();
3941 }
97de79e0
HS
3942 if (!ecc->read_page)
3943 ecc->read_page = nand_read_page_hwecc_oob_first;
6e0cb135 3944
6dfc6d25 3945 case NAND_ECC_HW:
8b6e50c9 3946 /* Use standard hwecc read page function? */
97de79e0
HS
3947 if (!ecc->read_page)
3948 ecc->read_page = nand_read_page_hwecc;
3949 if (!ecc->write_page)
3950 ecc->write_page = nand_write_page_hwecc;
3951 if (!ecc->read_page_raw)
3952 ecc->read_page_raw = nand_read_page_raw;
3953 if (!ecc->write_page_raw)
3954 ecc->write_page_raw = nand_write_page_raw;
3955 if (!ecc->read_oob)
3956 ecc->read_oob = nand_read_oob_std;
3957 if (!ecc->write_oob)
3958 ecc->write_oob = nand_write_oob_std;
3959 if (!ecc->read_subpage)
3960 ecc->read_subpage = nand_read_subpage;
3961 if (!ecc->write_subpage)
3962 ecc->write_subpage = nand_write_subpage_hwecc;
f5bbdacc 3963
6dfc6d25 3964 case NAND_ECC_HW_SYNDROME:
97de79e0
HS
3965 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3966 (!ecc->read_page ||
3967 ecc->read_page == nand_read_page_hwecc ||
3968 !ecc->write_page ||
3969 ecc->write_page == nand_write_page_hwecc)) {
2ac63d90 3970 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
6dfc6d25
TG
3971 BUG();
3972 }
8b6e50c9 3973 /* Use standard syndrome read/write page function? */
97de79e0
HS
3974 if (!ecc->read_page)
3975 ecc->read_page = nand_read_page_syndrome;
3976 if (!ecc->write_page)
3977 ecc->write_page = nand_write_page_syndrome;
3978 if (!ecc->read_page_raw)
3979 ecc->read_page_raw = nand_read_page_raw_syndrome;
3980 if (!ecc->write_page_raw)
3981 ecc->write_page_raw = nand_write_page_raw_syndrome;
3982 if (!ecc->read_oob)
3983 ecc->read_oob = nand_read_oob_syndrome;
3984 if (!ecc->write_oob)
3985 ecc->write_oob = nand_write_oob_syndrome;
3986
3987 if (mtd->writesize >= ecc->size) {
3988 if (!ecc->strength) {
e2788c98
MD
3989 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3990 BUG();
3991 }
6dfc6d25 3992 break;
e2788c98 3993 }
2ac63d90
RM
3994 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
3995 ecc->size, mtd->writesize);
97de79e0 3996 ecc->mode = NAND_ECC_SOFT;
61b03bd7 3997
6dfc6d25 3998 case NAND_ECC_SOFT:
97de79e0
HS
3999 ecc->calculate = nand_calculate_ecc;
4000 ecc->correct = nand_correct_data;
4001 ecc->read_page = nand_read_page_swecc;
4002 ecc->read_subpage = nand_read_subpage;
4003 ecc->write_page = nand_write_page_swecc;
4004 ecc->read_page_raw = nand_read_page_raw;
4005 ecc->write_page_raw = nand_write_page_raw;
4006 ecc->read_oob = nand_read_oob_std;
4007 ecc->write_oob = nand_write_oob_std;
4008 if (!ecc->size)
4009 ecc->size = 256;
4010 ecc->bytes = 3;
4011 ecc->strength = 1;
1da177e4 4012 break;
61b03bd7 4013
193bd400
ID
4014 case NAND_ECC_SOFT_BCH:
4015 if (!mtd_nand_has_bch()) {
148256fa 4016 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
193bd400
ID
4017 BUG();
4018 }
97de79e0
HS
4019 ecc->calculate = nand_bch_calculate_ecc;
4020 ecc->correct = nand_bch_correct_data;
4021 ecc->read_page = nand_read_page_swecc;
4022 ecc->read_subpage = nand_read_subpage;
4023 ecc->write_page = nand_write_page_swecc;
4024 ecc->read_page_raw = nand_read_page_raw;
4025 ecc->write_page_raw = nand_write_page_raw;
4026 ecc->read_oob = nand_read_oob_std;
4027 ecc->write_oob = nand_write_oob_std;
193bd400
ID
4028 /*
4029 * Board driver should supply ecc.size and ecc.bytes values to
4030 * select how many bits are correctable; see nand_bch_init()
8b6e50c9
BN
4031 * for details. Otherwise, default to 4 bits for large page
4032 * devices.
193bd400 4033 */
97de79e0
HS
4034 if (!ecc->size && (mtd->oobsize >= 64)) {
4035 ecc->size = 512;
4036 ecc->bytes = 7;
193bd400 4037 }
97de79e0
HS
4038 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4039 &ecc->layout);
4040 if (!ecc->priv) {
9a4d4d69 4041 pr_warn("BCH ECC initialization failed!\n");
193bd400
ID
4042 BUG();
4043 }
97de79e0 4044 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
193bd400
ID
4045 break;
4046
61b03bd7 4047 case NAND_ECC_NONE:
2ac63d90 4048 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
97de79e0
HS
4049 ecc->read_page = nand_read_page_raw;
4050 ecc->write_page = nand_write_page_raw;
4051 ecc->read_oob = nand_read_oob_std;
4052 ecc->read_page_raw = nand_read_page_raw;
4053 ecc->write_page_raw = nand_write_page_raw;
4054 ecc->write_oob = nand_write_oob_std;
4055 ecc->size = mtd->writesize;
4056 ecc->bytes = 0;
4057 ecc->strength = 0;
1da177e4 4058 break;
956e944c 4059
1da177e4 4060 default:
97de79e0 4061 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
61b03bd7 4062 BUG();
1da177e4 4063 }
61b03bd7 4064
9ce244b3 4065 /* For many systems, the standard OOB write also works for raw */
97de79e0
HS
4066 if (!ecc->read_oob_raw)
4067 ecc->read_oob_raw = ecc->read_oob;
4068 if (!ecc->write_oob_raw)
4069 ecc->write_oob_raw = ecc->write_oob;
9ce244b3 4070
5bd34c09
TG
4071 /*
4072 * The number of bytes available for a client to place data into
8b6e50c9 4073 * the out of band area.
5bd34c09 4074 */
97de79e0
HS
4075 ecc->layout->oobavail = 0;
4076 for (i = 0; ecc->layout->oobfree[i].length
4077 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4078 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4079 mtd->oobavail = ecc->layout->oobavail;
5bd34c09 4080
54c39e9b
TP
4081 /* ECC sanity check: warn if it's too weak */
4082 if (!nand_ecc_strength_good(mtd))
4083 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4084 mtd->name);
67a9ad9b 4085
7aa65bfd
TG
4086 /*
4087 * Set the number of read / write steps for one page depending on ECC
8b6e50c9 4088 * mode.
7aa65bfd 4089 */
97de79e0
HS
4090 ecc->steps = mtd->writesize / ecc->size;
4091 if (ecc->steps * ecc->size != mtd->writesize) {
9a4d4d69 4092 pr_warn("Invalid ECC parameters\n");
6dfc6d25 4093 BUG();
1da177e4 4094 }
97de79e0 4095 ecc->total = ecc->steps * ecc->bytes;
61b03bd7 4096
8b6e50c9 4097 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
1d0ed69d 4098 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
97de79e0 4099 switch (ecc->steps) {
29072b96
TG
4100 case 2:
4101 mtd->subpage_sft = 1;
4102 break;
4103 case 4:
4104 case 8:
81ec5364 4105 case 16:
29072b96
TG
4106 mtd->subpage_sft = 2;
4107 break;
4108 }
4109 }
4110 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4111
04bbd0ea 4112 /* Initialize state */
ace4dfee 4113 chip->state = FL_READY;
1da177e4 4114
1da177e4 4115 /* Invalidate the pagebuffer reference */
ace4dfee 4116 chip->pagebuf = -1;
1da177e4 4117
a5ff4f10 4118 /* Large page NAND with SOFT_ECC should support subpage reads */
4007e2d1
RL
4119 switch (ecc->mode) {
4120 case NAND_ECC_SOFT:
4121 case NAND_ECC_SOFT_BCH:
4122 if (chip->page_shift > 9)
4123 chip->options |= NAND_SUBPAGE_READ;
4124 break;
4125
4126 default:
4127 break;
4128 }
a5ff4f10 4129
1da177e4 4130 /* Fill in remaining MTD driver data */
963d1c28 4131 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
93edbad6
ML
4132 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4133 MTD_CAP_NANDFLASH;
3c3c10bb
AB
4134 mtd->_erase = nand_erase;
4135 mtd->_point = NULL;
4136 mtd->_unpoint = NULL;
4137 mtd->_read = nand_read;
4138 mtd->_write = nand_write;
4139 mtd->_panic_write = panic_nand_write;
4140 mtd->_read_oob = nand_read_oob;
4141 mtd->_write_oob = nand_write_oob;
4142 mtd->_sync = nand_sync;
4143 mtd->_lock = NULL;
4144 mtd->_unlock = NULL;
4145 mtd->_suspend = nand_suspend;
4146 mtd->_resume = nand_resume;
8471bb73 4147 mtd->_block_isreserved = nand_block_isreserved;
3c3c10bb
AB
4148 mtd->_block_isbad = nand_block_isbad;
4149 mtd->_block_markbad = nand_block_markbad;
cbcab65a 4150 mtd->writebufsize = mtd->writesize;
1da177e4 4151
6a918bad 4152 /* propagate ecc info to mtd_info */
97de79e0
HS
4153 mtd->ecclayout = ecc->layout;
4154 mtd->ecc_strength = ecc->strength;
4155 mtd->ecc_step_size = ecc->size;
ea3b2ea2
SL
4156 /*
4157 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4158 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4159 * properly set.
4160 */
4161 if (!mtd->bitflip_threshold)
4162 mtd->bitflip_threshold = mtd->ecc_strength;
1da177e4 4163
0040bf38 4164 /* Check, if we should skip the bad block table scan */
ace4dfee 4165 if (chip->options & NAND_SKIP_BBTSCAN)
0040bf38 4166 return 0;
1da177e4
LT
4167
4168 /* Build bad block table */
ace4dfee 4169 return chip->scan_bbt(mtd);
1da177e4 4170}
7351d3a5 4171EXPORT_SYMBOL(nand_scan_tail);
1da177e4 4172
8b6e50c9
BN
4173/*
4174 * is_module_text_address() isn't exported, and it's mostly a pointless
7351d3a5 4175 * test if this is a module _anyway_ -- they'd have to try _really_ hard
8b6e50c9
BN
4176 * to call us from in-kernel code if the core NAND support is modular.
4177 */
3b85c321
DW
4178#ifdef MODULE
4179#define caller_is_module() (1)
4180#else
4181#define caller_is_module() \
a6e6abd5 4182 is_module_text_address((unsigned long)__builtin_return_address(0))
3b85c321
DW
4183#endif
4184
4185/**
4186 * nand_scan - [NAND Interface] Scan for the NAND device
8b6e50c9
BN
4187 * @mtd: MTD device structure
4188 * @maxchips: number of chips to scan for
3b85c321 4189 *
8b6e50c9
BN
4190 * This fills out all the uninitialized function pointers with the defaults.
4191 * The flash ID is read and the mtd/chip structures are filled with the
4192 * appropriate values. The mtd->owner field must be set to the module of the
4193 * caller.
3b85c321
DW
4194 */
4195int nand_scan(struct mtd_info *mtd, int maxchips)
4196{
4197 int ret;
4198
4199 /* Many callers got this wrong, so check for it for a while... */
4200 if (!mtd->owner && caller_is_module()) {
d0370219 4201 pr_crit("%s called with NULL mtd->owner!\n", __func__);
3b85c321
DW
4202 BUG();
4203 }
4204
5e81e88a 4205 ret = nand_scan_ident(mtd, maxchips, NULL);
3b85c321
DW
4206 if (!ret)
4207 ret = nand_scan_tail(mtd);
4208 return ret;
4209}
7351d3a5 4210EXPORT_SYMBOL(nand_scan);
3b85c321 4211
1da177e4 4212/**
61b03bd7 4213 * nand_release - [NAND Interface] Free resources held by the NAND device
8b6e50c9
BN
4214 * @mtd: MTD device structure
4215 */
e0c7d767 4216void nand_release(struct mtd_info *mtd)
1da177e4 4217{
ace4dfee 4218 struct nand_chip *chip = mtd->priv;
1da177e4 4219
193bd400
ID
4220 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4221 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4222
5ffcaf3d 4223 mtd_device_unregister(mtd);
1da177e4 4224
fa671646 4225 /* Free bad block table memory */
ace4dfee 4226 kfree(chip->bbt);
4bf63fcb
DW
4227 if (!(chip->options & NAND_OWN_BUFFERS))
4228 kfree(chip->buffers);
58373ff0
BN
4229
4230 /* Free bad block descriptor memory */
4231 if (chip->badblock_pattern && chip->badblock_pattern->options
4232 & NAND_BBT_DYNAMICSTRUCT)
4233 kfree(chip->badblock_pattern);
1da177e4 4234}
e0c7d767 4235EXPORT_SYMBOL_GPL(nand_release);
8fe833c1
RP
4236
4237static int __init nand_base_init(void)
4238{
4239 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4240 return 0;
4241}
4242
4243static void __exit nand_base_exit(void)
4244{
4245 led_trigger_unregister_simple(nand_led_trigger);
4246}
4247
4248module_init(nand_base_init);
4249module_exit(nand_base_exit);
4250
e0c7d767 4251MODULE_LICENSE("GPL");
7351d3a5
FF
4252MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4253MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
e0c7d767 4254MODULE_DESCRIPTION("Generic NAND flash driver code");