1 /*
2 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
5 *
6 * Additional technical information is available on
7 * http://www.linux-mtd.infradead.org/doc/nand.html
8 *
9 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
10 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
11 *
12 * Credits:
13 * David Woodhouse for adding multichip support
14 *
15 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
18 * TODO:
19 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
21 * if we have HW ECC support.
22 * BBT table is not serialized, has to be fixed
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32 #include <linux/module.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
35 #include <linux/err.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/mm.h>
39 #include <linux/nmi.h>
40 #include <linux/types.h>
41 #include <linux/mtd/mtd.h>
42 #include <linux/mtd/rawnand.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/io.h>
48 #include <linux/mtd/partitions.h>
49 #include <linux/of.h>
50
51 static int nand_get_device(struct mtd_info *mtd, int new_state);
52
53 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
56 /* Define default oob placement schemes for large and small page devices */
nand_ooblayout_ecc_sp(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)57 static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59 {
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
68 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
72 } else {
73 if (mtd->oobsize == 8)
74 return -ERANGE;
75
76 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81 }
82
nand_ooblayout_free_sp(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)83 static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85 {
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104 }
105
106 const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109 };
110 EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
nand_ooblayout_ecc_lp(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)112 static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114 {
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
118 if (section || !ecc->total)
119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125 }
126
nand_ooblayout_free_lp(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)127 static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129 {
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140 }
141
142 const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145 };
146 EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
147
148 /*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
nand_ooblayout_ecc_lp_hamming(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)152 static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154 {
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177 }
178
nand_ooblayout_free_lp_hamming(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)179 static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181 {
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209 }
210
211 static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214 };
215
check_offs_len(struct mtd_info * mtd,loff_t ofs,uint64_t len)216 static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218 {
219 struct nand_chip *chip = mtd_to_nand(mtd);
220 int ret = 0;
221
222 /* Start address must align on block boundary */
223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
224 pr_debug("%s: unaligned address\n", __func__);
225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
230 pr_debug("%s: length not block aligned\n", __func__);
231 ret = -EINVAL;
232 }
233
234 return ret;
235 }
236
237 /**
238 * nand_release_device - [GENERIC] release chip
239 * @mtd: MTD device structure
240 *
241 * Release chip lock and wake up anyone waiting on the device.
242 */
nand_release_device(struct mtd_info * mtd)243 static void nand_release_device(struct mtd_info *mtd)
244 {
245 struct nand_chip *chip = mtd_to_nand(mtd);
246
247 /* Release the controller and the chip */
248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
253 }
254
255 /**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
257 * @mtd: MTD device structure
258 *
259 * Default read function for 8bit buswidth
260 */
nand_read_byte(struct mtd_info * mtd)261 static uint8_t nand_read_byte(struct mtd_info *mtd)
262 {
263 struct nand_chip *chip = mtd_to_nand(mtd);
264 return readb(chip->IO_ADDR_R);
265 }
266
267 /**
268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
269 * @mtd: MTD device structure
270 *
271 * Default read function for 16bit buswidth with endianness conversion.
272 *
273 */
nand_read_byte16(struct mtd_info * mtd)274 static uint8_t nand_read_byte16(struct mtd_info *mtd)
275 {
276 struct nand_chip *chip = mtd_to_nand(mtd);
277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
278 }
279
280 /**
281 * nand_read_word - [DEFAULT] read one word from the chip
282 * @mtd: MTD device structure
283 *
284 * Default read function for 16bit buswidth without endianness conversion.
285 */
nand_read_word(struct mtd_info * mtd)286 static u16 nand_read_word(struct mtd_info *mtd)
287 {
288 struct nand_chip *chip = mtd_to_nand(mtd);
289 return readw(chip->IO_ADDR_R);
290 }
291
292 /**
293 * nand_select_chip - [DEFAULT] control CE line
294 * @mtd: MTD device structure
295 * @chipnr: chipnumber to select, -1 for deselect
296 *
297 * Default select function for 1 chip devices.
298 */
nand_select_chip(struct mtd_info * mtd,int chipnr)299 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
300 {
301 struct nand_chip *chip = mtd_to_nand(mtd);
302
303 switch (chipnr) {
304 case -1:
305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
306 break;
307 case 0:
308 break;
309
310 default:
311 BUG();
312 }
313 }
314
315 /**
316 * nand_write_byte - [DEFAULT] write single byte to chip
317 * @mtd: MTD device structure
318 * @byte: value to write
319 *
320 * Default function to write a byte to I/O[7:0]
321 */
nand_write_byte(struct mtd_info * mtd,uint8_t byte)322 static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323 {
324 struct nand_chip *chip = mtd_to_nand(mtd);
325
326 chip->write_buf(mtd, &byte, 1);
327 }
328
329 /**
330 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331 * @mtd: MTD device structure
332 * @byte: value to write
333 *
334 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335 */
nand_write_byte16(struct mtd_info * mtd,uint8_t byte)336 static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337 {
338 struct nand_chip *chip = mtd_to_nand(mtd);
339 uint16_t word = byte;
340
341 /*
342 * It's not entirely clear what should happen to I/O[15:8] when writing
343 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344 *
345 * When the host supports a 16-bit bus width, only data is
346 * transferred at the 16-bit width. All address and command line
347 * transfers shall use only the lower 8-bits of the data bus. During
348 * command transfers, the host may place any value on the upper
349 * 8-bits of the data bus. During address transfers, the host shall
350 * set the upper 8-bits of the data bus to 00h.
351 *
352 * One user of the write_byte callback is nand_set_features. The
353 * four parameters are specified to be written to I/O[7:0], but this is
354 * neither an address nor a command transfer. Let's assume a 0 on the
355 * upper I/O lines is OK.
356 */
357 chip->write_buf(mtd, (uint8_t *)&word, 2);
358 }
359
360 /**
361 * nand_write_buf - [DEFAULT] write buffer to chip
362 * @mtd: MTD device structure
363 * @buf: data buffer
364 * @len: number of bytes to write
365 *
366 * Default write function for 8bit buswidth.
367 */
nand_write_buf(struct mtd_info * mtd,const uint8_t * buf,int len)368 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
369 {
370 struct nand_chip *chip = mtd_to_nand(mtd);
371
372 iowrite8_rep(chip->IO_ADDR_W, buf, len);
373 }
374
375 /**
376 * nand_read_buf - [DEFAULT] read chip data into buffer
377 * @mtd: MTD device structure
378 * @buf: buffer to store date
379 * @len: number of bytes to read
380 *
381 * Default read function for 8bit buswidth.
382 */
nand_read_buf(struct mtd_info * mtd,uint8_t * buf,int len)383 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
384 {
385 struct nand_chip *chip = mtd_to_nand(mtd);
386
387 ioread8_rep(chip->IO_ADDR_R, buf, len);
388 }
389
390 /**
391 * nand_write_buf16 - [DEFAULT] write buffer to chip
392 * @mtd: MTD device structure
393 * @buf: data buffer
394 * @len: number of bytes to write
395 *
396 * Default write function for 16bit buswidth.
397 */
nand_write_buf16(struct mtd_info * mtd,const uint8_t * buf,int len)398 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
399 {
400 struct nand_chip *chip = mtd_to_nand(mtd);
401 u16 *p = (u16 *) buf;
402
403 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
404 }
405
406 /**
407 * nand_read_buf16 - [DEFAULT] read chip data into buffer
408 * @mtd: MTD device structure
409 * @buf: buffer to store date
410 * @len: number of bytes to read
411 *
412 * Default read function for 16bit buswidth.
413 */
nand_read_buf16(struct mtd_info * mtd,uint8_t * buf,int len)414 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
415 {
416 struct nand_chip *chip = mtd_to_nand(mtd);
417 u16 *p = (u16 *) buf;
418
419 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
420 }
421
422 /**
423 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
424 * @mtd: MTD device structure
425 * @ofs: offset from device start
426 *
427 * Check, if the block is bad.
428 */
nand_block_bad(struct mtd_info * mtd,loff_t ofs)429 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
430 {
431 int page, page_end, res;
432 struct nand_chip *chip = mtd_to_nand(mtd);
433 u8 bad;
434
435 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
436 ofs += mtd->erasesize - mtd->writesize;
437
438 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
439 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
440
441 for (; page < page_end; page++) {
442 res = chip->ecc.read_oob(mtd, chip, page);
443 if (res < 0)
444 return res;
445
446 bad = chip->oob_poi[chip->badblockpos];
447
448 if (likely(chip->badblockbits == 8))
449 res = bad != 0xFF;
450 else
451 res = hweight8(bad) < chip->badblockbits;
452 if (res)
453 return res;
454 }
455
456 return 0;
457 }
458
459 /**
460 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
461 * @mtd: MTD device structure
462 * @ofs: offset from device start
463 *
464 * This is the default implementation, which can be overridden by a hardware
465 * specific driver. It provides the details for writing a bad block marker to a
466 * block.
467 */
nand_default_block_markbad(struct mtd_info * mtd,loff_t ofs)468 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
469 {
470 struct nand_chip *chip = mtd_to_nand(mtd);
471 struct mtd_oob_ops ops;
472 uint8_t buf[2] = { 0, 0 };
473 int ret = 0, res, i = 0;
474
475 memset(&ops, 0, sizeof(ops));
476 ops.oobbuf = buf;
477 ops.ooboffs = chip->badblockpos;
478 if (chip->options & NAND_BUSWIDTH_16) {
479 ops.ooboffs &= ~0x01;
480 ops.len = ops.ooblen = 2;
481 } else {
482 ops.len = ops.ooblen = 1;
483 }
484 ops.mode = MTD_OPS_PLACE_OOB;
485
486 /* Write to first/last page(s) if necessary */
487 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488 ofs += mtd->erasesize - mtd->writesize;
489 do {
490 res = nand_do_write_oob(mtd, ofs, &ops);
491 if (!ret)
492 ret = res;
493
494 i++;
495 ofs += mtd->writesize;
496 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
497
498 return ret;
499 }
500
501 /**
502 * nand_block_markbad_lowlevel - mark a block bad
503 * @mtd: MTD device structure
504 * @ofs: offset from device start
505 *
506 * This function performs the generic NAND bad block marking steps (i.e., bad
507 * block table(s) and/or marker(s)). We only allow the hardware driver to
508 * specify how to write bad block markers to OOB (chip->block_markbad).
509 *
510 * We try operations in the following order:
511 *
512 * (1) erase the affected block, to allow OOB marker to be written cleanly
513 * (2) write bad block marker to OOB area of affected block (unless flag
514 * NAND_BBT_NO_OOB_BBM is present)
515 * (3) update the BBT
516 *
517 * Note that we retain the first error encountered in (2) or (3), finish the
518 * procedures, and dump the error in the end.
519 */
nand_block_markbad_lowlevel(struct mtd_info * mtd,loff_t ofs)520 static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
521 {
522 struct nand_chip *chip = mtd_to_nand(mtd);
523 int res, ret = 0;
524
525 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
526 struct erase_info einfo;
527
528 /* Attempt erase before marking OOB */
529 memset(&einfo, 0, sizeof(einfo));
530 einfo.addr = ofs;
531 einfo.len = 1ULL << chip->phys_erase_shift;
532 nand_erase_nand(mtd, &einfo, 0);
533
534 /* Write bad block marker to OOB */
535 nand_get_device(mtd, FL_WRITING);
536 ret = chip->block_markbad(mtd, ofs);
537 nand_release_device(mtd);
538 }
539
540 /* Mark block bad in BBT */
541 if (chip->bbt) {
542 res = nand_markbad_bbt(mtd, ofs);
543 if (!ret)
544 ret = res;
545 }
546
547 if (!ret)
548 mtd->ecc_stats.badblocks++;
549
550 return ret;
551 }
552
553 /**
554 * nand_check_wp - [GENERIC] check if the chip is write protected
555 * @mtd: MTD device structure
556 *
557 * Check, if the device is write protected. The function expects, that the
558 * device is already selected.
559 */
nand_check_wp(struct mtd_info * mtd)560 static int nand_check_wp(struct mtd_info *mtd)
561 {
562 struct nand_chip *chip = mtd_to_nand(mtd);
563 u8 status;
564 int ret;
565
566 /* Broken xD cards report WP despite being writable */
567 if (chip->options & NAND_BROKEN_XD)
568 return 0;
569
570 /* Check the WP bit */
571 ret = nand_status_op(chip, &status);
572 if (ret)
573 return ret;
574
575 return status & NAND_STATUS_WP ? 0 : 1;
576 }
577
578 /**
579 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
580 * @mtd: MTD device structure
581 * @ofs: offset from device start
582 *
583 * Check if the block is marked as reserved.
584 */
nand_block_isreserved(struct mtd_info * mtd,loff_t ofs)585 static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
586 {
587 struct nand_chip *chip = mtd_to_nand(mtd);
588
589 if (!chip->bbt)
590 return 0;
591 /* Return info from the table */
592 return nand_isreserved_bbt(mtd, ofs);
593 }
594
595 /**
596 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
597 * @mtd: MTD device structure
598 * @ofs: offset from device start
599 * @allowbbt: 1, if its allowed to access the bbt area
600 *
601 * Check, if the block is bad. Either by reading the bad block table or
602 * calling of the scan function.
603 */
nand_block_checkbad(struct mtd_info * mtd,loff_t ofs,int allowbbt)604 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
605 {
606 struct nand_chip *chip = mtd_to_nand(mtd);
607
608 if (!chip->bbt)
609 return chip->block_bad(mtd, ofs);
610
611 /* Return info from the table */
612 return nand_isbad_bbt(mtd, ofs, allowbbt);
613 }
614
615 /**
616 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
617 * @mtd: MTD device structure
618 * @timeo: Timeout
619 *
620 * Helper function for nand_wait_ready used when needing to wait in interrupt
621 * context.
622 */
panic_nand_wait_ready(struct mtd_info * mtd,unsigned long timeo)623 static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
624 {
625 struct nand_chip *chip = mtd_to_nand(mtd);
626 int i;
627
628 /* Wait for the device to get ready */
629 for (i = 0; i < timeo; i++) {
630 if (chip->dev_ready(mtd))
631 break;
632 touch_softlockup_watchdog();
633 mdelay(1);
634 }
635 }
636
637 /**
638 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
639 * @mtd: MTD device structure
640 *
641 * Wait for the ready pin after a command, and warn if a timeout occurs.
642 */
nand_wait_ready(struct mtd_info * mtd)643 void nand_wait_ready(struct mtd_info *mtd)
644 {
645 struct nand_chip *chip = mtd_to_nand(mtd);
646 unsigned long timeo = 400;
647
648 if (in_interrupt() || oops_in_progress)
649 return panic_nand_wait_ready(mtd, timeo);
650
651 /* Wait until command is processed or timeout occurs */
652 timeo = jiffies + msecs_to_jiffies(timeo);
653 do {
654 if (chip->dev_ready(mtd))
655 return;
656 cond_resched();
657 } while (time_before(jiffies, timeo));
658
659 if (!chip->dev_ready(mtd))
660 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
661 }
662 EXPORT_SYMBOL_GPL(nand_wait_ready);
663
664 /**
665 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
666 * @mtd: MTD device structure
667 * @timeo: Timeout in ms
668 *
669 * Wait for status ready (i.e. command done) or timeout.
670 */
nand_wait_status_ready(struct mtd_info * mtd,unsigned long timeo)671 static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
672 {
673 register struct nand_chip *chip = mtd_to_nand(mtd);
674 int ret;
675
676 timeo = jiffies + msecs_to_jiffies(timeo);
677 do {
678 u8 status;
679
680 ret = nand_read_data_op(chip, &status, sizeof(status), true);
681 if (ret)
682 return;
683
684 if (status & NAND_STATUS_READY)
685 break;
686 touch_softlockup_watchdog();
687 } while (time_before(jiffies, timeo));
688 };
689
690 /**
691 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
692 * @chip: NAND chip structure
693 * @timeout_ms: Timeout in ms
694 *
695 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
696 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
697 * returned.
698 *
699 * This helper is intended to be used when the controller does not have access
700 * to the NAND R/B pin.
701 *
702 * Be aware that calling this helper from an ->exec_op() implementation means
703 * ->exec_op() must be re-entrant.
704 *
705 * Return 0 if the NAND chip is ready, a negative error otherwise.
706 */
nand_soft_waitrdy(struct nand_chip * chip,unsigned long timeout_ms)707 int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
708 {
709 const struct nand_sdr_timings *timings;
710 u8 status = 0;
711 int ret;
712
713 if (!chip->exec_op)
714 return -ENOTSUPP;
715
716 /* Wait tWB before polling the STATUS reg. */
717 timings = nand_get_sdr_timings(&chip->data_interface);
718 ndelay(PSEC_TO_NSEC(timings->tWB_max));
719
720 ret = nand_status_op(chip, NULL);
721 if (ret)
722 return ret;
723
724 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
725 do {
726 ret = nand_read_data_op(chip, &status, sizeof(status), true);
727 if (ret)
728 break;
729
730 if (status & NAND_STATUS_READY)
731 break;
732
733 /*
734 * Typical lowest execution time for a tR on most NANDs is 10us,
735 * use this as polling delay before doing something smarter (ie.
736 * deriving a delay from the timeout value, timeout_ms/ratio).
737 */
738 udelay(10);
739 } while (time_before(jiffies, timeout_ms));
740
741 /*
742 * We have to exit READ_STATUS mode in order to read real data on the
743 * bus in case the WAITRDY instruction is preceding a DATA_IN
744 * instruction.
745 */
746 nand_exit_status_op(chip);
747
748 if (ret)
749 return ret;
750
751 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
752 };
753 EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
754
755 /**
756 * nand_command - [DEFAULT] Send command to NAND device
757 * @mtd: MTD device structure
758 * @command: the command to be sent
759 * @column: the column address for this command, -1 if none
760 * @page_addr: the page address for this command, -1 if none
761 *
762 * Send command to NAND device. This function is used for small page devices
763 * (512 Bytes per page).
764 */
nand_command(struct mtd_info * mtd,unsigned int command,int column,int page_addr)765 static void nand_command(struct mtd_info *mtd, unsigned int command,
766 int column, int page_addr)
767 {
768 register struct nand_chip *chip = mtd_to_nand(mtd);
769 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
770
771 /* Write out the command to the device */
772 if (command == NAND_CMD_SEQIN) {
773 int readcmd;
774
775 if (column >= mtd->writesize) {
776 /* OOB area */
777 column -= mtd->writesize;
778 readcmd = NAND_CMD_READOOB;
779 } else if (column < 256) {
780 /* First 256 bytes --> READ0 */
781 readcmd = NAND_CMD_READ0;
782 } else {
783 column -= 256;
784 readcmd = NAND_CMD_READ1;
785 }
786 chip->cmd_ctrl(mtd, readcmd, ctrl);
787 ctrl &= ~NAND_CTRL_CHANGE;
788 }
789 if (command != NAND_CMD_NONE)
790 chip->cmd_ctrl(mtd, command, ctrl);
791
792 /* Address cycle, when necessary */
793 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
794 /* Serially input address */
795 if (column != -1) {
796 /* Adjust columns for 16 bit buswidth */
797 if (chip->options & NAND_BUSWIDTH_16 &&
798 !nand_opcode_8bits(command))
799 column >>= 1;
800 chip->cmd_ctrl(mtd, column, ctrl);
801 ctrl &= ~NAND_CTRL_CHANGE;
802 }
803 if (page_addr != -1) {
804 chip->cmd_ctrl(mtd, page_addr, ctrl);
805 ctrl &= ~NAND_CTRL_CHANGE;
806 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
807 if (chip->options & NAND_ROW_ADDR_3)
808 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
809 }
810 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
811
812 /*
813 * Program and erase have their own busy handlers status and sequential
814 * in needs no delay
815 */
816 switch (command) {
817
818 case NAND_CMD_NONE:
819 case NAND_CMD_PAGEPROG:
820 case NAND_CMD_ERASE1:
821 case NAND_CMD_ERASE2:
822 case NAND_CMD_SEQIN:
823 case NAND_CMD_STATUS:
824 case NAND_CMD_READID:
825 case NAND_CMD_SET_FEATURES:
826 return;
827
828 case NAND_CMD_RESET:
829 if (chip->dev_ready)
830 break;
831 udelay(chip->chip_delay);
832 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
833 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
834 chip->cmd_ctrl(mtd,
835 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
836 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
837 nand_wait_status_ready(mtd, 250);
838 return;
839
840 /* This applies to read commands */
841 case NAND_CMD_READ0:
842 /*
843 * READ0 is sometimes used to exit GET STATUS mode. When this
844 * is the case no address cycles are requested, and we can use
845 * this information to detect that we should not wait for the
846 * device to be ready.
847 */
848 if (column == -1 && page_addr == -1)
849 return;
850
851 default:
852 /*
853 * If we don't have access to the busy pin, we apply the given
854 * command delay
855 */
856 if (!chip->dev_ready) {
857 udelay(chip->chip_delay);
858 return;
859 }
860 }
861 /*
862 * Apply this short delay always to ensure that we do wait tWB in
863 * any case on any machine.
864 */
865 ndelay(100);
866
867 nand_wait_ready(mtd);
868 }
869
nand_ccs_delay(struct nand_chip * chip)870 static void nand_ccs_delay(struct nand_chip *chip)
871 {
872 /*
873 * The controller already takes care of waiting for tCCS when the RNDIN
874 * or RNDOUT command is sent, return directly.
875 */
876 if (!(chip->options & NAND_WAIT_TCCS))
877 return;
878
879 /*
880 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
881 * (which should be safe for all NANDs).
882 */
883 if (chip->setup_data_interface)
884 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
885 else
886 ndelay(500);
887 }
888
889 /**
890 * nand_command_lp - [DEFAULT] Send command to NAND large page device
891 * @mtd: MTD device structure
892 * @command: the command to be sent
893 * @column: the column address for this command, -1 if none
894 * @page_addr: the page address for this command, -1 if none
895 *
896 * Send command to NAND device. This is the version for the new large page
897 * devices. We don't have the separate regions as we have in the small page
898 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
899 */
nand_command_lp(struct mtd_info * mtd,unsigned int command,int column,int page_addr)900 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
901 int column, int page_addr)
902 {
903 register struct nand_chip *chip = mtd_to_nand(mtd);
904
905 /* Emulate NAND_CMD_READOOB */
906 if (command == NAND_CMD_READOOB) {
907 column += mtd->writesize;
908 command = NAND_CMD_READ0;
909 }
910
911 /* Command latch cycle */
912 if (command != NAND_CMD_NONE)
913 chip->cmd_ctrl(mtd, command,
914 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
915
916 if (column != -1 || page_addr != -1) {
917 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
918
919 /* Serially input address */
920 if (column != -1) {
921 /* Adjust columns for 16 bit buswidth */
922 if (chip->options & NAND_BUSWIDTH_16 &&
923 !nand_opcode_8bits(command))
924 column >>= 1;
925 chip->cmd_ctrl(mtd, column, ctrl);
926 ctrl &= ~NAND_CTRL_CHANGE;
927
928 /* Only output a single addr cycle for 8bits opcodes. */
929 if (!nand_opcode_8bits(command))
930 chip->cmd_ctrl(mtd, column >> 8, ctrl);
931 }
932 if (page_addr != -1) {
933 chip->cmd_ctrl(mtd, page_addr, ctrl);
934 chip->cmd_ctrl(mtd, page_addr >> 8,
935 NAND_NCE | NAND_ALE);
936 if (chip->options & NAND_ROW_ADDR_3)
937 chip->cmd_ctrl(mtd, page_addr >> 16,
938 NAND_NCE | NAND_ALE);
939 }
940 }
941 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
942
943 /*
944 * Program and erase have their own busy handlers status, sequential
945 * in and status need no delay.
946 */
947 switch (command) {
948
949 case NAND_CMD_NONE:
950 case NAND_CMD_CACHEDPROG:
951 case NAND_CMD_PAGEPROG:
952 case NAND_CMD_ERASE1:
953 case NAND_CMD_ERASE2:
954 case NAND_CMD_SEQIN:
955 case NAND_CMD_STATUS:
956 case NAND_CMD_READID:
957 case NAND_CMD_SET_FEATURES:
958 return;
959
960 case NAND_CMD_RNDIN:
961 nand_ccs_delay(chip);
962 return;
963
964 case NAND_CMD_RESET:
965 if (chip->dev_ready)
966 break;
967 udelay(chip->chip_delay);
968 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
969 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
970 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
971 NAND_NCE | NAND_CTRL_CHANGE);
972 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
973 nand_wait_status_ready(mtd, 250);
974 return;
975
976 case NAND_CMD_RNDOUT:
977 /* No ready / busy check necessary */
978 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
979 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
980 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
981 NAND_NCE | NAND_CTRL_CHANGE);
982
983 nand_ccs_delay(chip);
984 return;
985
986 case NAND_CMD_READ0:
987 /*
988 * READ0 is sometimes used to exit GET STATUS mode. When this
989 * is the case no address cycles are requested, and we can use
990 * this information to detect that READSTART should not be
991 * issued.
992 */
993 if (column == -1 && page_addr == -1)
994 return;
995
996 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
997 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
998 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
999 NAND_NCE | NAND_CTRL_CHANGE);
1000
1001 /* This applies to read commands */
1002 default:
1003 /*
1004 * If we don't have access to the busy pin, we apply the given
1005 * command delay.
1006 */
1007 if (!chip->dev_ready) {
1008 udelay(chip->chip_delay);
1009 return;
1010 }
1011 }
1012
1013 /*
1014 * Apply this short delay always to ensure that we do wait tWB in
1015 * any case on any machine.
1016 */
1017 ndelay(100);
1018
1019 nand_wait_ready(mtd);
1020 }
1021
1022 /**
1023 * panic_nand_get_device - [GENERIC] Get chip for selected access
1024 * @chip: the nand chip descriptor
1025 * @mtd: MTD device structure
1026 * @new_state: the state which is requested
1027 *
1028 * Used when in panic, no locks are taken.
1029 */
panic_nand_get_device(struct nand_chip * chip,struct mtd_info * mtd,int new_state)1030 static void panic_nand_get_device(struct nand_chip *chip,
1031 struct mtd_info *mtd, int new_state)
1032 {
1033 /* Hardware controller shared among independent devices */
1034 chip->controller->active = chip;
1035 chip->state = new_state;
1036 }
1037
1038 /**
1039 * nand_get_device - [GENERIC] Get chip for selected access
1040 * @mtd: MTD device structure
1041 * @new_state: the state which is requested
1042 *
1043 * Get the device and lock it for exclusive access
1044 */
1045 static int
nand_get_device(struct mtd_info * mtd,int new_state)1046 nand_get_device(struct mtd_info *mtd, int new_state)
1047 {
1048 struct nand_chip *chip = mtd_to_nand(mtd);
1049 spinlock_t *lock = &chip->controller->lock;
1050 wait_queue_head_t *wq = &chip->controller->wq;
1051 DECLARE_WAITQUEUE(wait, current);
1052 retry:
1053 spin_lock(lock);
1054
1055 /* Hardware controller shared among independent devices */
1056 if (!chip->controller->active)
1057 chip->controller->active = chip;
1058
1059 if (chip->controller->active == chip && chip->state == FL_READY) {
1060 chip->state = new_state;
1061 spin_unlock(lock);
1062 return 0;
1063 }
1064 if (new_state == FL_PM_SUSPENDED) {
1065 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1066 chip->state = FL_PM_SUSPENDED;
1067 spin_unlock(lock);
1068 return 0;
1069 }
1070 }
1071 set_current_state(TASK_UNINTERRUPTIBLE);
1072 add_wait_queue(wq, &wait);
1073 spin_unlock(lock);
1074 schedule();
1075 remove_wait_queue(wq, &wait);
1076 goto retry;
1077 }
1078
1079 /**
1080 * panic_nand_wait - [GENERIC] wait until the command is done
1081 * @mtd: MTD device structure
1082 * @chip: NAND chip structure
1083 * @timeo: timeout
1084 *
1085 * Wait for command done. This is a helper function for nand_wait used when
1086 * we are in interrupt context. May happen when in panic and trying to write
1087 * an oops through mtdoops.
1088 */
panic_nand_wait(struct mtd_info * mtd,struct nand_chip * chip,unsigned long timeo)1089 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1090 unsigned long timeo)
1091 {
1092 int i;
1093 for (i = 0; i < timeo; i++) {
1094 if (chip->dev_ready) {
1095 if (chip->dev_ready(mtd))
1096 break;
1097 } else {
1098 int ret;
1099 u8 status;
1100
1101 ret = nand_read_data_op(chip, &status, sizeof(status),
1102 true);
1103 if (ret)
1104 return;
1105
1106 if (status & NAND_STATUS_READY)
1107 break;
1108 }
1109 mdelay(1);
1110 }
1111 }
1112
1113 /**
1114 * nand_wait - [DEFAULT] wait until the command is done
1115 * @mtd: MTD device structure
1116 * @chip: NAND chip structure
1117 *
1118 * Wait for command done. This applies to erase and program only.
1119 */
nand_wait(struct mtd_info * mtd,struct nand_chip * chip)1120 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
1121 {
1122
1123 unsigned long timeo = 400;
1124 u8 status;
1125 int ret;
1126
1127 /*
1128 * Apply this short delay always to ensure that we do wait tWB in any
1129 * case on any machine.
1130 */
1131 ndelay(100);
1132
1133 ret = nand_status_op(chip, NULL);
1134 if (ret)
1135 return ret;
1136
1137 if (in_interrupt() || oops_in_progress)
1138 panic_nand_wait(mtd, chip, timeo);
1139 else {
1140 timeo = jiffies + msecs_to_jiffies(timeo);
1141 do {
1142 if (chip->dev_ready) {
1143 if (chip->dev_ready(mtd))
1144 break;
1145 } else {
1146 ret = nand_read_data_op(chip, &status,
1147 sizeof(status), true);
1148 if (ret)
1149 return ret;
1150
1151 if (status & NAND_STATUS_READY)
1152 break;
1153 }
1154 cond_resched();
1155 } while (time_before(jiffies, timeo));
1156 }
1157
1158 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1159 if (ret)
1160 return ret;
1161
1162 /* This can happen if in case of timeout or buggy dev_ready */
1163 WARN_ON(!(status & NAND_STATUS_READY));
1164 return status;
1165 }
1166
nand_supports_get_features(struct nand_chip * chip,int addr)1167 static bool nand_supports_get_features(struct nand_chip *chip, int addr)
1168 {
1169 return (chip->parameters.supports_set_get_features &&
1170 test_bit(addr, chip->parameters.get_feature_list));
1171 }
1172
nand_supports_set_features(struct nand_chip * chip,int addr)1173 static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1174 {
1175 return (chip->parameters.supports_set_get_features &&
1176 test_bit(addr, chip->parameters.set_feature_list));
1177 }
1178
1179 /**
1180 * nand_get_features - wrapper to perform a GET_FEATURE
1181 * @chip: NAND chip info structure
1182 * @addr: feature address
1183 * @subfeature_param: the subfeature parameters, a four bytes array
1184 *
1185 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1186 * operation cannot be handled.
1187 */
nand_get_features(struct nand_chip * chip,int addr,u8 * subfeature_param)1188 int nand_get_features(struct nand_chip *chip, int addr,
1189 u8 *subfeature_param)
1190 {
1191 struct mtd_info *mtd = nand_to_mtd(chip);
1192
1193 if (!nand_supports_get_features(chip, addr))
1194 return -ENOTSUPP;
1195
1196 return chip->get_features(mtd, chip, addr, subfeature_param);
1197 }
1198 EXPORT_SYMBOL_GPL(nand_get_features);
1199
1200 /**
1201 * nand_set_features - wrapper to perform a SET_FEATURE
1202 * @chip: NAND chip info structure
1203 * @addr: feature address
1204 * @subfeature_param: the subfeature parameters, a four bytes array
1205 *
1206 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1207 * operation cannot be handled.
1208 */
nand_set_features(struct nand_chip * chip,int addr,u8 * subfeature_param)1209 int nand_set_features(struct nand_chip *chip, int addr,
1210 u8 *subfeature_param)
1211 {
1212 struct mtd_info *mtd = nand_to_mtd(chip);
1213
1214 if (!nand_supports_set_features(chip, addr))
1215 return -ENOTSUPP;
1216
1217 return chip->set_features(mtd, chip, addr, subfeature_param);
1218 }
1219 EXPORT_SYMBOL_GPL(nand_set_features);
1220
1221 /**
1222 * nand_reset_data_interface - Reset data interface and timings
1223 * @chip: The NAND chip
1224 * @chipnr: Internal die id
1225 *
1226 * Reset the Data interface and timings to ONFI mode 0.
1227 *
1228 * Returns 0 for success or negative error code otherwise.
1229 */
nand_reset_data_interface(struct nand_chip * chip,int chipnr)1230 static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
1231 {
1232 struct mtd_info *mtd = nand_to_mtd(chip);
1233 int ret;
1234
1235 if (!chip->setup_data_interface)
1236 return 0;
1237
1238 /*
1239 * The ONFI specification says:
1240 * "
1241 * To transition from NV-DDR or NV-DDR2 to the SDR data
1242 * interface, the host shall use the Reset (FFh) command
1243 * using SDR timing mode 0. A device in any timing mode is
1244 * required to recognize Reset (FFh) command issued in SDR
1245 * timing mode 0.
1246 * "
1247 *
1248 * Configure the data interface in SDR mode and set the
1249 * timings to timing mode 0.
1250 */
1251
1252 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1253 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1254 if (ret)
1255 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1256
1257 return ret;
1258 }
1259
1260 /**
1261 * nand_setup_data_interface - Setup the best data interface and timings
1262 * @chip: The NAND chip
1263 * @chipnr: Internal die id
1264 *
1265 * Find and configure the best data interface and NAND timings supported by
1266 * the chip and the driver.
1267 * First tries to retrieve supported timing modes from ONFI information,
1268 * and if the NAND chip does not support ONFI, relies on the
1269 * ->onfi_timing_mode_default specified in the nand_ids table.
1270 *
1271 * Returns 0 for success or negative error code otherwise.
1272 */
nand_setup_data_interface(struct nand_chip * chip,int chipnr)1273 static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
1274 {
1275 struct mtd_info *mtd = nand_to_mtd(chip);
1276 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1277 chip->onfi_timing_mode_default,
1278 };
1279 int ret;
1280
1281 if (!chip->setup_data_interface)
1282 return 0;
1283
1284 /* Change the mode on the chip side (if supported by the NAND chip) */
1285 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
1286 chip->select_chip(mtd, chipnr);
1287 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1288 tmode_param);
1289 chip->select_chip(mtd, -1);
1290 if (ret)
1291 return ret;
1292 }
1293
1294 /* Change the mode on the controller side */
1295 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1296 if (ret)
1297 return ret;
1298
1299 /* Check the mode has been accepted by the chip, if supported */
1300 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
1301 return 0;
1302
1303 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1304 chip->select_chip(mtd, chipnr);
1305 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1306 tmode_param);
1307 chip->select_chip(mtd, -1);
1308 if (ret)
1309 goto err_reset_chip;
1310
1311 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1312 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1313 chip->onfi_timing_mode_default);
1314 goto err_reset_chip;
1315 }
1316
1317 return 0;
1318
1319 err_reset_chip:
1320 /*
1321 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1322 * timing mode.
1323 */
1324 nand_reset_data_interface(chip, chipnr);
1325 chip->select_chip(mtd, chipnr);
1326 nand_reset_op(chip);
1327 chip->select_chip(mtd, -1);
1328
1329 return ret;
1330 }
1331
1332 /**
1333 * nand_init_data_interface - find the best data interface and timings
1334 * @chip: The NAND chip
1335 *
1336 * Find the best data interface and NAND timings supported by the chip
1337 * and the driver.
1338 * First tries to retrieve supported timing modes from ONFI information,
1339 * and if the NAND chip does not support ONFI, relies on the
1340 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1341 * function nand_chip->data_interface is initialized with the best timing mode
1342 * available.
1343 *
1344 * Returns 0 for success or negative error code otherwise.
1345 */
nand_init_data_interface(struct nand_chip * chip)1346 static int nand_init_data_interface(struct nand_chip *chip)
1347 {
1348 struct mtd_info *mtd = nand_to_mtd(chip);
1349 int modes, mode, ret;
1350
1351 if (!chip->setup_data_interface)
1352 return 0;
1353
1354 /*
1355 * First try to identify the best timings from ONFI parameters and
1356 * if the NAND does not support ONFI, fallback to the default ONFI
1357 * timing mode.
1358 */
1359 modes = onfi_get_async_timing_mode(chip);
1360 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1361 if (!chip->onfi_timing_mode_default)
1362 return 0;
1363
1364 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1365 }
1366
1367
1368 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1369 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
1370 if (ret)
1371 continue;
1372
1373 /*
1374 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1375 * controller supports the requested timings.
1376 */
1377 ret = chip->setup_data_interface(mtd,
1378 NAND_DATA_IFACE_CHECK_ONLY,
1379 &chip->data_interface);
1380 if (!ret) {
1381 chip->onfi_timing_mode_default = mode;
1382 break;
1383 }
1384 }
1385
1386 return 0;
1387 }
1388
1389 /**
1390 * nand_fill_column_cycles - fill the column cycles of an address
1391 * @chip: The NAND chip
1392 * @addrs: Array of address cycles to fill
1393 * @offset_in_page: The offset in the page
1394 *
1395 * Fills the first or the first two bytes of the @addrs field depending
1396 * on the NAND bus width and the page size.
1397 *
1398 * Returns the number of cycles needed to encode the column, or a negative
1399 * error code in case one of the arguments is invalid.
1400 */
nand_fill_column_cycles(struct nand_chip * chip,u8 * addrs,unsigned int offset_in_page)1401 static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1402 unsigned int offset_in_page)
1403 {
1404 struct mtd_info *mtd = nand_to_mtd(chip);
1405
1406 /* Make sure the offset is less than the actual page size. */
1407 if (offset_in_page > mtd->writesize + mtd->oobsize)
1408 return -EINVAL;
1409
1410 /*
1411 * On small page NANDs, there's a dedicated command to access the OOB
1412 * area, and the column address is relative to the start of the OOB
1413 * area, not the start of the page. Asjust the address accordingly.
1414 */
1415 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1416 offset_in_page -= mtd->writesize;
1417
1418 /*
1419 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1420 * wide, then it must be divided by 2.
1421 */
1422 if (chip->options & NAND_BUSWIDTH_16) {
1423 if (WARN_ON(offset_in_page % 2))
1424 return -EINVAL;
1425
1426 offset_in_page /= 2;
1427 }
1428
1429 addrs[0] = offset_in_page;
1430
1431 /*
1432 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1433 * need 2
1434 */
1435 if (mtd->writesize <= 512)
1436 return 1;
1437
1438 addrs[1] = offset_in_page >> 8;
1439
1440 return 2;
1441 }
1442
nand_sp_exec_read_page_op(struct nand_chip * chip,unsigned int page,unsigned int offset_in_page,void * buf,unsigned int len)1443 static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1444 unsigned int offset_in_page, void *buf,
1445 unsigned int len)
1446 {
1447 struct mtd_info *mtd = nand_to_mtd(chip);
1448 const struct nand_sdr_timings *sdr =
1449 nand_get_sdr_timings(&chip->data_interface);
1450 u8 addrs[4];
1451 struct nand_op_instr instrs[] = {
1452 NAND_OP_CMD(NAND_CMD_READ0, 0),
1453 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1454 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1455 PSEC_TO_NSEC(sdr->tRR_min)),
1456 NAND_OP_DATA_IN(len, buf, 0),
1457 };
1458 struct nand_operation op = NAND_OPERATION(instrs);
1459 int ret;
1460
1461 /* Drop the DATA_IN instruction if len is set to 0. */
1462 if (!len)
1463 op.ninstrs--;
1464
1465 if (offset_in_page >= mtd->writesize)
1466 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1467 else if (offset_in_page >= 256 &&
1468 !(chip->options & NAND_BUSWIDTH_16))
1469 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1470
1471 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1472 if (ret < 0)
1473 return ret;
1474
1475 addrs[1] = page;
1476 addrs[2] = page >> 8;
1477
1478 if (chip->options & NAND_ROW_ADDR_3) {
1479 addrs[3] = page >> 16;
1480 instrs[1].ctx.addr.naddrs++;
1481 }
1482
1483 return nand_exec_op(chip, &op);
1484 }
1485
nand_lp_exec_read_page_op(struct nand_chip * chip,unsigned int page,unsigned int offset_in_page,void * buf,unsigned int len)1486 static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1487 unsigned int offset_in_page, void *buf,
1488 unsigned int len)
1489 {
1490 const struct nand_sdr_timings *sdr =
1491 nand_get_sdr_timings(&chip->data_interface);
1492 u8 addrs[5];
1493 struct nand_op_instr instrs[] = {
1494 NAND_OP_CMD(NAND_CMD_READ0, 0),
1495 NAND_OP_ADDR(4, addrs, 0),
1496 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1497 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1498 PSEC_TO_NSEC(sdr->tRR_min)),
1499 NAND_OP_DATA_IN(len, buf, 0),
1500 };
1501 struct nand_operation op = NAND_OPERATION(instrs);
1502 int ret;
1503
1504 /* Drop the DATA_IN instruction if len is set to 0. */
1505 if (!len)
1506 op.ninstrs--;
1507
1508 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1509 if (ret < 0)
1510 return ret;
1511
1512 addrs[2] = page;
1513 addrs[3] = page >> 8;
1514
1515 if (chip->options & NAND_ROW_ADDR_3) {
1516 addrs[4] = page >> 16;
1517 instrs[1].ctx.addr.naddrs++;
1518 }
1519
1520 return nand_exec_op(chip, &op);
1521 }
1522
1523 /**
1524 * nand_read_page_op - Do a READ PAGE operation
1525 * @chip: The NAND chip
1526 * @page: page to read
1527 * @offset_in_page: offset within the page
1528 * @buf: buffer used to store the data
1529 * @len: length of the buffer
1530 *
1531 * This function issues a READ PAGE operation.
1532 * This function does not select/unselect the CS line.
1533 *
1534 * Returns 0 on success, a negative error code otherwise.
1535 */
nand_read_page_op(struct nand_chip * chip,unsigned int page,unsigned int offset_in_page,void * buf,unsigned int len)1536 int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1537 unsigned int offset_in_page, void *buf, unsigned int len)
1538 {
1539 struct mtd_info *mtd = nand_to_mtd(chip);
1540
1541 if (len && !buf)
1542 return -EINVAL;
1543
1544 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1545 return -EINVAL;
1546
1547 if (chip->exec_op) {
1548 if (mtd->writesize > 512)
1549 return nand_lp_exec_read_page_op(chip, page,
1550 offset_in_page, buf,
1551 len);
1552
1553 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1554 buf, len);
1555 }
1556
1557 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1558 if (len)
1559 chip->read_buf(mtd, buf, len);
1560
1561 return 0;
1562 }
1563 EXPORT_SYMBOL_GPL(nand_read_page_op);
1564
1565 /**
1566 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1567 * @chip: The NAND chip
1568 * @page: parameter page to read
1569 * @buf: buffer used to store the data
1570 * @len: length of the buffer
1571 *
1572 * This function issues a READ PARAMETER PAGE operation.
1573 * This function does not select/unselect the CS line.
1574 *
1575 * Returns 0 on success, a negative error code otherwise.
1576 */
nand_read_param_page_op(struct nand_chip * chip,u8 page,void * buf,unsigned int len)1577 static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1578 unsigned int len)
1579 {
1580 struct mtd_info *mtd = nand_to_mtd(chip);
1581 unsigned int i;
1582 u8 *p = buf;
1583
1584 if (len && !buf)
1585 return -EINVAL;
1586
1587 if (chip->exec_op) {
1588 const struct nand_sdr_timings *sdr =
1589 nand_get_sdr_timings(&chip->data_interface);
1590 struct nand_op_instr instrs[] = {
1591 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1592 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1593 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1594 PSEC_TO_NSEC(sdr->tRR_min)),
1595 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1596 };
1597 struct nand_operation op = NAND_OPERATION(instrs);
1598
1599 /* Drop the DATA_IN instruction if len is set to 0. */
1600 if (!len)
1601 op.ninstrs--;
1602
1603 return nand_exec_op(chip, &op);
1604 }
1605
1606 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1607 for (i = 0; i < len; i++)
1608 p[i] = chip->read_byte(mtd);
1609
1610 return 0;
1611 }
1612
1613 /**
1614 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1615 * @chip: The NAND chip
1616 * @offset_in_page: offset within the page
1617 * @buf: buffer used to store the data
1618 * @len: length of the buffer
1619 * @force_8bit: force 8-bit bus access
1620 *
1621 * This function issues a CHANGE READ COLUMN operation.
1622 * This function does not select/unselect the CS line.
1623 *
1624 * Returns 0 on success, a negative error code otherwise.
1625 */
nand_change_read_column_op(struct nand_chip * chip,unsigned int offset_in_page,void * buf,unsigned int len,bool force_8bit)1626 int nand_change_read_column_op(struct nand_chip *chip,
1627 unsigned int offset_in_page, void *buf,
1628 unsigned int len, bool force_8bit)
1629 {
1630 struct mtd_info *mtd = nand_to_mtd(chip);
1631
1632 if (len && !buf)
1633 return -EINVAL;
1634
1635 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1636 return -EINVAL;
1637
1638 /* Small page NANDs do not support column change. */
1639 if (mtd->writesize <= 512)
1640 return -ENOTSUPP;
1641
1642 if (chip->exec_op) {
1643 const struct nand_sdr_timings *sdr =
1644 nand_get_sdr_timings(&chip->data_interface);
1645 u8 addrs[2] = {};
1646 struct nand_op_instr instrs[] = {
1647 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1648 NAND_OP_ADDR(2, addrs, 0),
1649 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1650 PSEC_TO_NSEC(sdr->tCCS_min)),
1651 NAND_OP_DATA_IN(len, buf, 0),
1652 };
1653 struct nand_operation op = NAND_OPERATION(instrs);
1654 int ret;
1655
1656 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1657 if (ret < 0)
1658 return ret;
1659
1660 /* Drop the DATA_IN instruction if len is set to 0. */
1661 if (!len)
1662 op.ninstrs--;
1663
1664 instrs[3].ctx.data.force_8bit = force_8bit;
1665
1666 return nand_exec_op(chip, &op);
1667 }
1668
1669 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1670 if (len)
1671 chip->read_buf(mtd, buf, len);
1672
1673 return 0;
1674 }
1675 EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1676
1677 /**
1678 * nand_read_oob_op - Do a READ OOB operation
1679 * @chip: The NAND chip
1680 * @page: page to read
1681 * @offset_in_oob: offset within the OOB area
1682 * @buf: buffer used to store the data
1683 * @len: length of the buffer
1684 *
1685 * This function issues a READ OOB operation.
1686 * This function does not select/unselect the CS line.
1687 *
1688 * Returns 0 on success, a negative error code otherwise.
1689 */
nand_read_oob_op(struct nand_chip * chip,unsigned int page,unsigned int offset_in_oob,void * buf,unsigned int len)1690 int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1691 unsigned int offset_in_oob, void *buf, unsigned int len)
1692 {
1693 struct mtd_info *mtd = nand_to_mtd(chip);
1694
1695 if (len && !buf)
1696 return -EINVAL;
1697
1698 if (offset_in_oob + len > mtd->oobsize)
1699 return -EINVAL;
1700
1701 if (chip->exec_op)
1702 return nand_read_page_op(chip, page,
1703 mtd->writesize + offset_in_oob,
1704 buf, len);
1705
1706 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1707 if (len)
1708 chip->read_buf(mtd, buf, len);
1709
1710 return 0;
1711 }
1712 EXPORT_SYMBOL_GPL(nand_read_oob_op);
1713
nand_exec_prog_page_op(struct nand_chip * chip,unsigned int page,unsigned int offset_in_page,const void * buf,unsigned int len,bool prog)1714 static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1715 unsigned int offset_in_page, const void *buf,
1716 unsigned int len, bool prog)
1717 {
1718 struct mtd_info *mtd = nand_to_mtd(chip);
1719 const struct nand_sdr_timings *sdr =
1720 nand_get_sdr_timings(&chip->data_interface);
1721 u8 addrs[5] = {};
1722 struct nand_op_instr instrs[] = {
1723 /*
1724 * The first instruction will be dropped if we're dealing
1725 * with a large page NAND and adjusted if we're dealing
1726 * with a small page NAND and the page offset is > 255.
1727 */
1728 NAND_OP_CMD(NAND_CMD_READ0, 0),
1729 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1730 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1731 NAND_OP_DATA_OUT(len, buf, 0),
1732 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1733 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1734 };
1735 struct nand_operation op = NAND_OPERATION(instrs);
1736 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1737 int ret;
1738 u8 status;
1739
1740 if (naddrs < 0)
1741 return naddrs;
1742
1743 addrs[naddrs++] = page;
1744 addrs[naddrs++] = page >> 8;
1745 if (chip->options & NAND_ROW_ADDR_3)
1746 addrs[naddrs++] = page >> 16;
1747
1748 instrs[2].ctx.addr.naddrs = naddrs;
1749
1750 /* Drop the last two instructions if we're not programming the page. */
1751 if (!prog) {
1752 op.ninstrs -= 2;
1753 /* Also drop the DATA_OUT instruction if empty. */
1754 if (!len)
1755 op.ninstrs--;
1756 }
1757
1758 if (mtd->writesize <= 512) {
1759 /*
1760 * Small pages need some more tweaking: we have to adjust the
1761 * first instruction depending on the page offset we're trying
1762 * to access.
1763 */
1764 if (offset_in_page >= mtd->writesize)
1765 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1766 else if (offset_in_page >= 256 &&
1767 !(chip->options & NAND_BUSWIDTH_16))
1768 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1769 } else {
1770 /*
1771 * Drop the first command if we're dealing with a large page
1772 * NAND.
1773 */
1774 op.instrs++;
1775 op.ninstrs--;
1776 }
1777
1778 ret = nand_exec_op(chip, &op);
1779 if (!prog || ret)
1780 return ret;
1781
1782 ret = nand_status_op(chip, &status);
1783 if (ret)
1784 return ret;
1785
1786 return status;
1787 }
1788
1789 /**
1790 * nand_prog_page_begin_op - starts a PROG PAGE operation
1791 * @chip: The NAND chip
1792 * @page: page to write
1793 * @offset_in_page: offset within the page
1794 * @buf: buffer containing the data to write to the page
1795 * @len: length of the buffer
1796 *
1797 * This function issues the first half of a PROG PAGE operation.
1798 * This function does not select/unselect the CS line.
1799 *
1800 * Returns 0 on success, a negative error code otherwise.
1801 */
nand_prog_page_begin_op(struct nand_chip * chip,unsigned int page,unsigned int offset_in_page,const void * buf,unsigned int len)1802 int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1803 unsigned int offset_in_page, const void *buf,
1804 unsigned int len)
1805 {
1806 struct mtd_info *mtd = nand_to_mtd(chip);
1807
1808 if (len && !buf)
1809 return -EINVAL;
1810
1811 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1812 return -EINVAL;
1813
1814 if (chip->exec_op)
1815 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1816 len, false);
1817
1818 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1819
1820 if (buf)
1821 chip->write_buf(mtd, buf, len);
1822
1823 return 0;
1824 }
1825 EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1826
1827 /**
1828 * nand_prog_page_end_op - ends a PROG PAGE operation
1829 * @chip: The NAND chip
1830 *
1831 * This function issues the second half of a PROG PAGE operation.
1832 * This function does not select/unselect the CS line.
1833 *
1834 * Returns 0 on success, a negative error code otherwise.
1835 */
nand_prog_page_end_op(struct nand_chip * chip)1836 int nand_prog_page_end_op(struct nand_chip *chip)
1837 {
1838 struct mtd_info *mtd = nand_to_mtd(chip);
1839 int ret;
1840 u8 status;
1841
1842 if (chip->exec_op) {
1843 const struct nand_sdr_timings *sdr =
1844 nand_get_sdr_timings(&chip->data_interface);
1845 struct nand_op_instr instrs[] = {
1846 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1847 PSEC_TO_NSEC(sdr->tWB_max)),
1848 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1849 };
1850 struct nand_operation op = NAND_OPERATION(instrs);
1851
1852 ret = nand_exec_op(chip, &op);
1853 if (ret)
1854 return ret;
1855
1856 ret = nand_status_op(chip, &status);
1857 if (ret)
1858 return ret;
1859 } else {
1860 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1861 ret = chip->waitfunc(mtd, chip);
1862 if (ret < 0)
1863 return ret;
1864
1865 status = ret;
1866 }
1867
1868 if (status & NAND_STATUS_FAIL)
1869 return -EIO;
1870
1871 return 0;
1872 }
1873 EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1874
1875 /**
1876 * nand_prog_page_op - Do a full PROG PAGE operation
1877 * @chip: The NAND chip
1878 * @page: page to write
1879 * @offset_in_page: offset within the page
1880 * @buf: buffer containing the data to write to the page
1881 * @len: length of the buffer
1882 *
1883 * This function issues a full PROG PAGE operation.
1884 * This function does not select/unselect the CS line.
1885 *
1886 * Returns 0 on success, a negative error code otherwise.
1887 */
nand_prog_page_op(struct nand_chip * chip,unsigned int page,unsigned int offset_in_page,const void * buf,unsigned int len)1888 int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1889 unsigned int offset_in_page, const void *buf,
1890 unsigned int len)
1891 {
1892 struct mtd_info *mtd = nand_to_mtd(chip);
1893 int status;
1894
1895 if (!len || !buf)
1896 return -EINVAL;
1897
1898 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1899 return -EINVAL;
1900
1901 if (chip->exec_op) {
1902 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1903 len, true);
1904 } else {
1905 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1906 chip->write_buf(mtd, buf, len);
1907 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1908 status = chip->waitfunc(mtd, chip);
1909 }
1910
1911 if (status & NAND_STATUS_FAIL)
1912 return -EIO;
1913
1914 return 0;
1915 }
1916 EXPORT_SYMBOL_GPL(nand_prog_page_op);
1917
1918 /**
1919 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1920 * @chip: The NAND chip
1921 * @offset_in_page: offset within the page
1922 * @buf: buffer containing the data to send to the NAND
1923 * @len: length of the buffer
1924 * @force_8bit: force 8-bit bus access
1925 *
1926 * This function issues a CHANGE WRITE COLUMN operation.
1927 * This function does not select/unselect the CS line.
1928 *
1929 * Returns 0 on success, a negative error code otherwise.
1930 */
nand_change_write_column_op(struct nand_chip * chip,unsigned int offset_in_page,const void * buf,unsigned int len,bool force_8bit)1931 int nand_change_write_column_op(struct nand_chip *chip,
1932 unsigned int offset_in_page,
1933 const void *buf, unsigned int len,
1934 bool force_8bit)
1935 {
1936 struct mtd_info *mtd = nand_to_mtd(chip);
1937
1938 if (len && !buf)
1939 return -EINVAL;
1940
1941 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1942 return -EINVAL;
1943
1944 /* Small page NANDs do not support column change. */
1945 if (mtd->writesize <= 512)
1946 return -ENOTSUPP;
1947
1948 if (chip->exec_op) {
1949 const struct nand_sdr_timings *sdr =
1950 nand_get_sdr_timings(&chip->data_interface);
1951 u8 addrs[2];
1952 struct nand_op_instr instrs[] = {
1953 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1954 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1955 NAND_OP_DATA_OUT(len, buf, 0),
1956 };
1957 struct nand_operation op = NAND_OPERATION(instrs);
1958 int ret;
1959
1960 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1961 if (ret < 0)
1962 return ret;
1963
1964 instrs[2].ctx.data.force_8bit = force_8bit;
1965
1966 /* Drop the DATA_OUT instruction if len is set to 0. */
1967 if (!len)
1968 op.ninstrs--;
1969
1970 return nand_exec_op(chip, &op);
1971 }
1972
1973 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1974 if (len)
1975 chip->write_buf(mtd, buf, len);
1976
1977 return 0;
1978 }
1979 EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1980
1981 /**
1982 * nand_readid_op - Do a READID operation
1983 * @chip: The NAND chip
1984 * @addr: address cycle to pass after the READID command
1985 * @buf: buffer used to store the ID
1986 * @len: length of the buffer
1987 *
1988 * This function sends a READID command and reads back the ID returned by the
1989 * NAND.
1990 * This function does not select/unselect the CS line.
1991 *
1992 * Returns 0 on success, a negative error code otherwise.
1993 */
nand_readid_op(struct nand_chip * chip,u8 addr,void * buf,unsigned int len)1994 int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1995 unsigned int len)
1996 {
1997 struct mtd_info *mtd = nand_to_mtd(chip);
1998 unsigned int i;
1999 u8 *id = buf;
2000
2001 if (len && !buf)
2002 return -EINVAL;
2003
2004 if (chip->exec_op) {
2005 const struct nand_sdr_timings *sdr =
2006 nand_get_sdr_timings(&chip->data_interface);
2007 struct nand_op_instr instrs[] = {
2008 NAND_OP_CMD(NAND_CMD_READID, 0),
2009 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2010 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2011 };
2012 struct nand_operation op = NAND_OPERATION(instrs);
2013
2014 /* Drop the DATA_IN instruction if len is set to 0. */
2015 if (!len)
2016 op.ninstrs--;
2017
2018 return nand_exec_op(chip, &op);
2019 }
2020
2021 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2022
2023 for (i = 0; i < len; i++)
2024 id[i] = chip->read_byte(mtd);
2025
2026 return 0;
2027 }
2028 EXPORT_SYMBOL_GPL(nand_readid_op);
2029
2030 /**
2031 * nand_status_op - Do a STATUS operation
2032 * @chip: The NAND chip
2033 * @status: out variable to store the NAND status
2034 *
2035 * This function sends a STATUS command and reads back the status returned by
2036 * the NAND.
2037 * This function does not select/unselect the CS line.
2038 *
2039 * Returns 0 on success, a negative error code otherwise.
2040 */
nand_status_op(struct nand_chip * chip,u8 * status)2041 int nand_status_op(struct nand_chip *chip, u8 *status)
2042 {
2043 struct mtd_info *mtd = nand_to_mtd(chip);
2044
2045 if (chip->exec_op) {
2046 const struct nand_sdr_timings *sdr =
2047 nand_get_sdr_timings(&chip->data_interface);
2048 struct nand_op_instr instrs[] = {
2049 NAND_OP_CMD(NAND_CMD_STATUS,
2050 PSEC_TO_NSEC(sdr->tADL_min)),
2051 NAND_OP_8BIT_DATA_IN(1, status, 0),
2052 };
2053 struct nand_operation op = NAND_OPERATION(instrs);
2054
2055 if (!status)
2056 op.ninstrs--;
2057
2058 return nand_exec_op(chip, &op);
2059 }
2060
2061 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2062 if (status)
2063 *status = chip->read_byte(mtd);
2064
2065 return 0;
2066 }
2067 EXPORT_SYMBOL_GPL(nand_status_op);
2068
2069 /**
2070 * nand_exit_status_op - Exit a STATUS operation
2071 * @chip: The NAND chip
2072 *
2073 * This function sends a READ0 command to cancel the effect of the STATUS
2074 * command to avoid reading only the status until a new read command is sent.
2075 *
2076 * This function does not select/unselect the CS line.
2077 *
2078 * Returns 0 on success, a negative error code otherwise.
2079 */
nand_exit_status_op(struct nand_chip * chip)2080 int nand_exit_status_op(struct nand_chip *chip)
2081 {
2082 struct mtd_info *mtd = nand_to_mtd(chip);
2083
2084 if (chip->exec_op) {
2085 struct nand_op_instr instrs[] = {
2086 NAND_OP_CMD(NAND_CMD_READ0, 0),
2087 };
2088 struct nand_operation op = NAND_OPERATION(instrs);
2089
2090 return nand_exec_op(chip, &op);
2091 }
2092
2093 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2094
2095 return 0;
2096 }
2097 EXPORT_SYMBOL_GPL(nand_exit_status_op);
2098
2099 /**
2100 * nand_erase_op - Do an erase operation
2101 * @chip: The NAND chip
2102 * @eraseblock: block to erase
2103 *
2104 * This function sends an ERASE command and waits for the NAND to be ready
2105 * before returning.
2106 * This function does not select/unselect the CS line.
2107 *
2108 * Returns 0 on success, a negative error code otherwise.
2109 */
nand_erase_op(struct nand_chip * chip,unsigned int eraseblock)2110 int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2111 {
2112 struct mtd_info *mtd = nand_to_mtd(chip);
2113 unsigned int page = eraseblock <<
2114 (chip->phys_erase_shift - chip->page_shift);
2115 int ret;
2116 u8 status;
2117
2118 if (chip->exec_op) {
2119 const struct nand_sdr_timings *sdr =
2120 nand_get_sdr_timings(&chip->data_interface);
2121 u8 addrs[3] = { page, page >> 8, page >> 16 };
2122 struct nand_op_instr instrs[] = {
2123 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2124 NAND_OP_ADDR(2, addrs, 0),
2125 NAND_OP_CMD(NAND_CMD_ERASE2,
2126 PSEC_TO_MSEC(sdr->tWB_max)),
2127 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2128 };
2129 struct nand_operation op = NAND_OPERATION(instrs);
2130
2131 if (chip->options & NAND_ROW_ADDR_3)
2132 instrs[1].ctx.addr.naddrs++;
2133
2134 ret = nand_exec_op(chip, &op);
2135 if (ret)
2136 return ret;
2137
2138 ret = nand_status_op(chip, &status);
2139 if (ret)
2140 return ret;
2141 } else {
2142 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2143 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2144
2145 ret = chip->waitfunc(mtd, chip);
2146 if (ret < 0)
2147 return ret;
2148
2149 status = ret;
2150 }
2151
2152 if (status & NAND_STATUS_FAIL)
2153 return -EIO;
2154
2155 return 0;
2156 }
2157 EXPORT_SYMBOL_GPL(nand_erase_op);
2158
2159 /**
2160 * nand_set_features_op - Do a SET FEATURES operation
2161 * @chip: The NAND chip
2162 * @feature: feature id
2163 * @data: 4 bytes of data
2164 *
2165 * This function sends a SET FEATURES command and waits for the NAND to be
2166 * ready before returning.
2167 * This function does not select/unselect the CS line.
2168 *
2169 * Returns 0 on success, a negative error code otherwise.
2170 */
nand_set_features_op(struct nand_chip * chip,u8 feature,const void * data)2171 static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2172 const void *data)
2173 {
2174 struct mtd_info *mtd = nand_to_mtd(chip);
2175 const u8 *params = data;
2176 int i, ret;
2177
2178 if (chip->exec_op) {
2179 const struct nand_sdr_timings *sdr =
2180 nand_get_sdr_timings(&chip->data_interface);
2181 struct nand_op_instr instrs[] = {
2182 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2183 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2184 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2185 PSEC_TO_NSEC(sdr->tWB_max)),
2186 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2187 };
2188 struct nand_operation op = NAND_OPERATION(instrs);
2189
2190 return nand_exec_op(chip, &op);
2191 }
2192
2193 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2194 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2195 chip->write_byte(mtd, params[i]);
2196
2197 ret = chip->waitfunc(mtd, chip);
2198 if (ret < 0)
2199 return ret;
2200
2201 if (ret & NAND_STATUS_FAIL)
2202 return -EIO;
2203
2204 return 0;
2205 }
2206
2207 /**
2208 * nand_get_features_op - Do a GET FEATURES operation
2209 * @chip: The NAND chip
2210 * @feature: feature id
2211 * @data: 4 bytes of data
2212 *
2213 * This function sends a GET FEATURES command and waits for the NAND to be
2214 * ready before returning.
2215 * This function does not select/unselect the CS line.
2216 *
2217 * Returns 0 on success, a negative error code otherwise.
2218 */
nand_get_features_op(struct nand_chip * chip,u8 feature,void * data)2219 static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2220 void *data)
2221 {
2222 struct mtd_info *mtd = nand_to_mtd(chip);
2223 u8 *params = data;
2224 int i;
2225
2226 if (chip->exec_op) {
2227 const struct nand_sdr_timings *sdr =
2228 nand_get_sdr_timings(&chip->data_interface);
2229 struct nand_op_instr instrs[] = {
2230 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2231 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2232 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2233 PSEC_TO_NSEC(sdr->tRR_min)),
2234 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2235 data, 0),
2236 };
2237 struct nand_operation op = NAND_OPERATION(instrs);
2238
2239 return nand_exec_op(chip, &op);
2240 }
2241
2242 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2243 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2244 params[i] = chip->read_byte(mtd);
2245
2246 return 0;
2247 }
2248
2249 /**
2250 * nand_reset_op - Do a reset operation
2251 * @chip: The NAND chip
2252 *
2253 * This function sends a RESET command and waits for the NAND to be ready
2254 * before returning.
2255 * This function does not select/unselect the CS line.
2256 *
2257 * Returns 0 on success, a negative error code otherwise.
2258 */
nand_reset_op(struct nand_chip * chip)2259 int nand_reset_op(struct nand_chip *chip)
2260 {
2261 struct mtd_info *mtd = nand_to_mtd(chip);
2262
2263 if (chip->exec_op) {
2264 const struct nand_sdr_timings *sdr =
2265 nand_get_sdr_timings(&chip->data_interface);
2266 struct nand_op_instr instrs[] = {
2267 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2268 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2269 };
2270 struct nand_operation op = NAND_OPERATION(instrs);
2271
2272 return nand_exec_op(chip, &op);
2273 }
2274
2275 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2276
2277 return 0;
2278 }
2279 EXPORT_SYMBOL_GPL(nand_reset_op);
2280
2281 /**
2282 * nand_read_data_op - Read data from the NAND
2283 * @chip: The NAND chip
2284 * @buf: buffer used to store the data
2285 * @len: length of the buffer
2286 * @force_8bit: force 8-bit bus access
2287 *
2288 * This function does a raw data read on the bus. Usually used after launching
2289 * another NAND operation like nand_read_page_op().
2290 * This function does not select/unselect the CS line.
2291 *
2292 * Returns 0 on success, a negative error code otherwise.
2293 */
nand_read_data_op(struct nand_chip * chip,void * buf,unsigned int len,bool force_8bit)2294 int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2295 bool force_8bit)
2296 {
2297 struct mtd_info *mtd = nand_to_mtd(chip);
2298
2299 if (!len || !buf)
2300 return -EINVAL;
2301
2302 if (chip->exec_op) {
2303 struct nand_op_instr instrs[] = {
2304 NAND_OP_DATA_IN(len, buf, 0),
2305 };
2306 struct nand_operation op = NAND_OPERATION(instrs);
2307
2308 instrs[0].ctx.data.force_8bit = force_8bit;
2309
2310 return nand_exec_op(chip, &op);
2311 }
2312
2313 if (force_8bit) {
2314 u8 *p = buf;
2315 unsigned int i;
2316
2317 for (i = 0; i < len; i++)
2318 p[i] = chip->read_byte(mtd);
2319 } else {
2320 chip->read_buf(mtd, buf, len);
2321 }
2322
2323 return 0;
2324 }
2325 EXPORT_SYMBOL_GPL(nand_read_data_op);
2326
2327 /**
2328 * nand_write_data_op - Write data from the NAND
2329 * @chip: The NAND chip
2330 * @buf: buffer containing the data to send on the bus
2331 * @len: length of the buffer
2332 * @force_8bit: force 8-bit bus access
2333 *
2334 * This function does a raw data write on the bus. Usually used after launching
2335 * another NAND operation like nand_write_page_begin_op().
2336 * This function does not select/unselect the CS line.
2337 *
2338 * Returns 0 on success, a negative error code otherwise.
2339 */
nand_write_data_op(struct nand_chip * chip,const void * buf,unsigned int len,bool force_8bit)2340 int nand_write_data_op(struct nand_chip *chip, const void *buf,
2341 unsigned int len, bool force_8bit)
2342 {
2343 struct mtd_info *mtd = nand_to_mtd(chip);
2344
2345 if (!len || !buf)
2346 return -EINVAL;
2347
2348 if (chip->exec_op) {
2349 struct nand_op_instr instrs[] = {
2350 NAND_OP_DATA_OUT(len, buf, 0),
2351 };
2352 struct nand_operation op = NAND_OPERATION(instrs);
2353
2354 instrs[0].ctx.data.force_8bit = force_8bit;
2355
2356 return nand_exec_op(chip, &op);
2357 }
2358
2359 if (force_8bit) {
2360 const u8 *p = buf;
2361 unsigned int i;
2362
2363 for (i = 0; i < len; i++)
2364 chip->write_byte(mtd, p[i]);
2365 } else {
2366 chip->write_buf(mtd, buf, len);
2367 }
2368
2369 return 0;
2370 }
2371 EXPORT_SYMBOL_GPL(nand_write_data_op);
2372
2373 /**
2374 * struct nand_op_parser_ctx - Context used by the parser
2375 * @instrs: array of all the instructions that must be addressed
2376 * @ninstrs: length of the @instrs array
2377 * @subop: Sub-operation to be passed to the NAND controller
2378 *
2379 * This structure is used by the core to split NAND operations into
2380 * sub-operations that can be handled by the NAND controller.
2381 */
2382 struct nand_op_parser_ctx {
2383 const struct nand_op_instr *instrs;
2384 unsigned int ninstrs;
2385 struct nand_subop subop;
2386 };
2387
2388 /**
2389 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2390 * @pat: the parser pattern element that matches @instr
2391 * @instr: pointer to the instruction to check
2392 * @start_offset: this is an in/out parameter. If @instr has already been
2393 * split, then @start_offset is the offset from which to start
2394 * (either an address cycle or an offset in the data buffer).
2395 * Conversely, if the function returns true (ie. instr must be
2396 * split), this parameter is updated to point to the first
2397 * data/address cycle that has not been taken care of.
2398 *
2399 * Some NAND controllers are limited and cannot send X address cycles with a
2400 * unique operation, or cannot read/write more than Y bytes at the same time.
2401 * In this case, split the instruction that does not fit in a single
2402 * controller-operation into two or more chunks.
2403 *
2404 * Returns true if the instruction must be split, false otherwise.
2405 * The @start_offset parameter is also updated to the offset at which the next
2406 * bundle of instruction must start (if an address or a data instruction).
2407 */
2408 static bool
nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem * pat,const struct nand_op_instr * instr,unsigned int * start_offset)2409 nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2410 const struct nand_op_instr *instr,
2411 unsigned int *start_offset)
2412 {
2413 switch (pat->type) {
2414 case NAND_OP_ADDR_INSTR:
2415 if (!pat->ctx.addr.maxcycles)
2416 break;
2417
2418 if (instr->ctx.addr.naddrs - *start_offset >
2419 pat->ctx.addr.maxcycles) {
2420 *start_offset += pat->ctx.addr.maxcycles;
2421 return true;
2422 }
2423 break;
2424
2425 case NAND_OP_DATA_IN_INSTR:
2426 case NAND_OP_DATA_OUT_INSTR:
2427 if (!pat->ctx.data.maxlen)
2428 break;
2429
2430 if (instr->ctx.data.len - *start_offset >
2431 pat->ctx.data.maxlen) {
2432 *start_offset += pat->ctx.data.maxlen;
2433 return true;
2434 }
2435 break;
2436
2437 default:
2438 break;
2439 }
2440
2441 return false;
2442 }
2443
2444 /**
2445 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2446 * remaining in the parser context
2447 * @pat: the pattern to test
2448 * @ctx: the parser context structure to match with the pattern @pat
2449 *
2450 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2451 * Returns true if this is the case, false ortherwise. When true is returned,
2452 * @ctx->subop is updated with the set of instructions to be passed to the
2453 * controller driver.
2454 */
2455 static bool
nand_op_parser_match_pat(const struct nand_op_parser_pattern * pat,struct nand_op_parser_ctx * ctx)2456 nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2457 struct nand_op_parser_ctx *ctx)
2458 {
2459 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2460 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2461 const struct nand_op_instr *instr = ctx->subop.instrs;
2462 unsigned int i, ninstrs;
2463
2464 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2465 /*
2466 * The pattern instruction does not match the operation
2467 * instruction. If the instruction is marked optional in the
2468 * pattern definition, we skip the pattern element and continue
2469 * to the next one. If the element is mandatory, there's no
2470 * match and we can return false directly.
2471 */
2472 if (instr->type != pat->elems[i].type) {
2473 if (!pat->elems[i].optional)
2474 return false;
2475
2476 continue;
2477 }
2478
2479 /*
2480 * Now check the pattern element constraints. If the pattern is
2481 * not able to handle the whole instruction in a single step,
2482 * we have to split it.
2483 * The last_instr_end_off value comes back updated to point to
2484 * the position where we have to split the instruction (the
2485 * start of the next subop chunk).
2486 */
2487 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2488 &instr_offset)) {
2489 ninstrs++;
2490 i++;
2491 break;
2492 }
2493
2494 instr++;
2495 ninstrs++;
2496 instr_offset = 0;
2497 }
2498
2499 /*
2500 * This can happen if all instructions of a pattern are optional.
2501 * Still, if there's not at least one instruction handled by this
2502 * pattern, this is not a match, and we should try the next one (if
2503 * any).
2504 */
2505 if (!ninstrs)
2506 return false;
2507
2508 /*
2509 * We had a match on the pattern head, but the pattern may be longer
2510 * than the instructions we're asked to execute. We need to make sure
2511 * there's no mandatory elements in the pattern tail.
2512 */
2513 for (; i < pat->nelems; i++) {
2514 if (!pat->elems[i].optional)
2515 return false;
2516 }
2517
2518 /*
2519 * We have a match: update the subop structure accordingly and return
2520 * true.
2521 */
2522 ctx->subop.ninstrs = ninstrs;
2523 ctx->subop.last_instr_end_off = instr_offset;
2524
2525 return true;
2526 }
2527
2528 #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
nand_op_parser_trace(const struct nand_op_parser_ctx * ctx)2529 static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2530 {
2531 const struct nand_op_instr *instr;
2532 char *prefix = " ";
2533 unsigned int i;
2534
2535 pr_debug("executing subop:\n");
2536
2537 for (i = 0; i < ctx->ninstrs; i++) {
2538 instr = &ctx->instrs[i];
2539
2540 if (instr == &ctx->subop.instrs[0])
2541 prefix = " ->";
2542
2543 switch (instr->type) {
2544 case NAND_OP_CMD_INSTR:
2545 pr_debug("%sCMD [0x%02x]\n", prefix,
2546 instr->ctx.cmd.opcode);
2547 break;
2548 case NAND_OP_ADDR_INSTR:
2549 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2550 instr->ctx.addr.naddrs,
2551 instr->ctx.addr.naddrs < 64 ?
2552 instr->ctx.addr.naddrs : 64,
2553 instr->ctx.addr.addrs);
2554 break;
2555 case NAND_OP_DATA_IN_INSTR:
2556 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2557 instr->ctx.data.len,
2558 instr->ctx.data.force_8bit ?
2559 ", force 8-bit" : "");
2560 break;
2561 case NAND_OP_DATA_OUT_INSTR:
2562 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2563 instr->ctx.data.len,
2564 instr->ctx.data.force_8bit ?
2565 ", force 8-bit" : "");
2566 break;
2567 case NAND_OP_WAITRDY_INSTR:
2568 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2569 instr->ctx.waitrdy.timeout_ms);
2570 break;
2571 }
2572
2573 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2574 prefix = " ";
2575 }
2576 }
2577 #else
nand_op_parser_trace(const struct nand_op_parser_ctx * ctx)2578 static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2579 {
2580 /* NOP */
2581 }
2582 #endif
2583
2584 /**
2585 * nand_op_parser_exec_op - exec_op parser
2586 * @chip: the NAND chip
2587 * @parser: patterns description provided by the controller driver
2588 * @op: the NAND operation to address
2589 * @check_only: when true, the function only checks if @op can be handled but
2590 * does not execute the operation
2591 *
2592 * Helper function designed to ease integration of NAND controller drivers that
2593 * only support a limited set of instruction sequences. The supported sequences
2594 * are described in @parser, and the framework takes care of splitting @op into
2595 * multiple sub-operations (if required) and pass them back to the ->exec()
2596 * callback of the matching pattern if @check_only is set to false.
2597 *
2598 * NAND controller drivers should call this function from their own ->exec_op()
2599 * implementation.
2600 *
2601 * Returns 0 on success, a negative error code otherwise. A failure can be
2602 * caused by an unsupported operation (none of the supported patterns is able
2603 * to handle the requested operation), or an error returned by one of the
2604 * matching pattern->exec() hook.
2605 */
nand_op_parser_exec_op(struct nand_chip * chip,const struct nand_op_parser * parser,const struct nand_operation * op,bool check_only)2606 int nand_op_parser_exec_op(struct nand_chip *chip,
2607 const struct nand_op_parser *parser,
2608 const struct nand_operation *op, bool check_only)
2609 {
2610 struct nand_op_parser_ctx ctx = {
2611 .subop.instrs = op->instrs,
2612 .instrs = op->instrs,
2613 .ninstrs = op->ninstrs,
2614 };
2615 unsigned int i;
2616
2617 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2618 int ret;
2619
2620 for (i = 0; i < parser->npatterns; i++) {
2621 const struct nand_op_parser_pattern *pattern;
2622
2623 pattern = &parser->patterns[i];
2624 if (!nand_op_parser_match_pat(pattern, &ctx))
2625 continue;
2626
2627 nand_op_parser_trace(&ctx);
2628
2629 if (check_only)
2630 break;
2631
2632 ret = pattern->exec(chip, &ctx.subop);
2633 if (ret)
2634 return ret;
2635
2636 break;
2637 }
2638
2639 if (i == parser->npatterns) {
2640 pr_debug("->exec_op() parser: pattern not found!\n");
2641 return -ENOTSUPP;
2642 }
2643
2644 /*
2645 * Update the context structure by pointing to the start of the
2646 * next subop.
2647 */
2648 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2649 if (ctx.subop.last_instr_end_off)
2650 ctx.subop.instrs -= 1;
2651
2652 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2653 }
2654
2655 return 0;
2656 }
2657 EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2658
nand_instr_is_data(const struct nand_op_instr * instr)2659 static bool nand_instr_is_data(const struct nand_op_instr *instr)
2660 {
2661 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2662 instr->type == NAND_OP_DATA_OUT_INSTR);
2663 }
2664
nand_subop_instr_is_valid(const struct nand_subop * subop,unsigned int instr_idx)2665 static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2666 unsigned int instr_idx)
2667 {
2668 return subop && instr_idx < subop->ninstrs;
2669 }
2670
nand_subop_get_start_off(const struct nand_subop * subop,unsigned int instr_idx)2671 static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2672 unsigned int instr_idx)
2673 {
2674 if (instr_idx)
2675 return 0;
2676
2677 return subop->first_instr_start_off;
2678 }
2679
2680 /**
2681 * nand_subop_get_addr_start_off - Get the start offset in an address array
2682 * @subop: The entire sub-operation
2683 * @instr_idx: Index of the instruction inside the sub-operation
2684 *
2685 * During driver development, one could be tempted to directly use the
2686 * ->addr.addrs field of address instructions. This is wrong as address
2687 * instructions might be split.
2688 *
2689 * Given an address instruction, returns the offset of the first cycle to issue.
2690 */
nand_subop_get_addr_start_off(const struct nand_subop * subop,unsigned int instr_idx)2691 unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2692 unsigned int instr_idx)
2693 {
2694 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2695 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2696 return 0;
2697
2698 return nand_subop_get_start_off(subop, instr_idx);
2699 }
2700 EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2701
2702 /**
2703 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2704 * @subop: The entire sub-operation
2705 * @instr_idx: Index of the instruction inside the sub-operation
2706 *
2707 * During driver development, one could be tempted to directly use the
2708 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2709 * might be split.
2710 *
2711 * Given an address instruction, returns the number of address cycle to issue.
2712 */
nand_subop_get_num_addr_cyc(const struct nand_subop * subop,unsigned int instr_idx)2713 unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2714 unsigned int instr_idx)
2715 {
2716 int start_off, end_off;
2717
2718 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2719 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2720 return 0;
2721
2722 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2723
2724 if (instr_idx == subop->ninstrs - 1 &&
2725 subop->last_instr_end_off)
2726 end_off = subop->last_instr_end_off;
2727 else
2728 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2729
2730 return end_off - start_off;
2731 }
2732 EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2733
2734 /**
2735 * nand_subop_get_data_start_off - Get the start offset in a data array
2736 * @subop: The entire sub-operation
2737 * @instr_idx: Index of the instruction inside the sub-operation
2738 *
2739 * During driver development, one could be tempted to directly use the
2740 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2741 * instructions might be split.
2742 *
2743 * Given a data instruction, returns the offset to start from.
2744 */
nand_subop_get_data_start_off(const struct nand_subop * subop,unsigned int instr_idx)2745 unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2746 unsigned int instr_idx)
2747 {
2748 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2749 !nand_instr_is_data(&subop->instrs[instr_idx])))
2750 return 0;
2751
2752 return nand_subop_get_start_off(subop, instr_idx);
2753 }
2754 EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2755
2756 /**
2757 * nand_subop_get_data_len - Get the number of bytes to retrieve
2758 * @subop: The entire sub-operation
2759 * @instr_idx: Index of the instruction inside the sub-operation
2760 *
2761 * During driver development, one could be tempted to directly use the
2762 * ->data->len field of a data instruction. This is wrong as data instructions
2763 * might be split.
2764 *
2765 * Returns the length of the chunk of data to send/receive.
2766 */
nand_subop_get_data_len(const struct nand_subop * subop,unsigned int instr_idx)2767 unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2768 unsigned int instr_idx)
2769 {
2770 int start_off = 0, end_off;
2771
2772 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2773 !nand_instr_is_data(&subop->instrs[instr_idx])))
2774 return 0;
2775
2776 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2777
2778 if (instr_idx == subop->ninstrs - 1 &&
2779 subop->last_instr_end_off)
2780 end_off = subop->last_instr_end_off;
2781 else
2782 end_off = subop->instrs[instr_idx].ctx.data.len;
2783
2784 return end_off - start_off;
2785 }
2786 EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2787
2788 /**
2789 * nand_reset - Reset and initialize a NAND device
2790 * @chip: The NAND chip
2791 * @chipnr: Internal die id
2792 *
2793 * Save the timings data structure, then apply SDR timings mode 0 (see
2794 * nand_reset_data_interface for details), do the reset operation, and
2795 * apply back the previous timings.
2796 *
2797 * Returns 0 on success, a negative error code otherwise.
2798 */
nand_reset(struct nand_chip * chip,int chipnr)2799 int nand_reset(struct nand_chip *chip, int chipnr)
2800 {
2801 struct mtd_info *mtd = nand_to_mtd(chip);
2802 struct nand_data_interface saved_data_intf = chip->data_interface;
2803 int ret;
2804
2805 ret = nand_reset_data_interface(chip, chipnr);
2806 if (ret)
2807 return ret;
2808
2809 /*
2810 * The CS line has to be released before we can apply the new NAND
2811 * interface settings, hence this weird ->select_chip() dance.
2812 */
2813 chip->select_chip(mtd, chipnr);
2814 ret = nand_reset_op(chip);
2815 chip->select_chip(mtd, -1);
2816 if (ret)
2817 return ret;
2818
2819 /*
2820 * A nand_reset_data_interface() put both the NAND chip and the NAND
2821 * controller in timings mode 0. If the default mode for this chip is
2822 * also 0, no need to proceed to the change again. Plus, at probe time,
2823 * nand_setup_data_interface() uses ->set/get_features() which would
2824 * fail anyway as the parameter page is not available yet.
2825 */
2826 if (!chip->onfi_timing_mode_default)
2827 return 0;
2828
2829 chip->data_interface = saved_data_intf;
2830 ret = nand_setup_data_interface(chip, chipnr);
2831 if (ret)
2832 return ret;
2833
2834 return 0;
2835 }
2836 EXPORT_SYMBOL_GPL(nand_reset);
2837
2838 /**
2839 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2840 * @buf: buffer to test
2841 * @len: buffer length
2842 * @bitflips_threshold: maximum number of bitflips
2843 *
2844 * Check if a buffer contains only 0xff, which means the underlying region
2845 * has been erased and is ready to be programmed.
2846 * The bitflips_threshold specify the maximum number of bitflips before
2847 * considering the region is not erased.
2848 * Note: The logic of this function has been extracted from the memweight
2849 * implementation, except that nand_check_erased_buf function exit before
2850 * testing the whole buffer if the number of bitflips exceed the
2851 * bitflips_threshold value.
2852 *
2853 * Returns a positive number of bitflips less than or equal to
2854 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2855 * threshold.
2856 */
nand_check_erased_buf(void * buf,int len,int bitflips_threshold)2857 static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2858 {
2859 const unsigned char *bitmap = buf;
2860 int bitflips = 0;
2861 int weight;
2862
2863 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2864 len--, bitmap++) {
2865 weight = hweight8(*bitmap);
2866 bitflips += BITS_PER_BYTE - weight;
2867 if (unlikely(bitflips > bitflips_threshold))
2868 return -EBADMSG;
2869 }
2870
2871 for (; len >= sizeof(long);
2872 len -= sizeof(long), bitmap += sizeof(long)) {
2873 unsigned long d = *((unsigned long *)bitmap);
2874 if (d == ~0UL)
2875 continue;
2876 weight = hweight_long(d);
2877 bitflips += BITS_PER_LONG - weight;
2878 if (unlikely(bitflips > bitflips_threshold))
2879 return -EBADMSG;
2880 }
2881
2882 for (; len > 0; len--, bitmap++) {
2883 weight = hweight8(*bitmap);
2884 bitflips += BITS_PER_BYTE - weight;
2885 if (unlikely(bitflips > bitflips_threshold))
2886 return -EBADMSG;
2887 }
2888
2889 return bitflips;
2890 }
2891
2892 /**
2893 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2894 * 0xff data
2895 * @data: data buffer to test
2896 * @datalen: data length
2897 * @ecc: ECC buffer
2898 * @ecclen: ECC length
2899 * @extraoob: extra OOB buffer
2900 * @extraooblen: extra OOB length
2901 * @bitflips_threshold: maximum number of bitflips
2902 *
2903 * Check if a data buffer and its associated ECC and OOB data contains only
2904 * 0xff pattern, which means the underlying region has been erased and is
2905 * ready to be programmed.
2906 * The bitflips_threshold specify the maximum number of bitflips before
2907 * considering the region as not erased.
2908 *
2909 * Note:
2910 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2911 * different from the NAND page size. When fixing bitflips, ECC engines will
2912 * report the number of errors per chunk, and the NAND core infrastructure
2913 * expect you to return the maximum number of bitflips for the whole page.
2914 * This is why you should always use this function on a single chunk and
2915 * not on the whole page. After checking each chunk you should update your
2916 * max_bitflips value accordingly.
2917 * 2/ When checking for bitflips in erased pages you should not only check
2918 * the payload data but also their associated ECC data, because a user might
2919 * have programmed almost all bits to 1 but a few. In this case, we
2920 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2921 * this case.
2922 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2923 * data are protected by the ECC engine.
2924 * It could also be used if you support subpages and want to attach some
2925 * extra OOB data to an ECC chunk.
2926 *
2927 * Returns a positive number of bitflips less than or equal to
2928 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2929 * threshold. In case of success, the passed buffers are filled with 0xff.
2930 */
nand_check_erased_ecc_chunk(void * data,int datalen,void * ecc,int ecclen,void * extraoob,int extraooblen,int bitflips_threshold)2931 int nand_check_erased_ecc_chunk(void *data, int datalen,
2932 void *ecc, int ecclen,
2933 void *extraoob, int extraooblen,
2934 int bitflips_threshold)
2935 {
2936 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2937
2938 data_bitflips = nand_check_erased_buf(data, datalen,
2939 bitflips_threshold);
2940 if (data_bitflips < 0)
2941 return data_bitflips;
2942
2943 bitflips_threshold -= data_bitflips;
2944
2945 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2946 if (ecc_bitflips < 0)
2947 return ecc_bitflips;
2948
2949 bitflips_threshold -= ecc_bitflips;
2950
2951 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2952 bitflips_threshold);
2953 if (extraoob_bitflips < 0)
2954 return extraoob_bitflips;
2955
2956 if (data_bitflips)
2957 memset(data, 0xff, datalen);
2958
2959 if (ecc_bitflips)
2960 memset(ecc, 0xff, ecclen);
2961
2962 if (extraoob_bitflips)
2963 memset(extraoob, 0xff, extraooblen);
2964
2965 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2966 }
2967 EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2968
2969 /**
2970 * nand_read_page_raw_notsupp - dummy read raw page function
2971 * @mtd: mtd info structure
2972 * @chip: nand chip info structure
2973 * @buf: buffer to store read data
2974 * @oob_required: caller requires OOB data read to chip->oob_poi
2975 * @page: page number to read
2976 *
2977 * Returns -ENOTSUPP unconditionally.
2978 */
nand_read_page_raw_notsupp(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)2979 int nand_read_page_raw_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
2980 u8 *buf, int oob_required, int page)
2981 {
2982 return -ENOTSUPP;
2983 }
2984 EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2985
2986 /**
2987 * nand_read_page_raw - [INTERN] read raw page data without ecc
2988 * @mtd: mtd info structure
2989 * @chip: nand chip info structure
2990 * @buf: buffer to store read data
2991 * @oob_required: caller requires OOB data read to chip->oob_poi
2992 * @page: page number to read
2993 *
2994 * Not for syndrome calculating ECC controllers, which use a special oob layout.
2995 */
nand_read_page_raw(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)2996 int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2997 uint8_t *buf, int oob_required, int page)
2998 {
2999 int ret;
3000
3001 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
3002 if (ret)
3003 return ret;
3004
3005 if (oob_required) {
3006 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3007 false);
3008 if (ret)
3009 return ret;
3010 }
3011
3012 return 0;
3013 }
3014 EXPORT_SYMBOL(nand_read_page_raw);
3015
3016 /**
3017 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
3018 * @mtd: mtd info structure
3019 * @chip: nand chip info structure
3020 * @buf: buffer to store read data
3021 * @oob_required: caller requires OOB data read to chip->oob_poi
3022 * @page: page number to read
3023 *
3024 * We need a special oob layout and handling even when OOB isn't used.
3025 */
nand_read_page_raw_syndrome(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)3026 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
3027 struct nand_chip *chip, uint8_t *buf,
3028 int oob_required, int page)
3029 {
3030 int eccsize = chip->ecc.size;
3031 int eccbytes = chip->ecc.bytes;
3032 uint8_t *oob = chip->oob_poi;
3033 int steps, size, ret;
3034
3035 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3036 if (ret)
3037 return ret;
3038
3039 for (steps = chip->ecc.steps; steps > 0; steps--) {
3040 ret = nand_read_data_op(chip, buf, eccsize, false);
3041 if (ret)
3042 return ret;
3043
3044 buf += eccsize;
3045
3046 if (chip->ecc.prepad) {
3047 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3048 false);
3049 if (ret)
3050 return ret;
3051
3052 oob += chip->ecc.prepad;
3053 }
3054
3055 ret = nand_read_data_op(chip, oob, eccbytes, false);
3056 if (ret)
3057 return ret;
3058
3059 oob += eccbytes;
3060
3061 if (chip->ecc.postpad) {
3062 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3063 false);
3064 if (ret)
3065 return ret;
3066
3067 oob += chip->ecc.postpad;
3068 }
3069 }
3070
3071 size = mtd->oobsize - (oob - chip->oob_poi);
3072 if (size) {
3073 ret = nand_read_data_op(chip, oob, size, false);
3074 if (ret)
3075 return ret;
3076 }
3077
3078 return 0;
3079 }
3080
3081 /**
3082 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
3083 * @mtd: mtd info structure
3084 * @chip: nand chip info structure
3085 * @buf: buffer to store read data
3086 * @oob_required: caller requires OOB data read to chip->oob_poi
3087 * @page: page number to read
3088 */
nand_read_page_swecc(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)3089 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
3090 uint8_t *buf, int oob_required, int page)
3091 {
3092 int i, eccsize = chip->ecc.size, ret;
3093 int eccbytes = chip->ecc.bytes;
3094 int eccsteps = chip->ecc.steps;
3095 uint8_t *p = buf;
3096 uint8_t *ecc_calc = chip->ecc.calc_buf;
3097 uint8_t *ecc_code = chip->ecc.code_buf;
3098 unsigned int max_bitflips = 0;
3099
3100 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
3101
3102 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3103 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3104
3105 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3106 chip->ecc.total);
3107 if (ret)
3108 return ret;
3109
3110 eccsteps = chip->ecc.steps;
3111 p = buf;
3112
3113 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3114 int stat;
3115
3116 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3117 if (stat < 0) {
3118 mtd->ecc_stats.failed++;
3119 } else {
3120 mtd->ecc_stats.corrected += stat;
3121 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3122 }
3123 }
3124 return max_bitflips;
3125 }
3126
3127 /**
3128 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
3129 * @mtd: mtd info structure
3130 * @chip: nand chip info structure
3131 * @data_offs: offset of requested data within the page
3132 * @readlen: data length
3133 * @bufpoi: buffer to store read data
3134 * @page: page number to read
3135 */
nand_read_subpage(struct mtd_info * mtd,struct nand_chip * chip,uint32_t data_offs,uint32_t readlen,uint8_t * bufpoi,int page)3136 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
3137 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3138 int page)
3139 {
3140 int start_step, end_step, num_steps, ret;
3141 uint8_t *p;
3142 int data_col_addr, i, gaps = 0;
3143 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3144 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
3145 int index, section = 0;
3146 unsigned int max_bitflips = 0;
3147 struct mtd_oob_region oobregion = { };
3148
3149 /* Column address within the page aligned to ECC size (256bytes) */
3150 start_step = data_offs / chip->ecc.size;
3151 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3152 num_steps = end_step - start_step + 1;
3153 index = start_step * chip->ecc.bytes;
3154
3155 /* Data size aligned to ECC ecc.size */
3156 datafrag_len = num_steps * chip->ecc.size;
3157 eccfrag_len = num_steps * chip->ecc.bytes;
3158
3159 data_col_addr = start_step * chip->ecc.size;
3160 /* If we read not a page aligned data */
3161 p = bufpoi + data_col_addr;
3162 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
3163 if (ret)
3164 return ret;
3165
3166 /* Calculate ECC */
3167 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
3168 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
3169
3170 /*
3171 * The performance is faster if we position offsets according to
3172 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
3173 */
3174 ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion);
3175 if (ret)
3176 return ret;
3177
3178 if (oobregion.length < eccfrag_len)
3179 gaps = 1;
3180
3181 if (gaps) {
3182 ret = nand_change_read_column_op(chip, mtd->writesize,
3183 chip->oob_poi, mtd->oobsize,
3184 false);
3185 if (ret)
3186 return ret;
3187 } else {
3188 /*
3189 * Send the command to read the particular ECC bytes take care
3190 * about buswidth alignment in read_buf.
3191 */
3192 aligned_pos = oobregion.offset & ~(busw - 1);
3193 aligned_len = eccfrag_len;
3194 if (oobregion.offset & (busw - 1))
3195 aligned_len++;
3196 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3197 (busw - 1))
3198 aligned_len++;
3199
3200 ret = nand_change_read_column_op(chip,
3201 mtd->writesize + aligned_pos,
3202 &chip->oob_poi[aligned_pos],
3203 aligned_len, false);
3204 if (ret)
3205 return ret;
3206 }
3207
3208 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
3209 chip->oob_poi, index, eccfrag_len);
3210 if (ret)
3211 return ret;
3212
3213 p = bufpoi + data_col_addr;
3214 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3215 int stat;
3216
3217 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3218 &chip->ecc.calc_buf[i]);
3219 if (stat == -EBADMSG &&
3220 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3221 /* check for empty pages with bitflips */
3222 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3223 &chip->ecc.code_buf[i],
3224 chip->ecc.bytes,
3225 NULL, 0,
3226 chip->ecc.strength);
3227 }
3228
3229 if (stat < 0) {
3230 mtd->ecc_stats.failed++;
3231 } else {
3232 mtd->ecc_stats.corrected += stat;
3233 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3234 }
3235 }
3236 return max_bitflips;
3237 }
3238
3239 /**
3240 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
3241 * @mtd: mtd info structure
3242 * @chip: nand chip info structure
3243 * @buf: buffer to store read data
3244 * @oob_required: caller requires OOB data read to chip->oob_poi
3245 * @page: page number to read
3246 *
3247 * Not for syndrome calculating ECC controllers which need a special oob layout.
3248 */
nand_read_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)3249 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
3250 uint8_t *buf, int oob_required, int page)
3251 {
3252 int i, eccsize = chip->ecc.size, ret;
3253 int eccbytes = chip->ecc.bytes;
3254 int eccsteps = chip->ecc.steps;
3255 uint8_t *p = buf;
3256 uint8_t *ecc_calc = chip->ecc.calc_buf;
3257 uint8_t *ecc_code = chip->ecc.code_buf;
3258 unsigned int max_bitflips = 0;
3259
3260 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3261 if (ret)
3262 return ret;
3263
3264 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3265 chip->ecc.hwctl(mtd, NAND_ECC_READ);
3266
3267 ret = nand_read_data_op(chip, p, eccsize, false);
3268 if (ret)
3269 return ret;
3270
3271 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3272 }
3273
3274 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3275 if (ret)
3276 return ret;
3277
3278 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3279 chip->ecc.total);
3280 if (ret)
3281 return ret;
3282
3283 eccsteps = chip->ecc.steps;
3284 p = buf;
3285
3286 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3287 int stat;
3288
3289 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3290 if (stat == -EBADMSG &&
3291 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3292 /* check for empty pages with bitflips */
3293 stat = nand_check_erased_ecc_chunk(p, eccsize,
3294 &ecc_code[i], eccbytes,
3295 NULL, 0,
3296 chip->ecc.strength);
3297 }
3298
3299 if (stat < 0) {
3300 mtd->ecc_stats.failed++;
3301 } else {
3302 mtd->ecc_stats.corrected += stat;
3303 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3304 }
3305 }
3306 return max_bitflips;
3307 }
3308
3309 /**
3310 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
3311 * @mtd: mtd info structure
3312 * @chip: nand chip info structure
3313 * @buf: buffer to store read data
3314 * @oob_required: caller requires OOB data read to chip->oob_poi
3315 * @page: page number to read
3316 *
3317 * Hardware ECC for large page chips, require OOB to be read first. For this
3318 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3319 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3320 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3321 * the data area, by overwriting the NAND manufacturer bad block markings.
3322 */
nand_read_page_hwecc_oob_first(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)3323 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
3324 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
3325 {
3326 int i, eccsize = chip->ecc.size, ret;
3327 int eccbytes = chip->ecc.bytes;
3328 int eccsteps = chip->ecc.steps;
3329 uint8_t *p = buf;
3330 uint8_t *ecc_code = chip->ecc.code_buf;
3331 uint8_t *ecc_calc = chip->ecc.calc_buf;
3332 unsigned int max_bitflips = 0;
3333
3334 /* Read the OOB area first */
3335 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3336 if (ret)
3337 return ret;
3338
3339 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3340 if (ret)
3341 return ret;
3342
3343 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3344 chip->ecc.total);
3345 if (ret)
3346 return ret;
3347
3348 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3349 int stat;
3350
3351 chip->ecc.hwctl(mtd, NAND_ECC_READ);
3352
3353 ret = nand_read_data_op(chip, p, eccsize, false);
3354 if (ret)
3355 return ret;
3356
3357 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3358
3359 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
3360 if (stat == -EBADMSG &&
3361 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3362 /* check for empty pages with bitflips */
3363 stat = nand_check_erased_ecc_chunk(p, eccsize,
3364 &ecc_code[i], eccbytes,
3365 NULL, 0,
3366 chip->ecc.strength);
3367 }
3368
3369 if (stat < 0) {
3370 mtd->ecc_stats.failed++;
3371 } else {
3372 mtd->ecc_stats.corrected += stat;
3373 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3374 }
3375 }
3376 return max_bitflips;
3377 }
3378
3379 /**
3380 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
3381 * @mtd: mtd info structure
3382 * @chip: nand chip info structure
3383 * @buf: buffer to store read data
3384 * @oob_required: caller requires OOB data read to chip->oob_poi
3385 * @page: page number to read
3386 *
3387 * The hw generator calculates the error syndrome automatically. Therefore we
3388 * need a special oob layout and handling.
3389 */
nand_read_page_syndrome(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)3390 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3391 uint8_t *buf, int oob_required, int page)
3392 {
3393 int ret, i, eccsize = chip->ecc.size;
3394 int eccbytes = chip->ecc.bytes;
3395 int eccsteps = chip->ecc.steps;
3396 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
3397 uint8_t *p = buf;
3398 uint8_t *oob = chip->oob_poi;
3399 unsigned int max_bitflips = 0;
3400
3401 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3402 if (ret)
3403 return ret;
3404
3405 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3406 int stat;
3407
3408 chip->ecc.hwctl(mtd, NAND_ECC_READ);
3409
3410 ret = nand_read_data_op(chip, p, eccsize, false);
3411 if (ret)
3412 return ret;
3413
3414 if (chip->ecc.prepad) {
3415 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3416 false);
3417 if (ret)
3418 return ret;
3419
3420 oob += chip->ecc.prepad;
3421 }
3422
3423 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
3424
3425 ret = nand_read_data_op(chip, oob, eccbytes, false);
3426 if (ret)
3427 return ret;
3428
3429 stat = chip->ecc.correct(mtd, p, oob, NULL);
3430
3431 oob += eccbytes;
3432
3433 if (chip->ecc.postpad) {
3434 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3435 false);
3436 if (ret)
3437 return ret;
3438
3439 oob += chip->ecc.postpad;
3440 }
3441
3442 if (stat == -EBADMSG &&
3443 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3444 /* check for empty pages with bitflips */
3445 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3446 oob - eccpadbytes,
3447 eccpadbytes,
3448 NULL, 0,
3449 chip->ecc.strength);
3450 }
3451
3452 if (stat < 0) {
3453 mtd->ecc_stats.failed++;
3454 } else {
3455 mtd->ecc_stats.corrected += stat;
3456 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3457 }
3458 }
3459
3460 /* Calculate remaining oob bytes */
3461 i = mtd->oobsize - (oob - chip->oob_poi);
3462 if (i) {
3463 ret = nand_read_data_op(chip, oob, i, false);
3464 if (ret)
3465 return ret;
3466 }
3467
3468 return max_bitflips;
3469 }
3470
3471 /**
3472 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
3473 * @mtd: mtd info structure
3474 * @oob: oob destination address
3475 * @ops: oob ops structure
3476 * @len: size of oob to transfer
3477 */
nand_transfer_oob(struct mtd_info * mtd,uint8_t * oob,struct mtd_oob_ops * ops,size_t len)3478 static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
3479 struct mtd_oob_ops *ops, size_t len)
3480 {
3481 struct nand_chip *chip = mtd_to_nand(mtd);
3482 int ret;
3483
3484 switch (ops->mode) {
3485
3486 case MTD_OPS_PLACE_OOB:
3487 case MTD_OPS_RAW:
3488 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3489 return oob + len;
3490
3491 case MTD_OPS_AUTO_OOB:
3492 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3493 ops->ooboffs, len);
3494 BUG_ON(ret);
3495 return oob + len;
3496
3497 default:
3498 BUG();
3499 }
3500 return NULL;
3501 }
3502
3503 /**
3504 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3505 * @mtd: MTD device structure
3506 * @retry_mode: the retry mode to use
3507 *
3508 * Some vendors supply a special command to shift the Vt threshold, to be used
3509 * when there are too many bitflips in a page (i.e., ECC error). After setting
3510 * a new threshold, the host should retry reading the page.
3511 */
nand_setup_read_retry(struct mtd_info * mtd,int retry_mode)3512 static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3513 {
3514 struct nand_chip *chip = mtd_to_nand(mtd);
3515
3516 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3517
3518 if (retry_mode >= chip->read_retries)
3519 return -EINVAL;
3520
3521 if (!chip->setup_read_retry)
3522 return -EOPNOTSUPP;
3523
3524 return chip->setup_read_retry(mtd, retry_mode);
3525 }
3526
3527 /**
3528 * nand_do_read_ops - [INTERN] Read data with ECC
3529 * @mtd: MTD device structure
3530 * @from: offset to read from
3531 * @ops: oob ops structure
3532 *
3533 * Internal function. Called with chip held.
3534 */
nand_do_read_ops(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)3535 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3536 struct mtd_oob_ops *ops)
3537 {
3538 int chipnr, page, realpage, col, bytes, aligned, oob_required;
3539 struct nand_chip *chip = mtd_to_nand(mtd);
3540 int ret = 0;
3541 uint32_t readlen = ops->len;
3542 uint32_t oobreadlen = ops->ooblen;
3543 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
3544
3545 uint8_t *bufpoi, *oob, *buf;
3546 int use_bufpoi;
3547 unsigned int max_bitflips = 0;
3548 int retry_mode = 0;
3549 bool ecc_fail = false;
3550
3551 chipnr = (int)(from >> chip->chip_shift);
3552 chip->select_chip(mtd, chipnr);
3553
3554 realpage = (int)(from >> chip->page_shift);
3555 page = realpage & chip->pagemask;
3556
3557 col = (int)(from & (mtd->writesize - 1));
3558
3559 buf = ops->datbuf;
3560 oob = ops->oobbuf;
3561 oob_required = oob ? 1 : 0;
3562
3563 while (1) {
3564 unsigned int ecc_failures = mtd->ecc_stats.failed;
3565
3566 bytes = min(mtd->writesize - col, readlen);
3567 aligned = (bytes == mtd->writesize);
3568
3569 if (!aligned)
3570 use_bufpoi = 1;
3571 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3572 use_bufpoi = !virt_addr_valid(buf) ||
3573 !IS_ALIGNED((unsigned long)buf,
3574 chip->buf_align);
3575 else
3576 use_bufpoi = 0;
3577
3578 /* Is the current page in the buffer? */
3579 if (realpage != chip->pagebuf || oob) {
3580 bufpoi = use_bufpoi ? chip->data_buf : buf;
3581
3582 if (use_bufpoi && aligned)
3583 pr_debug("%s: using read bounce buffer for buf@%p\n",
3584 __func__, buf);
3585
3586 read_retry:
3587 /*
3588 * Now read the page into the buffer. Absent an error,
3589 * the read methods return max bitflips per ecc step.
3590 */
3591 if (unlikely(ops->mode == MTD_OPS_RAW))
3592 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
3593 oob_required,
3594 page);
3595 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3596 !oob)
3597 ret = chip->ecc.read_subpage(mtd, chip,
3598 col, bytes, bufpoi,
3599 page);
3600 else
3601 ret = chip->ecc.read_page(mtd, chip, bufpoi,
3602 oob_required, page);
3603 if (ret < 0) {
3604 if (use_bufpoi)
3605 /* Invalidate page cache */
3606 chip->pagebuf = -1;
3607 break;
3608 }
3609
3610 /* Transfer not aligned data */
3611 if (use_bufpoi) {
3612 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
3613 !(mtd->ecc_stats.failed - ecc_failures) &&
3614 (ops->mode != MTD_OPS_RAW)) {
3615 chip->pagebuf = realpage;
3616 chip->pagebuf_bitflips = ret;
3617 } else {
3618 /* Invalidate page cache */
3619 chip->pagebuf = -1;
3620 }
3621 memcpy(buf, chip->data_buf + col, bytes);
3622 }
3623
3624 if (unlikely(oob)) {
3625 int toread = min(oobreadlen, max_oobsize);
3626
3627 if (toread) {
3628 oob = nand_transfer_oob(mtd,
3629 oob, ops, toread);
3630 oobreadlen -= toread;
3631 }
3632 }
3633
3634 if (chip->options & NAND_NEED_READRDY) {
3635 /* Apply delay or wait for ready/busy pin */
3636 if (!chip->dev_ready)
3637 udelay(chip->chip_delay);
3638 else
3639 nand_wait_ready(mtd);
3640 }
3641
3642 if (mtd->ecc_stats.failed - ecc_failures) {
3643 if (retry_mode + 1 < chip->read_retries) {
3644 retry_mode++;
3645 ret = nand_setup_read_retry(mtd,
3646 retry_mode);
3647 if (ret < 0)
3648 break;
3649
3650 /* Reset failures; retry */
3651 mtd->ecc_stats.failed = ecc_failures;
3652 goto read_retry;
3653 } else {
3654 /* No more retry modes; real failure */
3655 ecc_fail = true;
3656 }
3657 }
3658
3659 buf += bytes;
3660 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3661 } else {
3662 memcpy(buf, chip->data_buf + col, bytes);
3663 buf += bytes;
3664 max_bitflips = max_t(unsigned int, max_bitflips,
3665 chip->pagebuf_bitflips);
3666 }
3667
3668 readlen -= bytes;
3669
3670 /* Reset to retry mode 0 */
3671 if (retry_mode) {
3672 ret = nand_setup_read_retry(mtd, 0);
3673 if (ret < 0)
3674 break;
3675 retry_mode = 0;
3676 }
3677
3678 if (!readlen)
3679 break;
3680
3681 /* For subsequent reads align to page boundary */
3682 col = 0;
3683 /* Increment page address */
3684 realpage++;
3685
3686 page = realpage & chip->pagemask;
3687 /* Check, if we cross a chip boundary */
3688 if (!page) {
3689 chipnr++;
3690 chip->select_chip(mtd, -1);
3691 chip->select_chip(mtd, chipnr);
3692 }
3693 }
3694 chip->select_chip(mtd, -1);
3695
3696 ops->retlen = ops->len - (size_t) readlen;
3697 if (oob)
3698 ops->oobretlen = ops->ooblen - oobreadlen;
3699
3700 if (ret < 0)
3701 return ret;
3702
3703 if (ecc_fail)
3704 return -EBADMSG;
3705
3706 return max_bitflips;
3707 }
3708
3709 /**
3710 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
3711 * @mtd: mtd info structure
3712 * @chip: nand chip info structure
3713 * @page: page number to read
3714 */
nand_read_oob_std(struct mtd_info * mtd,struct nand_chip * chip,int page)3715 int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
3716 {
3717 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3718 }
3719 EXPORT_SYMBOL(nand_read_oob_std);
3720
3721 /**
3722 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
3723 * with syndromes
3724 * @mtd: mtd info structure
3725 * @chip: nand chip info structure
3726 * @page: page number to read
3727 */
nand_read_oob_syndrome(struct mtd_info * mtd,struct nand_chip * chip,int page)3728 int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3729 int page)
3730 {
3731 int length = mtd->oobsize;
3732 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3733 int eccsize = chip->ecc.size;
3734 uint8_t *bufpoi = chip->oob_poi;
3735 int i, toread, sndrnd = 0, pos, ret;
3736
3737 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3738 if (ret)
3739 return ret;
3740
3741 for (i = 0; i < chip->ecc.steps; i++) {
3742 if (sndrnd) {
3743 int ret;
3744
3745 pos = eccsize + i * (eccsize + chunk);
3746 if (mtd->writesize > 512)
3747 ret = nand_change_read_column_op(chip, pos,
3748 NULL, 0,
3749 false);
3750 else
3751 ret = nand_read_page_op(chip, page, pos, NULL,
3752 0);
3753
3754 if (ret)
3755 return ret;
3756 } else
3757 sndrnd = 1;
3758 toread = min_t(int, length, chunk);
3759
3760 ret = nand_read_data_op(chip, bufpoi, toread, false);
3761 if (ret)
3762 return ret;
3763
3764 bufpoi += toread;
3765 length -= toread;
3766 }
3767 if (length > 0) {
3768 ret = nand_read_data_op(chip, bufpoi, length, false);
3769 if (ret)
3770 return ret;
3771 }
3772
3773 return 0;
3774 }
3775 EXPORT_SYMBOL(nand_read_oob_syndrome);
3776
3777 /**
3778 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
3779 * @mtd: mtd info structure
3780 * @chip: nand chip info structure
3781 * @page: page number to write
3782 */
nand_write_oob_std(struct mtd_info * mtd,struct nand_chip * chip,int page)3783 int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
3784 {
3785 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3786 mtd->oobsize);
3787 }
3788 EXPORT_SYMBOL(nand_write_oob_std);
3789
3790 /**
3791 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
3792 * with syndrome - only for large page flash
3793 * @mtd: mtd info structure
3794 * @chip: nand chip info structure
3795 * @page: page number to write
3796 */
nand_write_oob_syndrome(struct mtd_info * mtd,struct nand_chip * chip,int page)3797 int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3798 int page)
3799 {
3800 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3801 int eccsize = chip->ecc.size, length = mtd->oobsize;
3802 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
3803 const uint8_t *bufpoi = chip->oob_poi;
3804
3805 /*
3806 * data-ecc-data-ecc ... ecc-oob
3807 * or
3808 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3809 */
3810 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3811 pos = steps * (eccsize + chunk);
3812 steps = 0;
3813 } else
3814 pos = eccsize;
3815
3816 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3817 if (ret)
3818 return ret;
3819
3820 for (i = 0; i < steps; i++) {
3821 if (sndcmd) {
3822 if (mtd->writesize <= 512) {
3823 uint32_t fill = 0xFFFFFFFF;
3824
3825 len = eccsize;
3826 while (len > 0) {
3827 int num = min_t(int, len, 4);
3828
3829 ret = nand_write_data_op(chip, &fill,
3830 num, false);
3831 if (ret)
3832 return ret;
3833
3834 len -= num;
3835 }
3836 } else {
3837 pos = eccsize + i * (eccsize + chunk);
3838 ret = nand_change_write_column_op(chip, pos,
3839 NULL, 0,
3840 false);
3841 if (ret)
3842 return ret;
3843 }
3844 } else
3845 sndcmd = 1;
3846 len = min_t(int, length, chunk);
3847
3848 ret = nand_write_data_op(chip, bufpoi, len, false);
3849 if (ret)
3850 return ret;
3851
3852 bufpoi += len;
3853 length -= len;
3854 }
3855 if (length > 0) {
3856 ret = nand_write_data_op(chip, bufpoi, length, false);
3857 if (ret)
3858 return ret;
3859 }
3860
3861 return nand_prog_page_end_op(chip);
3862 }
3863 EXPORT_SYMBOL(nand_write_oob_syndrome);
3864
3865 /**
3866 * nand_do_read_oob - [INTERN] NAND read out-of-band
3867 * @mtd: MTD device structure
3868 * @from: offset to read from
3869 * @ops: oob operations description structure
3870 *
3871 * NAND read out-of-band data from the spare area.
3872 */
nand_do_read_oob(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)3873 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3874 struct mtd_oob_ops *ops)
3875 {
3876 unsigned int max_bitflips = 0;
3877 int page, realpage, chipnr;
3878 struct nand_chip *chip = mtd_to_nand(mtd);
3879 struct mtd_ecc_stats stats;
3880 int readlen = ops->ooblen;
3881 int len;
3882 uint8_t *buf = ops->oobbuf;
3883 int ret = 0;
3884
3885 pr_debug("%s: from = 0x%08Lx, len = %i\n",
3886 __func__, (unsigned long long)from, readlen);
3887
3888 stats = mtd->ecc_stats;
3889
3890 len = mtd_oobavail(mtd, ops);
3891
3892 chipnr = (int)(from >> chip->chip_shift);
3893 chip->select_chip(mtd, chipnr);
3894
3895 /* Shift to get page */
3896 realpage = (int)(from >> chip->page_shift);
3897 page = realpage & chip->pagemask;
3898
3899 while (1) {
3900 if (ops->mode == MTD_OPS_RAW)
3901 ret = chip->ecc.read_oob_raw(mtd, chip, page);
3902 else
3903 ret = chip->ecc.read_oob(mtd, chip, page);
3904
3905 if (ret < 0)
3906 break;
3907
3908 len = min(len, readlen);
3909 buf = nand_transfer_oob(mtd, buf, ops, len);
3910
3911 if (chip->options & NAND_NEED_READRDY) {
3912 /* Apply delay or wait for ready/busy pin */
3913 if (!chip->dev_ready)
3914 udelay(chip->chip_delay);
3915 else
3916 nand_wait_ready(mtd);
3917 }
3918
3919 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3920
3921 readlen -= len;
3922 if (!readlen)
3923 break;
3924
3925 /* Increment page address */
3926 realpage++;
3927
3928 page = realpage & chip->pagemask;
3929 /* Check, if we cross a chip boundary */
3930 if (!page) {
3931 chipnr++;
3932 chip->select_chip(mtd, -1);
3933 chip->select_chip(mtd, chipnr);
3934 }
3935 }
3936 chip->select_chip(mtd, -1);
3937
3938 ops->oobretlen = ops->ooblen - readlen;
3939
3940 if (ret < 0)
3941 return ret;
3942
3943 if (mtd->ecc_stats.failed - stats.failed)
3944 return -EBADMSG;
3945
3946 return max_bitflips;
3947 }
3948
3949 /**
3950 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
3951 * @mtd: MTD device structure
3952 * @from: offset to read from
3953 * @ops: oob operation description structure
3954 *
3955 * NAND read data and/or out-of-band data.
3956 */
nand_read_oob(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)3957 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3958 struct mtd_oob_ops *ops)
3959 {
3960 int ret;
3961
3962 ops->retlen = 0;
3963
3964 if (ops->mode != MTD_OPS_PLACE_OOB &&
3965 ops->mode != MTD_OPS_AUTO_OOB &&
3966 ops->mode != MTD_OPS_RAW)
3967 return -ENOTSUPP;
3968
3969 nand_get_device(mtd, FL_READING);
3970
3971 if (!ops->datbuf)
3972 ret = nand_do_read_oob(mtd, from, ops);
3973 else
3974 ret = nand_do_read_ops(mtd, from, ops);
3975
3976 nand_release_device(mtd);
3977 return ret;
3978 }
3979
3980 /**
3981 * nand_write_page_raw_notsupp - dummy raw page write function
3982 * @mtd: mtd info structure
3983 * @chip: nand chip info structure
3984 * @buf: data buffer
3985 * @oob_required: must write chip->oob_poi to OOB
3986 * @page: page number to write
3987 *
3988 * Returns -ENOTSUPP unconditionally.
3989 */
nand_write_page_raw_notsupp(struct mtd_info * mtd,struct nand_chip * chip,const u8 * buf,int oob_required,int page)3990 int nand_write_page_raw_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
3991 const u8 *buf, int oob_required, int page)
3992 {
3993 return -ENOTSUPP;
3994 }
3995 EXPORT_SYMBOL(nand_write_page_raw_notsupp);
3996
3997 /**
3998 * nand_write_page_raw - [INTERN] raw page write function
3999 * @mtd: mtd info structure
4000 * @chip: nand chip info structure
4001 * @buf: data buffer
4002 * @oob_required: must write chip->oob_poi to OOB
4003 * @page: page number to write
4004 *
4005 * Not for syndrome calculating ECC controllers, which use a special oob layout.
4006 */
nand_write_page_raw(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)4007 int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
4008 const uint8_t *buf, int oob_required, int page)
4009 {
4010 int ret;
4011
4012 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
4013 if (ret)
4014 return ret;
4015
4016 if (oob_required) {
4017 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
4018 false);
4019 if (ret)
4020 return ret;
4021 }
4022
4023 return nand_prog_page_end_op(chip);
4024 }
4025 EXPORT_SYMBOL(nand_write_page_raw);
4026
4027 /**
4028 * nand_write_page_raw_syndrome - [INTERN] raw page write function
4029 * @mtd: mtd info structure
4030 * @chip: nand chip info structure
4031 * @buf: data buffer
4032 * @oob_required: must write chip->oob_poi to OOB
4033 * @page: page number to write
4034 *
4035 * We need a special oob layout and handling even when ECC isn't checked.
4036 */
nand_write_page_raw_syndrome(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)4037 static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
4038 struct nand_chip *chip,
4039 const uint8_t *buf, int oob_required,
4040 int page)
4041 {
4042 int eccsize = chip->ecc.size;
4043 int eccbytes = chip->ecc.bytes;
4044 uint8_t *oob = chip->oob_poi;
4045 int steps, size, ret;
4046
4047 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4048 if (ret)
4049 return ret;
4050
4051 for (steps = chip->ecc.steps; steps > 0; steps--) {
4052 ret = nand_write_data_op(chip, buf, eccsize, false);
4053 if (ret)
4054 return ret;
4055
4056 buf += eccsize;
4057
4058 if (chip->ecc.prepad) {
4059 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4060 false);
4061 if (ret)
4062 return ret;
4063
4064 oob += chip->ecc.prepad;
4065 }
4066
4067 ret = nand_write_data_op(chip, oob, eccbytes, false);
4068 if (ret)
4069 return ret;
4070
4071 oob += eccbytes;
4072
4073 if (chip->ecc.postpad) {
4074 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4075 false);
4076 if (ret)
4077 return ret;
4078
4079 oob += chip->ecc.postpad;
4080 }
4081 }
4082
4083 size = mtd->oobsize - (oob - chip->oob_poi);
4084 if (size) {
4085 ret = nand_write_data_op(chip, oob, size, false);
4086 if (ret)
4087 return ret;
4088 }
4089
4090 return nand_prog_page_end_op(chip);
4091 }
4092 /**
4093 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
4094 * @mtd: mtd info structure
4095 * @chip: nand chip info structure
4096 * @buf: data buffer
4097 * @oob_required: must write chip->oob_poi to OOB
4098 * @page: page number to write
4099 */
nand_write_page_swecc(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)4100 static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
4101 const uint8_t *buf, int oob_required,
4102 int page)
4103 {
4104 int i, eccsize = chip->ecc.size, ret;
4105 int eccbytes = chip->ecc.bytes;
4106 int eccsteps = chip->ecc.steps;
4107 uint8_t *ecc_calc = chip->ecc.calc_buf;
4108 const uint8_t *p = buf;
4109
4110 /* Software ECC calculation */
4111 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4112 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4113
4114 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4115 chip->ecc.total);
4116 if (ret)
4117 return ret;
4118
4119 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
4120 }
4121
4122 /**
4123 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
4124 * @mtd: mtd info structure
4125 * @chip: nand chip info structure
4126 * @buf: data buffer
4127 * @oob_required: must write chip->oob_poi to OOB
4128 * @page: page number to write
4129 */
nand_write_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)4130 static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
4131 const uint8_t *buf, int oob_required,
4132 int page)
4133 {
4134 int i, eccsize = chip->ecc.size, ret;
4135 int eccbytes = chip->ecc.bytes;
4136 int eccsteps = chip->ecc.steps;
4137 uint8_t *ecc_calc = chip->ecc.calc_buf;
4138 const uint8_t *p = buf;
4139
4140 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4141 if (ret)
4142 return ret;
4143
4144 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4145 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4146
4147 ret = nand_write_data_op(chip, p, eccsize, false);
4148 if (ret)
4149 return ret;
4150
4151 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4152 }
4153
4154 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4155 chip->ecc.total);
4156 if (ret)
4157 return ret;
4158
4159 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4160 if (ret)
4161 return ret;
4162
4163 return nand_prog_page_end_op(chip);
4164 }
4165
4166
4167 /**
4168 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
4169 * @mtd: mtd info structure
4170 * @chip: nand chip info structure
4171 * @offset: column address of subpage within the page
4172 * @data_len: data length
4173 * @buf: data buffer
4174 * @oob_required: must write chip->oob_poi to OOB
4175 * @page: page number to write
4176 */
nand_write_subpage_hwecc(struct mtd_info * mtd,struct nand_chip * chip,uint32_t offset,uint32_t data_len,const uint8_t * buf,int oob_required,int page)4177 static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4178 struct nand_chip *chip, uint32_t offset,
4179 uint32_t data_len, const uint8_t *buf,
4180 int oob_required, int page)
4181 {
4182 uint8_t *oob_buf = chip->oob_poi;
4183 uint8_t *ecc_calc = chip->ecc.calc_buf;
4184 int ecc_size = chip->ecc.size;
4185 int ecc_bytes = chip->ecc.bytes;
4186 int ecc_steps = chip->ecc.steps;
4187 uint32_t start_step = offset / ecc_size;
4188 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4189 int oob_bytes = mtd->oobsize / ecc_steps;
4190 int step, ret;
4191
4192 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4193 if (ret)
4194 return ret;
4195
4196 for (step = 0; step < ecc_steps; step++) {
4197 /* configure controller for WRITE access */
4198 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4199
4200 /* write data (untouched subpages already masked by 0xFF) */
4201 ret = nand_write_data_op(chip, buf, ecc_size, false);
4202 if (ret)
4203 return ret;
4204
4205 /* mask ECC of un-touched subpages by padding 0xFF */
4206 if ((step < start_step) || (step > end_step))
4207 memset(ecc_calc, 0xff, ecc_bytes);
4208 else
4209 chip->ecc.calculate(mtd, buf, ecc_calc);
4210
4211 /* mask OOB of un-touched subpages by padding 0xFF */
4212 /* if oob_required, preserve OOB metadata of written subpage */
4213 if (!oob_required || (step < start_step) || (step > end_step))
4214 memset(oob_buf, 0xff, oob_bytes);
4215
4216 buf += ecc_size;
4217 ecc_calc += ecc_bytes;
4218 oob_buf += oob_bytes;
4219 }
4220
4221 /* copy calculated ECC for whole page to chip->buffer->oob */
4222 /* this include masked-value(0xFF) for unwritten subpages */
4223 ecc_calc = chip->ecc.calc_buf;
4224 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4225 chip->ecc.total);
4226 if (ret)
4227 return ret;
4228
4229 /* write OOB buffer to NAND device */
4230 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4231 if (ret)
4232 return ret;
4233
4234 return nand_prog_page_end_op(chip);
4235 }
4236
4237
4238 /**
4239 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
4240 * @mtd: mtd info structure
4241 * @chip: nand chip info structure
4242 * @buf: data buffer
4243 * @oob_required: must write chip->oob_poi to OOB
4244 * @page: page number to write
4245 *
4246 * The hw generator calculates the error syndrome automatically. Therefore we
4247 * need a special oob layout and handling.
4248 */
nand_write_page_syndrome(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)4249 static int nand_write_page_syndrome(struct mtd_info *mtd,
4250 struct nand_chip *chip,
4251 const uint8_t *buf, int oob_required,
4252 int page)
4253 {
4254 int i, eccsize = chip->ecc.size;
4255 int eccbytes = chip->ecc.bytes;
4256 int eccsteps = chip->ecc.steps;
4257 const uint8_t *p = buf;
4258 uint8_t *oob = chip->oob_poi;
4259 int ret;
4260
4261 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4262 if (ret)
4263 return ret;
4264
4265 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4266 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4267
4268 ret = nand_write_data_op(chip, p, eccsize, false);
4269 if (ret)
4270 return ret;
4271
4272 if (chip->ecc.prepad) {
4273 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4274 false);
4275 if (ret)
4276 return ret;
4277
4278 oob += chip->ecc.prepad;
4279 }
4280
4281 chip->ecc.calculate(mtd, p, oob);
4282
4283 ret = nand_write_data_op(chip, oob, eccbytes, false);
4284 if (ret)
4285 return ret;
4286
4287 oob += eccbytes;
4288
4289 if (chip->ecc.postpad) {
4290 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4291 false);
4292 if (ret)
4293 return ret;
4294
4295 oob += chip->ecc.postpad;
4296 }
4297 }
4298
4299 /* Calculate remaining oob bytes */
4300 i = mtd->oobsize - (oob - chip->oob_poi);
4301 if (i) {
4302 ret = nand_write_data_op(chip, oob, i, false);
4303 if (ret)
4304 return ret;
4305 }
4306
4307 return nand_prog_page_end_op(chip);
4308 }
4309
4310 /**
4311 * nand_write_page - write one page
4312 * @mtd: MTD device structure
4313 * @chip: NAND chip descriptor
4314 * @offset: address offset within the page
4315 * @data_len: length of actual data to be written
4316 * @buf: the data to write
4317 * @oob_required: must write chip->oob_poi to OOB
4318 * @page: page number to write
4319 * @raw: use _raw version of write_page
4320 */
nand_write_page(struct mtd_info * mtd,struct nand_chip * chip,uint32_t offset,int data_len,const uint8_t * buf,int oob_required,int page,int raw)4321 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
4322 uint32_t offset, int data_len, const uint8_t *buf,
4323 int oob_required, int page, int raw)
4324 {
4325 int status, subpage;
4326
4327 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4328 chip->ecc.write_subpage)
4329 subpage = offset || (data_len < mtd->writesize);
4330 else
4331 subpage = 0;
4332
4333 if (unlikely(raw))
4334 status = chip->ecc.write_page_raw(mtd, chip, buf,
4335 oob_required, page);
4336 else if (subpage)
4337 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
4338 buf, oob_required, page);
4339 else
4340 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4341 page);
4342
4343 if (status < 0)
4344 return status;
4345
4346 return 0;
4347 }
4348
4349 /**
4350 * nand_fill_oob - [INTERN] Transfer client buffer to oob
4351 * @mtd: MTD device structure
4352 * @oob: oob data buffer
4353 * @len: oob data write length
4354 * @ops: oob ops structure
4355 */
nand_fill_oob(struct mtd_info * mtd,uint8_t * oob,size_t len,struct mtd_oob_ops * ops)4356 static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4357 struct mtd_oob_ops *ops)
4358 {
4359 struct nand_chip *chip = mtd_to_nand(mtd);
4360 int ret;
4361
4362 /*
4363 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4364 * data from a previous OOB read.
4365 */
4366 memset(chip->oob_poi, 0xff, mtd->oobsize);
4367
4368 switch (ops->mode) {
4369
4370 case MTD_OPS_PLACE_OOB:
4371 case MTD_OPS_RAW:
4372 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4373 return oob + len;
4374
4375 case MTD_OPS_AUTO_OOB:
4376 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4377 ops->ooboffs, len);
4378 BUG_ON(ret);
4379 return oob + len;
4380
4381 default:
4382 BUG();
4383 }
4384 return NULL;
4385 }
4386
4387 #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
4388
4389 /**
4390 * nand_do_write_ops - [INTERN] NAND write with ECC
4391 * @mtd: MTD device structure
4392 * @to: offset to write to
4393 * @ops: oob operations description structure
4394 *
4395 * NAND write with ECC.
4396 */
nand_do_write_ops(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)4397 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4398 struct mtd_oob_ops *ops)
4399 {
4400 int chipnr, realpage, page, column;
4401 struct nand_chip *chip = mtd_to_nand(mtd);
4402 uint32_t writelen = ops->len;
4403
4404 uint32_t oobwritelen = ops->ooblen;
4405 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
4406
4407 uint8_t *oob = ops->oobbuf;
4408 uint8_t *buf = ops->datbuf;
4409 int ret;
4410 int oob_required = oob ? 1 : 0;
4411
4412 ops->retlen = 0;
4413 if (!writelen)
4414 return 0;
4415
4416 /* Reject writes, which are not page aligned */
4417 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
4418 pr_notice("%s: attempt to write non page aligned data\n",
4419 __func__);
4420 return -EINVAL;
4421 }
4422
4423 column = to & (mtd->writesize - 1);
4424
4425 chipnr = (int)(to >> chip->chip_shift);
4426 chip->select_chip(mtd, chipnr);
4427
4428 /* Check, if it is write protected */
4429 if (nand_check_wp(mtd)) {
4430 ret = -EIO;
4431 goto err_out;
4432 }
4433
4434 realpage = (int)(to >> chip->page_shift);
4435 page = realpage & chip->pagemask;
4436
4437 /* Invalidate the page cache, when we write to the cached page */
4438 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4439 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
4440 chip->pagebuf = -1;
4441
4442 /* Don't allow multipage oob writes with offset */
4443 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4444 ret = -EINVAL;
4445 goto err_out;
4446 }
4447
4448 while (1) {
4449 int bytes = mtd->writesize;
4450 uint8_t *wbuf = buf;
4451 int use_bufpoi;
4452 int part_pagewr = (column || writelen < mtd->writesize);
4453
4454 if (part_pagewr)
4455 use_bufpoi = 1;
4456 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
4457 use_bufpoi = !virt_addr_valid(buf) ||
4458 !IS_ALIGNED((unsigned long)buf,
4459 chip->buf_align);
4460 else
4461 use_bufpoi = 0;
4462
4463 /* Partial page write?, or need to use bounce buffer */
4464 if (use_bufpoi) {
4465 pr_debug("%s: using write bounce buffer for buf@%p\n",
4466 __func__, buf);
4467 if (part_pagewr)
4468 bytes = min_t(int, bytes - column, writelen);
4469 chip->pagebuf = -1;
4470 memset(chip->data_buf, 0xff, mtd->writesize);
4471 memcpy(&chip->data_buf[column], buf, bytes);
4472 wbuf = chip->data_buf;
4473 }
4474
4475 if (unlikely(oob)) {
4476 size_t len = min(oobwritelen, oobmaxlen);
4477 oob = nand_fill_oob(mtd, oob, len, ops);
4478 oobwritelen -= len;
4479 } else {
4480 /* We still need to erase leftover OOB data */
4481 memset(chip->oob_poi, 0xff, mtd->oobsize);
4482 }
4483
4484 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
4485 oob_required, page,
4486 (ops->mode == MTD_OPS_RAW));
4487 if (ret)
4488 break;
4489
4490 writelen -= bytes;
4491 if (!writelen)
4492 break;
4493
4494 column = 0;
4495 buf += bytes;
4496 realpage++;
4497
4498 page = realpage & chip->pagemask;
4499 /* Check, if we cross a chip boundary */
4500 if (!page) {
4501 chipnr++;
4502 chip->select_chip(mtd, -1);
4503 chip->select_chip(mtd, chipnr);
4504 }
4505 }
4506
4507 ops->retlen = ops->len - writelen;
4508 if (unlikely(oob))
4509 ops->oobretlen = ops->ooblen;
4510
4511 err_out:
4512 chip->select_chip(mtd, -1);
4513 return ret;
4514 }
4515
4516 /**
4517 * panic_nand_write - [MTD Interface] NAND write with ECC
4518 * @mtd: MTD device structure
4519 * @to: offset to write to
4520 * @len: number of bytes to write
4521 * @retlen: pointer to variable to store the number of written bytes
4522 * @buf: the data to write
4523 *
4524 * NAND write with ECC. Used when performing writes in interrupt context, this
4525 * may for example be called by mtdoops when writing an oops while in panic.
4526 */
panic_nand_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const uint8_t * buf)4527 static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4528 size_t *retlen, const uint8_t *buf)
4529 {
4530 struct nand_chip *chip = mtd_to_nand(mtd);
4531 int chipnr = (int)(to >> chip->chip_shift);
4532 struct mtd_oob_ops ops;
4533 int ret;
4534
4535 /* Grab the device */
4536 panic_nand_get_device(chip, mtd, FL_WRITING);
4537
4538 chip->select_chip(mtd, chipnr);
4539
4540 /* Wait for the device to get ready */
4541 panic_nand_wait(mtd, chip, 400);
4542
4543 memset(&ops, 0, sizeof(ops));
4544 ops.len = len;
4545 ops.datbuf = (uint8_t *)buf;
4546 ops.mode = MTD_OPS_PLACE_OOB;
4547
4548 ret = nand_do_write_ops(mtd, to, &ops);
4549
4550 *retlen = ops.retlen;
4551 return ret;
4552 }
4553
4554 /**
4555 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
4556 * @mtd: MTD device structure
4557 * @to: offset to write to
4558 * @ops: oob operation description structure
4559 *
4560 * NAND write out-of-band.
4561 */
nand_do_write_oob(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)4562 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4563 struct mtd_oob_ops *ops)
4564 {
4565 int chipnr, page, status, len;
4566 struct nand_chip *chip = mtd_to_nand(mtd);
4567
4568 pr_debug("%s: to = 0x%08x, len = %i\n",
4569 __func__, (unsigned int)to, (int)ops->ooblen);
4570
4571 len = mtd_oobavail(mtd, ops);
4572
4573 /* Do not allow write past end of page */
4574 if ((ops->ooboffs + ops->ooblen) > len) {
4575 pr_debug("%s: attempt to write past end of page\n",
4576 __func__);
4577 return -EINVAL;
4578 }
4579
4580 chipnr = (int)(to >> chip->chip_shift);
4581
4582 /*
4583 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4584 * of my DiskOnChip 2000 test units) will clear the whole data page too
4585 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4586 * it in the doc2000 driver in August 1999. dwmw2.
4587 */
4588 nand_reset(chip, chipnr);
4589
4590 chip->select_chip(mtd, chipnr);
4591
4592 /* Shift to get page */
4593 page = (int)(to >> chip->page_shift);
4594
4595 /* Check, if it is write protected */
4596 if (nand_check_wp(mtd)) {
4597 chip->select_chip(mtd, -1);
4598 return -EROFS;
4599 }
4600
4601 /* Invalidate the page cache, if we write to the cached page */
4602 if (page == chip->pagebuf)
4603 chip->pagebuf = -1;
4604
4605 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
4606
4607 if (ops->mode == MTD_OPS_RAW)
4608 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4609 else
4610 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
4611
4612 chip->select_chip(mtd, -1);
4613
4614 if (status)
4615 return status;
4616
4617 ops->oobretlen = ops->ooblen;
4618
4619 return 0;
4620 }
4621
4622 /**
4623 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
4624 * @mtd: MTD device structure
4625 * @to: offset to write to
4626 * @ops: oob operation description structure
4627 */
nand_write_oob(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)4628 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4629 struct mtd_oob_ops *ops)
4630 {
4631 int ret = -ENOTSUPP;
4632
4633 ops->retlen = 0;
4634
4635 nand_get_device(mtd, FL_WRITING);
4636
4637 switch (ops->mode) {
4638 case MTD_OPS_PLACE_OOB:
4639 case MTD_OPS_AUTO_OOB:
4640 case MTD_OPS_RAW:
4641 break;
4642
4643 default:
4644 goto out;
4645 }
4646
4647 if (!ops->datbuf)
4648 ret = nand_do_write_oob(mtd, to, ops);
4649 else
4650 ret = nand_do_write_ops(mtd, to, ops);
4651
4652 out:
4653 nand_release_device(mtd);
4654 return ret;
4655 }
4656
4657 /**
4658 * single_erase - [GENERIC] NAND standard block erase command function
4659 * @mtd: MTD device structure
4660 * @page: the page address of the block which will be erased
4661 *
4662 * Standard erase command for NAND chips. Returns NAND status.
4663 */
single_erase(struct mtd_info * mtd,int page)4664 static int single_erase(struct mtd_info *mtd, int page)
4665 {
4666 struct nand_chip *chip = mtd_to_nand(mtd);
4667 unsigned int eraseblock;
4668
4669 /* Send commands to erase a block */
4670 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
4671
4672 return nand_erase_op(chip, eraseblock);
4673 }
4674
4675 /**
4676 * nand_erase - [MTD Interface] erase block(s)
4677 * @mtd: MTD device structure
4678 * @instr: erase instruction
4679 *
4680 * Erase one ore more blocks.
4681 */
nand_erase(struct mtd_info * mtd,struct erase_info * instr)4682 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
4683 {
4684 return nand_erase_nand(mtd, instr, 0);
4685 }
4686
4687 /**
4688 * nand_erase_nand - [INTERN] erase block(s)
4689 * @mtd: MTD device structure
4690 * @instr: erase instruction
4691 * @allowbbt: allow erasing the bbt area
4692 *
4693 * Erase one ore more blocks.
4694 */
nand_erase_nand(struct mtd_info * mtd,struct erase_info * instr,int allowbbt)4695 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4696 int allowbbt)
4697 {
4698 int page, status, pages_per_block, ret, chipnr;
4699 struct nand_chip *chip = mtd_to_nand(mtd);
4700 loff_t len;
4701
4702 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4703 __func__, (unsigned long long)instr->addr,
4704 (unsigned long long)instr->len);
4705
4706 if (check_offs_len(mtd, instr->addr, instr->len))
4707 return -EINVAL;
4708
4709 /* Grab the lock and see if the device is available */
4710 nand_get_device(mtd, FL_ERASING);
4711
4712 /* Shift to get first page */
4713 page = (int)(instr->addr >> chip->page_shift);
4714 chipnr = (int)(instr->addr >> chip->chip_shift);
4715
4716 /* Calculate pages in each block */
4717 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
4718
4719 /* Select the NAND device */
4720 chip->select_chip(mtd, chipnr);
4721
4722 /* Check, if it is write protected */
4723 if (nand_check_wp(mtd)) {
4724 pr_debug("%s: device is write protected!\n",
4725 __func__);
4726 ret = -EIO;
4727 goto erase_exit;
4728 }
4729
4730 /* Loop through the pages */
4731 len = instr->len;
4732
4733 while (len) {
4734 /* Check if we have a bad block, we do not erase bad blocks! */
4735 if (nand_block_checkbad(mtd, ((loff_t) page) <<
4736 chip->page_shift, allowbbt)) {
4737 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4738 __func__, page);
4739 ret = -EIO;
4740 goto erase_exit;
4741 }
4742
4743 /*
4744 * Invalidate the page cache, if we erase the block which
4745 * contains the current cached page.
4746 */
4747 if (page <= chip->pagebuf && chip->pagebuf <
4748 (page + pages_per_block))
4749 chip->pagebuf = -1;
4750
4751 status = chip->erase(mtd, page & chip->pagemask);
4752
4753 /* See if block erase succeeded */
4754 if (status) {
4755 pr_debug("%s: failed erase, page 0x%08x\n",
4756 __func__, page);
4757 ret = -EIO;
4758 instr->fail_addr =
4759 ((loff_t)page << chip->page_shift);
4760 goto erase_exit;
4761 }
4762
4763 /* Increment page address and decrement length */
4764 len -= (1ULL << chip->phys_erase_shift);
4765 page += pages_per_block;
4766
4767 /* Check, if we cross a chip boundary */
4768 if (len && !(page & chip->pagemask)) {
4769 chipnr++;
4770 chip->select_chip(mtd, -1);
4771 chip->select_chip(mtd, chipnr);
4772 }
4773 }
4774
4775 ret = 0;
4776 erase_exit:
4777
4778 /* Deselect and wake up anyone waiting on the device */
4779 chip->select_chip(mtd, -1);
4780 nand_release_device(mtd);
4781
4782 /* Return more or less happy */
4783 return ret;
4784 }
4785
4786 /**
4787 * nand_sync - [MTD Interface] sync
4788 * @mtd: MTD device structure
4789 *
4790 * Sync is actually a wait for chip ready function.
4791 */
nand_sync(struct mtd_info * mtd)4792 static void nand_sync(struct mtd_info *mtd)
4793 {
4794 pr_debug("%s: called\n", __func__);
4795
4796 /* Grab the lock and see if the device is available */
4797 nand_get_device(mtd, FL_SYNCING);
4798 /* Release it and go back */
4799 nand_release_device(mtd);
4800 }
4801
4802 /**
4803 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
4804 * @mtd: MTD device structure
4805 * @offs: offset relative to mtd start
4806 */
nand_block_isbad(struct mtd_info * mtd,loff_t offs)4807 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
4808 {
4809 struct nand_chip *chip = mtd_to_nand(mtd);
4810 int chipnr = (int)(offs >> chip->chip_shift);
4811 int ret;
4812
4813 /* Select the NAND device */
4814 nand_get_device(mtd, FL_READING);
4815 chip->select_chip(mtd, chipnr);
4816
4817 ret = nand_block_checkbad(mtd, offs, 0);
4818
4819 chip->select_chip(mtd, -1);
4820 nand_release_device(mtd);
4821
4822 return ret;
4823 }
4824
4825 /**
4826 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
4827 * @mtd: MTD device structure
4828 * @ofs: offset relative to mtd start
4829 */
nand_block_markbad(struct mtd_info * mtd,loff_t ofs)4830 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
4831 {
4832 int ret;
4833
4834 ret = nand_block_isbad(mtd, ofs);
4835 if (ret) {
4836 /* If it was bad already, return success and do nothing */
4837 if (ret > 0)
4838 return 0;
4839 return ret;
4840 }
4841
4842 return nand_block_markbad_lowlevel(mtd, ofs);
4843 }
4844
4845 /**
4846 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4847 * @mtd: MTD device structure
4848 * @ofs: offset relative to mtd start
4849 * @len: length of mtd
4850 */
nand_max_bad_blocks(struct mtd_info * mtd,loff_t ofs,size_t len)4851 static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4852 {
4853 struct nand_chip *chip = mtd_to_nand(mtd);
4854 u32 part_start_block;
4855 u32 part_end_block;
4856 u32 part_start_die;
4857 u32 part_end_die;
4858
4859 /*
4860 * max_bb_per_die and blocks_per_die used to determine
4861 * the maximum bad block count.
4862 */
4863 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4864 return -ENOTSUPP;
4865
4866 /* Get the start and end of the partition in erase blocks. */
4867 part_start_block = mtd_div_by_eb(ofs, mtd);
4868 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4869
4870 /* Get the start and end LUNs of the partition. */
4871 part_start_die = part_start_block / chip->blocks_per_die;
4872 part_end_die = part_end_block / chip->blocks_per_die;
4873
4874 /*
4875 * Look up the bad blocks per unit and multiply by the number of units
4876 * that the partition spans.
4877 */
4878 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4879 }
4880
4881 /**
4882 * nand_default_set_features- [REPLACEABLE] set NAND chip features
4883 * @mtd: MTD device structure
4884 * @chip: nand chip info structure
4885 * @addr: feature address.
4886 * @subfeature_param: the subfeature parameters, a four bytes array.
4887 */
nand_default_set_features(struct mtd_info * mtd,struct nand_chip * chip,int addr,uint8_t * subfeature_param)4888 static int nand_default_set_features(struct mtd_info *mtd,
4889 struct nand_chip *chip, int addr,
4890 uint8_t *subfeature_param)
4891 {
4892 return nand_set_features_op(chip, addr, subfeature_param);
4893 }
4894
4895 /**
4896 * nand_default_get_features- [REPLACEABLE] get NAND chip features
4897 * @mtd: MTD device structure
4898 * @chip: nand chip info structure
4899 * @addr: feature address.
4900 * @subfeature_param: the subfeature parameters, a four bytes array.
4901 */
nand_default_get_features(struct mtd_info * mtd,struct nand_chip * chip,int addr,uint8_t * subfeature_param)4902 static int nand_default_get_features(struct mtd_info *mtd,
4903 struct nand_chip *chip, int addr,
4904 uint8_t *subfeature_param)
4905 {
4906 return nand_get_features_op(chip, addr, subfeature_param);
4907 }
4908
4909 /**
4910 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
4911 * @mtd: MTD device structure
4912 * @chip: nand chip info structure
4913 * @addr: feature address.
4914 * @subfeature_param: the subfeature parameters, a four bytes array.
4915 *
4916 * Should be used by NAND controller drivers that do not support the SET/GET
4917 * FEATURES operations.
4918 */
nand_get_set_features_notsupp(struct mtd_info * mtd,struct nand_chip * chip,int addr,u8 * subfeature_param)4919 int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4920 int addr, u8 *subfeature_param)
4921 {
4922 return -ENOTSUPP;
4923 }
4924 EXPORT_SYMBOL(nand_get_set_features_notsupp);
4925
4926 /**
4927 * nand_suspend - [MTD Interface] Suspend the NAND flash
4928 * @mtd: MTD device structure
4929 */
nand_suspend(struct mtd_info * mtd)4930 static int nand_suspend(struct mtd_info *mtd)
4931 {
4932 return nand_get_device(mtd, FL_PM_SUSPENDED);
4933 }
4934
4935 /**
4936 * nand_resume - [MTD Interface] Resume the NAND flash
4937 * @mtd: MTD device structure
4938 */
nand_resume(struct mtd_info * mtd)4939 static void nand_resume(struct mtd_info *mtd)
4940 {
4941 struct nand_chip *chip = mtd_to_nand(mtd);
4942
4943 if (chip->state == FL_PM_SUSPENDED)
4944 nand_release_device(mtd);
4945 else
4946 pr_err("%s called for a chip which is not in suspended state\n",
4947 __func__);
4948 }
4949
4950 /**
4951 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4952 * prevent further operations
4953 * @mtd: MTD device structure
4954 */
nand_shutdown(struct mtd_info * mtd)4955 static void nand_shutdown(struct mtd_info *mtd)
4956 {
4957 nand_get_device(mtd, FL_PM_SUSPENDED);
4958 }
4959
4960 /* Set default functions */
nand_set_defaults(struct nand_chip * chip)4961 static void nand_set_defaults(struct nand_chip *chip)
4962 {
4963 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4964
4965 /* check for proper chip_delay setup, set 20us if not */
4966 if (!chip->chip_delay)
4967 chip->chip_delay = 20;
4968
4969 /* check, if a user supplied command function given */
4970 if (!chip->cmdfunc && !chip->exec_op)
4971 chip->cmdfunc = nand_command;
4972
4973 /* check, if a user supplied wait function given */
4974 if (chip->waitfunc == NULL)
4975 chip->waitfunc = nand_wait;
4976
4977 if (!chip->select_chip)
4978 chip->select_chip = nand_select_chip;
4979
4980 /* set for ONFI nand */
4981 if (!chip->set_features)
4982 chip->set_features = nand_default_set_features;
4983 if (!chip->get_features)
4984 chip->get_features = nand_default_get_features;
4985
4986 /* If called twice, pointers that depend on busw may need to be reset */
4987 if (!chip->read_byte || chip->read_byte == nand_read_byte)
4988 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4989 if (!chip->read_word)
4990 chip->read_word = nand_read_word;
4991 if (!chip->block_bad)
4992 chip->block_bad = nand_block_bad;
4993 if (!chip->block_markbad)
4994 chip->block_markbad = nand_default_block_markbad;
4995 if (!chip->write_buf || chip->write_buf == nand_write_buf)
4996 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
4997 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4998 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
4999 if (!chip->read_buf || chip->read_buf == nand_read_buf)
5000 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
5001
5002 if (!chip->controller) {
5003 chip->controller = &chip->dummy_controller;
5004 nand_controller_init(chip->controller);
5005 }
5006
5007 if (!chip->buf_align)
5008 chip->buf_align = 1;
5009 }
5010
5011 /* Sanitize ONFI strings so we can safely print them */
sanitize_string(uint8_t * s,size_t len)5012 static void sanitize_string(uint8_t *s, size_t len)
5013 {
5014 ssize_t i;
5015
5016 /* Null terminate */
5017 s[len - 1] = 0;
5018
5019 /* Remove non printable chars */
5020 for (i = 0; i < len - 1; i++) {
5021 if (s[i] < ' ' || s[i] > 127)
5022 s[i] = '?';
5023 }
5024
5025 /* Remove trailing spaces */
5026 strim(s);
5027 }
5028
onfi_crc16(u16 crc,u8 const * p,size_t len)5029 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5030 {
5031 int i;
5032 while (len--) {
5033 crc ^= *p++ << 8;
5034 for (i = 0; i < 8; i++)
5035 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5036 }
5037
5038 return crc;
5039 }
5040
5041 /* Parse the Extended Parameter Page. */
nand_flash_detect_ext_param_page(struct nand_chip * chip,struct nand_onfi_params * p)5042 static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5043 struct nand_onfi_params *p)
5044 {
5045 struct onfi_ext_param_page *ep;
5046 struct onfi_ext_section *s;
5047 struct onfi_ext_ecc_info *ecc;
5048 uint8_t *cursor;
5049 int ret;
5050 int len;
5051 int i;
5052
5053 len = le16_to_cpu(p->ext_param_page_length) * 16;
5054 ep = kmalloc(len, GFP_KERNEL);
5055 if (!ep)
5056 return -ENOMEM;
5057
5058 /* Send our own NAND_CMD_PARAM. */
5059 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5060 if (ret)
5061 goto ext_out;
5062
5063 /* Use the Change Read Column command to skip the ONFI param pages. */
5064 ret = nand_change_read_column_op(chip,
5065 sizeof(*p) * p->num_of_param_pages,
5066 ep, len, true);
5067 if (ret)
5068 goto ext_out;
5069
5070 ret = -EINVAL;
5071 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5072 != le16_to_cpu(ep->crc))) {
5073 pr_debug("fail in the CRC.\n");
5074 goto ext_out;
5075 }
5076
5077 /*
5078 * Check the signature.
5079 * Do not strictly follow the ONFI spec, maybe changed in future.
5080 */
5081 if (strncmp(ep->sig, "EPPS", 4)) {
5082 pr_debug("The signature is invalid.\n");
5083 goto ext_out;
5084 }
5085
5086 /* find the ECC section. */
5087 cursor = (uint8_t *)(ep + 1);
5088 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5089 s = ep->sections + i;
5090 if (s->type == ONFI_SECTION_TYPE_2)
5091 break;
5092 cursor += s->length * 16;
5093 }
5094 if (i == ONFI_EXT_SECTION_MAX) {
5095 pr_debug("We can not find the ECC section.\n");
5096 goto ext_out;
5097 }
5098
5099 /* get the info we want. */
5100 ecc = (struct onfi_ext_ecc_info *)cursor;
5101
5102 if (!ecc->codeword_size) {
5103 pr_debug("Invalid codeword size\n");
5104 goto ext_out;
5105 }
5106
5107 chip->ecc_strength_ds = ecc->ecc_bits;
5108 chip->ecc_step_ds = 1 << ecc->codeword_size;
5109 ret = 0;
5110
5111 ext_out:
5112 kfree(ep);
5113 return ret;
5114 }
5115
5116 /*
5117 * Recover data with bit-wise majority
5118 */
nand_bit_wise_majority(const void ** srcbufs,unsigned int nsrcbufs,void * dstbuf,unsigned int bufsize)5119 static void nand_bit_wise_majority(const void **srcbufs,
5120 unsigned int nsrcbufs,
5121 void *dstbuf,
5122 unsigned int bufsize)
5123 {
5124 int i, j, k;
5125
5126 for (i = 0; i < bufsize; i++) {
5127 u8 val = 0;
5128
5129 for (j = 0; j < 8; j++) {
5130 unsigned int cnt = 0;
5131
5132 for (k = 0; k < nsrcbufs; k++) {
5133 const u8 *srcbuf = srcbufs[k];
5134
5135 if (srcbuf[i] & BIT(j))
5136 cnt++;
5137 }
5138
5139 if (cnt > nsrcbufs / 2)
5140 val |= BIT(j);
5141 }
5142
5143 ((u8 *)dstbuf)[i] = val;
5144 }
5145 }
5146
5147 /*
5148 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
5149 */
nand_flash_detect_onfi(struct nand_chip * chip)5150 static int nand_flash_detect_onfi(struct nand_chip *chip)
5151 {
5152 struct mtd_info *mtd = nand_to_mtd(chip);
5153 struct nand_onfi_params *p;
5154 struct onfi_params *onfi;
5155 int onfi_version = 0;
5156 char id[4];
5157 int i, ret, val;
5158
5159 /* Try ONFI for unknown chip or LP */
5160 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5161 if (ret || strncmp(id, "ONFI", 4))
5162 return 0;
5163
5164 /* ONFI chip: allocate a buffer to hold its parameter page */
5165 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
5166 if (!p)
5167 return -ENOMEM;
5168
5169 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5170 if (ret) {
5171 ret = 0;
5172 goto free_onfi_param_page;
5173 }
5174
5175 for (i = 0; i < 3; i++) {
5176 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
5177 if (ret) {
5178 ret = 0;
5179 goto free_onfi_param_page;
5180 }
5181
5182 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
5183 le16_to_cpu(p->crc)) {
5184 if (i)
5185 memcpy(p, &p[i], sizeof(*p));
5186 break;
5187 }
5188 }
5189
5190 if (i == 3) {
5191 const void *srcbufs[3] = {p, p + 1, p + 2};
5192
5193 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5194 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5195 sizeof(*p));
5196
5197 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5198 le16_to_cpu(p->crc)) {
5199 pr_err("ONFI parameter recovery failed, aborting\n");
5200 goto free_onfi_param_page;
5201 }
5202 }
5203
5204 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5205 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5206 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5207
5208 /* Check version */
5209 val = le16_to_cpu(p->revision);
5210 if (val & ONFI_VERSION_2_3)
5211 onfi_version = 23;
5212 else if (val & ONFI_VERSION_2_2)
5213 onfi_version = 22;
5214 else if (val & ONFI_VERSION_2_1)
5215 onfi_version = 21;
5216 else if (val & ONFI_VERSION_2_0)
5217 onfi_version = 20;
5218 else if (val & ONFI_VERSION_1_0)
5219 onfi_version = 10;
5220
5221 if (!onfi_version) {
5222 pr_info("unsupported ONFI version: %d\n", val);
5223 goto free_onfi_param_page;
5224 }
5225
5226 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5227 sanitize_string(p->model, sizeof(p->model));
5228 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5229 if (!chip->parameters.model) {
5230 ret = -ENOMEM;
5231 goto free_onfi_param_page;
5232 }
5233
5234 mtd->writesize = le32_to_cpu(p->byte_per_page);
5235
5236 /*
5237 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5238 * (don't ask me who thought of this...). MTD assumes that these
5239 * dimensions will be power-of-2, so just truncate the remaining area.
5240 */
5241 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5242 mtd->erasesize *= mtd->writesize;
5243
5244 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5245
5246 /* See erasesize comment */
5247 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5248 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5249 chip->bits_per_cell = p->bits_per_cell;
5250
5251 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5252 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5253
5254 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
5255 chip->options |= NAND_BUSWIDTH_16;
5256
5257 if (p->ecc_bits != 0xff) {
5258 chip->ecc_strength_ds = p->ecc_bits;
5259 chip->ecc_step_ds = 512;
5260 } else if (onfi_version >= 21 &&
5261 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
5262
5263 /*
5264 * The nand_flash_detect_ext_param_page() uses the
5265 * Change Read Column command which maybe not supported
5266 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5267 * now. We do not replace user supplied command function.
5268 */
5269 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5270 chip->cmdfunc = nand_command_lp;
5271
5272 /* The Extended Parameter Page is supported since ONFI 2.1. */
5273 if (nand_flash_detect_ext_param_page(chip, p))
5274 pr_warn("Failed to detect ONFI extended param page\n");
5275 } else {
5276 pr_warn("Could not retrieve ONFI ECC requirements\n");
5277 }
5278
5279 /* Save some parameters from the parameter page for future use */
5280 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
5281 chip->parameters.supports_set_get_features = true;
5282 bitmap_set(chip->parameters.get_feature_list,
5283 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5284 bitmap_set(chip->parameters.set_feature_list,
5285 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5286 }
5287
5288 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5289 if (!onfi) {
5290 ret = -ENOMEM;
5291 goto free_model;
5292 }
5293
5294 onfi->version = onfi_version;
5295 onfi->tPROG = le16_to_cpu(p->t_prog);
5296 onfi->tBERS = le16_to_cpu(p->t_bers);
5297 onfi->tR = le16_to_cpu(p->t_r);
5298 onfi->tCCS = le16_to_cpu(p->t_ccs);
5299 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5300 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5301 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5302 chip->parameters.onfi = onfi;
5303
5304 /* Identification done, free the full ONFI parameter page and exit */
5305 kfree(p);
5306
5307 return 1;
5308
5309 free_model:
5310 kfree(chip->parameters.model);
5311 free_onfi_param_page:
5312 kfree(p);
5313
5314 return ret;
5315 }
5316
5317 /*
5318 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5319 */
nand_flash_detect_jedec(struct nand_chip * chip)5320 static int nand_flash_detect_jedec(struct nand_chip *chip)
5321 {
5322 struct mtd_info *mtd = nand_to_mtd(chip);
5323 struct nand_jedec_params *p;
5324 struct jedec_ecc_info *ecc;
5325 int jedec_version = 0;
5326 char id[5];
5327 int i, val, ret;
5328
5329 /* Try JEDEC for unknown chip or LP */
5330 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5331 if (ret || strncmp(id, "JEDEC", sizeof(id)))
5332 return 0;
5333
5334 /* JEDEC chip: allocate a buffer to hold its parameter page */
5335 p = kzalloc(sizeof(*p), GFP_KERNEL);
5336 if (!p)
5337 return -ENOMEM;
5338
5339 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
5340 if (ret) {
5341 ret = 0;
5342 goto free_jedec_param_page;
5343 }
5344
5345 for (i = 0; i < 3; i++) {
5346 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5347 if (ret) {
5348 ret = 0;
5349 goto free_jedec_param_page;
5350 }
5351
5352 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5353 le16_to_cpu(p->crc))
5354 break;
5355 }
5356
5357 if (i == 3) {
5358 pr_err("Could not find valid JEDEC parameter page; aborting\n");
5359 goto free_jedec_param_page;
5360 }
5361
5362 /* Check version */
5363 val = le16_to_cpu(p->revision);
5364 if (val & (1 << 2))
5365 jedec_version = 10;
5366 else if (val & (1 << 1))
5367 jedec_version = 1; /* vendor specific version */
5368
5369 if (!jedec_version) {
5370 pr_info("unsupported JEDEC version: %d\n", val);
5371 goto free_jedec_param_page;
5372 }
5373
5374 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5375 sanitize_string(p->model, sizeof(p->model));
5376 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5377 if (!chip->parameters.model) {
5378 ret = -ENOMEM;
5379 goto free_jedec_param_page;
5380 }
5381
5382 mtd->writesize = le32_to_cpu(p->byte_per_page);
5383
5384 /* Please reference to the comment for nand_flash_detect_onfi. */
5385 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5386 mtd->erasesize *= mtd->writesize;
5387
5388 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5389
5390 /* Please reference to the comment for nand_flash_detect_onfi. */
5391 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5392 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5393 chip->bits_per_cell = p->bits_per_cell;
5394
5395 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
5396 chip->options |= NAND_BUSWIDTH_16;
5397
5398 /* ECC info */
5399 ecc = &p->ecc_info[0];
5400
5401 if (ecc->codeword_size >= 9) {
5402 chip->ecc_strength_ds = ecc->ecc_bits;
5403 chip->ecc_step_ds = 1 << ecc->codeword_size;
5404 } else {
5405 pr_warn("Invalid codeword size\n");
5406 }
5407
5408 free_jedec_param_page:
5409 kfree(p);
5410 return ret;
5411 }
5412
5413 /*
5414 * nand_id_has_period - Check if an ID string has a given wraparound period
5415 * @id_data: the ID string
5416 * @arrlen: the length of the @id_data array
5417 * @period: the period of repitition
5418 *
5419 * Check if an ID string is repeated within a given sequence of bytes at
5420 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
5421 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
5422 * if the repetition has a period of @period; otherwise, returns zero.
5423 */
nand_id_has_period(u8 * id_data,int arrlen,int period)5424 static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5425 {
5426 int i, j;
5427 for (i = 0; i < period; i++)
5428 for (j = i + period; j < arrlen; j += period)
5429 if (id_data[i] != id_data[j])
5430 return 0;
5431 return 1;
5432 }
5433
5434 /*
5435 * nand_id_len - Get the length of an ID string returned by CMD_READID
5436 * @id_data: the ID string
5437 * @arrlen: the length of the @id_data array
5438
5439 * Returns the length of the ID string, according to known wraparound/trailing
5440 * zero patterns. If no pattern exists, returns the length of the array.
5441 */
nand_id_len(u8 * id_data,int arrlen)5442 static int nand_id_len(u8 *id_data, int arrlen)
5443 {
5444 int last_nonzero, period;
5445
5446 /* Find last non-zero byte */
5447 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5448 if (id_data[last_nonzero])
5449 break;
5450
5451 /* All zeros */
5452 if (last_nonzero < 0)
5453 return 0;
5454
5455 /* Calculate wraparound period */
5456 for (period = 1; period < arrlen; period++)
5457 if (nand_id_has_period(id_data, arrlen, period))
5458 break;
5459
5460 /* There's a repeated pattern */
5461 if (period < arrlen)
5462 return period;
5463
5464 /* There are trailing zeros */
5465 if (last_nonzero < arrlen - 1)
5466 return last_nonzero + 1;
5467
5468 /* No pattern detected */
5469 return arrlen;
5470 }
5471
5472 /* Extract the bits of per cell from the 3rd byte of the extended ID */
nand_get_bits_per_cell(u8 cellinfo)5473 static int nand_get_bits_per_cell(u8 cellinfo)
5474 {
5475 int bits;
5476
5477 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5478 bits >>= NAND_CI_CELLTYPE_SHIFT;
5479 return bits + 1;
5480 }
5481
5482 /*
5483 * Many new NAND share similar device ID codes, which represent the size of the
5484 * chip. The rest of the parameters must be decoded according to generic or
5485 * manufacturer-specific "extended ID" decoding patterns.
5486 */
nand_decode_ext_id(struct nand_chip * chip)5487 void nand_decode_ext_id(struct nand_chip *chip)
5488 {
5489 struct mtd_info *mtd = nand_to_mtd(chip);
5490 int extid;
5491 u8 *id_data = chip->id.data;
5492 /* The 3rd id byte holds MLC / multichip data */
5493 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
5494 /* The 4th id byte is the important one */
5495 extid = id_data[3];
5496
5497 /* Calc pagesize */
5498 mtd->writesize = 1024 << (extid & 0x03);
5499 extid >>= 2;
5500 /* Calc oobsize */
5501 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5502 extid >>= 2;
5503 /* Calc blocksize. Blocksize is multiples of 64KiB */
5504 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5505 extid >>= 2;
5506 /* Get buswidth information */
5507 if (extid & 0x1)
5508 chip->options |= NAND_BUSWIDTH_16;
5509 }
5510 EXPORT_SYMBOL_GPL(nand_decode_ext_id);
5511
5512 /*
5513 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5514 * decodes a matching ID table entry and assigns the MTD size parameters for
5515 * the chip.
5516 */
nand_decode_id(struct nand_chip * chip,struct nand_flash_dev * type)5517 static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
5518 {
5519 struct mtd_info *mtd = nand_to_mtd(chip);
5520
5521 mtd->erasesize = type->erasesize;
5522 mtd->writesize = type->pagesize;
5523 mtd->oobsize = mtd->writesize / 32;
5524
5525 /* All legacy ID NAND are small-page, SLC */
5526 chip->bits_per_cell = 1;
5527 }
5528
5529 /*
5530 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5531 * heuristic patterns using various detected parameters (e.g., manufacturer,
5532 * page size, cell-type information).
5533 */
nand_decode_bbm_options(struct nand_chip * chip)5534 static void nand_decode_bbm_options(struct nand_chip *chip)
5535 {
5536 struct mtd_info *mtd = nand_to_mtd(chip);
5537
5538 /* Set the bad block position */
5539 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5540 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5541 else
5542 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
5543 }
5544
is_full_id_nand(struct nand_flash_dev * type)5545 static inline bool is_full_id_nand(struct nand_flash_dev *type)
5546 {
5547 return type->id_len;
5548 }
5549
find_full_id_nand(struct nand_chip * chip,struct nand_flash_dev * type)5550 static bool find_full_id_nand(struct nand_chip *chip,
5551 struct nand_flash_dev *type)
5552 {
5553 struct mtd_info *mtd = nand_to_mtd(chip);
5554 u8 *id_data = chip->id.data;
5555
5556 if (!strncmp(type->id, id_data, type->id_len)) {
5557 mtd->writesize = type->pagesize;
5558 mtd->erasesize = type->erasesize;
5559 mtd->oobsize = type->oobsize;
5560
5561 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
5562 chip->chipsize = (uint64_t)type->chipsize << 20;
5563 chip->options |= type->options;
5564 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5565 chip->ecc_step_ds = NAND_ECC_STEP(type);
5566 chip->onfi_timing_mode_default =
5567 type->onfi_timing_mode_default;
5568
5569 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5570 if (!chip->parameters.model)
5571 return false;
5572
5573 return true;
5574 }
5575 return false;
5576 }
5577
5578 /*
5579 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5580 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5581 * table.
5582 */
nand_manufacturer_detect(struct nand_chip * chip)5583 static void nand_manufacturer_detect(struct nand_chip *chip)
5584 {
5585 /*
5586 * Try manufacturer detection if available and use
5587 * nand_decode_ext_id() otherwise.
5588 */
5589 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5590 chip->manufacturer.desc->ops->detect) {
5591 /* The 3rd id byte holds MLC / multichip data */
5592 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
5593 chip->manufacturer.desc->ops->detect(chip);
5594 } else {
5595 nand_decode_ext_id(chip);
5596 }
5597 }
5598
5599 /*
5600 * Manufacturer initialization. This function is called for all NANDs including
5601 * ONFI and JEDEC compliant ones.
5602 * Manufacturer drivers should put all their specific initialization code in
5603 * their ->init() hook.
5604 */
nand_manufacturer_init(struct nand_chip * chip)5605 static int nand_manufacturer_init(struct nand_chip *chip)
5606 {
5607 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5608 !chip->manufacturer.desc->ops->init)
5609 return 0;
5610
5611 return chip->manufacturer.desc->ops->init(chip);
5612 }
5613
5614 /*
5615 * Manufacturer cleanup. This function is called for all NANDs including
5616 * ONFI and JEDEC compliant ones.
5617 * Manufacturer drivers should put all their specific cleanup code in their
5618 * ->cleanup() hook.
5619 */
nand_manufacturer_cleanup(struct nand_chip * chip)5620 static void nand_manufacturer_cleanup(struct nand_chip *chip)
5621 {
5622 /* Release manufacturer private data */
5623 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5624 chip->manufacturer.desc->ops->cleanup)
5625 chip->manufacturer.desc->ops->cleanup(chip);
5626 }
5627
5628 /*
5629 * Get the flash and manufacturer id and lookup if the type is supported.
5630 */
nand_detect(struct nand_chip * chip,struct nand_flash_dev * type)5631 static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
5632 {
5633 const struct nand_manufacturer *manufacturer;
5634 struct mtd_info *mtd = nand_to_mtd(chip);
5635 int busw, ret;
5636 u8 *id_data = chip->id.data;
5637 u8 maf_id, dev_id;
5638
5639 /*
5640 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
5641 * after power-up.
5642 */
5643 ret = nand_reset(chip, 0);
5644 if (ret)
5645 return ret;
5646
5647 /* Select the device */
5648 chip->select_chip(mtd, 0);
5649
5650 /* Send the command for reading device ID */
5651 ret = nand_readid_op(chip, 0, id_data, 2);
5652 if (ret)
5653 return ret;
5654
5655 /* Read manufacturer and device IDs */
5656 maf_id = id_data[0];
5657 dev_id = id_data[1];
5658
5659 /*
5660 * Try again to make sure, as some systems the bus-hold or other
5661 * interface concerns can cause random data which looks like a
5662 * possibly credible NAND flash to appear. If the two results do
5663 * not match, ignore the device completely.
5664 */
5665
5666 /* Read entire ID string */
5667 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5668 if (ret)
5669 return ret;
5670
5671 if (id_data[0] != maf_id || id_data[1] != dev_id) {
5672 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
5673 maf_id, dev_id, id_data[0], id_data[1]);
5674 return -ENODEV;
5675 }
5676
5677 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
5678
5679 /* Try to identify manufacturer */
5680 manufacturer = nand_get_manufacturer(maf_id);
5681 chip->manufacturer.desc = manufacturer;
5682
5683 if (!type)
5684 type = nand_flash_ids;
5685
5686 /*
5687 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5688 * override it.
5689 * This is required to make sure initial NAND bus width set by the
5690 * NAND controller driver is coherent with the real NAND bus width
5691 * (extracted by auto-detection code).
5692 */
5693 busw = chip->options & NAND_BUSWIDTH_16;
5694
5695 /*
5696 * The flag is only set (never cleared), reset it to its default value
5697 * before starting auto-detection.
5698 */
5699 chip->options &= ~NAND_BUSWIDTH_16;
5700
5701 for (; type->name != NULL; type++) {
5702 if (is_full_id_nand(type)) {
5703 if (find_full_id_nand(chip, type))
5704 goto ident_done;
5705 } else if (dev_id == type->dev_id) {
5706 break;
5707 }
5708 }
5709
5710 if (!type->name || !type->pagesize) {
5711 /* Check if the chip is ONFI compliant */
5712 ret = nand_flash_detect_onfi(chip);
5713 if (ret < 0)
5714 return ret;
5715 else if (ret)
5716 goto ident_done;
5717
5718 /* Check if the chip is JEDEC compliant */
5719 ret = nand_flash_detect_jedec(chip);
5720 if (ret < 0)
5721 return ret;
5722 else if (ret)
5723 goto ident_done;
5724 }
5725
5726 if (!type->name)
5727 return -ENODEV;
5728
5729 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5730 if (!chip->parameters.model)
5731 return -ENOMEM;
5732
5733 chip->chipsize = (uint64_t)type->chipsize << 20;
5734
5735 if (!type->pagesize)
5736 nand_manufacturer_detect(chip);
5737 else
5738 nand_decode_id(chip, type);
5739
5740 /* Get chip options */
5741 chip->options |= type->options;
5742
5743 ident_done:
5744 if (!mtd->name)
5745 mtd->name = chip->parameters.model;
5746
5747 if (chip->options & NAND_BUSWIDTH_AUTO) {
5748 WARN_ON(busw & NAND_BUSWIDTH_16);
5749 nand_set_defaults(chip);
5750 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5751 /*
5752 * Check, if buswidth is correct. Hardware drivers should set
5753 * chip correct!
5754 */
5755 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
5756 maf_id, dev_id);
5757 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5758 mtd->name);
5759 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5760 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
5761 ret = -EINVAL;
5762
5763 goto free_detect_allocation;
5764 }
5765
5766 nand_decode_bbm_options(chip);
5767
5768 /* Calculate the address shift from the page size */
5769 chip->page_shift = ffs(mtd->writesize) - 1;
5770 /* Convert chipsize to number of pages per chip -1 */
5771 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
5772
5773 chip->bbt_erase_shift = chip->phys_erase_shift =
5774 ffs(mtd->erasesize) - 1;
5775 if (chip->chipsize & 0xffffffff)
5776 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
5777 else {
5778 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5779 chip->chip_shift += 32 - 1;
5780 }
5781
5782 if (chip->chip_shift - chip->page_shift > 16)
5783 chip->options |= NAND_ROW_ADDR_3;
5784
5785 chip->badblockbits = 8;
5786 chip->erase = single_erase;
5787
5788 /* Do not replace user supplied command function! */
5789 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5790 chip->cmdfunc = nand_command_lp;
5791
5792 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
5793 maf_id, dev_id);
5794 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5795 chip->parameters.model);
5796 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
5797 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
5798 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
5799 return 0;
5800
5801 free_detect_allocation:
5802 kfree(chip->parameters.model);
5803
5804 return ret;
5805 }
5806
5807 static const char * const nand_ecc_modes[] = {
5808 [NAND_ECC_NONE] = "none",
5809 [NAND_ECC_SOFT] = "soft",
5810 [NAND_ECC_HW] = "hw",
5811 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5812 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
5813 [NAND_ECC_ON_DIE] = "on-die",
5814 };
5815
of_get_nand_ecc_mode(struct device_node * np)5816 static int of_get_nand_ecc_mode(struct device_node *np)
5817 {
5818 const char *pm;
5819 int err, i;
5820
5821 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5822 if (err < 0)
5823 return err;
5824
5825 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5826 if (!strcasecmp(pm, nand_ecc_modes[i]))
5827 return i;
5828
5829 /*
5830 * For backward compatibility we support few obsoleted values that don't
5831 * have their mappings into nand_ecc_modes_t anymore (they were merged
5832 * with other enums).
5833 */
5834 if (!strcasecmp(pm, "soft_bch"))
5835 return NAND_ECC_SOFT;
5836
5837 return -ENODEV;
5838 }
5839
5840 static const char * const nand_ecc_algos[] = {
5841 [NAND_ECC_HAMMING] = "hamming",
5842 [NAND_ECC_BCH] = "bch",
5843 [NAND_ECC_RS] = "rs",
5844 };
5845
of_get_nand_ecc_algo(struct device_node * np)5846 static int of_get_nand_ecc_algo(struct device_node *np)
5847 {
5848 const char *pm;
5849 int err, i;
5850
5851 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5852 if (!err) {
5853 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5854 if (!strcasecmp(pm, nand_ecc_algos[i]))
5855 return i;
5856 return -ENODEV;
5857 }
5858
5859 /*
5860 * For backward compatibility we also read "nand-ecc-mode" checking
5861 * for some obsoleted values that were specifying ECC algorithm.
5862 */
5863 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5864 if (err < 0)
5865 return err;
5866
5867 if (!strcasecmp(pm, "soft"))
5868 return NAND_ECC_HAMMING;
5869 else if (!strcasecmp(pm, "soft_bch"))
5870 return NAND_ECC_BCH;
5871
5872 return -ENODEV;
5873 }
5874
of_get_nand_ecc_step_size(struct device_node * np)5875 static int of_get_nand_ecc_step_size(struct device_node *np)
5876 {
5877 int ret;
5878 u32 val;
5879
5880 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5881 return ret ? ret : val;
5882 }
5883
of_get_nand_ecc_strength(struct device_node * np)5884 static int of_get_nand_ecc_strength(struct device_node *np)
5885 {
5886 int ret;
5887 u32 val;
5888
5889 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5890 return ret ? ret : val;
5891 }
5892
of_get_nand_bus_width(struct device_node * np)5893 static int of_get_nand_bus_width(struct device_node *np)
5894 {
5895 u32 val;
5896
5897 if (of_property_read_u32(np, "nand-bus-width", &val))
5898 return 8;
5899
5900 switch (val) {
5901 case 8:
5902 case 16:
5903 return val;
5904 default:
5905 return -EIO;
5906 }
5907 }
5908
of_get_nand_on_flash_bbt(struct device_node * np)5909 static bool of_get_nand_on_flash_bbt(struct device_node *np)
5910 {
5911 return of_property_read_bool(np, "nand-on-flash-bbt");
5912 }
5913
nand_dt_init(struct nand_chip * chip)5914 static int nand_dt_init(struct nand_chip *chip)
5915 {
5916 struct device_node *dn = nand_get_flash_node(chip);
5917 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
5918
5919 if (!dn)
5920 return 0;
5921
5922 if (of_get_nand_bus_width(dn) == 16)
5923 chip->options |= NAND_BUSWIDTH_16;
5924
5925 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5926 chip->options |= NAND_IS_BOOT_MEDIUM;
5927
5928 if (of_get_nand_on_flash_bbt(dn))
5929 chip->bbt_options |= NAND_BBT_USE_FLASH;
5930
5931 ecc_mode = of_get_nand_ecc_mode(dn);
5932 ecc_algo = of_get_nand_ecc_algo(dn);
5933 ecc_strength = of_get_nand_ecc_strength(dn);
5934 ecc_step = of_get_nand_ecc_step_size(dn);
5935
5936 if (ecc_mode >= 0)
5937 chip->ecc.mode = ecc_mode;
5938
5939 if (ecc_algo >= 0)
5940 chip->ecc.algo = ecc_algo;
5941
5942 if (ecc_strength >= 0)
5943 chip->ecc.strength = ecc_strength;
5944
5945 if (ecc_step > 0)
5946 chip->ecc.size = ecc_step;
5947
5948 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5949 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5950
5951 return 0;
5952 }
5953
5954 /**
5955 * nand_scan_ident - Scan for the NAND device
5956 * @mtd: MTD device structure
5957 * @maxchips: number of chips to scan for
5958 * @table: alternative NAND ID table
5959 *
5960 * This is the first phase of the normal nand_scan() function. It reads the
5961 * flash ID and sets up MTD fields accordingly.
5962 *
5963 * This helper used to be called directly from controller drivers that needed
5964 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5965 * prevented dynamic allocations during this phase which was unconvenient and
5966 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
5967 */
nand_scan_ident(struct mtd_info * mtd,int maxchips,struct nand_flash_dev * table)5968 static int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5969 struct nand_flash_dev *table)
5970 {
5971 int i, nand_maf_id, nand_dev_id;
5972 struct nand_chip *chip = mtd_to_nand(mtd);
5973 int ret;
5974
5975 /* Enforce the right timings for reset/detection */
5976 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5977
5978 ret = nand_dt_init(chip);
5979 if (ret)
5980 return ret;
5981
5982 if (!mtd->name && mtd->dev.parent)
5983 mtd->name = dev_name(mtd->dev.parent);
5984
5985 /*
5986 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5987 * populated.
5988 */
5989 if (!chip->exec_op) {
5990 /*
5991 * Default functions assigned for ->cmdfunc() and
5992 * ->select_chip() both expect ->cmd_ctrl() to be populated.
5993 */
5994 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5995 pr_err("->cmd_ctrl() should be provided\n");
5996 return -EINVAL;
5997 }
5998 }
5999
6000 /* Set the default functions */
6001 nand_set_defaults(chip);
6002
6003 /* Read the flash type */
6004 ret = nand_detect(chip, table);
6005 if (ret) {
6006 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
6007 pr_warn("No NAND device found\n");
6008 chip->select_chip(mtd, -1);
6009 return ret;
6010 }
6011
6012 nand_maf_id = chip->id.data[0];
6013 nand_dev_id = chip->id.data[1];
6014
6015 chip->select_chip(mtd, -1);
6016
6017 /* Check for a chip array */
6018 for (i = 1; i < maxchips; i++) {
6019 u8 id[2];
6020
6021 /* See comment in nand_get_flash_type for reset */
6022 nand_reset(chip, i);
6023
6024 chip->select_chip(mtd, i);
6025 /* Send the command for reading device ID */
6026 nand_readid_op(chip, 0, id, sizeof(id));
6027 /* Read manufacturer and device IDs */
6028 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
6029 chip->select_chip(mtd, -1);
6030 break;
6031 }
6032 chip->select_chip(mtd, -1);
6033 }
6034 if (i > 1)
6035 pr_info("%d chips detected\n", i);
6036
6037 /* Store the number of chips and calc total size for mtd */
6038 chip->numchips = i;
6039 mtd->size = i * chip->chipsize;
6040
6041 return 0;
6042 }
6043
nand_scan_ident_cleanup(struct nand_chip * chip)6044 static void nand_scan_ident_cleanup(struct nand_chip *chip)
6045 {
6046 kfree(chip->parameters.model);
6047 kfree(chip->parameters.onfi);
6048 }
6049
nand_set_ecc_soft_ops(struct mtd_info * mtd)6050 static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6051 {
6052 struct nand_chip *chip = mtd_to_nand(mtd);
6053 struct nand_ecc_ctrl *ecc = &chip->ecc;
6054
6055 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
6056 return -EINVAL;
6057
6058 switch (ecc->algo) {
6059 case NAND_ECC_HAMMING:
6060 ecc->calculate = nand_calculate_ecc;
6061 ecc->correct = nand_correct_data;
6062 ecc->read_page = nand_read_page_swecc;
6063 ecc->read_subpage = nand_read_subpage;
6064 ecc->write_page = nand_write_page_swecc;
6065 ecc->read_page_raw = nand_read_page_raw;
6066 ecc->write_page_raw = nand_write_page_raw;
6067 ecc->read_oob = nand_read_oob_std;
6068 ecc->write_oob = nand_write_oob_std;
6069 if (!ecc->size)
6070 ecc->size = 256;
6071 ecc->bytes = 3;
6072 ecc->strength = 1;
6073 return 0;
6074 case NAND_ECC_BCH:
6075 if (!mtd_nand_has_bch()) {
6076 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6077 return -EINVAL;
6078 }
6079 ecc->calculate = nand_bch_calculate_ecc;
6080 ecc->correct = nand_bch_correct_data;
6081 ecc->read_page = nand_read_page_swecc;
6082 ecc->read_subpage = nand_read_subpage;
6083 ecc->write_page = nand_write_page_swecc;
6084 ecc->read_page_raw = nand_read_page_raw;
6085 ecc->write_page_raw = nand_write_page_raw;
6086 ecc->read_oob = nand_read_oob_std;
6087 ecc->write_oob = nand_write_oob_std;
6088
6089 /*
6090 * Board driver should supply ecc.size and ecc.strength
6091 * values to select how many bits are correctable.
6092 * Otherwise, default to 4 bits for large page devices.
6093 */
6094 if (!ecc->size && (mtd->oobsize >= 64)) {
6095 ecc->size = 512;
6096 ecc->strength = 4;
6097 }
6098
6099 /*
6100 * if no ecc placement scheme was provided pickup the default
6101 * large page one.
6102 */
6103 if (!mtd->ooblayout) {
6104 /* handle large page devices only */
6105 if (mtd->oobsize < 64) {
6106 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6107 return -EINVAL;
6108 }
6109
6110 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
6111
6112 }
6113
6114 /*
6115 * We can only maximize ECC config when the default layout is
6116 * used, otherwise we don't know how many bytes can really be
6117 * used.
6118 */
6119 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6120 ecc->options & NAND_ECC_MAXIMIZE) {
6121 int steps, bytes;
6122
6123 /* Always prefer 1k blocks over 512bytes ones */
6124 ecc->size = 1024;
6125 steps = mtd->writesize / ecc->size;
6126
6127 /* Reserve 2 bytes for the BBM */
6128 bytes = (mtd->oobsize - 2) / steps;
6129 ecc->strength = bytes * 8 / fls(8 * ecc->size);
6130 }
6131
6132 /* See nand_bch_init() for details. */
6133 ecc->bytes = 0;
6134 ecc->priv = nand_bch_init(mtd);
6135 if (!ecc->priv) {
6136 WARN(1, "BCH ECC initialization failed!\n");
6137 return -EINVAL;
6138 }
6139 return 0;
6140 default:
6141 WARN(1, "Unsupported ECC algorithm!\n");
6142 return -EINVAL;
6143 }
6144 }
6145
6146 /**
6147 * nand_check_ecc_caps - check the sanity of preset ECC settings
6148 * @chip: nand chip info structure
6149 * @caps: ECC caps info structure
6150 * @oobavail: OOB size that the ECC engine can use
6151 *
6152 * When ECC step size and strength are already set, check if they are supported
6153 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6154 * On success, the calculated ECC bytes is set.
6155 */
6156 static int
nand_check_ecc_caps(struct nand_chip * chip,const struct nand_ecc_caps * caps,int oobavail)6157 nand_check_ecc_caps(struct nand_chip *chip,
6158 const struct nand_ecc_caps *caps, int oobavail)
6159 {
6160 struct mtd_info *mtd = nand_to_mtd(chip);
6161 const struct nand_ecc_step_info *stepinfo;
6162 int preset_step = chip->ecc.size;
6163 int preset_strength = chip->ecc.strength;
6164 int ecc_bytes, nsteps = mtd->writesize / preset_step;
6165 int i, j;
6166
6167 for (i = 0; i < caps->nstepinfos; i++) {
6168 stepinfo = &caps->stepinfos[i];
6169
6170 if (stepinfo->stepsize != preset_step)
6171 continue;
6172
6173 for (j = 0; j < stepinfo->nstrengths; j++) {
6174 if (stepinfo->strengths[j] != preset_strength)
6175 continue;
6176
6177 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6178 preset_strength);
6179 if (WARN_ON_ONCE(ecc_bytes < 0))
6180 return ecc_bytes;
6181
6182 if (ecc_bytes * nsteps > oobavail) {
6183 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6184 preset_step, preset_strength);
6185 return -ENOSPC;
6186 }
6187
6188 chip->ecc.bytes = ecc_bytes;
6189
6190 return 0;
6191 }
6192 }
6193
6194 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6195 preset_step, preset_strength);
6196
6197 return -ENOTSUPP;
6198 }
6199
6200 /**
6201 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6202 * @chip: nand chip info structure
6203 * @caps: ECC engine caps info structure
6204 * @oobavail: OOB size that the ECC engine can use
6205 *
6206 * If a chip's ECC requirement is provided, try to meet it with the least
6207 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6208 * On success, the chosen ECC settings are set.
6209 */
6210 static int
nand_match_ecc_req(struct nand_chip * chip,const struct nand_ecc_caps * caps,int oobavail)6211 nand_match_ecc_req(struct nand_chip *chip,
6212 const struct nand_ecc_caps *caps, int oobavail)
6213 {
6214 struct mtd_info *mtd = nand_to_mtd(chip);
6215 const struct nand_ecc_step_info *stepinfo;
6216 int req_step = chip->ecc_step_ds;
6217 int req_strength = chip->ecc_strength_ds;
6218 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6219 int best_step, best_strength, best_ecc_bytes;
6220 int best_ecc_bytes_total = INT_MAX;
6221 int i, j;
6222
6223 /* No information provided by the NAND chip */
6224 if (!req_step || !req_strength)
6225 return -ENOTSUPP;
6226
6227 /* number of correctable bits the chip requires in a page */
6228 req_corr = mtd->writesize / req_step * req_strength;
6229
6230 for (i = 0; i < caps->nstepinfos; i++) {
6231 stepinfo = &caps->stepinfos[i];
6232 step_size = stepinfo->stepsize;
6233
6234 for (j = 0; j < stepinfo->nstrengths; j++) {
6235 strength = stepinfo->strengths[j];
6236
6237 /*
6238 * If both step size and strength are smaller than the
6239 * chip's requirement, it is not easy to compare the
6240 * resulted reliability.
6241 */
6242 if (step_size < req_step && strength < req_strength)
6243 continue;
6244
6245 if (mtd->writesize % step_size)
6246 continue;
6247
6248 nsteps = mtd->writesize / step_size;
6249
6250 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6251 if (WARN_ON_ONCE(ecc_bytes < 0))
6252 continue;
6253 ecc_bytes_total = ecc_bytes * nsteps;
6254
6255 if (ecc_bytes_total > oobavail ||
6256 strength * nsteps < req_corr)
6257 continue;
6258
6259 /*
6260 * We assume the best is to meet the chip's requrement
6261 * with the least number of ECC bytes.
6262 */
6263 if (ecc_bytes_total < best_ecc_bytes_total) {
6264 best_ecc_bytes_total = ecc_bytes_total;
6265 best_step = step_size;
6266 best_strength = strength;
6267 best_ecc_bytes = ecc_bytes;
6268 }
6269 }
6270 }
6271
6272 if (best_ecc_bytes_total == INT_MAX)
6273 return -ENOTSUPP;
6274
6275 chip->ecc.size = best_step;
6276 chip->ecc.strength = best_strength;
6277 chip->ecc.bytes = best_ecc_bytes;
6278
6279 return 0;
6280 }
6281
6282 /**
6283 * nand_maximize_ecc - choose the max ECC strength available
6284 * @chip: nand chip info structure
6285 * @caps: ECC engine caps info structure
6286 * @oobavail: OOB size that the ECC engine can use
6287 *
6288 * Choose the max ECC strength that is supported on the controller, and can fit
6289 * within the chip's OOB. On success, the chosen ECC settings are set.
6290 */
6291 static int
nand_maximize_ecc(struct nand_chip * chip,const struct nand_ecc_caps * caps,int oobavail)6292 nand_maximize_ecc(struct nand_chip *chip,
6293 const struct nand_ecc_caps *caps, int oobavail)
6294 {
6295 struct mtd_info *mtd = nand_to_mtd(chip);
6296 const struct nand_ecc_step_info *stepinfo;
6297 int step_size, strength, nsteps, ecc_bytes, corr;
6298 int best_corr = 0;
6299 int best_step = 0;
6300 int best_strength, best_ecc_bytes;
6301 int i, j;
6302
6303 for (i = 0; i < caps->nstepinfos; i++) {
6304 stepinfo = &caps->stepinfos[i];
6305 step_size = stepinfo->stepsize;
6306
6307 /* If chip->ecc.size is already set, respect it */
6308 if (chip->ecc.size && step_size != chip->ecc.size)
6309 continue;
6310
6311 for (j = 0; j < stepinfo->nstrengths; j++) {
6312 strength = stepinfo->strengths[j];
6313
6314 if (mtd->writesize % step_size)
6315 continue;
6316
6317 nsteps = mtd->writesize / step_size;
6318
6319 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6320 if (WARN_ON_ONCE(ecc_bytes < 0))
6321 continue;
6322
6323 if (ecc_bytes * nsteps > oobavail)
6324 continue;
6325
6326 corr = strength * nsteps;
6327
6328 /*
6329 * If the number of correctable bits is the same,
6330 * bigger step_size has more reliability.
6331 */
6332 if (corr > best_corr ||
6333 (corr == best_corr && step_size > best_step)) {
6334 best_corr = corr;
6335 best_step = step_size;
6336 best_strength = strength;
6337 best_ecc_bytes = ecc_bytes;
6338 }
6339 }
6340 }
6341
6342 if (!best_corr)
6343 return -ENOTSUPP;
6344
6345 chip->ecc.size = best_step;
6346 chip->ecc.strength = best_strength;
6347 chip->ecc.bytes = best_ecc_bytes;
6348
6349 return 0;
6350 }
6351
6352 /**
6353 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6354 * @chip: nand chip info structure
6355 * @caps: ECC engine caps info structure
6356 * @oobavail: OOB size that the ECC engine can use
6357 *
6358 * Choose the ECC configuration according to following logic
6359 *
6360 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6361 * then check if it is supported by this controller.
6362 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6363 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6364 * to the chip's requirement. If available OOB size can't fit the chip
6365 * requirement then fallback to the maximum ECC step size and ECC strength.
6366 *
6367 * On success, the chosen ECC settings are set.
6368 */
nand_ecc_choose_conf(struct nand_chip * chip,const struct nand_ecc_caps * caps,int oobavail)6369 int nand_ecc_choose_conf(struct nand_chip *chip,
6370 const struct nand_ecc_caps *caps, int oobavail)
6371 {
6372 struct mtd_info *mtd = nand_to_mtd(chip);
6373
6374 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6375 return -EINVAL;
6376
6377 if (chip->ecc.size && chip->ecc.strength)
6378 return nand_check_ecc_caps(chip, caps, oobavail);
6379
6380 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6381 return nand_maximize_ecc(chip, caps, oobavail);
6382
6383 if (!nand_match_ecc_req(chip, caps, oobavail))
6384 return 0;
6385
6386 return nand_maximize_ecc(chip, caps, oobavail);
6387 }
6388 EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6389
6390 /*
6391 * Check if the chip configuration meet the datasheet requirements.
6392
6393 * If our configuration corrects A bits per B bytes and the minimum
6394 * required correction level is X bits per Y bytes, then we must ensure
6395 * both of the following are true:
6396 *
6397 * (1) A / B >= X / Y
6398 * (2) A >= X
6399 *
6400 * Requirement (1) ensures we can correct for the required bitflip density.
6401 * Requirement (2) ensures we can correct even when all bitflips are clumped
6402 * in the same sector.
6403 */
nand_ecc_strength_good(struct mtd_info * mtd)6404 static bool nand_ecc_strength_good(struct mtd_info *mtd)
6405 {
6406 struct nand_chip *chip = mtd_to_nand(mtd);
6407 struct nand_ecc_ctrl *ecc = &chip->ecc;
6408 int corr, ds_corr;
6409
6410 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6411 /* Not enough information */
6412 return true;
6413
6414 /*
6415 * We get the number of corrected bits per page to compare
6416 * the correction density.
6417 */
6418 corr = (mtd->writesize * ecc->strength) / ecc->size;
6419 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6420
6421 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6422 }
6423
6424 /**
6425 * nand_scan_tail - Scan for the NAND device
6426 * @mtd: MTD device structure
6427 *
6428 * This is the second phase of the normal nand_scan() function. It fills out
6429 * all the uninitialized function pointers with the defaults and scans for a
6430 * bad block table if appropriate.
6431 */
nand_scan_tail(struct mtd_info * mtd)6432 static int nand_scan_tail(struct mtd_info *mtd)
6433 {
6434 struct nand_chip *chip = mtd_to_nand(mtd);
6435 struct nand_ecc_ctrl *ecc = &chip->ecc;
6436 int ret, i;
6437
6438 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
6439 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
6440 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
6441 return -EINVAL;
6442 }
6443
6444 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
6445 if (!chip->data_buf)
6446 return -ENOMEM;
6447
6448 /*
6449 * FIXME: some NAND manufacturer drivers expect the first die to be
6450 * selected when manufacturer->init() is called. They should be fixed
6451 * to explictly select the relevant die when interacting with the NAND
6452 * chip.
6453 */
6454 chip->select_chip(mtd, 0);
6455 ret = nand_manufacturer_init(chip);
6456 chip->select_chip(mtd, -1);
6457 if (ret)
6458 goto err_free_buf;
6459
6460 /* Set the internal oob buffer location, just after the page data */
6461 chip->oob_poi = chip->data_buf + mtd->writesize;
6462
6463 /*
6464 * If no default placement scheme is given, select an appropriate one.
6465 */
6466 if (!mtd->ooblayout &&
6467 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
6468 switch (mtd->oobsize) {
6469 case 8:
6470 case 16:
6471 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
6472 break;
6473 case 64:
6474 case 128:
6475 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
6476 break;
6477 default:
6478 /*
6479 * Expose the whole OOB area to users if ECC_NONE
6480 * is passed. We could do that for all kind of
6481 * ->oobsize, but we must keep the old large/small
6482 * page with ECC layout when ->oobsize <= 128 for
6483 * compatibility reasons.
6484 */
6485 if (ecc->mode == NAND_ECC_NONE) {
6486 mtd_set_ooblayout(mtd,
6487 &nand_ooblayout_lp_ops);
6488 break;
6489 }
6490
6491 WARN(1, "No oob scheme defined for oobsize %d\n",
6492 mtd->oobsize);
6493 ret = -EINVAL;
6494 goto err_nand_manuf_cleanup;
6495 }
6496 }
6497
6498 /*
6499 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
6500 * selected and we have 256 byte pagesize fallback to software ECC
6501 */
6502
6503 switch (ecc->mode) {
6504 case NAND_ECC_HW_OOB_FIRST:
6505 /* Similar to NAND_ECC_HW, but a separate read_page handle */
6506 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
6507 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6508 ret = -EINVAL;
6509 goto err_nand_manuf_cleanup;
6510 }
6511 if (!ecc->read_page)
6512 ecc->read_page = nand_read_page_hwecc_oob_first;
6513
6514 case NAND_ECC_HW:
6515 /* Use standard hwecc read page function? */
6516 if (!ecc->read_page)
6517 ecc->read_page = nand_read_page_hwecc;
6518 if (!ecc->write_page)
6519 ecc->write_page = nand_write_page_hwecc;
6520 if (!ecc->read_page_raw)
6521 ecc->read_page_raw = nand_read_page_raw;
6522 if (!ecc->write_page_raw)
6523 ecc->write_page_raw = nand_write_page_raw;
6524 if (!ecc->read_oob)
6525 ecc->read_oob = nand_read_oob_std;
6526 if (!ecc->write_oob)
6527 ecc->write_oob = nand_write_oob_std;
6528 if (!ecc->read_subpage)
6529 ecc->read_subpage = nand_read_subpage;
6530 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
6531 ecc->write_subpage = nand_write_subpage_hwecc;
6532
6533 case NAND_ECC_HW_SYNDROME:
6534 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6535 (!ecc->read_page ||
6536 ecc->read_page == nand_read_page_hwecc ||
6537 !ecc->write_page ||
6538 ecc->write_page == nand_write_page_hwecc)) {
6539 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6540 ret = -EINVAL;
6541 goto err_nand_manuf_cleanup;
6542 }
6543 /* Use standard syndrome read/write page function? */
6544 if (!ecc->read_page)
6545 ecc->read_page = nand_read_page_syndrome;
6546 if (!ecc->write_page)
6547 ecc->write_page = nand_write_page_syndrome;
6548 if (!ecc->read_page_raw)
6549 ecc->read_page_raw = nand_read_page_raw_syndrome;
6550 if (!ecc->write_page_raw)
6551 ecc->write_page_raw = nand_write_page_raw_syndrome;
6552 if (!ecc->read_oob)
6553 ecc->read_oob = nand_read_oob_syndrome;
6554 if (!ecc->write_oob)
6555 ecc->write_oob = nand_write_oob_syndrome;
6556
6557 if (mtd->writesize >= ecc->size) {
6558 if (!ecc->strength) {
6559 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6560 ret = -EINVAL;
6561 goto err_nand_manuf_cleanup;
6562 }
6563 break;
6564 }
6565 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6566 ecc->size, mtd->writesize);
6567 ecc->mode = NAND_ECC_SOFT;
6568 ecc->algo = NAND_ECC_HAMMING;
6569
6570 case NAND_ECC_SOFT:
6571 ret = nand_set_ecc_soft_ops(mtd);
6572 if (ret) {
6573 ret = -EINVAL;
6574 goto err_nand_manuf_cleanup;
6575 }
6576 break;
6577
6578 case NAND_ECC_ON_DIE:
6579 if (!ecc->read_page || !ecc->write_page) {
6580 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6581 ret = -EINVAL;
6582 goto err_nand_manuf_cleanup;
6583 }
6584 if (!ecc->read_oob)
6585 ecc->read_oob = nand_read_oob_std;
6586 if (!ecc->write_oob)
6587 ecc->write_oob = nand_write_oob_std;
6588 break;
6589
6590 case NAND_ECC_NONE:
6591 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
6592 ecc->read_page = nand_read_page_raw;
6593 ecc->write_page = nand_write_page_raw;
6594 ecc->read_oob = nand_read_oob_std;
6595 ecc->read_page_raw = nand_read_page_raw;
6596 ecc->write_page_raw = nand_write_page_raw;
6597 ecc->write_oob = nand_write_oob_std;
6598 ecc->size = mtd->writesize;
6599 ecc->bytes = 0;
6600 ecc->strength = 0;
6601 break;
6602
6603 default:
6604 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6605 ret = -EINVAL;
6606 goto err_nand_manuf_cleanup;
6607 }
6608
6609 if (ecc->correct || ecc->calculate) {
6610 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6611 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6612 if (!ecc->calc_buf || !ecc->code_buf) {
6613 ret = -ENOMEM;
6614 goto err_nand_manuf_cleanup;
6615 }
6616 }
6617
6618 /* For many systems, the standard OOB write also works for raw */
6619 if (!ecc->read_oob_raw)
6620 ecc->read_oob_raw = ecc->read_oob;
6621 if (!ecc->write_oob_raw)
6622 ecc->write_oob_raw = ecc->write_oob;
6623
6624 /* propagate ecc info to mtd_info */
6625 mtd->ecc_strength = ecc->strength;
6626 mtd->ecc_step_size = ecc->size;
6627
6628 /*
6629 * Set the number of read / write steps for one page depending on ECC
6630 * mode.
6631 */
6632 ecc->steps = mtd->writesize / ecc->size;
6633 if (ecc->steps * ecc->size != mtd->writesize) {
6634 WARN(1, "Invalid ECC parameters\n");
6635 ret = -EINVAL;
6636 goto err_nand_manuf_cleanup;
6637 }
6638 ecc->total = ecc->steps * ecc->bytes;
6639 if (ecc->total > mtd->oobsize) {
6640 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6641 ret = -EINVAL;
6642 goto err_nand_manuf_cleanup;
6643 }
6644
6645 /*
6646 * The number of bytes available for a client to place data into
6647 * the out of band area.
6648 */
6649 ret = mtd_ooblayout_count_freebytes(mtd);
6650 if (ret < 0)
6651 ret = 0;
6652
6653 mtd->oobavail = ret;
6654
6655 /* ECC sanity check: warn if it's too weak */
6656 if (!nand_ecc_strength_good(mtd))
6657 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6658 mtd->name);
6659
6660 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
6661 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
6662 switch (ecc->steps) {
6663 case 2:
6664 mtd->subpage_sft = 1;
6665 break;
6666 case 4:
6667 case 8:
6668 case 16:
6669 mtd->subpage_sft = 2;
6670 break;
6671 }
6672 }
6673 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6674
6675 /* Initialize state */
6676 chip->state = FL_READY;
6677
6678 /* Invalidate the pagebuffer reference */
6679 chip->pagebuf = -1;
6680
6681 /* Large page NAND with SOFT_ECC should support subpage reads */
6682 switch (ecc->mode) {
6683 case NAND_ECC_SOFT:
6684 if (chip->page_shift > 9)
6685 chip->options |= NAND_SUBPAGE_READ;
6686 break;
6687
6688 default:
6689 break;
6690 }
6691
6692 /* Fill in remaining MTD driver data */
6693 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
6694 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6695 MTD_CAP_NANDFLASH;
6696 mtd->_erase = nand_erase;
6697 mtd->_point = NULL;
6698 mtd->_unpoint = NULL;
6699 mtd->_panic_write = panic_nand_write;
6700 mtd->_read_oob = nand_read_oob;
6701 mtd->_write_oob = nand_write_oob;
6702 mtd->_sync = nand_sync;
6703 mtd->_lock = NULL;
6704 mtd->_unlock = NULL;
6705 mtd->_suspend = nand_suspend;
6706 mtd->_resume = nand_resume;
6707 mtd->_reboot = nand_shutdown;
6708 mtd->_block_isreserved = nand_block_isreserved;
6709 mtd->_block_isbad = nand_block_isbad;
6710 mtd->_block_markbad = nand_block_markbad;
6711 mtd->_max_bad_blocks = nand_max_bad_blocks;
6712 mtd->writebufsize = mtd->writesize;
6713
6714 /*
6715 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6716 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6717 * properly set.
6718 */
6719 if (!mtd->bitflip_threshold)
6720 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
6721
6722 /* Initialize the ->data_interface field. */
6723 ret = nand_init_data_interface(chip);
6724 if (ret)
6725 goto err_nand_manuf_cleanup;
6726
6727 /* Enter fastest possible mode on all dies. */
6728 for (i = 0; i < chip->numchips; i++) {
6729 ret = nand_setup_data_interface(chip, i);
6730 if (ret)
6731 goto err_nand_manuf_cleanup;
6732 }
6733
6734 /* Check, if we should skip the bad block table scan */
6735 if (chip->options & NAND_SKIP_BBTSCAN)
6736 return 0;
6737
6738 /* Build bad block table */
6739 ret = nand_create_bbt(chip);
6740 if (ret)
6741 goto err_nand_manuf_cleanup;
6742
6743 return 0;
6744
6745
6746 err_nand_manuf_cleanup:
6747 nand_manufacturer_cleanup(chip);
6748
6749 err_free_buf:
6750 kfree(chip->data_buf);
6751 kfree(ecc->code_buf);
6752 kfree(ecc->calc_buf);
6753
6754 return ret;
6755 }
6756
nand_attach(struct nand_chip * chip)6757 static int nand_attach(struct nand_chip *chip)
6758 {
6759 if (chip->controller->ops && chip->controller->ops->attach_chip)
6760 return chip->controller->ops->attach_chip(chip);
6761
6762 return 0;
6763 }
6764
nand_detach(struct nand_chip * chip)6765 static void nand_detach(struct nand_chip *chip)
6766 {
6767 if (chip->controller->ops && chip->controller->ops->detach_chip)
6768 chip->controller->ops->detach_chip(chip);
6769 }
6770
6771 /**
6772 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
6773 * @mtd: MTD device structure
6774 * @maxchips: number of chips to scan for. @nand_scan_ident() will not be run if
6775 * this parameter is zero (useful for specific drivers that must
6776 * handle this part of the process themselves, e.g docg4).
6777 * @ids: optional flash IDs table
6778 *
6779 * This fills out all the uninitialized function pointers with the defaults.
6780 * The flash ID is read and the mtd/chip structures are filled with the
6781 * appropriate values.
6782 */
nand_scan_with_ids(struct mtd_info * mtd,int maxchips,struct nand_flash_dev * ids)6783 int nand_scan_with_ids(struct mtd_info *mtd, int maxchips,
6784 struct nand_flash_dev *ids)
6785 {
6786 struct nand_chip *chip = mtd_to_nand(mtd);
6787 int ret;
6788
6789 if (maxchips) {
6790 ret = nand_scan_ident(mtd, maxchips, ids);
6791 if (ret)
6792 return ret;
6793 }
6794
6795 ret = nand_attach(chip);
6796 if (ret)
6797 goto cleanup_ident;
6798
6799 ret = nand_scan_tail(mtd);
6800 if (ret)
6801 goto detach_chip;
6802
6803 return 0;
6804
6805 detach_chip:
6806 nand_detach(chip);
6807 cleanup_ident:
6808 nand_scan_ident_cleanup(chip);
6809
6810 return ret;
6811 }
6812 EXPORT_SYMBOL(nand_scan_with_ids);
6813
6814 /**
6815 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6816 * @chip: NAND chip object
6817 */
nand_cleanup(struct nand_chip * chip)6818 void nand_cleanup(struct nand_chip *chip)
6819 {
6820 if (chip->ecc.mode == NAND_ECC_SOFT &&
6821 chip->ecc.algo == NAND_ECC_BCH)
6822 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6823
6824 /* Free bad block table memory */
6825 kfree(chip->bbt);
6826 kfree(chip->data_buf);
6827 kfree(chip->ecc.code_buf);
6828 kfree(chip->ecc.calc_buf);
6829
6830 /* Free bad block descriptor memory */
6831 if (chip->badblock_pattern && chip->badblock_pattern->options
6832 & NAND_BBT_DYNAMICSTRUCT)
6833 kfree(chip->badblock_pattern);
6834
6835 /* Free manufacturer priv data. */
6836 nand_manufacturer_cleanup(chip);
6837
6838 /* Free controller specific allocations after chip identification */
6839 nand_detach(chip);
6840
6841 /* Free identification phase allocations */
6842 nand_scan_ident_cleanup(chip);
6843 }
6844
6845 EXPORT_SYMBOL_GPL(nand_cleanup);
6846
6847 /**
6848 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6849 * held by the NAND device
6850 * @mtd: MTD device structure
6851 */
nand_release(struct mtd_info * mtd)6852 void nand_release(struct mtd_info *mtd)
6853 {
6854 mtd_device_unregister(mtd);
6855 nand_cleanup(mtd_to_nand(mtd));
6856 }
6857 EXPORT_SYMBOL_GPL(nand_release);
6858
6859 MODULE_LICENSE("GPL");
6860 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6861 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
6862 MODULE_DESCRIPTION("Generic NAND flash driver code");
6863