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