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